Beispiel #1
0
/* ::cortex::Fast::Expression::fromList(list{Expression} list) */
Fast_Expression Fast_Expression_fromList(Fast_Expression_list list) {
/* $begin(::cortex::Fast::Expression::fromList) */
    Fast_Expression result = NULL;

    /* Convert list to comma expression */
    if (list) {
        if (cx_llSize(list) == 1) {
            result = cx_llGet(list, 0);
        } else {
            cx_ll toList = cx_llNew(); /* Copy list */
            cx_iter iter;
            Fast_Expression expr;
            
            result = Fast_Expression(Fast_Comma__create());

            iter = cx_llIter(list);
            while(cx_iterHasNext(&iter)) {
                expr = cx_iterNext(&iter);
                cx_llAppend(toList, expr); cx_keep_ext(result, expr, "add expression from list to comma-expression");
            }
            Fast_Comma(result)->expressions = toList;
            Fast_Parser_collect(yparser(), result);
        }
    }
    
    return result;
/* $end */
}
Beispiel #2
0
/* ::cortex::Fast::Expression::toList() */
Fast_Expression_list Fast_Expression_toList_v(Fast_Expression _this) {
/* $begin(::cortex::Fast::Expression::toList) */
    Fast_Node_list result = NULL;
    
    if (_this) {
        result = cx_llNew();
        cx_llInsert(result, _this); cx_keep_ext(NULL, _this, "convert single expression to list");
    }
    
    return result;
/* $end */
}
Beispiel #3
0
/* Register exithandler */
void cx_onexit(void(*handler)(void*), void* userData) {
    struct cx_exitHandler* h;

    h = cx_malloc(sizeof(struct cx_exitHandler));
    h->handler = handler;
    h->userData = userData;

    cx_mutexLock(&cx_adminLock);
    if (!cx_exitHandlers) {
        cx_exitHandlers = cx_llNew();
    }
    cx_llInsert(cx_exitHandlers, h);
    cx_mutexUnlock(&cx_adminLock);
}
Beispiel #4
0
/* Run the server in a separate thread */
void* web_run(void *data) {
    struct mg_server *server;
    web_server _this = data;
    char portStr[6];
    sprintf(portStr, "%u", _this->port);
    cx_event e;
    cx_ll events = cx_llNew();

    // Create and configure the server
    server = mg_create_server(_this, web_handler);
    mg_set_option(server, "listening_port", portStr);

    // Serve request. Hit Ctrl-C to terminate the program
    for (;;) {
        mg_poll_server(server, 50);

        if (_this->exiting) {
            break;
        }

        /* Handle queued events */
        cx_lock(_this);

        while ((e = cx_llTakeFirst(_this->events))) {
            cx_llAppend(events, e);
        }
        cx_unlock(_this);

        /* Process events outside of lock */
        while ((e = cx_llTakeFirst(events))) {
            cx_event_handle(e);
            cx_free(e);
            mg_poll_server(server, 1);
        }
    }

    // Cleanup, and free server instance
    mg_destroy_server(&server);

    return NULL;
}
cx_word Fast_Initializer_offset(Fast_StaticInitializer _this, cx_uint32 variable) {
    cx_word result, base;
    cx_uint16 fp = Fast_Initializer(_this)->fp;
    Fast_InitializerFrame *frame = &Fast_Initializer(_this)->frames[fp?fp-1:0];
    Fast_StaticInitializerFrame *baseFrame = &(_this->frames[fp?fp-1:0]);
    Fast_InitializerFrame *thisFrame = &Fast_Initializer(_this)->frames[fp];
    result = 0;

    base = baseFrame->ptr[variable];
    if (!base) {
        Fast_Parser_error(yparser(), "parser error: base is zero in offset calculation");
        goto error;
    }

    /* Switch on current type */
    switch(frame->type->kind) {
    case CX_PRIMITIVE:
        result = base;
        break;
    case CX_COMPOSITE:
        if (fp) {
            result = base + thisFrame->member->offset;
        } else {
            result = base;
        }
        break;
    case CX_ITERATOR:
        result = base;
        break;
    case CX_COLLECTION: {
        if (fp) {
            cx_uint32 elementSize = cx_type_sizeof(cx_collection(frame->type)->elementType);
            switch(cx_collection(frame->type)->kind) {
            case CX_SEQUENCE:
                ((cx_objectSeq*)base)->length++;
                ((cx_objectSeq*)base)->buffer = cx_realloc(((cx_objectSeq*)base)->buffer, ((cx_objectSeq*)base)->length * elementSize);
                base = (cx_word)((cx_objectSeq*)base)->buffer;
            case CX_ARRAY:
                result = base + thisFrame->location * cx_type_sizeof(cx_collection(frame->type)->elementType);
                memset((void*)result, 0, elementSize);
                break;
            case CX_LIST: {
                if (cx_collection_elementRequiresAlloc(cx_collection(frame->type))) {
                    result = (cx_word)cx_calloc(elementSize);
                }
                if (!*(cx_ll*)base) {
                    *(cx_ll*)base = cx_llNew();
                }
                cx_llAppend(*(cx_ll*)base, (void*)result);
                if (!result) {
                    result = (cx_word)cx_llGetPtr(*(cx_ll*)base, cx_llSize(*(cx_ll*)base)-1);
                }
                break;
            }
            case CX_MAP: {
                cx_type keyType = cx_map(frame->type)->keyType;
                if (!thisFrame->isKey) {
                    if (cx_collection_elementRequiresAlloc(cx_collection(frame->type))) {
                        result = (cx_word)cx_calloc(elementSize);
                    }
                    if (!*(cx_rbtree*)base) {
                        *(cx_rbtree*)base = cx_rbtreeNew(frame->type);
                    }
                    cx_rbtreeSet(*(cx_rbtree*)base, (void*)_this->frames[fp].keyPtr[variable], (void*)result);
                    if (!result) {
                        if (_this->frames[fp].keyPtr[variable]) {
                            result = (cx_word)cx_rbtreeGetPtr(*(cx_rbtree*)base, (void*)_this->frames[fp].keyPtr[variable]);
                        } else {
                            Fast_Parser_error(yparser(), "cannot set element without keyvalue");
                            goto error;
                        }
                    }
                } else {
                    result = (cx_word)cx_calloc(cx_type_sizeof(keyType));
                    _this->frames[fp].keyPtr[variable] = result;
                    thisFrame->isKey = FALSE;
                }
                break;
            }
            }
        } else {
            result = base;
        }
        break;
    }
    default: {
        cx_id id;
        Fast_Parser_error(yparser(), "invalid initializer type '%s'", Fast_Parser_id(frame->type, id));
        break;
    }
    }

    return result;
error:
    return 0;
}