void compileBlock2(void) { Object* typeObj; Type* actualType; if (lookAhead->tokenType == KW_TYPE) { eat(KW_TYPE); do { eat(TK_IDENT); checkFreshIdent(currentToken->string); typeObj = createTypeObject(currentToken->string); eat(SB_EQ); actualType = compileType(); typeObj->typeAttrs->actualType = actualType; declareObject(typeObj); eat(SB_SEMICOLON); } while (lookAhead->tokenType == TK_IDENT); compileBlock3(); } else compileBlock3(); }
void compileVarDecl(void) { // TODO eat(TK_IDENT); eat(SB_COLON); compileType(); eat(SB_SEMICOLON); }
void compileBlock3(void) { Object* varObj; Type* varType; if (lookAhead->tokenType == KW_VAR) { eat(KW_VAR); do { eat(TK_IDENT); checkFreshIdent(currentToken->string); varObj = createVariableObject(currentToken->string); eat(SB_COLON); varType = compileType(); varObj->varAttrs->type = varType; declareObject(varObj); eat(SB_SEMICOLON); } while (lookAhead->tokenType == TK_IDENT); compileBlock4(); } else compileBlock4(); }
void compileBlock2(void) { Object* typeObj; Type* actualType; if (lookAhead->tokenType == KW_TYPE) { eat(KW_TYPE); do { eat(TK_IDENT); // TODO: Check if a type identifier is fresh in the block checkFreshIdent(currentToken->string); // create a type object typeObj = createTypeObject(currentToken->string); eat(SB_EQ); // Get the actual type actualType = compileType(); typeObj->typeAttrs->actualType = actualType; // Declare the type object declareObject(typeObj); eat(SB_SEMICOLON); } while (lookAhead->tokenType == TK_IDENT); compileBlock3(); } else compileBlock3(); }
void compileType(void) { // TODO switch (lookAhead->tokenType) { case KW_INTEGER: eat(KW_INTEGER); break; case KW_CHAR: eat(KW_CHAR); break; case TK_IDENT: eat(TK_IDENT); break; case KW_ARRAY: eat(KW_ARRAY); eat(SB_LSEL); eat(TK_NUMBER); eat(SB_RSEL); eat(KW_OF); compileType(); break; default: error(ERR_INVALIDTYPE, lookAhead->lineNo, lookAhead->colNo); break; } }
void compileBlock3(void) { Object* varObj; Type* varType; if (lookAhead->tokenType == KW_VAR) { eat(KW_VAR); do { eat(TK_IDENT); // TODO: Check if a variable identifier is fresh in the block checkFreshIdent(currentToken->string); // Create a variable object varObj = createVariableObject(currentToken->string); eat(SB_COLON); // Get the variable type varType = compileType(); varObj->varAttrs->type = varType; // Declare the variable object declareObject(varObj); eat(SB_SEMICOLON); } while (lookAhead->tokenType == TK_IDENT); compileBlock4(); } else compileBlock4(); }
void compileVarDecl(void) { // TODO // assert("Parsing a variable..."); eat(TK_IDENT); eat(SB_COLON); compileType(); eat(SB_SEMICOLON); // assert("Variable parsed!"); }
void compileTypeDecl(void) { // TODO assert("Parsing a type..."); eat(TK_IDENT); eat(SB_EQ); compileType(); eat(SB_SEMICOLON); assert("Type parsed!"); }
void compileVarDecl(void) { // TODO if(lookAhead->tokenType != TK_IDENT) error(ERR_INVALIDVARDECL, lookAhead->lineNo, lookAhead->colNo); eat(TK_IDENT); eat(SB_COLON); compileType(); eat(SB_SEMICOLON); }
Type* compileType(void) { Type* type; Type* elementType; int arraySize; Object* obj; switch (lookAhead->tokenType) { case KW_INTEGER: eat(KW_INTEGER); type = makeIntType(); break; case KW_CHAR: eat(KW_CHAR); type = makeCharType(); break; case KW_STRING: eat(KW_STRING); type=makeStringType(); break; case KW_FLOAT: eat(KW_FLOAT); type=makeFloatType(); break; case KW_ARRAY: eat(KW_ARRAY); eat(SB_LSEL); eat(TK_NUMBER); arraySize = currentToken->value; eat(SB_RSEL); eat(KW_OF); elementType = compileType(); type = makeArrayType(arraySize, elementType); break; case TK_IDENT: eat(TK_IDENT); obj = checkDeclaredType(currentToken->string); type = duplicateType(obj->typeAttrs->actualType); break; default: error(ERR_INVALID_TYPE, lookAhead->lineNo, lookAhead->colNo); break; } return type; }
Type* compileType(void) { // TODO: create and return a type Type* type = NULL; switch (lookAhead->tokenType) { case KW_INTEGER: eat(KW_INTEGER); type = (Type*) malloc(sizeof(Type)); type->typeClass = TP_INT; break; case KW_CHAR: eat(KW_CHAR); type = (Type*) malloc(sizeof(Type)); type->typeClass = TP_CHAR; break; case KW_ARRAY: type = (Type*) malloc(sizeof(Type)); type->typeClass = TP_ARRAY; eat(KW_ARRAY); eat(SB_LSEL); eat(TK_NUMBER); type->arraySize = currentToken->value; eat(SB_RSEL); eat(KW_OF); type->elementType = compileType(); break; case TK_IDENT: eat(TK_IDENT); Object* obj = lookupObject(currentToken->string); if (obj == NULL) { error(ERR_UNDECLARED_TYPE, currentToken->lineNo, currentToken->colNo); } else if (obj->kind != OBJ_TYPE) { error(ERR_INVALID_TYPE, currentToken->lineNo, currentToken->colNo); } type = duplicateType(obj->typeAttrs->actualType); break; default: error(ERR_INVALID_TYPE, lookAhead->lineNo, lookAhead->colNo); break; } return type; }
Type* compileType(void) { Type* type; Type* elementType; int arraySize; Object* obj; switch (lookAhead->tokenType) { case KW_INTEGER: eat(KW_INTEGER); type = makeIntType(); break; case KW_CHAR: eat(KW_CHAR); type = makeCharType(); break; case KW_ARRAY: eat(KW_ARRAY); eat(SB_LSEL); eat(TK_NUMBER); arraySize = currentToken->value; eat(SB_RSEL); eat(KW_OF); elementType = compileType(); type = makeArrayType(arraySize, elementType); break; case TK_IDENT: eat(TK_IDENT); // check if the type identifier is declared and get its actual type obj = checkDeclaredType(currentToken->string); if (obj != NULL) type = duplicateType(obj->typeAttrs->actualType); else error(ERR_UNDECLARED_TYPE, currentToken->colNo, currentToken->lineNo); break; default: error(ERR_INVALID_TYPE, lookAhead->lineNo, lookAhead->colNo); break; } return type; }
void compileBlock3(void) { // TODO: create and declare variable objects# Object* obj = NULL; if (lookAhead->tokenType == KW_VAR) { eat(KW_VAR); do { eat(TK_IDENT); obj = createVariableObject(currentToken->string); eat(SB_COLON); obj->varAttrs->type = compileType(); eat(SB_SEMICOLON); declareObject(obj); } while (lookAhead->tokenType == TK_IDENT); compileBlock4(); } else compileBlock4(); }
void compileBlock2(void) { // TODO: create and declare type objects# Object* obj = NULL; if (lookAhead->tokenType == KW_TYPE) { eat(KW_TYPE); do { eat(TK_IDENT); obj = createTypeObject(currentToken->string); eat(SB_EQ); obj->typeAttrs->actualType = compileType(); eat(SB_SEMICOLON); declareObject(obj); } while (lookAhead->tokenType == TK_IDENT); compileBlock3(); } else compileBlock3(); }
Type* compileType(void) { Type* type; Type* elementType; int arraySize; Object* obj; switch (lookAhead->tokenType) { case KW_INTEGER: eat(KW_INTEGER); type = makeIntType(); break; case KW_CHAR: eat(KW_CHAR); type = makeCharType(); break; case KW_ARRAY: eat(KW_ARRAY); eat(SB_LSEL); eat(TK_NUMBER); arraySize = currentToken->value; eat(SB_RSEL); eat(KW_OF); elementType = compileType(); type = makeArrayType(arraySize, elementType); break; case TK_IDENT: eat(TK_IDENT); // TODO: check if the type idntifier is declared and get its actual type break; default: error(ERR_INVALID_TYPE, lookAhead->lineNo, lookAhead->colNo); break; } return type; }
void compileTypeDecl(void) { eat(TK_IDENT); eat(SB_EQ); compileType(); eat(SB_SEMICOLON); }