Example #1
0
void print_object(pointer P, symbol_table* table)
{
    FILE* out = stdout;
    if(P == NIL)
    {
        fputs("NIL", out);
        return;
    }

    switch(get_type_id(P))
    {
    case DT_Pair:
        if(is_type(pair_car(P), DT_Pair))
        {
            putc('(', out);
            print_object(pair_car(P), table);
            putc(')', out);
        }
        else
            print_object(pair_car(P), table);

        putc(' ', out);
        if(pair_cdr(P) != NIL)
        {
            if(!is_type(pair_cdr(P), DT_Pair))
                fputs(". ", out);

            print_object(pair_cdr(P), table);
        }
        break;
    case DT_Symbol:
        fputs(string_from_symbol(table, *get_symbol(P)), out);
        break;
    case DT_Int:
        fprintf(out, "%d", get_int(P));
        break;
    case DT_Real:
        fprintf(out, "%f", get_real(P));
        break;
    case DT_String:
        fputs(get_string(P), out);
        break;
    case DT_Char:
        putc(get_char(P), out);
        break;
    case DT_TypeInfo:
        print_typeinfo(P, table, out);
        break;
    case DT_Invalid:
        fputs("#INVALID#", out);
        break;
    case DT_Any:
        fputs("#ANY#", out);
        break;
    }
}
Example #2
0
void ploy_free(pointer P)
{
    if(P == NIL ||
            *(dynamic_types*)P & DT_Static_Flag)
        return; // Abort free for static or NIL objects.
    const dynamic_type* T = get_type(get_type_id(P));
    if(T->finish)
        T->finish(P);
    free(P);
}
Example #3
0
static LEXEME *getTypeList(LEXEME *lex, SYMBOL *funcsp, INITLIST **lptr)
{
    *lptr = NULL;
    do
    {
        TYPE *tp = NULL;
        lex = getsym(); /* past ( or , */
        lex = get_type_id(lex, &tp, funcsp, sc_cast, FALSE, TRUE);
        if (!tp)
            break;
        if (tp->type != bt_templateparam)
        {
            *lptr = Alloc(sizeof(INITLIST));
            (*lptr)->tp = tp;
            (*lptr)->exp = intNode(en_c_i, 0);
            lptr = &(*lptr)->next;
        }
        else if (tp->templateParam->p->packed)
        {
            TEMPLATEPARAMLIST *tpl = tp->templateParam->p->byPack.pack;
            needkw(&lex, ellipse);
            while (tpl)
            {
                if (tpl->p->byClass.val)
                {
                    *lptr = Alloc(sizeof(INITLIST));
                    (*lptr)->tp = tpl->p->byClass.val;
                    (*lptr)->exp = intNode(en_c_i, 0);
                    lptr = &(*lptr)->next;
                }
                tpl = tpl->next;
            }
            
        }
        else   
        {
            if (tp->templateParam->p->byClass.val)
            {
                *lptr = Alloc(sizeof(INITLIST));
                (*lptr)->tp = tp->templateParam->p->byClass.val;
                (*lptr)->exp = intNode(en_c_i, 0);
                lptr = &(*lptr)->next;
            }
        }
    } while (MATCHKW(lex, comma));
    needkw(&lex, closepa);
    return lex;
}
Example #4
0
LEXEME *expression_lambda(LEXEME *lex, SYMBOL *funcsp, TYPE *atp, TYPE **tp, EXPRESSION **exp, int flags)
{
    LAMBDA *self;
    SYMBOL *vpl, *ths;
    HASHREC *hrl;
    TYPE *ltp;
    STRUCTSYM ssl;
    if (funcsp)
        funcsp->noinline = TRUE;
    IncGlobalFlag();
    self = Alloc(sizeof(LAMBDA));
    ltp = Alloc(sizeof(TYPE));
    ltp->type = bt_struct;
    ltp->syms = CreateHashTable(1);
    ltp->tags = CreateHashTable(1);
    ltp->size = 0;
    self->captured = CreateHashTable(1);
    self->oldSyms = localNameSpace->syms;
    self->oldTags = localNameSpace->tags;
    self->index = lambdaIndex;
    self->captureMode = cmNone;
    self->isMutable = FALSE;
    self->captureThis = FALSE;
    self->cls = makeID(sc_type, ltp, NULL, LambdaName());
    ltp->sp = self->cls;
    SetLinkerNames(self->cls, lk_cdecl);
    self->cls->islambda = TRUE;
    self->cls->structAlign = getAlign(sc_global, &stdpointer);
    self->func = makeID(sc_member, ltp, NULL, "$internalFunc");
    self->func->parentClass = self->cls;
    self->functp = &stdauto;
    self->enclosingFunc = theCurrentFunc;
    if (lambdas)
        lambdas->prev = self;
    self->next = lambdas;
    lambdas = self;
    
    localNameSpace->syms = CreateHashTable(1);
    localNameSpace->tags = CreateHashTable(1);
    if (lambdas->next)
    {
        self->lthis = lambdas->next->lthis;
    }
    else if (funcsp && funcsp->parentClass)
    {
        self->lthis = ((SYMBOL *)funcsp->tp->syms->table[0]->p);
    }
    lex = getsym(); // past [
    if (funcsp)
    {
        // can have a capture list;
        if (MATCHKW(lex, assign))
        {
            lex = getsym();
            if (MATCHKW(lex, comma) || MATCHKW(lex, closebr))
            {
                self->captureMode = cmValue;
                skip(&lex, comma);
            }
            else
            {
                lex = backupsym();
            }
        }
        else if (MATCHKW(lex, and))
        {
            lex = getsym();
            if (MATCHKW(lex, comma) || MATCHKW(lex, closebr))
            {
                self->captureMode = cmRef;
                skip(&lex, comma);
            }
            else
            {
                lex = backupsym();
            }
        }
        if (!MATCHKW(lex, comma) && !MATCHKW(lex, closebr))
        {
            do            
            {
                enum e_cm localMode = self->captureMode;
                if (MATCHKW(lex, comma))
                    skip(&lex, comma);
                if (MATCHKW(lex, kw_this))
                {
                    lex = getsym();
                    if (localMode == cmValue || !self->lthis)
                    {
                        error(ERR_CANNOT_CAPTURE_THIS);
                    }
                    else
                    {
                        if (self->captureThis)
                        {
                            error(ERR_CAPTURE_ITEM_LISTED_MULTIPLE_TIMES);
                        }
                        self->captureThis = TRUE;
                        lambda_capture(NULL, cmThis, TRUE);
                    }
                    continue;
                }
                else if (MATCHKW(lex, and))
                {
                    if (localMode == cmRef)
                    {
                        error(ERR_INVALID_LAMBDA_CAPTURE_MODE);
                    }
                    localMode = cmRef;
                    lex = getsym();
                }
                else 
                {
                    if (localMode == cmValue)
                    {
                        error(ERR_INVALID_LAMBDA_CAPTURE_MODE);
                    }
                    localMode = cmValue;
                }
                if (ISID(lex))
                {
                    SYMBOL *sp = search(lex->value.s.a, localNameSpace->syms);
                    LAMBDA *current = lambdas;
                    while (current && !sp)
                    {
                        sp = search(lex->value.s.a, current->oldSyms);
                        current = current->next;
                        
                    }
                    lex = getsym();
                    if (sp)
                    {
                        if (sp->packed)
                        {
                            if (!MATCHKW(lex, ellipse))
                                error(ERR_PACK_SPECIFIER_REQUIRED_HERE);
                            else
                                lex = getsym();
                        }
                        if (sp->packed)
                        {
                            int n;
                            TEMPLATEPARAMLIST * templateParam = sp->tp->templateParam->p->byPack.pack;
                            HASHREC *hr;
                            for (n=0; templateParam; templateParam = templateParam->next, n++);
                            hr = funcsp->tp->syms->table[0];
                            while (hr && ((SYMBOL *)hr->p) != sp)
                                hr = hr->next;
                            while (hr && n)
                            {
                                lambda_capture((SYMBOL *)hr->p, localMode, TRUE);
                                hr = hr->next;
                                n--;
                            }
                        }
                        else
                        {
                            lambda_capture(sp, localMode, TRUE);
                        }
                    }
                    else
                        errorstr(ERR_UNDEFINED_IDENTIFIER, lex->value.s.a);
                }
                else
                {
                    error(ERR_INVALID_LAMBDA_CAPTURE_MODE);
                }
            } while (MATCHKW(lex, comma));
        }
        needkw(&lex, closebr);
    }
    else
    {
        if (!MATCHKW(lex, closebr))
        {
            error(ERR_LAMBDA_CANNOT_CAPTURE);
        }
        else
        {
            lex = getsym();
        }
    }
    if (MATCHKW(lex, openpa))
    {
        TYPE *tpx = &stdvoid;
        HASHREC *hr;
        lex = getFunctionParams(lex, NULL, &self->func, &tpx, FALSE, sc_auto);
        self->funcargs = self->func->tp->syms->table[0];
        hr = self->func->tp->syms->table[0];
        while (hr)
        {
            SYMBOL *sym = (SYMBOL *)hr->p;
            if (sym->init)
            {
                error(ERR_CANNOT_DEFAULT_PARAMETERS_WITH_LAMBDA);
            }
            hr = hr->next;
        }
        if (MATCHKW(lex, kw_mutable))
        {
            HASHREC *hr = self->captured->table[0];
            while (hr)
            {
                LAMBDASP *lsp = (LAMBDASP *)hr->p;
                if (lsp->sym->lambdaMode == cmValue)
                {
                    lsp->sym->tp = basetype(lsp->sym->tp);
                }
                hr = hr->next;
            }
            self->isMutable = TRUE;
            
            lex = getsym();
        }
        ParseAttributeSpecifiers(&lex, funcsp, TRUE);
        if (MATCHKW(lex, pointsto))
        {
            lex = getsym();
            lex = get_type_id(lex, &self->functp, funcsp, sc_cast, FALSE, TRUE);
        }
    }
    else
    {
        TYPE *tp1 = Alloc(sizeof(TYPE));
        SYMBOL *spi;
        tp1->type = bt_func;
        tp1->size = getSize(bt_pointer);
        tp1->btp = &stdvoid;
        tp1->sp = self->func;
        self->func->tp = tp1;
        spi = makeID(sc_parameter, tp1, NULL, AnonymousName());
        spi->anonymous = TRUE;
        spi->tp = Alloc(sizeof(TYPE));
        spi->tp->type = bt_void;
        insert(spi, localNameSpace->syms);
        SetLinkerNames(spi, lk_cpp);
        self->func->tp->syms = localNameSpace->syms;
        self->funcargs = self->func->tp->syms->table[0];
        self->func->tp->syms->table[0] = NULL;
    }
    vpl = makeID(sc_parameter, &stdpointer, NULL, AnonymousName());
    vpl->assigned = vpl->used = TRUE;
    SetLinkerNames(vpl, lk_cdecl);
    hrl = Alloc(sizeof(HASHREC));
    hrl->p = (struct _hrintern_ *)vpl;
    hrl->next = lambdas->func->tp->syms->table[0];
    lambdas->func->tp->syms->table[0] = hrl;
    vpl = makeID(sc_parameter, &stdpointer, NULL, AnonymousName());
    vpl->assigned = vpl->used = TRUE;
    SetLinkerNames(vpl, lk_cdecl);
    hrl = Alloc(sizeof(HASHREC));
    hrl->p = (struct _hrintern_ *)vpl;
    hrl->next = lambdas->func->tp->syms->table[0];
    lambdas->func->tp->syms->table[0] = hrl;
    SetLinkerNames(lambdas->func, lk_cdecl);
    injectThisPtr(lambdas->func, basetype(lambdas->func->tp)->syms);
    lambdas->func->tp->btp = self->functp;
    lambdas->func->linkage = lk_virtual;
    lambdas->func->isInline = TRUE;
    ssl.str = self->cls;
    ssl.tmpl = NULL;
    addStructureDeclaration(&ssl);
    ths = makeID(sc_member, &stdpointer, NULL, "$this");
    lambda_insert(ths, lambdas);
    if (MATCHKW(lex, begin))
    {
        lex = body(lex, self->func);
    }
    else
    {
        error(ERR_LAMBDA_FUNCTION_BODY_EXPECTED);
    }
    dropStructureDeclaration();
    localNameSpace->syms = self->oldSyms;
    localNameSpace->tags = self->oldTags;
    inferType();
    finishClass();
    *exp = createLambda(0);
    *tp = lambdas->cls->tp;
    lambdas = lambdas->next;
    if (lambdas)
        lambdas->prev = NULL;
    DecGlobalFlag();
    return lex;
}
Example #5
0
bool is_type(pointer P, dynamic_types type)
{
    return (P != NIL) && (get_type_id(P) == type);
}