Lab 6
This commit is contained in:
88
def.yy
88
def.yy
@@ -33,8 +33,10 @@ int memoryCounter = 0;
|
||||
std::stack<StackElement> expressionsStack;
|
||||
FILE *tripletFile;
|
||||
int tempVarCounter = 0;
|
||||
int stringCounter = 0;
|
||||
|
||||
std::vector<std::string> asmCode;
|
||||
std::map<std::string, std::string> stringLiterals;
|
||||
|
||||
bool isNumber(const std::string& s) {
|
||||
if (s.empty()) return false;
|
||||
@@ -86,11 +88,49 @@ void generateAsm(const std::string& result, const std::string& arg1,
|
||||
}
|
||||
}
|
||||
|
||||
void generatePrintInt(const std::string& var) {
|
||||
asmCode.push_back("# print_integer(" + var + ")");
|
||||
asmCode.push_back(generateLoad("$a0", var));
|
||||
asmCode.push_back("li $v0, 1");
|
||||
asmCode.push_back("syscall");
|
||||
}
|
||||
|
||||
void generatePrintFloat(const std::string& var) {
|
||||
asmCode.push_back("# print_float(" + var + ")");
|
||||
asmCode.push_back("lwc1 $f12, " + var);
|
||||
asmCode.push_back("li $v0, 2");
|
||||
asmCode.push_back("syscall");
|
||||
}
|
||||
|
||||
void generatePrintString(const std::string& strLabel) {
|
||||
asmCode.push_back("# print_string(" + strLabel + ")");
|
||||
asmCode.push_back("la $a0, " + strLabel);
|
||||
asmCode.push_back("li $v0, 4");
|
||||
asmCode.push_back("syscall");
|
||||
}
|
||||
|
||||
void generateReadInt(const std::string& var) {
|
||||
asmCode.push_back("# read_integer -> " + var);
|
||||
asmCode.push_back("li $v0, 5");
|
||||
asmCode.push_back("syscall");
|
||||
asmCode.push_back("sw $v0, " + var);
|
||||
}
|
||||
|
||||
void generateReadFloat(const std::string& var) {
|
||||
asmCode.push_back("# read_float -> " + var);
|
||||
asmCode.push_back("li $v0, 6");
|
||||
asmCode.push_back("syscall");
|
||||
asmCode.push_back("swc1 $f0, " + var);
|
||||
}
|
||||
|
||||
void writeDataSection(FILE* out) {
|
||||
fprintf(out, ".data\n");
|
||||
for (auto& pair : symbolTable) {
|
||||
fprintf(out, "%s: .word 0\n", pair.first.c_str());
|
||||
}
|
||||
for (auto& pair : stringLiterals) {
|
||||
fprintf(out, "%s: .asciiz %s\n", pair.first.c_str(), pair.second.c_str());
|
||||
}
|
||||
fprintf(out, "\n");
|
||||
}
|
||||
|
||||
@@ -139,8 +179,9 @@ void writeTriplet(const std::string& result, const std::string& arg1,
|
||||
}
|
||||
|
||||
%token <text> ID
|
||||
%token <text> STRING_LIT
|
||||
%token <ival> INT_LIT
|
||||
%token LET
|
||||
%token LET PRINT_INT PRINT_FLOAT PRINT_STRING READ_INT READ_FLOAT
|
||||
%token INT_TYPE
|
||||
%token SEMICOLON COLON
|
||||
|
||||
@@ -163,6 +204,8 @@ statement_list
|
||||
statement
|
||||
: variable_declaration { printf("Deklaracja zmiennej\n"); }
|
||||
| assignment { printf("Instrukcja przypisania\n"); }
|
||||
| print_statement { printf("Instrukcja wypisania\n"); }
|
||||
| read_statement { printf("Instrukcja odczytu\n"); }
|
||||
| expression SEMICOLON {
|
||||
printf("instrukcja\n");
|
||||
if (!expressionsStack.empty()) {
|
||||
@@ -196,6 +239,47 @@ variable_declaration
|
||||
}
|
||||
;
|
||||
|
||||
print_statement
|
||||
: PRINT_INT '(' expression ')' SEMICOLON
|
||||
{
|
||||
printf("print_integer\n");
|
||||
if (!expressionsStack.empty()) {
|
||||
StackElement expr = expressionsStack.top();
|
||||
expressionsStack.pop();
|
||||
generatePrintInt(expr.value);
|
||||
}
|
||||
}
|
||||
| PRINT_FLOAT '(' expression ')' SEMICOLON
|
||||
{
|
||||
printf("print_float\n");
|
||||
if (!expressionsStack.empty()) {
|
||||
StackElement expr = expressionsStack.top();
|
||||
expressionsStack.pop();
|
||||
generatePrintFloat(expr.value);
|
||||
}
|
||||
}
|
||||
| PRINT_STRING '(' STRING_LIT ')' SEMICOLON
|
||||
{
|
||||
printf("print_string: %s\n", $3);
|
||||
std::string label = "str" + std::to_string(stringCounter++);
|
||||
stringLiterals[label] = std::string($3);
|
||||
generatePrintString(label);
|
||||
}
|
||||
;
|
||||
|
||||
read_statement
|
||||
: READ_INT '(' ID ')' SEMICOLON
|
||||
{
|
||||
printf("read_integer -> %s\n", $3);
|
||||
generateReadInt(std::string($3));
|
||||
}
|
||||
| READ_FLOAT '(' ID ')' SEMICOLON
|
||||
{
|
||||
printf("read_float -> %s\n", $3);
|
||||
generateReadFloat(std::string($3));
|
||||
}
|
||||
;
|
||||
|
||||
expression
|
||||
: expression '+' expression {
|
||||
printf("Wyrazenie z +\n");
|
||||
@@ -275,7 +359,6 @@ int main(int argc, char *argv[]) {
|
||||
return OUTFILE_ERROR;
|
||||
}
|
||||
|
||||
// Otwórz plik wyjściowy na asembler
|
||||
yyout = fopen("output.asm", "w");
|
||||
if (!yyout) {
|
||||
fprintf(stderr, "Blad output.asm\n");
|
||||
@@ -292,7 +375,6 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
fclose(yyout);
|
||||
|
||||
// Zapisz tablicę symboli do symbols.txt
|
||||
saveSymbolTable();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user