Ejemplo n.º 1
0
void Block__link(VM* vm)
{
    Block* self = (Block*) circa_block(vm->input(0));
    Block* source = (Block*) circa_block(vm->input(1));

    block_link_missing_functions(self, source);
}
Ejemplo n.º 2
0
void bootstrap_kernel()
{
    memset(&FUNCS, 0, sizeof(FUNCS));
    memset(&TYPES, 0, sizeof(TYPES));

    // Allocate a World object.
    g_world = alloc_world();
    g_world->bootstrapStatus = s_Bootstrapping;
    World* world = g_world;

    // Instanciate the types that are used by Type.
    TYPES.table = create_type_unconstructed();
    TYPES.nil = create_type_unconstructed();
    TYPES.string = create_type_unconstructed();
    TYPES.type = create_type_unconstructed();

    // Now we can fully instanciate types.
    type_finish_construction(TYPES.table);
    type_finish_construction(TYPES.nil);
    type_finish_construction(TYPES.string);
    type_finish_construction(TYPES.type);
    string_setup_type(TYPES.string);

    // Initialize remaining global types.
    TYPES.any = create_type();
    TYPES.blob = create_type();
    TYPES.block = create_type();
    TYPES.bool_type = create_type();
    TYPES.error = create_type();
    TYPES.float_type = create_type();
    TYPES.int_type = create_type();
    TYPES.list = create_type();
    TYPES.native_ptr = create_type();
    TYPES.table = create_type();
    TYPES.opaque_pointer = create_type();
    TYPES.symbol = create_type();
    TYPES.term = create_type();
    TYPES.vm = create_type();
    TYPES.void_type = create_type();

    for_each_root_type(type_set_root);

    any_setup_type(TYPES.any);
    blob_setup_type(TYPES.blob);
    block_setup_type(TYPES.block);
    bool_setup_type(TYPES.bool_type);
    hashtable_setup_type(TYPES.table);
    int_setup_type(TYPES.int_type);
    list_t::setup_type(TYPES.list);
    symbol_setup_type(TYPES.symbol);
    native_ptr_setup_type(TYPES.native_ptr);
    null_setup_type(TYPES.nil);
    number_setup_type(TYPES.float_type);
    opaque_pointer_setup_type(TYPES.opaque_pointer);
    term_setup_type(TYPES.term);
    string_setup_type(TYPES.error); // errors are just stored as strings for now
    type_t::setup_type(TYPES.type);
    void_setup_type(TYPES.void_type);
    vm_setup_type(TYPES.vm);

    // Finish initializing World (this requires List and Hashtable types)
    world_initialize(g_world);

    // Create builtins block.
    Value builtinsStr;
    set_string(&builtinsStr, "builtins");
    Block* builtins = create_module(g_world);
    module_set_name(world, builtins, &builtinsStr);
    g_world->builtins = builtins;

    // Create function_decl function.
    Term* functionDeclFunction = builtins->appendNew();
    rename(functionDeclFunction, "function_decl");
    FUNCS.function_decl = functionDeclFunction;
    FUNCS.function_decl->function = FUNCS.function_decl;
    make_nested_contents(FUNCS.function_decl);
    block_set_function_has_nested(nested_contents(FUNCS.function_decl), true);

    // Create value function
    Term* valueFunc = builtins->appendNew();
    rename(valueFunc, "value");
    FUNCS.value = valueFunc;

    // Create Type type
    Term* typeType = builtins->appendNew();
    typeType->function = FUNCS.value;
    typeType->type = TYPES.type;
    term_value(typeType)->value_type = TYPES.type;
    term_value(typeType)->value_data.ptr = TYPES.type;
    TYPES.type->declaringTerm = typeType;
    rename(typeType, "Type");

    // Create Any type
    Term* anyType = builtins->appendNew();
    anyType->function = valueFunc;
    anyType->type = TYPES.type;
    term_value(anyType)->value_type = TYPES.type;
    term_value(anyType)->value_data.ptr = TYPES.any;
    TYPES.any->declaringTerm = anyType;
    rename(anyType, "any");

    // Initialize value() func
    valueFunc->type = TYPES.any;
    valueFunc->function = FUNCS.function_decl;
    make_nested_contents(valueFunc);
    block_set_evaluation_empty(nested_contents(valueFunc), true);

    // Initialize primitive types (this requires value() function)
    create_type_value(builtins, TYPES.blob, "Blob");
    create_type_value(builtins, TYPES.bool_type, "bool");
    create_type_value(builtins, TYPES.block, "Block");
    create_type_value(builtins, TYPES.float_type, "number");
    create_type_value(builtins, TYPES.int_type, "int");
    create_type_value(builtins, TYPES.list, "List");
    create_type_value(builtins, TYPES.opaque_pointer, "opaque_pointer");
    create_type_value(builtins, TYPES.native_ptr, "native_ptr");
    create_type_value(builtins, TYPES.string, "String");
    create_type_value(builtins, TYPES.symbol, "Symbol");
    create_type_value(builtins, TYPES.term, "Term");
    create_type_value(builtins, TYPES.table, "Table");
    create_type_value(builtins, TYPES.void_type, "void");
    create_type_value(builtins, TYPES.vm, "VM");

    // Create global symbol table (requires Hashtable type)
    symbol_initialize_global_table();

    // Setup output_placeholder() function, needed to declare functions properly.
    FUNCS.output = apply(builtins, FUNCS.function_decl, TermList(), "output_placeholder");
    nested_contents(FUNCS.output)->overrides.specializeType = output_placeholder_specializeType;
    ca_assert(get_output_type(nested_contents(FUNCS.output), 0) == TYPES.any);

    // Now that output_placeholder is created, fix the value() function.
    {
        Term* output = append_output_placeholder(nested_contents(valueFunc), NULL);
        set_declared_type(output, TYPES.any);
        finish_building_function(nested_contents(valueFunc));
    }

    ca_assert(get_output_type(nested_contents(valueFunc), 0) == TYPES.any);

    // input_placeholder() is needed before we can declare a function with inputs
    FUNCS.input = apply(builtins, FUNCS.function_decl, TermList(), "input_placeholder");
    block_set_evaluation_empty(nested_contents(FUNCS.input), true);

    // Now that we have input_placeholder(), declare one input on output_placeholder()
    apply(nested_contents(FUNCS.output),
        FUNCS.input, TermList())->setBoolProp(s_Optional, true);

    // Initialize a few more types
    TYPES.selector = unbox_type(create_value(builtins, TYPES.type, "Selector"));
    list_t::setup_type(TYPES.selector);

    // Need the comment() function before parsing stdlib.ca
    FUNCS.comment = apply(builtins, FUNCS.function_decl, TermList(), "comment");

    // Parse stdlib.ca
    parse(builtins, parse_statement_list, find_builtin_file("$builtins/stdlib.ca"));
    set_string(block_insert_property(builtins, s_ModuleName), "stdlib");

    blob_install_functions(world->builtinPatch);
    selector_setup_funcs(world->builtinPatch);
    closures_install_functions(world->builtinPatch);
    reflection_install_functions(world->builtinPatch);
    misc_builtins_setup_functions(world->builtinPatch);
    type_install_functions(world->builtinPatch);
    vm_install_functions(world->builtinPatch);

    block_set_bool_prop(builtins, s_Builtins, true);

    ca_assert(FUNCS.declared_state != NULL);

    nested_contents(FUNCS.add)->overrides.specializeType = specializeType_add_sub_mult;
    nested_contents(FUNCS.sub)->overrides.specializeType = specializeType_add_sub_mult;
    nested_contents(FUNCS.mult)->overrides.specializeType = specializeType_add_sub_mult;
    nested_contents(FUNCS.div)->overrides.specializeType = specializeType_div;

    FUNCS.get_with_symbol = builtins->get("get_with_symbol");
    FUNCS.length = builtins->get("length");
    FUNCS.list_append = builtins->get("List.append");
    FUNCS.native_patch = builtins->get("native_patch");
    FUNCS.package = builtins->get("package");

    nested_contents(builtins->get("Type.cast"))->overrides.specializeType = Type_cast_specializeType;

    // Finish setting up types that are declared in stdlib.ca.
    TYPES.color = as_type(builtins->get("Color"));
    TYPES.func = as_type(builtins->get("Func"));
    TYPES.module_ref = as_type(builtins->get("Module"));
    TYPES.vec2 = as_type(builtins->get("Vec2"));

    // Fix function_decl now that Func type is available.
    {
        set_declared_type(append_output_placeholder(nested_contents(FUNCS.function_decl), NULL),
            TYPES.func);
        set_declared_type(FUNCS.function_decl, TYPES.func);
        finish_building_function(nested_contents(FUNCS.function_decl));
    }

    // Also, now that Func type is available, update all static closures.
    for (BlockIterator it(builtins); it; ++it) {
        Term* term = *it;
        if (is_function(term)) {
            set_declared_type(term, TYPES.func);
            if (term->owningBlock == builtins)
                // Functions at top level must be static closures
                update_static_closure_force(term);
            else
                update_static_closure_if_possible(term);
        }
    }

    nested_contents(FUNCS.list_append)->overrides.specializeType = List__append_specializeType;

    #define set_evaluation_empty(name) block_set_evaluation_empty(nested_contents(FUNCS.name), true)
        set_evaluation_empty(annotate);
        set_evaluation_empty(annotate_block);
        set_evaluation_empty(return_func);
        set_evaluation_empty(discard);
        set_evaluation_empty(break_func);
        set_evaluation_empty(continue_func);
        set_evaluation_empty(comment);
        set_evaluation_empty(extra_output);
        set_evaluation_empty(loop_iterator);
        set_evaluation_empty(static_error);
    #undef set_evaluation_empty

    block_link_missing_functions(builtins, builtins);
}