Commit 0c32bb1e authored by zygzagZ's avatar zygzagZ

compiler, quad code and information passing

parent 0b617d1c
CC=g++
CCFLAGS=-g -W -Wall -O0 -std=c++2a -Wno-unused-parameter
OBJS=Absyn.o Lexer.o Parser.o Printer.o TypeCheck.o Info.o ParseError.o Compiler.o
OBJS=Absyn.o Lexer.o Parser.o Printer.o TypeCheck.o Info.o Skeleton.o ParseError.o Compiler.o Quadruple.o
.PHONY : clean distclean
......@@ -29,9 +29,6 @@ Printer.o : src/Printer.cpp src/Printer.h src/Absyn.h
Skeleton.o : src/Skeleton.cpp src/Skeleton.h src/Absyn.h
${CC} ${CCFLAGS} -Wno-unused-parameter -c src/Skeleton.cpp
Latte.o : src/Latte.cpp src/Parser.h src/Printer.h src/Absyn.h src/ParseError.h src/TypeCheck.h src/Info.h src/InfoList.h
${CC} ${CCFLAGS} -c src/Latte.cpp
TypeCheck.o : src/TypeCheck.cpp src/TypeCheck.h src/Info.h src/InfoList.h src/Absyn.h src/ParseError.h
${CC} ${CCFLAGS} -c src/TypeCheck.cpp
......@@ -44,6 +41,14 @@ Info.o : src/Info.cpp src/Info.h src/InfoList.h src/Absyn.h
ParseError.o : src/ParseError.cpp src/Info.h src/InfoList.h src/Absyn.h src/ParseError.h
${CC} ${CCFLAGS} -c src/ParseError.cpp
Quadruple.o : src/codeGen/Quadruple.cpp src/Info.h src/InfoList.h src/Absyn.h src/codeGen/Variable.h
${CC} ${CCFLAGS} -c src/codeGen/Quadruple.cpp
Variable.o : src/codeGen/Quadruple.h src/codeGen/Variable.cpp src/Info.h src/InfoList.h src/Absyn.h
${CC} ${CCFLAGS} -c src/codeGen/Variable.cpp
Latte.o : src/Latte.cpp src/Parser.h src/Printer.h src/Absyn.h src/ParseError.h src/TypeCheck.h src/Compiler.h src/Info.h src/InfoList.h
${CC} ${CCFLAGS} -c src/Latte.cpp
lib/runtime.o : lib/runtime.c Makefile
gcc -m32 -static -L/usr/lib/gcc-cross/i686-linux-gnu/8/ -c lib/runtime.c -o lib/runtime.o -fno-stack-protector
......
#include "Compiler.h"
#include <filesystem>
Compiler::Compiler(std::string f)
: TypeCheck(), file(f), buf()
{
};
#include <iostream>
extern std::filesystem::path binaryPath;
const std::string Compiler::extension = "s";
......@@ -24,7 +20,6 @@ void Compiler::externalCommand(std::filesystem::path lat) {
}
std::string Compiler::compile(Visitable *v) {
check(v);
for (auto i : {"printStr", "printInt", "readInt", "readStr", "error", "exit", "_start"}) {
buf << ".globl " << i << "\n";
}
......
#ifndef COMPILER_HEADER
#define COMPILER_HEADER
#include "TypeCheck.h"
#include "TypeDefs.h"
#include "Absyn.h"
#include "Skeleton.h"
#include <filesystem>
#include <utility>
#include "codeGen/Variable.h"
#include "ParseError.h"
#include "Info.h"
class Compiler : public TypeCheck
class Compiler : public Skeleton
{
public:
Compiler(std::string f);
Compiler(const std::string& f, shared_ptr<Scope> s) : file(f), buf(), scope(std::move(s)) {};
std::string compile(Visitable *v);
static const std::string extension;
......@@ -16,6 +22,23 @@ public:
private:
std::filesystem::path file;
std::stringstream buf;
shared_ptr<Scope> scope;
VariablePtr lastVar;
VariablePtr evalExpr(Visitable *expr) {
if (!expr) throw runtime_error("No expr to eval");
lastVar = nullptr;
try {
expr->accept(this);
} catch (const ParseError &err) {
throw ParseError(err, expr);
}
if (!lastVar) throw ParseError("No variable found", expr);
auto ret = lastVar;
lastVar = nullptr;
return ret;
};
};
#endif
......@@ -3,9 +3,9 @@
#include "Parser.h"
#include "Printer.h"
#include "Absyn.h"
#include "Compiler.h"
#include "ParseError.h"
#include "TypeCheck.h"
#include "Compiler.h"
#include <fstream>
#include <iostream>
#include <filesystem>
......@@ -74,9 +74,12 @@ int main(int argc, char ** argv)
binaryPath = argv[0];
std::filesystem::path file(filename ? filename : "Latte");
auto scope = make_shared<Scope>();
TypeCheck typeCheck(scope);
Compiler compiler(file.string(), scope);
try {
Compiler c(file.string());
std::string out = c.compile(parse_tree);
typeCheck.check(parse_tree);
std::string out = compiler.compile(parse_tree);
if (!filename) {
printf("%s\n", out.data());
......
......@@ -29,7 +29,6 @@ void Skeleton::visitPIdent(PIdent *p_ident)
/* Code For PIdent Goes Here */
visitString(p_ident->string_);
visitInteger(p_ident->integer_);
}
......
......@@ -538,11 +538,6 @@ void TypeCheck::visitListExpr(ListExpr *list_expr)
void TypeCheck::visitInteger(Integer x) {}
void TypeCheck::visitString(String x) {}
TypeCheck::TypeCheck()
: state(initialized), scope(make_shared<Scope>())
{}
void TypeCheck::addIOFunctions(Type &type) {
string name = type.printName();
name[0] += 'A' - 'a';
......
......@@ -5,6 +5,7 @@
#include "Info.h"
#include "ParseError.h"
#include <memory>
#include <utility>
class TypeCheck : public Visitor
......@@ -59,7 +60,8 @@ class TypeCheck : public Visitor
};
public:
BindingPtr getParentBinding() const { return scope->currentClass ? static_pointer_cast<Binding>(scope->currentClass) : static_pointer_cast<Binding>(scope); }
TypeCheck();
TypeCheck() : state(initialized), scope(make_shared<Scope>()) {}
explicit TypeCheck(shared_ptr<Scope> s) : state(initialized), scope(std::move(s)) {}
void check(Visitable *v);
protected:
void visitClassDef(ClassDef *p);
......
#include "Quadruple.h"
#ifndef ZAD2_QUAD_H
#define ZAD2_QUAD_H
#include <string>
#include "../Info.h"
#include "Variable.h"
using namespace std;
class Quadruple {
public:
enum Op {
UNARY,
Neg,
Not,
BINARY,
Plus,
Minus,
Mul,
Div,
And,
Or,
Xor,
};
string label;
};
class QAssign : Quadruple {
VariablePtr loc;
Op op;
vector<VariablePtr> args;
VarInfoPtr varInfo;
};
#endif //ZAD2_QUAD_H
//
// Created by zygzagz on 02.01.2021.
//
#include "Variable.h"
//
// Created by zygzagz on 02.01.2021.
//
#ifndef ZAD2_VARIABLE_H
#define ZAD2_VARIABLE_H
class Variable : std::enable_shared_from_this<Variable> {
// registers
// is mem?
// where in mem - id or offset
};
using VariablePtr = shared_ptr<Variable>;
#endif //ZAD2_VARIABLE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment