yangtau/hedgehog

splite id and value

github-actions opened this issue · 1 comments

splite id and value

// TODO: splite id and value

}

static int compile_ast_node_value(struct compiler_context* ctx, void* _value) {
    struct hg_value* val = _value;

    int rc = 0;

    if (VAL_IS_BOOL(*val)) {
        chunk_write(ctx->chk, VAL_AS_BOOL(*val) ? OP_TRUE : OP_FALSE);
    } else if (VAL_IS_OBJ(*val) && (VAL_AS_OBJ(*val)->type == HG_OBJ_SYMBOL)) {
        // TODO: splite id and value
        bool islocal;
        int loc;
        if ((loc = find_variable(ctx, *val, &islocal)) == -1) {
            fprintf(stderr, "compile error: reference before definition `");
            hg_value_write(*val, stderr, true);
            fprintf(stderr, "`\n");
            rc = -1;
        } else {
            chunk_write(ctx->chk, islocal ? OP_GET_LOCAL : OP_GET_STATIC);
            chunk_write_word(ctx->chk, loc);
        }
    } else {
        uint16_t loc = chunk_add_const(ctx->chk, *val);
        chunk_write(ctx->chk, OP_GET_CONST);
        chunk_write_word(ctx->chk, loc);
    }
    return rc;
}

static int compile_ast_node_stats(struct compiler_context* ctx, void* _stats) {
    struct ast_node_array* stats = _stats;

    int rc = 0;
    for (size_t i = 0; i < stats->len; i++) {
        rc |= compile_ast_node(ctx, stats->arr[i]);
    }

    return rc;
}

static int compile_ast_node_tuple(struct compiler_context* ctx, void* _tuple) {
    struct ast_node_array* tuple = _tuple;
    switch (tuple->len) {
    case 0: // nil
        chunk_write(ctx->chk, OP_NIL);
        break;
    case 1: // unpack if there is only one element in the tuple
        compile_ast_node(ctx, tuple->arr[0]);
        break;
    default:
        // TODO: Compile tuple

9bd8f0ab25866676cbf8cbffb3a8dae5f2cd26bd