void Branch__format_function_heading(caStack* stack)
{
    Branch* branch = as_branch(circa_input(stack, 0));
    caValue* output = circa_output(stack, 0);
    circa_set_list(output, 0);
    function_format_header_source(output, branch);
}
void Branch__format_source(caStack* stack)
{
    Branch* branch = as_branch(circa_input(stack, 0));

    caValue* output = circa_output(stack, 0);
    circa_set_list(output, 0);
    format_branch_source((caValue*) output, branch);
}
Beispiel #3
0
void make_server(Stack* stack)
{
    Value* ip = circa_input(stack, 0);
    Value* port = circa_input(stack, 1);
    Value* type = circa_input(stack, 2);

    Server* server = new Server();

    if (circa_string_equals(type, ":tcp")) {
        server->serverType = TCP;
    } else if (circa_string_equals(type, ":websock")) {
        server->serverType = WEBSOCK;
        memset(&server->parser_settings, 0, sizeof(server->parser_settings));
        server->parser_settings.on_message_begin = http_on_message_begin;
        server->parser_settings.on_url = http_on_url;
        server->parser_settings.on_status = http_on_status;
        server->parser_settings.on_header_field = http_on_header_field;
        server->parser_settings.on_header_value = http_on_header_value;
        server->parser_settings.on_headers_complete = http_on_headers_complete;
        server->parser_settings.on_body = http_on_body;
        server->parser_settings.on_message_complete = http_on_message_complete;

    } else {
        Value msg;
        circa_set_string(&msg, "Unrecognized server type: ");
        circa_string_append_val(&msg, type);
        circa_output_error_val(stack, &msg);
        delete server;
        return;
    }

    uv_loop_t* loop = get_uv_loop(stack->world);
    circa_set_list(&server->connections, 0);

    uv_tcp_init(stack->world->libuvWorld->uv_loop, &server->uv_tcp);

    struct sockaddr_in bind_addr = uv_ip4_addr(circa_string(ip), circa_int(port));

    uv_tcp_bind(&server->uv_tcp, bind_addr);
    int err = uv_listen((uv_stream_t*) &server->uv_tcp, 128, server_on_connect);
    server->uv_tcp.data = server;

    if (err) {
        Value msg;
        circa_set_string(&msg, "Listen error: ");
        circa_string_append(&msg, uv_err_name(uv_last_error(loop)));
        circa_output_error_val(stack, &msg);
        return;
    }

    Value* out = circa_set_default_output(stack, 0);
    circa_set_native_ptr(circa_index(out, 0), server, ServerRelease);
}
Beispiel #4
0
void Term__inputs(VM* vm)
{
    Term* t = as_term_ref(vm->input(0));
    if (t == NULL)
        return vm->throw_str("NULL term");

    Value* output = vm->output();
    circa_set_list(output, t->numInputs());

    for (int i=0; i < t->numInputs(); i++)
        set_term_ref(circa_index(output, i), t->input(i));
}
Beispiel #5
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

}
void Term__inputs(caStack* stack)
{
    Term* t = as_term_ref(circa_input(stack, 0));
    if (t == NULL)
        return circa_output_error(stack, "NULL reference");

    caValue* output = circa_output(stack, 0);
    circa_set_list(output, t->numInputs());

    for (int i=0; i < t->numInputs(); i++)
        set_term_ref(circa_index(output, i), t->input(i));
}
Beispiel #7
0
void Block__statements(VM* vm)
{
    Block* block = (Block*) circa_block(vm->input(0));
    if (block == NULL)
        return vm->throw_str("NULL block");

    Value* out = vm->output();

    circa_set_list(out, 0);

    for (int i=0; i < block->length(); i++)
        if (is_statement(block->get(i)))
            circa_set_term(circa_append(out), (caTerm*) block->get(i));
}
void Branch__statements(caStack* stack)
{
    Branch* branch = (Branch*) circa_branch(circa_input(stack, 0));
    if (branch == NULL)
        return circa_output_error(stack, "NULL branch");

    caValue* out = circa_output(stack, 0);

    circa_set_list(out, 0);

    for (int i=0; i < branch->length(); i++)
        if (is_statement(branch->get(i)))
            circa_set_term(circa_append(out), (caTerm*) branch->get(i));
}
Beispiel #9
0
void ImprovWindow::init(caWorld* world)
{
    _world = world;

    _inputEvents = circa_alloc_value();
    circa_set_list(_inputEvents, 0);

    _stack = circa_create_stack(_world);

    caBlock* main = circa_load_module(_world, NULL, "improv_top_layer");

    if (main == NULL) {
        printf("fatal: Couldn't load improv_top_layer.ca module\n");
        return;
    }

    circa_stack_init(_stack, main);
}
Beispiel #10
0
void test_cast_first_inputs()
{
    // Pass an input of [1] to a branch that expects a compound type.
    // The function will need to cast the [1] to T in order for it to work.

    Branch branch;
    branch.compile("type T { int i }");
    Term* f = branch.compile("def f(T t) -> int { return t.i }");

    Stack context;
    push_frame(&context, function_contents(f));

    caValue* in = circa_input((caStack*) &context, 0);
    circa_set_list(in, 1);
    circa_set_int(circa_index(in, 0), 5);

    run_interpreter(&context);

    test_assert(circa_int(circa_output((caStack*) &context, 0)) == 5);
}
Beispiel #11
0
static void server_on_connect(uv_stream_t *uv_server, int status)
{
    Server* server = (Server*) uv_server->data;

    if (status == -1) {
        printf("server_on_connect failed\n");
        return;
    }

    Connection* connection = new Connection();
    connection->server = server;
    uv_tcp_init(uv_server->loop, &connection->uv_tcp);
    connection->uv_tcp.data = connection;

    switch (connection->server->serverType) {
    case WEBSOCK:
        http_parser_init(&connection->parser, HTTP_REQUEST);
        circa_set_map(&connection->httpHeaders);
        connection->parser.data = connection;
        connection->state = WEBSOCK_NEGOTIATE_STATE;
        break;
    case TCP:
        connection->state = TCP_STATE;
        break;
    }

    if (uv_accept(uv_server, (uv_stream_t*) &connection->uv_tcp) != 0)
        internal_error("uv_accept error\n");

    if (uv_read_start((uv_stream_t*) &connection->uv_tcp, alloc_buffer, on_read) != 0)
        internal_error("uv_read_start error\n");

    Value* wrapped = circa_append(&server->connections);
    circa_set_list(wrapped, 1);
    circa_set_native_ptr(circa_index(wrapped, 0), connection, ConnectionRelease);
    
    printf("on new connection\n");
}
Beispiel #12
0
 Connection() {
     server = NULL;
     circa_set_string(&incomingStr, "");
     circa_set_list(&incomingMsgs, 0);
 }
Beispiel #13
0
void Connection__receive(Stack* stack)
{
    Connection* connection = (Connection*) circa_native_ptr(circa_index(circa_input(stack, 0), 0));
    circa_move(&connection->incomingMsgs, circa_output(stack, 0));
    circa_set_list(&connection->incomingMsgs, 0);
}
Beispiel #14
0
void ImprovWindow::mainLoop()
{
    // Main loop.
    while (true) {
        
        // Handle events.
        SDL_Event sdlEvent;
        while (SDL_PollEvent(&sdlEvent)) {

            switch (sdlEvent.type) 
            {
            case SDL_QUIT:
                printf("received SDL_QUIT\n");
                return;

            case SDL_KEYDOWN: {

                #ifndef NACL
                    // Quit on Escape key
                    if (sdlEvent.key.keysym.sym == SDLK_ESCAPE)
                        return;

                    #if 0
                    // Fullscreen on command-F
                    if (sdlEvent.key.keysym.sym == SDLK_f && (sdlEvent.key.keysym.mod & KMOD_CTRL))
                        toggleFullscreen();
                    #endif
                #endif

                caValue* event = circa_append(_inputEvents);
                circa_set_list(event, 2);
                circa_set_symbol(circa_index(event, 0), "down");
                sdl_key_to_symbol(sdlEvent.key.keysym.sym, circa_index(event, 1));
                break;
            }
            case SDL_KEYUP: {
                caValue* event = circa_append(_inputEvents);
                circa_set_list(event, 2);
                circa_set_symbol(circa_index(event, 0), "up");
                sdl_key_to_symbol(sdlEvent.key.keysym.sym, circa_index(event, 1));
                break;
            }

            case SDL_MOUSEBUTTONDOWN: {
                double mouseX = sdlEvent.button.x;
                double mouseY = sdlEvent.button.y;

                caValue* event = circa_append(_inputEvents);
                circa_set_list(event, 3);
                circa_set_symbol(circa_index(event, 0), "down");
                if (sdlEvent.button.button == SDL_BUTTON_LEFT)
                    circa_set_symbol(circa_index(event, 1), "left_mouse");
                else
                    circa_set_symbol(circa_index(event, 1), "right_mouse");
                circa_set_vec2(circa_index(event, 2), mouseX, mouseY);
                break;
            }

            case SDL_MOUSEBUTTONUP: {
                double mouseX = sdlEvent.button.x;
                double mouseY = sdlEvent.button.y;

                caValue* event = circa_append(_inputEvents);
                circa_set_list(event, 3);
                circa_set_symbol(circa_index(event, 0), "up");
                if (sdlEvent.button.button == SDL_BUTTON_LEFT)
                    circa_set_symbol(circa_index(event, 1), "left_mouse");
                else
                    circa_set_symbol(circa_index(event, 1), "right_mouse");
                circa_set_vec2(circa_index(event, 2), mouseX, mouseY);
                break;
            }

            case SDL_MOUSEWHEEL: {
                caValue* event = circa_append(_inputEvents);
                circa_set_list(event, 3);
                circa_set_symbol(circa_index(event, 0), "mouse_wheel");
                circa_set_vec2(circa_index(event, 1), sdlEvent.wheel.x, sdlEvent.wheel.y);
                break;
            }

            case SDL_WINDOWEVENT: {
                switch (sdlEvent.window.event) {
                case SDL_WINDOWEVENT_RESIZED:
                    int width = sdlEvent.window.data1;
                    int height = sdlEvent.window.data2;
                    setSize(width, height);
                    break;
                }
                break;
            }
            }
        }

        // Done handling events. Redraw screen.

        _elapsedTime = SDL_GetTicks() / 1000.0;

        int mouseX, mouseY;
        SDL_GetMouseState(&mouseX, &mouseY);
        _mouseX = mouseX;
        _mouseY = mouseY;

        redraw();

        SDL_GL_SwapWindow(_sdl_window);
        SDL_Delay(1);
    }
}