// Program Block stuff void ProgramBlock::generate_pre() { // get the program id and set it as the open file TokenPtr p = *this->get_unprocessed()->begin(); // open a file to write to this->get_analyzer()->open_file(p->get_lexeme()); // generate program entry point write_raw("MOV SP D0"); // push begin symbols for (auto i = get_symbol_list()->begin(); i != get_symbol_list()->end(); i++) { if (static_pointer_cast<SymData>(*i)->get_var_type() == STRING) { write_raw("PUSH #\"\""); } else if (static_pointer_cast<SymData>(*i)->get_var_type() == FLOATING) { write_raw("PUSH #0.0"); } else { write_raw("PUSH #0"); } } write_raw(""); }
void ActivationBlock::catch_token(TokenPtr symbol) { if (this->activity == DEFINITION) { if (symbol->get_token() != MP_BEGIN && symbol->get_token() != MP_END && symbol->get_token() != MP_SEMI_COLON) { this->get_unprocessed()->push_back(symbol); } } else if (this->activity == CALL) { bool first_id = true; if (first_id == true && symbol->get_token() == MP_ID) { this->caller_name = symbol->get_lexeme(); first_id = false; } else { if (symbol->get_token() == MP_ID || symbol->get_token() == MP_INT_LITERAL || symbol->get_token() == MP_STRING_LITERAL || symbol->get_token() == MP_FLOAT_LITERAL) { this->get_unprocessed()->push_back(symbol); } } } }
bool Parser::IsInFirst(TokenPtr token, Production production) { if (production == Production::COMMAND) { if (token->get_token_class() == TokenClassEnum::IDENTIFIER) { return true; } else if (token->get_token_class() == TokenClassEnum::L_BRACE) { return true; } else if (token->get_token_class() == TokenClassEnum::RESERVED_WORD) { if (token->get_lexeme() == "while" || token->get_lexeme() == "do" || token->get_lexeme() == "if") { return true; } } } else if (production == Production::VARIABLE_DECLARATION) { if (token->get_token_class() == TokenClassEnum::RESERVED_WORD) { if (token->get_lexeme() == "int" || token->get_lexeme() == "float" || token->get_lexeme() == "char") { return true; } } } return false; }
SymbolPtr CodeBlock::translate(TokenPtr token) { if (token->get_token() == MP_ID) { string search_lexeme = token->get_lexeme(); unsigned int search_level = this->get_nesting_level(); SymbolListPtr filtered_data = this->get_analyzer()->get_symtable()->data_in_scope_at(search_lexeme, search_level); if (this->check_filter_size(filtered_data)) { SymbolPtr found = *filtered_data->begin(); found->set_col(token->get_column()); found->set_row(token->get_line()); return found; } else { SymbolListPtr global_data = this->get_analyzer()->get_symtable()->data_in_scope_at(search_lexeme, 0); if (this->check_filter_size(global_data)) { SymbolPtr found = *global_data->begin(); found->set_col(token->get_column()); found->set_row(token->get_line()); return found; } else { if (global_data == nullptr) { report_error_lc("Semantic Error", "ID '" + search_lexeme + "' not found", token->get_line(), token->get_column()); } else { report_error("Semantic Error", "ID '" + search_lexeme + "' redefined as..."); for (auto it = global_data->begin(); it != global_data->end(); it++) { SymDataPtr dptr = static_pointer_cast<SymData>(*it); report_error_lc("Definition @", (*it)->get_symbol_name() + " as " + var_type_to_string(dptr->get_var_type()), (*it)->get_row(), (*it)->get_col()); } } this->valid = false; return nullptr; } } } else if (token->get_token() == MP_INT_LITERAL) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), INTEGER_LITERAL)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_STRING_LITERAL) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), STRING_LITERAL)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_FLOAT_LITERAL) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), FLOATING_LITERAL)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_TRUE) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), BOOLEAN_LITERAL_T)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_FALSE) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), BOOLEAN_LITERAL_F)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_LEFT_PAREN) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), LPAREN)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_RIGHT_PAREN) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), RPAREN)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_PLUS) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), ADD)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_MINUS) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), SUB)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_MULT) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), MUL)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_DIV) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), DIV)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_DIV_KW) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), DIV)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_MOD_KW) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), MOD)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_AND) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), AND)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_OR) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), OR)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_NOT) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), NOT)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_LESSTHAN) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), ILT)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_EQUALS) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), IEQ)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_LESSTHAN_EQUALTO) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), ILE)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_GREATERTHAN) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), IGT)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_GREATERTHAN_EQUALTO) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), IGE)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else if (token->get_token() == MP_NOT_EQUAL) { SymbolPtr s = SymbolPtr(new SymConstant(token->get_lexeme(), INE)); s->set_row(token->get_line()); s->set_col(token->get_column()); return s; } else { return nullptr; } }