Type* compileLValue(void) { // DONE: parse a lvalue (a variable, an array element, a parameter, the current function identifier) Object* var; Type* varType; eat(TK_IDENT); // check if the identifier is a function identifier, or a variable identifier, or a parameter var = checkDeclaredLValueIdent(currentToken->string); switch (var->kind) { case OBJ_VARIABLE: if (var->varAttrs->type->typeClass == TP_ARRAY) { varType = compileIndexes(var->varAttrs->type); } else varType = var->varAttrs->type; break; case OBJ_PARAMETER: varType = var->paramAttrs->type; break; case OBJ_FUNCTION: varType = var->funcAttrs->returnType; break; default: error(ERR_INVALID_LVALUE,currentToken->lineNo, currentToken->colNo); } return varType; }
Type* compileLValue(void) { Object* var; Type* varType; eat(TK_IDENT); var = checkDeclaredLValueIdent(currentToken->string); switch (var->kind) { case OBJ_VARIABLE: genVariableAddress(var); if (var->varAttrs->type->typeClass == TP_ARRAY) { varType = compileIndexes(var->varAttrs->type); } else varType = var->varAttrs->type; break; case OBJ_PARAMETER: // TODO: push parameter value onto stack if the parameter is a reference (defined with keyword VAR // push parameter address onto stack if the parameter is a value varType = var->paramAttrs->type; break; case OBJ_FUNCTION: // TODO: push the return value address onto the stack varType = var->funcAttrs->returnType; break; default: error(ERR_INVALID_LVALUE,currentToken->lineNo, currentToken->colNo); } return varType; }
Type* compileLValue(void) { Object* var; Type* varType; eat(TK_IDENT); var = checkDeclaredLValueIdent(currentToken->string); switch (var->kind) { case OBJ_VARIABLE: if (var->varAttrs->type->typeClass == TP_ARRAY) { varType = compileIndexes(var->varAttrs->type); } else varType = var->varAttrs->type; break; case OBJ_PARAMETER: varType = var->paramAttrs->type; break; case OBJ_FUNCTION: varType = var->funcAttrs->returnType; break; default: error(ERR_INVALID_LVALUE,currentToken->lineNo, currentToken->colNo); } return varType; }
Type* compileLValue(void) { Object* var; Type* varType; eat(TK_IDENT); var = checkDeclaredLValueIdent(currentToken->string); switch (var->kind) { case OBJ_VARIABLE: // TODO: push the variable address onto the stack genVariableAddress(var); if (var->varAttrs->type->typeClass == TP_ARRAY) { // compute the element address varType = compileIndexes(var->varAttrs->type); } else varType = var->varAttrs->type; break; case OBJ_PARAMETER: // TEMPORARY: halt the program genHL(); varType = var->paramAttrs->type; break; case OBJ_FUNCTION: // TEMPORARY: halt the program genHL(); varType = var->funcAttrs->returnType; break; default: error(ERR_INVALID_LVALUE,currentToken->lineNo, currentToken->colNo); } return varType; }
void compileFactor(void) { // TODO switch (lookAhead->tokenType) { case TK_NUMBER: eat(TK_NUMBER); break; case TK_IDENT: eat(TK_IDENT); compileIndexes(); compileArguments(); break; case TK_STRING: eat(TK_STRING); break; case TK_CHAR: eat(TK_CHAR); break; case SB_LPAR: compileExpression(); eat(SB_RPAR); break; default: error(ERR_INVALIDFACTOR, lookAhead->lineNo, lookAhead->colNo); break; } }
void compileFactor(void) { Object* obj; switch (lookAhead->tokenType) { case TK_NUMBER: eat(TK_NUMBER); break; case TK_CHAR: eat(TK_CHAR); break; case TK_IDENT: eat(TK_IDENT); // check if the identifier is declared obj = checkDeclaredIdent(currentToken->string); switch (obj->kind) { case OBJ_CONSTANT: break; case OBJ_VARIABLE: compileIndexes(); break; case OBJ_PARAMETER: break; case OBJ_FUNCTION: compileArguments(); break; default: error(ERR_INVALID_FACTOR,currentToken->lineNo, currentToken->colNo); break; } break; default: error(ERR_INVALID_FACTOR, lookAhead->lineNo, lookAhead->colNo); } }
void compileFactor(void) { // TODO Token *tmp=lookAhead; //de chuyen tiep lookAhead o duoi switch(tmp->tokenType) { case TK_NUMBER: case TK_CHAR: compileUnsignedConstant(); break; case SB_LPAR: eat(SB_LPAR); compileExpression(); eat(SB_RPAR); break; case TK_IDENT: eat(TK_IDENT); if(lookAhead->tokenType==SB_LPAR) { compileArguments(); } else if(lookAhead->tokenType==SB_LSEL) { compileIndexes(); } //else //compileUnsignedConstant(); break; default: error(ERR_INVALIDFACTOR, lookAhead->lineNo, lookAhead->colNo); break; } //free(tmp); }
void compileFactor(void) { switch (lookAhead->tokenType) { case TK_NUMBER: eat(TK_NUMBER); break; case TK_CHAR: eat(TK_CHAR); break; case TK_IDENT: eat(TK_IDENT); switch (lookAhead->tokenType) { case SB_LPAR: compileArguments(); break; case SB_LSEL: compileIndexes(); break; default: break; } break; default: error(ERR_INVALID_FACTOR, lookAhead->lineNo, lookAhead->colNo); } }
Type* compileFactor(void) { // DONE: parse a factor and return the factor's type Object* obj; Type* type; switch (lookAhead->tokenType) { case TK_NUMBER: eat(TK_NUMBER); type = intType; break; case TK_CHAR: eat(TK_CHAR); type = charType; break; case TK_IDENT: eat(TK_IDENT); // check if the identifier is declared obj = checkDeclaredIdent(currentToken->string); switch (obj->kind) { case OBJ_CONSTANT: switch (obj->constAttrs->value->type) { case TP_INT: type = intType; break; case TP_CHAR: type = charType; break; default: break; } break; case OBJ_VARIABLE: if (obj->varAttrs->type->typeClass == TP_ARRAY) { type = compileIndexes(obj->varAttrs->type); } else { type = obj->varAttrs->type; } break; case OBJ_PARAMETER: type = obj->paramAttrs->type; break; case OBJ_FUNCTION: compileArguments(obj->funcAttrs->paramList); type = obj->funcAttrs->returnType; break; default: error(ERR_INVALID_FACTOR,currentToken->lineNo, currentToken->colNo); break; } break; default: error(ERR_INVALID_FACTOR, lookAhead->lineNo, lookAhead->colNo); } return type; }
void compileAssignSt(void) { assert("Parsing an assign statement ...."); // TODO eat(TK_IDENT); compileIndexes(); eat(SB_ASSIGN); compileExpression(); assert("Assign statement parsed ...."); }
void compileIndexes(void) { // TODO if (lookAhead->tokenType == SB_LSEL) { eat(SB_LSEL); compileExpression(); eat(SB_RSEL); compileIndexes(); } }
void compileLValue(void) { Object* var; eat(TK_IDENT); // check if the identifier is a function identifier, or a variable identifier, or a parameter var = checkDeclaredLValueIdent(currentToken->string); if (var->kind == OBJ_VARIABLE) compileIndexes(); }
void compileAssignSt(void) { assert("Parsing an assign statement ...."); // TODO eat(TK_IDENT); if(lookAhead->tokenType==SB_LSEL) compileIndexes(); eat(SB_ASSIGN); compileExpression(); assert("Assign statement parsed ...."); }
Type* compileFactor(void) { // parse a factor and return the factor's type Object* obj = NULL; Type* type = NULL; switch (lookAhead->tokenType) { case TK_NUMBER: eat(TK_NUMBER); type = makeIntType(); break; case TK_CHAR: eat(TK_CHAR); type = makeCharType(); break; case TK_IDENT: eat(TK_IDENT); // check if the identifier is declared obj = checkDeclaredIdent(currentToken->string); switch (obj->kind) { case OBJ_CONSTANT: // use as an empty type type = makeIntType(); // assign the type of the constant type->typeClass = obj->constAttrs->value->type; break; case OBJ_VARIABLE: if (obj->varAttrs->type->typeClass != TP_ARRAY) type = obj->varAttrs->type; else type = compileIndexes(obj->varAttrs->type); break; case OBJ_PARAMETER: type = obj->paramAttrs->type; break; case OBJ_FUNCTION: type = obj->funcAttrs->returnType; compileArguments(obj->funcAttrs->paramList); break; default: error(ERR_INVALID_FACTOR,currentToken->lineNo, currentToken->colNo); break; } break; default: error(ERR_INVALID_FACTOR, lookAhead->lineNo, lookAhead->colNo); } return type; }
Type* compileLValue(void) { // TODO: parse a lvalue (a variable, an array element, a parameter, the current function identifier) Object* var; Type* varType; eat(TK_IDENT); // check if the identifier is a function identifier, or a variable identifier, or a parameter var = checkDeclaredLValueIdent(currentToken->string); if (var->kind == OBJ_VARIABLE) compileIndexes(); return varType; }
Type* compileLValue(void) { // parse a lvalue (a variable, an array element, a parameter, the current function identifier) Object* var = NULL; Type* varType = NULL; eat(TK_IDENT); // check if the identifier is a function identifier, or a variable identifier, or a parameter var = checkDeclaredLValueIdent(currentToken->string); if (var->kind == OBJ_VARIABLE) varType = compileIndexes(var->varAttrs->type); else if (var->kind == OBJ_FUNCTION) varType = var->funcAttrs->returnType; else if (var->kind == OBJ_PARAMETER) varType = var->paramAttrs->type; return varType; }
void compileAssignSt(void) { //khong hieu lam assert("Parsing an assign statement ...."); eat(TK_IDENT); switch(lookAhead->tokenType){ case SB_LSEL: //variable compileIndexes(); eat(SB_ASSIGN); compileExpression(); break; case SB_ASSIGN: //function eat(SB_ASSIGN); compileExpression(); break; default: error(ERR_INVALIDSTATEMENT, lookAhead->lineNo, lookAhead->colNo); break; } assert("Assign statement parsed ...."); }
void compileFactor(void) { if(lookAhead->tokenType == TK_NUMBER || lookAhead->tokenType == TK_CHAR ){ compileUnsignedConstant(); } else if (lookAhead->tokenType == TK_IDENT){ eat(TK_IDENT); if( lookAhead->tokenType == SB_LSEL){ compileIndexes(); } else if(lookAhead->tokenType == SB_LPAR){ compileArguments(); } } else if(lookAhead->tokenType == SB_LPAR){ eat(SB_LPAR); compileExpression(); eat(SB_RPAR); } else error(ERR_INVALIDFACTOR, lookAhead->lineNo, lookAhead->colNo); }
void compileIndexes(void) { // TODO switch (lookAhead->tokenType) { case SB_LSEL: eat(SB_LSEL); compileExpression(); eat(SB_RSEL); compileIndexes(); break; case SB_TIMES: case SB_SLASH: case SB_MOD: case SB_PLUS: case SB_MINUS: case SB_EQ: case SB_NEQ: case SB_LE: case SB_LT: case SB_GE: case SB_GT: case KW_TO: case KW_DO: case SB_COMMA: case SB_LPAR: case SB_RSEL: case SB_RPAR: case SB_SEMICOLON: case KW_END: case KW_ELSE: case KW_THEN: case SB_ASSIGN: break; default: error(ERR_INVALIDSTATEMENT, lookAhead->lineNo, lookAhead->colNo); break; } }
void compileLValue(void) { eat(TK_IDENT); compileIndexes(); }
Type* compileFactor(void) { Type* type; Object* obj; switch (lookAhead->tokenType) { case TK_NUMBER: eat(TK_NUMBER); type = intType; genLC(currentToken->value); break; case TK_CHAR: eat(TK_CHAR); type = charType; genLC(currentToken->value); break; case TK_IDENT: eat(TK_IDENT); obj = checkDeclaredIdent(currentToken->string); switch (obj->kind) { case OBJ_CONSTANT: switch (obj->constAttrs->value->type) { case TP_INT: type = intType; genLC(obj->constAttrs->value->intValue); break; case TP_CHAR: type = charType; genLC(obj->constAttrs->value->charValue); break; default: break; } break; case OBJ_VARIABLE: if (obj->varAttrs->type->typeClass == TP_ARRAY) { genVariableAddress(obj); type = compileIndexes(obj->varAttrs->type); genLI(); } else { type = obj->varAttrs->type; genVariableValue(obj); } break; case OBJ_PARAMETER: // TODO: push parameter's value onto the stack type = obj->paramAttrs->type; break; case OBJ_FUNCTION: // TODO: generate function call if (isPredefinedFunction(obj)) { compileArguments(obj->funcAttrs->paramList); genPredefinedFunctionCall(obj); } else { compileArguments(obj->funcAttrs->paramList); } type = obj->funcAttrs->returnType; break; default: error(ERR_INVALID_FACTOR,currentToken->lineNo, currentToken->colNo); break; } break; case SB_LPAR: eat(SB_LPAR); type = compileExpression(); eat(SB_RPAR); break; default: error(ERR_INVALID_FACTOR, lookAhead->lineNo, lookAhead->colNo); } return type; }