static int Compiler_compileDoNode(Context* context, Node* node) { // backup current block Block* oldBlock = context->block; // create context for the new function context->block = Block_new(); SymTab_enterScope(context->symtab); // pop the arguments from the stack // and save the registers to the symtab int argc = 0; Node* arg = node->left; while (arg) { Block_append(context->block, OP_POP, context->reg); SymTab_store(context->symtab, arg->data.sval, context->reg++); arg = arg->right; argc++; } // compile the function code int ret = Compiler_compile(context, node->right); Block_append(context->block, OP_RET, ret); int block = Module_addBlock(context->module, context->block); // restore the old context SymTab_leaveScope(context->symtab); context->block = oldBlock; // create the function Block_append(context->block, OP_FUN, context->reg, block, argc); return context->reg++; }
int main() { Suite *suite_0 = Suite_new("RoutingTable"); Block *block_0 = Block_new(blockTypeBefore, NULL, &block_0_callback); Suite_push_block(suite_0, block_0); Block *block_1 = Block_new(blockTypeSpec, "nodes should be findable", &block_1_callback); Suite_push_block(suite_0, block_1); Block *block_2 = Block_new(blockTypeSpec, "nodes should be removeable", &block_2_callback); Suite_push_block(suite_0, block_2); Suite_run(suite_0); CSpec_stats(); return CSpec.failures; }
int Compiler_compileRootNode(Context* context, Node* root) { context->block = Block_new(); int id = Module_addBlock(context->module, context->block); // this code is only executed if an error occurs in the // compilation process further down if (setjmp(context->error_buf)) { return -1; } if (root) { int ret = Compiler_compile(context, root); Block_append(context->block, OP_RET, ret); } else { Block_append(context->block, OP_NIL, context->reg); Block_append(context->block, OP_RET, context->reg++); } return id; }