ConstantValue* compileUnsignedConstant(void) { ConstantValue* constValue; Object* obj; switch (lookAhead->tokenType) { case TK_NUMBER: eat(TK_NUMBER); constValue = makeIntConstant(currentToken->value); break; case TK_IDENT: eat(TK_IDENT); obj = checkDeclaredConstant(currentToken->string); constValue = duplicateConstantValue(obj->constAttrs->value); break; case TK_CHAR: eat(TK_CHAR); constValue = makeCharConstant(currentToken->string[0]); break; default: error(ERR_INVALID_CONSTANT, lookAhead->lineNo, lookAhead->colNo); break; } return constValue; }
ConstantValue* compileConstant2(void) { // TODO: create and return a constant value ConstantValue* constValue = NULL; switch (lookAhead->tokenType) { case TK_NUMBER: eat(TK_NUMBER); constValue = makeIntConstant(currentToken->value); break; case TK_IDENT: eat(TK_IDENT); Object * obj = lookupObject(currentToken->string); if (obj == NULL) error(ERR_UNDECLARED_CONSTANT, currentToken->lineNo, currentToken->colNo); else if (obj->kind != OBJ_CONSTANT) error(ERR_INVALID_CONSTANT, currentToken->lineNo, currentToken->colNo); constValue = duplicateConstantValue(obj->constAttrs->value); break; default: error(ERR_INVALID_CONSTANT, lookAhead->lineNo, lookAhead->colNo); break; } return constValue; }
ConstantValue* compileUnsignedConstant(void) { // TODO: create and return an unsigned constant value ConstantValue* constValue = NULL; switch (lookAhead->tokenType) { case TK_NUMBER: eat(TK_NUMBER); constValue = makeIntConstant(currentToken->value); break; case TK_IDENT: eat(TK_IDENT); Object* obj = lookupObject(currentToken->string); if (obj == NULL) { error(ERR_UNDECLARED_CONSTANT, currentToken->lineNo, currentToken->colNo); } else if (obj->kind != OBJ_CONSTANT) { error(ERR_INVALID_CONSTANT, currentToken->lineNo, currentToken->colNo); } constValue = (ConstantValue*) malloc(sizeof(ConstantValue)); *constValue = *(obj->constAttrs->value); break; case TK_CHAR: eat(TK_CHAR); constValue = makeCharConstant(currentToken->string[0]); break; default: error(ERR_INVALID_CONSTANT, lookAhead->lineNo, lookAhead->colNo); break; } return constValue; }
ConstantValue* compileConstant2(void) { ConstantValue* constValue; Object* obj; switch (lookAhead->tokenType) { case TK_NUMBER: eat(TK_NUMBER); constValue = makeIntConstant(currentToken->value); break; case TK_IDENT: eat(TK_IDENT); // check if the integer constant identifier is declared and get its value obj = checkDeclaredConstant(currentToken->string); if (obj != NULL) constValue = duplicateConstantValue(obj->constAttrs->value); else error(ERR_UNDECLARED_CONSTANT, currentToken->colNo, currentToken->lineNo); break; default: error(ERR_INVALID_CONSTANT, lookAhead->lineNo, lookAhead->colNo); break; } return constValue; }
ConstantValue* compileConstant2(void) { ConstantValue* constValue; Object* obj; switch (lookAhead->tokenType) { case TK_NUMBER: eat(TK_NUMBER); constValue = makeIntConstant(currentToken->value); break; case TK_IDENT: eat(TK_IDENT); // TODO: check if the integer constant identifier is declared and get its value break; default: error(ERR_INVALID_CONSTANT, lookAhead->lineNo, lookAhead->colNo); break; } return constValue; }
int main() { Object* obj; initSymTab(); obj = createProgramObject("PRG"); enterBlock(obj->progAttrs->scope); obj = createConstantObject("c1"); obj->constAttrs->value = makeIntConstant(10); declareObject(obj); obj = createConstantObject("c2"); obj->constAttrs->value = makeCharConstant('a'); declareObject(obj); obj = createTypeObject("t1"); obj->typeAttrs->actualType = makeArrayType(10, makeIntType()); declareObject(obj); obj = createVariableObject("v1"); obj->varAttrs->type = makeIntType(); declareObject(obj); obj = createVariableObject("v2"); obj->varAttrs->type = makeArrayType(10, makeArrayType(10, makeIntType())); declareObject(obj); obj = createFunctionObject("f"); obj->funcAttrs->returnType = makeIntType(); declareObject(obj); enterBlock(obj->funcAttrs->scope); obj = createParameterObject("p1", PARAM_VALUE, symtab->currentScope->owner); obj->paramAttrs->type = makeIntType(); declareObject(obj); obj = createParameterObject("p2", PARAM_REFERENCE, symtab->currentScope->owner); obj->paramAttrs->type = makeCharType(); declareObject(obj); exitBlock(); obj = createProcedureObject("p"); declareObject(obj); enterBlock(obj->procAttrs->scope); obj = createParameterObject("v1", PARAM_VALUE, symtab->currentScope->owner); obj->paramAttrs->type = makeIntType(); declareObject(obj); obj = createConstantObject("c1"); obj->constAttrs->value = makeCharConstant('a'); declareObject(obj); obj = createConstantObject("c3"); obj->constAttrs->value = makeIntConstant(10); declareObject(obj); obj = createTypeObject("t1"); obj->typeAttrs->actualType = makeIntType(); declareObject(obj); obj = createTypeObject("t2"); obj->typeAttrs->actualType = makeArrayType(10, makeIntType()); declareObject(obj); obj = createVariableObject("v2"); obj->varAttrs->type = makeArrayType(10, makeIntType()); declareObject(obj); obj = createVariableObject("v3"); obj->varAttrs->type = makeCharType(); declareObject(obj); exitBlock(); exitBlock(); printObject(symtab->program, 0); cleanSymTab(); return 0; }