Beispiel #1
0
int main(int argc, char** argv)
{
    caWorld* world = circa_initialize();

    circa_load_module_from_file(world, "ClassA", "ClassA.ca");

    // circa_dump_b(circa_kernel(world));

    caStack* stack = circa_alloc_stack(world);

    circa_push_function_by_name(stack, "create_ClassA");
    circa_run(stack);

    if (circa_has_error(stack))
        circa_print_error_to_stdout(stack);

    caValue* classA = circa_alloc_value();
    circa_move(circa_output(stack, 0), classA);
    circa_pop(stack);

    // Dump to stdout
    circa_push_function_by_name(stack, "ClassA.dump");
    circa_copy(classA, circa_input(stack, 0));
    circa_run(stack);
    if (circa_has_error(stack))
        circa_print_error_to_stdout(stack);
    circa_pop(stack);

    for (int i=0; i < 5; i++) {
        // Increment
        circa_push_function_by_name(stack, "ClassA.increment");
        circa_copy(classA, circa_input(stack, 0));
        circa_run(stack);
        if (circa_has_error(stack))
            circa_print_error_to_stdout(stack);

        // Using index #1 not 0:
        circa_move(circa_output(stack, 1), classA);
        circa_pop(stack);

        // And dump
        circa_push_function_by_name(stack, "ClassA.dump");
        circa_copy(classA, circa_input(stack, 0));
        circa_run(stack);
        if (circa_has_error(stack))
            circa_print_error_to_stdout(stack);
        circa_pop(stack);
    }

    circa_dealloc_value(classA);
    circa_dealloc_stack(stack);
    circa_shutdown(world);
}
Beispiel #2
0
void ImprovWindow::redraw()
{

#ifndef NACL
    circa_update_changed_files(_world);
#endif
    
    if (_width == 0 && _height == 0) {
        printf("error: App::redraw() called before setSize()\n");
        return;
    }
    
    double startTime = get_time();
    
    glClear( GL_COLOR_BUFFER_BIT /*| GL_DEPTH_BUFFER_BIT*/ );
    
    circa_copy(_inputEvents, circa_env_insert(_stack, "inputEvents"));
    
    circa_set_vec2(circa_env_insert(_stack, "mouse"), _mouseX, _mouseY);
    circa_set_vec2(circa_env_insert(_stack, "windowSize"), _width, _height);
    circa_set_float(circa_env_insert(_stack, "time"), _elapsedTime);
    circa_set_float(circa_env_insert(_stack, "lastFrameDuration"), _lastFrameDuration);
    
#ifdef NACL
    circa_set_bool(circa_env_insert(_stack, "gl_es2"), true);
#endif
    
    circa_run(_stack);
    
    if (circa_has_error(_stack)) {
        circa_dump_stack_trace(_stack);
        
#ifndef NACL
        exit(1);
#endif
    }
    
    // Cleanup stack
    circa_restart(_stack);
    
    // Cleanup
    circa_set_list(_inputEvents, 0);
    
    _lastFrameDuration = get_time() - startTime;
    
#define DUMP_PERF_STATS 0
#if DUMP_PERF_STATS
    printf("frame duration = %f\n", (float) _lastFrameDuration);
    circa_perf_stats_dump();
    circa_perf_stats_reset();
#endif

}
Beispiel #3
0
void test_explicit_output()
{
    World* world = global_world();

    test_write_fake_file("module.ca", 1, "99 -> output");

    circa_load_module_from_file(world, "Module", "module.ca");

    Stack* stack = circa_create_stack(world);

    circa_push_module(stack, "Module");
    circa_run(stack);

    test_assert(stack);
    test_equals(circa_output(stack, 0), "99");

    circa_free_stack(stack);
}
Beispiel #4
0
int main(int argc, char** argv)
{
    const int iteratations = 100;

    caWorld* world = circa_initialize();
    caStack* stack = circa_alloc_stack(world);

    // Write initial version
    write_script_to_file(0);

    caBranch* module = circa_load_module_from_file(world, "file_to_reload", "file_to_reload.ca");

    int currentKey = 0;

    for (int i=0; i < iteratations; i++) {

        // Update file on every other iteration
        if ((i % 2) == 1) {
            printf("writing to file: %d\n", i);
            currentKey = i;
            write_script_to_file(i);
            sleep(2);
        }

        circa_refresh_module(module);

        circa_push_function_by_name(stack, "f");
        circa_run(stack);
        if (circa_has_error(stack)) {
            circa_print_error_to_stdout(stack);
            break;
        }

        int readValue = circa_int(circa_output(stack, 0));

        if (currentKey != readValue) {
            printf("Failed, currentKey (%d) != readValue (%d)\n", currentKey, readValue);
            break;
        }
        circa_pop(stack);
    }

    circa_shutdown(world);
}
Beispiel #5
0
int run_command_line(caWorld* world, caValue* args)
{
    RawOutputPrefs rawOutputPrefs;
    bool printRaw = false;
    bool printState = false;
    bool dontRunScript = false;
    bool printTrace = false;

    // Prepended options
    while (true) {

        if (list_length(args) == 0)
            break;

        if (string_eq(list_get(args, 0), "-break-on")) {
            DEBUG_BREAK_ON_TERM = atoi(as_cstring(list_get(args, 1)));

            list_remove_index(args, 0);
            list_remove_index(args, 0);
            std::cout << "breaking on creation of term: " << DEBUG_BREAK_ON_TERM << std::endl;
            continue;
        }

        if (string_eq(list_get(args, 0), "-path")) {
            // Add a module path
            module_add_search_path(world, as_cstring(list_get(args, 1)));
            list_remove_index(args, 0);
            list_remove_index(args, 0);
            continue;
        }

        if (string_eq(list_get(args, 0), "-p")) {
            printRaw = true;
            list_remove_index(args, 0);
            continue;
        }

        if (string_eq(list_get(args, 0), "-pp")) {
            printRaw = true;
            rawOutputPrefs.showProperties = true;
            list_remove_index(args, 0);
            continue;
        }

        if (string_eq(list_get(args, 0), "-b") || string_eq(list_get(args, 0), "-pb")) {
            printRaw = true;
            rawOutputPrefs.showBytecode = true;
            list_remove_index(args, 0);
            continue;
        }

        if (string_eq(list_get(args, 0), "-n")) {
            dontRunScript = true;
            list_remove_index(args, 0);
            continue;
        }
        if (string_eq(list_get(args, 0), "-print-state")) {
            printState = true;
            list_remove_index(args, 0);
            continue;
        }
        if (string_eq(list_get(args, 0), "-t")) {
            printTrace = true;
            list_remove_index(args, 0);
            continue;
        }

        if (string_eq(list_get(args, 0), "-load")) {
            caValue* filename = list_get(args, 1);

            Value moduleName;
            module_get_default_name_from_filename(filename, &moduleName);

            list_remove_index(args, 0);
            list_remove_index(args, 0);
            continue;
        }

        break;
    }

    // No arguments remaining
    if (list_length(args) == 0) {
        print_usage();
        return 0;
    }
    
    Block* mainBlock = fetch_module(world, "main");

    // Check to handle args[0] as a dash-command.

    // Print help
    if (string_eq(list_get(args, 0), "-help")) {
        print_usage();
        return 0;
    }

    // Eval mode
    if (string_eq(list_get(args, 0), "-e")) {
        list_remove_index(args, 0);

        Value command;
        set_string(&command, "");

        bool firstArg = true;
        while (!list_empty(args)) {
            if (!firstArg)
                string_append(&command, " ");
            string_append(&command, list_get(args, 0));
            list_remove_index(args, 0);
            firstArg = false;
        }

        caValue* result = term_value(mainBlock->eval(as_cstring(&command)));
        std::cout << to_string(result) << std::endl;
        return 0;
    }

    // Start repl
    if (string_eq(list_get(args, 0), "-repl")) {
        run_repl_stdin(world);
        return 0;
    }

    if (string_eq(list_get(args, 0), "-call")) {
        Symbol loadResult = load_script(mainBlock, as_cstring(list_get(args, 1)));

        if (loadResult == sym_Failure) {
            std::cout << "Failed to load file: " <<  as_cstring(list_get(args, 1)) << std::endl;
            return -1;
        }

        block_finish_changes(mainBlock);

        caStack* stack = circa_alloc_stack(world);

        // Push function
        caBlock* func = circa_find_function_local(mainBlock, as_cstring(list_get(args, 2)));
        circa_push_function(stack, func);

        // Push inputs
        for (int i=3, inputIndex = 0; i < circa_count(args); i++) {
            caValue* val = circa_input(stack, inputIndex++);
            circa_parse_string(as_cstring(list_get(args, i)), val);
        }

        circa_run(stack);

        if (circa_has_error(stack)) {
            circa_print_error_to_stdout(stack);
        }

        // Print outputs
        for (int i=0;; i++) {
            caValue* out = circa_output(stack, i);
            if (out == NULL)
                break;

            std::cout << to_string(circa_output(stack, i)) << std::endl;
        }
        
        circa_dealloc_stack(stack);
    }

    // Start debugger repl
    if (string_eq(list_get(args, 0), "-d"))
        return run_debugger_repl(as_cstring(list_get(args, 1)));

    // Generate cpp headers
    if (string_eq(list_get(args, 0), "-gh")) {
        load_script(mainBlock, as_cstring(list_get(args, 1)));
        std::cout << generate_cpp_headers(mainBlock);
        return 0;
    }

    // Run file checker
    if (string_eq(list_get(args, 0), "-check"))
        return run_file_checker(as_cstring(list_get(args, 1)));

    // Export parsed information
    if (string_eq(list_get(args, 0), "-export")) {
        const char* filename = "";
        const char* format = "";
        if (list_length(args) >= 2)
            format = as_cstring(list_get(args, 1));
        if (list_length(args) >= 3)
            filename = as_cstring(list_get(args, 2));
        return run_exporting_parser(format, filename);
    }

    // Build tool
    if (string_eq(list_get(args, 0), "-build")) {
        return run_build_tool(args);
    }

    // C++ gen
    if (string_eq(list_get(args, 0), "-cppgen")) {
        Value remainingArgs;
        list_slice(args, 1, -1, &remainingArgs);
        run_generate_cpp(&remainingArgs);
        return 0;
    }

    // Command reader (from stdin)
    if (string_eq(list_get(args, 0), "-run-stdin")) {
        run_commands_from_stdin();
        return 0;
    }

    // Reproduce source text
    if (string_eq(list_get(args, 0), "-source-repro")) {
        load_script(mainBlock, as_cstring(list_get(args, 1)));
        std::cout << get_block_source_text(mainBlock);
        return 0;
    }

    // Rewrite source, this is useful for upgrading old source
    if (string_eq(list_get(args, 0), "-rewrite-source")) {
        load_script(mainBlock, as_cstring(list_get(args, 1)));
        std::string contents = get_block_source_text(mainBlock);
        write_text_file(as_cstring(list_get(args, 1)), contents.c_str());
        return 0;
    }

    // Default behavior with no flags: run args[0] as a script filename.

    // Add the script's folder to module search paths.
    caValue* filename = list_get(args, 0);
    Value directory;
    get_directory_for_filename(filename, &directory);
    module_add_search_path(world, as_cstring(&directory));

    load_script(mainBlock, as_cstring(filename));
    block_finish_changes(mainBlock);
    refresh_bytecode(mainBlock);

    if (printRaw)
        print_block(mainBlock, &rawOutputPrefs, std::cout);

    if (dontRunScript)
        return 0;

    Stack* stack = alloc_stack(world);

    push_frame(stack, mainBlock);

    run_interpreter(stack);

    if (printState)
        std::cout << to_string(&stack->state) << std::endl;

    if (error_occurred(stack)) {
        std::cout << "Error occurred:\n";
        print_error_stack(stack, std::cout);
        std::cout << std::endl;
        std::cout << "Stack:\n";
        dump(stack);
        return 1;
    }

    return 0;
}