static int Compiler_compileNameNode(Context* context, Node* node) { const char* name = node->data.sval; if (!strcmp(name, "true")) { Block_append(context->block, OP_BOOL, context->reg, 1); return context->reg++; } else if (!strcmp(name, "false")) { Block_append(context->block, OP_BOOL, context->reg, 0); return context->reg++; } else if (!strcmp(name, "nil")) { Block_append(context->block, OP_NIL, context->reg); return context->reg++; } Reg reg = SymTab_lookup(context->symtab, name); if (reg < 0) { fprintf(stderr, "name %s not defined\n", name); Compiler_error(context); } return reg; }
static int Compiler_compileAssignNode(Context* context, Node* node) { int reg = SymTab_lookup(context->symtab, node->data.sval); int obj = Compiler_compile(context, node->right); if (reg < 0) { SymTab_store(context->symtab, node->data.sval, context->reg); reg = context->reg++; } Block_append(context->block, OP_MOV, reg, obj); return reg; }
int tree_gen_assignmentExpression(struct tree *t) { //get the token name char *tname = tree_get_ident(t); //if no token name, this is a right hand assignmentExpression with a // literal, which we'll ignore now and handle when we see the entire //expression later (below) if(tname == NULL) { return(0); } //else, this is an entire assignmentExpression to a identifier else { SymTabEntry *p = SymTab_lookup(tname); //get the subtree symbol for report info struct tree *temp = NULL; tree_get_subtree(tname, t, &temp); //use before declaration check if(p == NULL) { if(temp != NULL) { struct tree_token *symbol = temp->leaf; fprintf(stderr, "ERROR: identifier '%s' used before declared, in file '%s' on line %d\n", tname, symbol->fname, symbol->lineno); } else { fprintf(stderr, "ERROR: identifier '%s' used before declared, and some horrible internal errors also occured.\n", tname); } exit(ERROR_SEMANTIC); } //assignment to const check if(p->aux_flag != NULL && strcmp(p->aux_flag, "const") == 0) { if(temp != NULL) { struct tree_token *symbol = temp->leaf; fprintf(stderr, "ERROR: assignment to constant '%s', in file '%s' on line %d\n", tname, symbol->fname, symbol->lineno); } else { fprintf(stderr, "ERROR: assignment to constant '%s', and some horrible internal errors also occured.\n", tname); } exit(ERROR_SEMANTIC); } } //end else (if tname != NULL //otherwise, some tac gen here return(0); //success }