32 lines
889 B
C++
32 lines
889 B
C++
#pragma once
|
|
|
|
#include <map>
|
|
#include <string>
|
|
#include "antlr4-runtime.h"
|
|
#include "generated/ifccBaseVisitor.h"
|
|
|
|
|
|
class CodeGenVisitor : public ifccBaseVisitor {
|
|
|
|
struct VarInfo {
|
|
int offset; // stack offset relative to sp
|
|
bool initialized; // true after a set_stmt assigns a value
|
|
};
|
|
|
|
// Symbol table: variable name -> VarInfo
|
|
std::map<std::string, VarInfo> symbolTable;
|
|
int nextOffset = 0; // first variable at [sp, #0], next at [sp, #4], etc.
|
|
|
|
public:
|
|
std::any visitProg(ifccParser::ProgContext *ctx) override ;
|
|
std::any visitStmt(ifccParser::StmtContext *ctx) override ;
|
|
std::any visitReturn_stmt(ifccParser::Return_stmtContext *ctx) override ;
|
|
std::any visitDecl_stmt(ifccParser::Decl_stmtContext *ctx) override ;
|
|
std::any visitSet_stmt(ifccParser::Set_stmtContext *ctx) override ;
|
|
std::any visitVal(ifccParser::ValContext *ctx) override ;
|
|
|
|
};
|
|
|
|
|
|
|