示例#1
0
static void compile_stmt_list(AstNode *ast, SNodeArray *sna)
{
    List *list  = ast->list;

    LIST_FOREACH(list, first, next, current) {
        AstNode *node = (AstNode*) current->data;

        compile_stmt(node, sna);
    }
示例#2
0
文件: codegen.c 项目: thomaslee/cue
static void
compile_stmt(CueCodeGenerator *cg, CueAstStmt *stmt)
{
    switch (stmt->type) {
        case CUE_AST_STMT_USE:
            {
                break;
            }
        case CUE_AST_STMT_EXPR:
            {
                compile_expr(cg, stmt->v.expr.value);
                _fmt(cg, ";\n");
                break;
            }
        case CUE_AST_STMT_RET:
            {
                _fmt(cg, "return");
                if (stmt->v.ret.value) {
                    _fmt(cg, " (");
                    compile_expr(cg, stmt->v.ret.value);
                    _fmt(cg, ")");
                }
                _fmt(cg, ";\n");
                break;
            }
        case CUE_AST_STMT_FUNC_DECL:
            {
                CueListNode *node;

                _fmt(cg, "int\n"); /* TODO return types :) */
                _fmt(cg, "_%s__%s(void)", _join(cg->pool, "_", cg->ns), stmt->v.func_decl.name);
                _fmt(cg, "{\n");
                cg->indent++;
                node = stmt->v.func_decl.body->head;
                while (node != NULL) {
                    compile_stmt(cg, (CueAstStmt*)node->data);
                    node = node->next;
                }
                cg->indent--;
                _fmt(cg, "}\n\n");
                break;
            }
        default:
            fprintf(stderr, "unknown stmt type: %d\n", stmt->type);
    };
}
示例#3
0
文件: codegen.c 项目: thomaslee/cue
int
cue_code_generator_generate(CueCodeGenerator *cg, CueAstRoot *ast)
{
    int nsdepth = 0;
    CueList *body = ast->v.module.body;
    CueList *package = ast->v.module.package;
    CueList *package_str = cue_list_new(cg->pool);
    CueListNode *cur = cue_list_head(package);

    prelude(cg);
    while (cur != NULL) {
        CueAstExpr *name = cur->data;
        enter_namespace(cg, name->v.name.id);
        cue_list_push(package_str, name->v.name.id);
        cur = cur->next;
        nsdepth++;
    }

    _fmt(cg, "// enter package %s\n\n", _join(cg->pool, ".", package_str));

    cur = body->head;
    while (cur != NULL) {
        compile_stmt(cg, (CueAstStmt*)cur->data);
        cur = cur->next;
    }

    while (nsdepth > 0) {
        leave_namespace(cg);
        nsdepth--;
    }

    _fmt(cg, "// leave package %s\n\n", _join(cg->pool, ".", package_str));

    prologue(cg);

    return 0;
}