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 firth_code_gen_elt(firth_parse_element * elt, firth_compilation_result *result){
if (elt->type == OP){
if (firth_elt_token_equals(elt,".")){
strcat(result->lmsm_assembly, "SDUP
SPOP
OUT
");
} else if (firth_elt_token_equals(elt,"+")){
strcat(result->lmsm_assembly, "SADD
");
} else if (firth_elt_token_equals(elt,"-")){
strcat(result->lmsm_assembly, "SSUB
");
// TODO - add assembly generation for *,/, max and min
} else if (firth_elt_token_equals(elt, "get")){
strcat(result->lmsm_assembly, "INP
SPUSH
");
} else if (firth_elt_token_equals(elt, "pop")){
strcat(result->lmsm_assembly, "SPOP
");
} else if (firth_elt_token_equals(elt, "dup")){
strcat(result->lmsm_assembly, "SDUP
");
} else if (firth_elt_token_equals(elt, "swap")){
strcat(result->lmsm_assembly, "SSWAP
");
} else if (firth_elt_token_equals(elt, "return")){
strcat(result->lmsm_assembly, "RET
");
}
} else if (elt->type == NUMBER){
strcat(result->lmsm_assembly, "LDI ");
strcat(result->lmsm_assembly, elt->token->value);
strcat(result->lmsm_assembly, "
");
strcat(result->lmsm_assembly, "SPUSH
");
} else if (elt->type == ZERO_TEST){
char if_zero_label[20];
sprintf(if_zero_label, "if_zero_%d", result->label_num++);
char end_zero_label[20];
sprintf(end_zero_label, "end_zero_%d", result->label_num++);
// branch if top of stack zero
strcat(result->lmsm_assembly, "SPOP
BRZ ");
if (elt->left_children->first){
strcat(result->lmsm_assembly, if_zero_label);
} else {
strcat(result->lmsm_assembly, end_zero_label);
}
strcat(result->lmsm_assembly, "
");
// generate else
if (elt->right_children->first){
struct firth_parse_element *child = elt->right_children->first;
while (child != NULL){
firth_code_gen_elt(child, result);
child = child->next_sibling;
}
}
// jump to end of zero condition
strcat(result->lmsm_assembly, "BRA ");
strcat(result->lmsm_assembly, end_zero_label);
strcat(result->lmsm_assembly, "
");
// generate if zero condition
if (elt->left_children->first){
strcat(result->lmsm_assembly, if_zero_label);
strcat(result->lmsm_assembly, "");
struct firth_parse_element *child = elt->left_children->first;
while (child != NULL){
firth_code_gen_elt(child, result);
child = child->next_sibling;
}
}
// label end of zero conditional
strcat(result->lmsm_assembly, end_zero_label);
strcat(result->lmsm_assembly, "");
} else if (elt->type == CALL){
strcat(result->lmsm_assembly, "CALL ");
strcat(result->lmsm_assembly, elt->token->value);
strcat(result->lmsm_assembly, "
");
} else if (elt->type == DEF){
// function label
strcat(result->lmsm_assembly, elt->name->value);
strcat(result->lmsm_assembly, "");
// function body
if (elt->left_children->first){
struct firth_parse_element *child = elt->left_children->first;
while (child != NULL){
firth_code_gen_elt(child, result);
child = child->next_sibling;
}
}
// always append a RET
strcat(result->lmsm_assembly, "RET
");
}
}
firth_parse_element *firth_parse_op(firth_tokens *tokens, firth_compilation_result *result){
if (firth_match_token("+", tokens)||
firth_match_token("-", tokens)||
// TODO - add *,/, max and min
firth_match_token("get", tokens)||
firth_match_token("dup", tokens)||
firth_match_token("pop", tokens)||
firth_match_token("swap", tokens)||
firth_match_token("return", tokens)||
firth_match_token(".", tokens)){
return firth_make_elt(firth_take_token(tokens), OP);
}
return NULL;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!