Exemple #1
0
SLexerBuffer*
lex_CreateFileBuffer(FILE* fileHandle) {
    char strterm = 0;
    char* pFile;
    bool bWasSpace = true;

    SLexerBuffer* pBuffer = (SLexerBuffer*) mem_Alloc(sizeof(SLexerBuffer));
    memset(pBuffer, 0, sizeof(SLexerBuffer));

    size_t size = fsize(fileHandle);

    pFile = (char*) mem_Alloc(size);
    size = fread(pFile, sizeof(uint8_t), size, fileHandle);

    pBuffer->buffer = (char*) mem_Alloc(size + 1);
    char* dest = pBuffer->buffer;

    char* mem = pFile;

    while (mem < pFile + size) {
        if (*mem == '"' || *mem == '\'') {
            strterm = *mem;
            *dest++ = *mem++;
            while (*mem && *mem != strterm) {
                if (*mem == '\\')
                    *dest++ = *mem++;

                *dest++ = *mem++;
            }
            *dest++ = *mem++;
            bWasSpace = false;
        } else if ((mem[0] == 10 && mem[1] == 13) || (mem[0] == 13 && mem[1] == 10)) {
            *dest++ = '\n';
            mem += 2;
            bWasSpace = true;
        } else if (mem[0] == 10 || mem[0] == 13) {
            *dest++ = '\n';
            mem += 1;
            bWasSpace = true;
        } else if (*mem == ';' || (bWasSpace && mem[0] == '*')) {
            ++mem;
            while (*mem && *mem != 13 && *mem != 10)
                ++mem;
            bWasSpace = false;
        } else {
            bWasSpace = isspace((uint8_t) *mem) ? true : false;
            *dest++ = *mem++;
        }
    }


    *dest++ = '\n';
    pBuffer->bufferSize = dest - pBuffer->buffer;
    pBuffer->atLineStart = true;

    mem_Free(pFile);
    return pBuffer;
}
Exemple #2
0
/*
    Function: drawObject_Create

    Description -
    Creates and returns a new object for the the draw manager.

    4 arguments:
    void (*drawFunction)(void *object) - The draw function of this object.
    int objSize - The size of the objects data. This is to be filled in by a seperate function.
*/
Draw_Object *drawObject_Create(Draw_Type *type, int lifeTime, SDL_Surface *destSurface)
{
    Draw_Object *drawObject = (Draw_Object *)mem_Alloc(sizeof(Draw_Object));

    drawObject->object = (void *)mem_Alloc(type->objSize);

    drawObject->type = type;
    drawObject->lifeTime = lifeTime;
    drawObject->destSurface = destSurface;

    return drawObject;
}
Exemple #3
0
SLexerBuffer*
lex_CreateMemoryBuffer(const char* memory, size_t size) {
    SLexerBuffer* pBuffer = (SLexerBuffer*) mem_Alloc(sizeof(SLexerBuffer));

    pBuffer->buffer = (char*) mem_Alloc(size);
    memcpy(pBuffer->buffer, memory, size);
    pBuffer->charStack.count = 0;
    pBuffer->index = 0;
    pBuffer->bufferSize = size;
    pBuffer->atLineStart = true;
    pBuffer->state = LEX_STATE_NORMAL;
    return pBuffer;
}
Exemple #4
0
void drawManager_AddType(Draw_Manager *dM, Draw_Type *dT)
{
    int x = 0;
    Draw_Type **newArray = NULL;

    dM->numTypes ++;

    /*Create the new array to hold the draw types*/
    newArray = (Draw_Type **)mem_Alloc(dM->numTypes * sizeof(Draw_Type *));

    for(x = 0; x < (dM->numTypes - 1); x++)
    {
        newArray[x] = dM->drawTypes[x];
    }

    newArray[x] = dT;

    /*Release the array in the draw manager and replace it with the new one*/
    if(dM->drawTypes != NULL)
    {
        mem_Free(dM->drawTypes);
    }

    dM->drawTypes = newArray;

    return;
}
Exemple #5
0
/*
 * NewScope()
 *
 */
Scope *NewScopeInPool(MemoryPool *pool)
{
    Scope *lScope;

    lScope = mem_Alloc(pool, sizeof(Scope));
    lScope->pool = pool;
    lScope->parent = NULL;
    lScope->funScope = NULL;
    lScope->symbols = NULL;
    lScope->tags = NULL;
    lScope->params = NULL;
    lScope->returnType = NULL;
    lScope->level = 0;
    lScope->funindex = 0;
    lScope->InFormalParameters = 0;
    lScope->HasVoidParameter = 0;
    lScope->HasReturnStmt = 0;
    lScope->IsStructScope = 0;
    lScope->HasSemantics = 0;
    lScope->pid = PID_NONE_ID;
    lScope->programs = NULL;
    lScope->initStmts = NULL;
    if ((lScope->next = ScopeList))
        ScopeList->prev = lScope;
    lScope->prev = 0;
    ScopeList = lScope;
    mem_AddCleanup(pool, unlinkScope, lScope);
    return lScope;
} // NewScope
Exemple #6
0
Symbol *NewSymbol(SourceLoc *loc, Scope *fScope, int name, Type *fType, symbolkind kind)
{
    Symbol *lSymb;
    char *pch;
    int ii;

    lSymb = (Symbol *) mem_Alloc(fScope->pool, sizeof(Symbol));
    lSymb->left = NULL;
    lSymb->right = NULL;
    lSymb->next = NULL;
    lSymb->name = name;
    lSymb->storageClass = SC_UNKNOWN;
    lSymb->type = fType;
    lSymb->loc = *loc;
    lSymb->kind = kind;
    lSymb->properties = 0;
    lSymb->flags = 0;
    lSymb->tempptr = NULL;
    lSymb->tempptr2 = NULL;
    
    // Clear union area:

    pch = (char *) &lSymb->details;
    for (ii = 0; ii < sizeof(lSymb->details); ii++)
        *pch++ = 0;
    return lSymb;
} // NewSymbol
Exemple #7
0
extern void
dep_AddDependency(string* filename) {
    if (!hasDependency(filename)) {
        SDependency* dependency = (SDependency*) mem_Alloc(sizeof(SDependency));
        dependency->filename = str_Copy(filename);
        dependency->next = NULL;
        *g_nextDependency = dependency;
        g_nextDependency = &dependency->next;
    }
}
Exemple #8
0
int mem_AddCleanup(MemoryPool *pool, void (*fn)(void *), void *arg) {
    struct cleanup *cleanup;

    pool->free = (pool->free + sizeof(void *) - 1) & ~(sizeof(void *)-1);
    cleanup = mem_Alloc(pool, sizeof(struct cleanup));
    if (!cleanup) return -1;
    cleanup->next = pool->cleanup;
    cleanup->fn = fn;
    cleanup->arg = arg;
    pool->cleanup = cleanup;
    return 0;
}
Exemple #9
0
int TPpContext::mem_AddCleanup(MemoryPool *pool, void (*fn)(void *, void*), void* arg1, void* arg2)
{
    struct cleanup *cleanup;

    pool->free = (pool->free + sizeof(void *) - 1) & ~(sizeof(void *)-1);
    cleanup = (struct cleanup *)(mem_Alloc(pool, sizeof(struct cleanup)));
    if (!cleanup) return -1;
    cleanup->next = pool->cleanup;
    cleanup->fn = fn;
    cleanup->arg1 = arg1;
    cleanup->arg2 = arg2;
    pool->cleanup = cleanup;
    return 0;
}
TokenStream *NewTokenStream(const char *name, MemoryPool *pool)
{
    TokenStream *pTok;

    if (!pool)
        pTok = (TokenStream *) malloc(sizeof(TokenStream));
    else
        pTok = (TokenStream*)mem_Alloc(pool, sizeof(TokenStream));
    pTok->next = NULL;
    pTok->name = idstr(name, pool);
    pTok->head = NULL;
    pTok->current = NULL;
    lNewBlock(pTok, pool);
    return pTok;
} // NewTokenStream
Exemple #11
0
/****************************************************************************************
 *                        os_memoryCAlloc()
 ****************************************************************************************
DESCRIPTION:    Allocates an array in memory with elements initialized to 0.

ARGUMENTS:      OsContext   -   our adapter context.
                Number      -   Number of elements
                Size        -   Length in bytes of each element

RETURN:         None

NOTES:
*****************************************************************************************/
void* mem_Calloc (TI_HANDLE hMem, TI_UINT32 number, TI_UINT32 size)
{
	TMemMng *pMemMng = (TMemMng *)hMem;
	void *ptr;
	TI_UINT32 total;

	total = number * size;

	ptr = mem_Alloc (hMem, total);

	if (ptr != NULL) {
		os_memorySet (pMemMng->hOs, ptr, 0, total);
	}

	return ptr;
}
Exemple #12
0
Draw_Type *drawType_Create(int ID, void (*drawFunction)(Draw_Object *drawObj), void (*cleanFunction)(Draw_Object *drawObj), int objSize, int *totalObjectLocations)
{
    Draw_Type *drawType = (Draw_Type *)mem_Alloc(sizeof(Draw_Type));

    drawType->ID = ID;
    drawType->drawFunction = drawFunction;
    drawType->cleanFunction = cleanFunction;
    drawType->objSize = objSize;

    drawType->totalFree = 0;
    drawType->unusedObjectList = NULL;

    drawType->totalObjectLocations = totalObjectLocations;

    return drawType;
}
Exemple #13
0
/*
* Allocate a new symbol node;
*
*/
TPpContext::Symbol* TPpContext::NewSymbol(int atom)
{
    Symbol* lSymb;
    char* pch;
    int ii;

    lSymb = (Symbol *) mem_Alloc(pool, sizeof(Symbol));
    lSymb->atom = atom;

    // Clear macro
    pch = (char*) &lSymb->mac;
    for (ii = 0; ii < sizeof(lSymb->mac); ii++)
        *pch++ = 0;

    return lSymb;
}
static char *idstr(const char *fstr, MemoryPool *pool)
{
    size_t len;
    char *str, *t;
    const char *f;

    len = strlen(fstr);
    if (!pool)
        str = (char *) malloc(len + 1);
    else
        str = (char *) mem_Alloc(pool, len + 1);
    
    for (f=fstr, t=str; *f; f++) {
        if (isalnum(*f)) *t++ = *f;
        else if (*f == '.' || *f == '/') *t++ = '_';
    }
    *t = 0;
    return str;
} // idstr
Exemple #15
0
static SSection* sect_Create(char* name)
{
	SSection* sect = mem_Alloc(sizeof(SSection));
	memset(sect, 0, sizeof(SSection));
	
	sect->FreeSpace = g_pConfiguration->nMaxSectionSize;
	if(pSectionList)
	{
		SSection* list = pSectionList;
		while(list->pNext)
			list = list_GetNext(list);
		list_InsertAfter(list, sect);
	}
	else
	{
		pSectionList = sect;
	}
	strcpy(sect->Name, name);
	return sect;
}
Exemple #16
0
/*
 * NewScope()
 *
 */
Scope *NewScopeInPool(MemoryPool *pool)
{
    Scope *lScope;

    lScope = (Scope *)mem_Alloc(pool, sizeof(Scope));
    lScope->pool = pool;
    lScope->parent = NULL;
    lScope->funScope = NULL;
    lScope->symbols = NULL;
    
    lScope->level = 0;

    lScope->programs = NULL;
    if ((lScope->next = ScopeList))
        ScopeList->prev = lScope;
    lScope->prev = 0;
    ScopeList = lScope;
    mem_AddCleanup(pool, unlinkScope, lScope);
    return lScope;
} // NewScope
Exemple #17
0
/****************************************************************************************
 *                        os_memoryCAlloc()                                 
 ****************************************************************************************
DESCRIPTION:    Allocates an array in memory with elements initialized to 0.

ARGUMENTS:      OsContext   -   our adapter context.
                Number      -   Number of elements
                Size        -   Length in bytes of each element

RETURN:         None

NOTES:          
*****************************************************************************************/
void *mem_Calloc(TI_HANDLE hMem, TI_UINT32 number, TI_UINT32 size)
{
	TMemMng *pMemMng = (TMemMng *) hMem;
	void *ptr;
	TI_UINT32 total;

	total = number * size;

#ifdef TI_MEM_ALLOC_TRACE
	os_printf("os_memoryCAlloc(%u, %u) : %u\n", number, size, total);
#endif

	ptr = mem_Alloc(hMem, total);

	if (ptr != NULL) {
		os_memorySet(pMemMng->hOs, ptr, 0, total);
	}

	return ptr;
}
Exemple #18
0
Symbol *NewSymbol(SourceLoc *loc, Scope *fScope, int name, symbolkind kind)
{
    Symbol *lSymb;
    char *pch;
    int ii;

    lSymb = (Symbol *) mem_Alloc(fScope->pool, sizeof(Symbol));
    lSymb->left = NULL;
    lSymb->right = NULL;
    lSymb->next = NULL;
    lSymb->name = name;
    lSymb->loc = *loc;
    lSymb->kind = kind;
    
    // Clear union area:

    pch = (char *) &lSymb->details;
    for (ii = 0; ii < sizeof(lSymb->details); ii++)
        *pch++ = 0;
    return lSymb;
} // NewSymbol
static TokenBlock *lNewBlock(TokenStream *fTok, MemoryPool *pool)
{
    TokenBlock *lBlock;

    if (!pool)
        lBlock = (TokenBlock *) malloc(sizeof(TokenBlock) + 256);
    else
        lBlock = (TokenBlock *) mem_Alloc(pool, sizeof(TokenBlock) + 256);
    lBlock->count = 0;
    lBlock->current = 0;
    lBlock->data = (unsigned char *) lBlock + sizeof(TokenBlock);
    lBlock->max = 256;
    lBlock->next = NULL;
    if (fTok->head) {
        fTok->current->next = lBlock;
    } else {
        fTok->head = lBlock;
    }
    fTok->current = lBlock;
    return lBlock;
} // lNewBlock
Exemple #20
0
// Handle #define
int TPpContext::CPPdefine(TPpToken* ppToken)
{
    MacroSymbol mac;
    Symbol *symb;

    // get macro name
    int token = scanToken(ppToken);
    if (token != PpAtomIdentifier) {
        parseContext.ppError(ppToken->loc, "must be followed by macro name", "#define", "");
        return token;
    }
    if (ppToken->loc.string >= 0) {
        // We are in user code; check for reserved name use:
        parseContext.reservedPpErrorCheck(ppToken->loc, ppToken->name, "#define");
    }

    // save the original atom
    const int defAtom = ppToken->atom;

    // gather parameters to the macro, between (...)
    token = scanToken(ppToken);
    if (token == '(' && ! ppToken->space) {
        int argc = 0;
        int args[maxMacroArgs];
        do {
            token = scanToken(ppToken);
            if (argc == 0 && token == ')') 
                break;
            if (token != PpAtomIdentifier) {
                parseContext.ppError(ppToken->loc, "bad argument", "#define", "");

                return token;
            }
            // check for duplication of parameter name
            bool duplicate = false;
            for (int a = 0; a < argc; ++a) {
                if (args[a] == ppToken->atom) {
                    parseContext.ppError(ppToken->loc, "duplicate macro parameter", "#define", "");
                    duplicate = true;
                    break;
                }
            }
            if (! duplicate) {
                if (argc < maxMacroArgs)
                    args[argc++] = ppToken->atom;
                else
                    parseContext.ppError(ppToken->loc, "too many macro parameters", "#define", "");
            }
            token = scanToken(ppToken);
        } while (token == ',');
        if (token != ')') {
            parseContext.ppError(ppToken->loc, "missing parenthesis", "#define", "");

            return token;
        }
        mac.argc = argc;
        mac.args = (int*)mem_Alloc(pool, argc * sizeof(int));
        memcpy(mac.args, args, argc * sizeof(int));
        token = scanToken(ppToken);
    }

    // record the definition of the macro
    TSourceLoc defineLoc = ppToken->loc; // because ppToken is going to go to the next line before we report errors
    mac.body = new TokenStream;
    while (token != '\n' && token != EndOfInput) {
        RecordToken(mac.body, token, ppToken);
        token = scanToken(ppToken);
        if (token != '\n' && ppToken->space)
            RecordToken(mac.body, ' ', ppToken);
    }

    // check for duplicate definition
    symb = LookUpSymbol(defAtom);
    if (symb) {
        if (! symb->mac.undef) {
            // Already defined -- need to make sure they are identical:
            // "Two replacement lists are identical if and only if the preprocessing tokens in both have the same number,
            // ordering, spelling, and white-space separation, where all white-space separations are considered identical."
            if (symb->mac.argc != mac.argc)
                parseContext.ppError(defineLoc, "Macro redefined; different number of arguments:", "#define", GetAtomString(defAtom));
            else {
                for (int argc = 0; argc < mac.argc; argc++) {
                    if (symb->mac.args[argc] != mac.args[argc])
                        parseContext.ppError(defineLoc, "Macro redefined; different argument names:", "#define", GetAtomString(defAtom));
                }
                RewindTokenStream(symb->mac.body);
                RewindTokenStream(mac.body);
                int newToken;
                do {
                    int oldToken;
                    TPpToken oldPpToken;
                    TPpToken newPpToken;                    
                    oldToken = ReadToken(symb->mac.body, &oldPpToken);
                    newToken = ReadToken(mac.body, &newPpToken);
                    if (oldToken != newToken || oldPpToken != newPpToken) {
                        parseContext.ppError(defineLoc, "Macro redefined; different substitutions:", "#define", GetAtomString(defAtom));
                        break; 
                    }
                } while (newToken > 0);
            }
        }
    } else
        symb = AddSymbol(defAtom);

    delete symb->mac.body;
    symb->mac = mac;

    return '\n';
}
Exemple #21
0
// Handle #define
int TPpContext::CPPdefine(TPpToken* ppToken)
{
    int token, atom, args[maxMacroArgs], argc;
    MacroSymbol mac;
    Symbol *symb;
    token = currentInput->scan(this, currentInput, ppToken);
    if (token != CPP_IDENTIFIER) {
        parseContext.error(ppToken->loc, "must be followed by macro name", "#define", "");
        return token;
    }
    atom = ppToken->atom;
    const char* definedName = GetAtomString(atom);
    if (ppToken->loc.string >= 0) {
        // We are in user code; check for reserved name use:
        parseContext.reservedPpErrorCheck(ppToken->loc, definedName, "#define");
    }
    token = currentInput->scan(this, currentInput, ppToken);
    if (token == '(' && !ppToken->ival) {
        // gather arguments
        argc = 0;
        do {
            token = currentInput->scan(this, currentInput, ppToken);
            if (argc == 0 && token == ')') 
                break;
            if (token != CPP_IDENTIFIER) {
                parseContext.error(ppToken->loc, "bad argument", "#define", "");

                return token;
            }
            // check for duplication
            bool duplicate = false;
            for (int a = 0; a < argc; ++a) {
                if (args[a] == ppToken->atom) {
                    parseContext.error(ppToken->loc, "duplicate macro parameter", "#define", "");
                    duplicate = true;
                    break;
                }
            }
            if (! duplicate) {
                if (argc < maxMacroArgs)
                    args[argc++] = ppToken->atom;
                else
                    parseContext.error(ppToken->loc, "too many macro parameters", "#define", "");                    
            }
            token = currentInput->scan(this, currentInput, ppToken);
        } while (token == ',');
        if (token != ')') {            
            parseContext.error(ppToken->loc, "missing parenthesis", "#define", "");

            return token;
        }
        mac.argc = argc;
        mac.args = (int*)mem_Alloc(pool, argc * sizeof(int));
        memcpy(mac.args, args, argc * sizeof(int));
        token = currentInput->scan(this, currentInput, ppToken);
    }
    TSourceLoc defineLoc = ppToken->loc; // because ppToken is going to go to the next line before we report errors
    mac.body = new TokenStream;
    while (token != '\n') {
        if (token == '\\') {
            parseContext.lineContinuationCheck(ppToken->loc);
            token = currentInput->scan(this, currentInput, ppToken);
            if (token == '\n')
                token = currentInput->scan(this, currentInput, ppToken);
        }
        RecordToken(mac.body, token, ppToken);
        int spaceCandidate = currentInput->getch(this, currentInput, ppToken);
        if (spaceCandidate == ' ' || spaceCandidate == '\t')
            RecordToken(mac.body, ' ', 0);
        else
            currentInput->ungetch(this, currentInput, spaceCandidate, ppToken);
        token = currentInput->scan(this, currentInput, ppToken);
    }

    symb = LookUpSymbol(atom);
    if (symb) {
        if (! symb->mac.undef) {
            // Already defined -- need to make sure they are identical:
            // "Two replacement lists are identical if and only if the preprocessing tokens in both have the same number,
            // ordering, spelling, and white-space separation, where all white-space separations are considered identical."
            if (symb->mac.argc != mac.argc)
                parseContext.error(defineLoc, "Macro redefined; different number of arguments:", "#define", GetAtomString(atom));
            else {
                for (argc=0; argc < mac.argc; argc++) {
                    if (symb->mac.args[argc] != mac.args[argc])
                        parseContext.error(defineLoc, "Macro redefined; different argument names:", "#define", GetAtomString(atom));
                }
                RewindTokenStream(symb->mac.body);
                RewindTokenStream(mac.body);
                int newToken;
                do {
                    int oldToken;
                    TPpToken oldPpToken;
                    TPpToken newPpToken;                    
                    oldToken = ReadToken(symb->mac.body, &oldPpToken);
                    newToken = ReadToken(mac.body, &newPpToken);
                    if (oldToken != newToken || oldPpToken != newPpToken) {
                        parseContext.error(defineLoc, "Macro redefined; different substitutions:", "#define", GetAtomString(atom));
                        break; 
                    }
                } while (newToken > 0);
            }
        }
    } else {
        symb = AddSymbol(atom);
    }
    delete symb->mac.body;
    symb->mac = mac;

    return '\n';
}
Exemple #22
0
int TPpContext::CPPdefine(TPpToken * yylvalpp)
{
    int token, name, args[maxMacroArgs], argc;
    MacroSymbol mac;
    Symbol *symb;
    memset(&mac, 0, sizeof(mac));
    token = currentInput->scan(this, currentInput, yylvalpp);
    if (token != CPP_IDENTIFIER) {
        parseContext.error(yylvalpp->loc, "must be followed by macro name", "#define", "");
        return token;
    }
    name = yylvalpp->atom;
    token = currentInput->scan(this, currentInput, yylvalpp);
    if (token == '(' && !yylvalpp->ival) {
        // gather arguments
        argc = 0;
        do {
            token = currentInput->scan(this, currentInput, yylvalpp);
            if (argc == 0 && token == ')') 
                break;
            if (token != CPP_IDENTIFIER) {
                parseContext.error(yylvalpp->loc, "bad argument", "#define", "");

                return token;
            }
            if (argc < maxMacroArgs)
                args[argc++] = yylvalpp->atom;
            token = currentInput->scan(this, currentInput, yylvalpp);
        } while (token == ',');
        if (token != ')') {            
            parseContext.error(yylvalpp->loc, "missing parenthesis", "#define", "");

            return token;
        }
        mac.argc = argc;
        mac.args = (int*)mem_Alloc(macros->pool, argc * sizeof(int));
        memcpy(mac.args, args, argc * sizeof(int));
        token = currentInput->scan(this, currentInput, yylvalpp);
    }
    mac.body = NewTokenStream(GetAtomString(&atomTable, name), macros->pool);
    while (token != '\n') {
        while (token == '\\') {
            token = currentInput->scan(this, currentInput, yylvalpp);
            if (token == '\n')
                token = currentInput->scan(this, currentInput, yylvalpp);
            else
                RecordToken(mac.body, '\\', yylvalpp);
        }
        RecordToken(mac.body, token, yylvalpp);
        token = currentInput->scan(this, currentInput, yylvalpp);
    };

    symb = LookUpSymbol(macros, name);
    if (symb) {
        if (!symb->details.mac.undef) {
            // already defined -- need to make sure they are identical
            if (symb->details.mac.argc != mac.argc)
                goto error;
            for (argc=0; argc < mac.argc; argc++)
                if (symb->details.mac.args[argc] != mac.args[argc])
                    goto error;
            RewindTokenStream(symb->details.mac.body);
            RewindTokenStream(mac.body);
            do {
                int old_lval, old_token;
                old_token = ReadToken(symb->details.mac.body, yylvalpp);
                old_lval = yylvalpp->ival;
                token = ReadToken(mac.body, yylvalpp);
                if (token != old_token || yylvalpp->ival != old_lval) { 
error:
                    parseContext.error(yylvalpp->loc, "Macro Redefined", "#define", GetStringOfAtom(&atomTable, name));
                    break; 
                }
            } while (token > 0);
        }
        //FreeMacro(&symb->details.mac);
    } else {
        symb = AddSymbol(&yylvalpp->loc, macros, name, MACRO_S);
    }
    symb->details.mac = mac;

    return '\n';
} // CPPdefine
Exemple #23
0
static int CPPdefine(yystypepp * yylvalpp)
{
    int token, name, args[MAX_MACRO_ARGS], argc;
    const char *message;
    MacroSymbol mac;
    Symbol *symb;
    SourceLoc dummyLoc;
    memset(&mac, 0, sizeof(mac));
    token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
    if (token != CPP_IDENTIFIER) {
        CPPErrorToInfoLog("#define");
        return token;
    }
    name = yylvalpp->sc_ident;
    token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
    if (token == '(' && !yylvalpp->sc_int) {
        // gather arguments
        argc = 0;
        do {
            token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
            if (argc == 0 && token == ')') break;
            if (token != CPP_IDENTIFIER) {
				CPPErrorToInfoLog("#define");
                return token;
            }
            if (argc < MAX_MACRO_ARGS)
                args[argc++] = yylvalpp->sc_ident;
            token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
        } while (token == ',');
        if (token != ')') {
            CPPErrorToInfoLog("#define");
            return token;
        }
        mac.argc = argc;
        mac.args = mem_Alloc(macros->pool, argc * sizeof(int));
        memcpy(mac.args, args, argc * sizeof(int));
        token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
	}
    mac.body = NewTokenStream(GetAtomString(atable, name), macros->pool);
    while (token != '\n') {
        if (token == '\\') {
            CPPErrorToInfoLog("The line continuation character (\\) is not part of the OpenGL ES Shading Language");
            return token;
        } else if (token <= 0) { // EOF or error
            CPPErrorToInfoLog("unexpected end of input in #define preprocessor directive - expected a newline");
            return 0;
        }
        RecordToken(mac.body, token, yylvalpp);
        token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
    };

    symb = LookUpSymbol(macros, name);
    if (symb) {
        if (!symb->details.mac.undef) {
            // already defined -- need to make sure they are identical
            if (symb->details.mac.argc != mac.argc) goto error;
            for (argc=0; argc < mac.argc; argc++)
                if (symb->details.mac.args[argc] != mac.args[argc])
                    goto error;
            RewindTokenStream(symb->details.mac.body);
            RewindTokenStream(mac.body);
            do {
                int old_lval, old_token;
                old_token = ReadToken(symb->details.mac.body, yylvalpp);
                old_lval = yylvalpp->sc_int;
                token = ReadToken(mac.body, yylvalpp);
                if (token != old_token || yylvalpp->sc_int != old_lval) { 
                error:
                    StoreStr("Macro Redefined");
                    StoreStr(GetStringOfAtom(atable,name));
                    message=GetStrfromTStr();
                    DecLineNumber();
                    CPPShInfoLogMsg(message);
                    IncLineNumber();
                    ResetTString();
                    break; }
            } while (token > 0);
        }
        //FreeMacro(&symb->details.mac);
    } else {
        dummyLoc.file = 0;
        dummyLoc.line = 0;
        symb = AddSymbol(&dummyLoc, macros, name, MACRO_S);
    }
    symb->details.mac = mac;
    return '\n';
} // CPPdefine
Exemple #24
0
SMachineOptions*
z80_AllocOptions(void) {
    return mem_Alloc(sizeof(SMachineOptions));
}