This commit is contained in:
2026-01-09 21:59:30 +01:00
parent 6b16ce98bc
commit 3a4c292473
5 changed files with 90 additions and 9 deletions

67
def.yy
View File

@@ -31,9 +31,11 @@ std::map<std::string, SymbolInfo> symbolTable;
int memoryCounter = 0;
std::stack<StackElement> expressionsStack;
std::stack<std::string> labelStack;
FILE *tripletFile;
int tempVarCounter = 0;
int stringCounter = 0;
int labelCounter = 0;
std::vector<std::string> asmCode;
std::map<std::string, std::string> stringLiterals;
@@ -57,6 +59,10 @@ void addSymbol(const std::string& name, const std::string& type) {
}
}
std::string generateLabel() {
return "LBL" + std::to_string(labelCounter++);
}
// ASM
std::string generateLoad(const std::string& reg, const std::string& value) {
@@ -171,6 +177,42 @@ void writeTriplet(const std::string& result, const std::string& arg1,
generateAsm(result, arg1, arg2, op);
}
void generateIfJumpStatement(const std::string& op) {
if (expressionsStack.size() < 2) {
yyerror("Blad: niewystarczajaca liczba elementów w warunku");
return;
}
StackElement rhs = expressionsStack.top();
expressionsStack.pop();
StackElement lhs = expressionsStack.top();
expressionsStack.pop();
std::string label = generateLabel();
labelStack.push(label);
asmCode.push_back(generateLoad("$t2", lhs.value));
asmCode.push_back(generateLoad("$t3", rhs.value));
std::string jumpInstr;
if (op == "<") jumpInstr = "bge";
else if (op == ">") jumpInstr = "ble";
else if (op == "<=") jumpInstr = "bgt";
else if (op == ">=") jumpInstr = "blt";
else if (op == "==") jumpInstr = "bne";
else if (op == "!=") jumpInstr = "beq";
asmCode.push_back(jumpInstr + " $t2, $t3, " + label);
}
void generateIfEndStatement() {
if (!labelStack.empty()) {
std::string label = labelStack.top();
labelStack.pop();
asmCode.push_back(label + ":");
}
}
%}
%union {
@@ -181,10 +223,13 @@ void writeTriplet(const std::string& result, const std::string& arg1,
%token <text> ID
%token <text> STRING_LIT
%token <ival> INT_LIT
%token IF
%token LET PRINT_INT PRINT_FLOAT PRINT_STRING READ_INT READ_FLOAT
%token INT_TYPE
%token SEMICOLON COLON
%token LE GE EQ NE
%left '<' '>' LE GE EQ NE
%left '+' '-'
%left '*' '/'
@@ -206,6 +251,7 @@ statement
| assignment { printf("Instrukcja przypisania\n"); }
| print_statement { printf("Instrukcja wypisania\n"); }
| read_statement { printf("Instrukcja odczytu\n"); }
| if_expr { printf("Instrukcja warunkowa\n"); }
| expression SEMICOLON {
printf("instrukcja\n");
if (!expressionsStack.empty()) {
@@ -214,6 +260,27 @@ statement
}
;
if_expr
: if_begin code_block { generateIfEndStatement(); }
;
if_begin
: IF '(' cond_expr ')' { generateIfJumpStatement($<text>3); }
;
code_block
: '{' statement_list '}'
;
cond_expr
: expression '<' expression { $<text>$ = strdup("<"); }
| expression '>' expression { $<text>$ = strdup(">"); }
| expression LE expression { $<text>$ = strdup("<="); }
| expression GE expression { $<text>$ = strdup(">="); }
| expression EQ expression { $<text>$ = strdup("=="); }
| expression NE expression { $<text>$ = strdup("!="); }
;
variable_declaration
: LET ID COLON INT_TYPE '=' expression SEMICOLON
{