Question: i need help doing the todo void firth _ code _ gen _ elt ( firth _ parse _ element * elt, firth _ compilation
i need help doing the todo
void firthcodegeneltfirthparseelement elt, firthcompilationresult result
if elttype OP
if firthelttokenequalselt
strcatresultlmsmassembly, "SDUP
SPOP
OUT
;
else if firthelttokenequalselt
strcatresultlmsmassembly, "SADD
;
else if firthelttokenequalselt
strcatresultlmsmassembly, "SSUB
;
TODO add assembly generation for max and min
else if firthelttokenequalselt "get"
strcatresultlmsmassembly, "INP
SPUSH
;
else if firthelttokenequalselt "pop"
strcatresultlmsmassembly, "SPOP
;
else if firthelttokenequalselt "dup"
strcatresultlmsmassembly, "SDUP
;
else if firthelttokenequalselt "swap"
strcatresultlmsmassembly, "SSWAP
;
else if firthelttokenequalselt "return"
strcatresultlmsmassembly, "RET
;
else if elttype NUMBER
strcatresultlmsmassembly, "LDI ;
strcatresultlmsmassembly, elttokenvalue;
strcatresultlmsmassembly,
;
strcatresultlmsmassembly, "SPUSH
;
else if elttype ZEROTEST
char ifzerolabel;
sprintfifzerolabel, ifzerod resultlabelnum;
char endzerolabel;
sprintfendzerolabel, "endzerod resultlabelnum;
branch if top of stack zero
strcatresultlmsmassembly, "SPOP
BRZ ;
if eltleftchildrenfirst
strcatresultlmsmassembly, ifzerolabel;
else
strcatresultlmsmassembly, endzerolabel;
strcatresultlmsmassembly,
;
generate else
if eltrightchildrenfirst
struct firthparseelement child eltrightchildrenfirst;
while child NULL
firthcodegeneltchild result;
child childnextsibling;
jump to end of zero condition
strcatresultlmsmassembly, "BRA ;
strcatresultlmsmassembly, endzerolabel;
strcatresultlmsmassembly,
;
generate if zero condition
if eltleftchildrenfirst
strcatresultlmsmassembly, ifzerolabel;
strcatresultlmsmassembly, ;
struct firthparseelement child eltleftchildrenfirst;
while child NULL
firthcodegeneltchild result;
child childnextsibling;
label end of zero conditional
strcatresultlmsmassembly, endzerolabel;
strcatresultlmsmassembly, ;
else if elttype CALL
strcatresultlmsmassembly, "CALL ;
strcatresultlmsmassembly, elttokenvalue;
strcatresultlmsmassembly,
;
else if elttype DEF
function label
strcatresultlmsmassembly, eltnamevalue;
strcatresultlmsmassembly, ;
function body
if eltleftchildrenfirst
struct firthparseelement child eltleftchildrenfirst;
while child NULL
firthcodegeneltchild result;
child childnextsibling;
always append a RET
strcatresultlmsmassembly, "RET
;
firthparseelement firthparseopfirthtokens tokens firthcompilationresult result
if firthmatchtoken tokens
firthmatchtoken tokens
TODO add max and min
firthmatchtokenget tokens
firthmatchtokendup tokens
firthmatchtokenpop tokens
firthmatchtokenswap tokens
firthmatchtokenreturn tokens
firthmatchtoken tokens
return firthmakeeltfirthtaketokentokens OP;
return NULL;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
