diff --git a/code/compiler/06/CMakeLists.txt b/code/compiler/06/CMakeLists.txt index 2ad7e91..5e5dab4 100644 --- a/code/compiler/06/CMakeLists.txt +++ b/code/compiler/06/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable(compiler type.cpp type.hpp error.cpp error.hpp binop.cpp binop.hpp + instruction.cpp instruction.hpp ${BISON_parser_OUTPUTS} ${FLEX_scanner_OUTPUTS} main.cpp diff --git a/code/compiler/06/ast.cpp b/code/compiler/06/ast.cpp index ad85a09..8c5c3f9 100644 --- a/code/compiler/06/ast.cpp +++ b/code/compiler/06/ast.cpp @@ -2,7 +2,7 @@ #include #include "error.hpp" -void print_indent(int n, std::ostream& to) { +static void print_indent(int n, std::ostream& to) { while(n--) to << " "; } diff --git a/code/compiler/06/instruction.cpp b/code/compiler/06/instruction.cpp new file mode 100644 index 0000000..b540ff6 --- /dev/null +++ b/code/compiler/06/instruction.cpp @@ -0,0 +1,76 @@ +#include "instruction.hpp" + +static void print_indent(int n, std::ostream& to) { + while(n--) to << " "; +} + +void instruction_pushint::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "PushInt(" << value << ")" << std::endl; +} + +void instruction_pushglobal::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "PushGlobal(" << name << ")" << std::endl; +} + +void instruction_push::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "Push(" << offset << ")" << std::endl; +} + +void instruction_mkapp::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "Push()" << std::endl; +} + +void instruction_update::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "Offset(" << offset << ")" << std::endl; +} + +void instruction_pack::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "Pack(" << tag << ", " << size << ")" << std::endl; +} + +void instruction_split::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "Split()" << std::endl; +} + +void instruction_jump::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "Jump(" << std::endl; + for(auto& instruction_set : branches) { + for(auto& instruction : instruction_set) { + instruction->print(indent + 2, to); + } + to << std::endl; + } +} + +void instruction_slide::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "Slide(" << offset << ")" << std::endl; +} + +void instruction_binop::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "BinOp(" << op_action(op) << ")" << std::endl; +} + +void instruction_eval::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "Eval()" << std::endl; +} + +void instruction_alloc::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "Alloc(" << amount << ")" << std::endl; +} + +void instruction_unwind::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "Unwind()" << std::endl; +} diff --git a/code/compiler/06/instruction.hpp b/code/compiler/06/instruction.hpp index 40196e4..879cd05 100644 --- a/code/compiler/06/instruction.hpp +++ b/code/compiler/06/instruction.hpp @@ -1,12 +1,15 @@ #pragma once #include #include -#include "binop.hpp" #include #include +#include +#include "binop.hpp" struct instruction { virtual ~instruction() = default; + + virtual void print(int indent, std::ostream& to) const = 0; }; using instruction_ptr = std::unique_ptr; @@ -16,6 +19,8 @@ struct instruction_pushint : public instruction { instruction_pushint(int v) : value(v) {} + + void print(int indent, std::ostream& to) const; }; struct instruction_pushglobal : public instruction { @@ -23,6 +28,8 @@ struct instruction_pushglobal : public instruction { instruction_pushglobal(std::string n) : name(std::move(n)) {} + + void print(int indent, std::ostream& to) const; }; struct instruction_push : public instruction { @@ -30,10 +37,12 @@ struct instruction_push : public instruction { instruction_push(int o) : offset(o) {} + + void print(int indent, std::ostream& to) const; }; struct instruction_mkapp : public instruction { - + void print(int indent, std::ostream& to) const; }; struct instruction_update : public instruction { @@ -41,6 +50,8 @@ struct instruction_update : public instruction { instruction_update(int o) : offset(o) {} + + void print(int indent, std::ostream& to) const; }; struct instruction_pack : public instruction { @@ -49,15 +60,19 @@ struct instruction_pack : public instruction { instruction_pack(int t, int s) : tag(t), size(s) {} + + void print(int indent, std::ostream& to) const; }; struct instruction_split : public instruction { - + void print(int indent, std::ostream& to) const; }; struct instruction_jump : public instruction { std::vector> branches; std::map tag_mappings; + + void print(int indent, std::ostream& to) const; }; struct instruction_slide : public instruction { @@ -65,6 +80,8 @@ struct instruction_slide : public instruction { instruction_slide(int o) : offset(o) {} + + void print(int indent, std::ostream& to) const; }; struct instruction_binop : public instruction { @@ -72,10 +89,12 @@ struct instruction_binop : public instruction { instruction_binop(binop o) : op(o) {} + + void print(int indent, std::ostream& to) const; }; struct instruction_eval : public instruction { - + void print(int indent, std::ostream& to) const; }; struct instruction_alloc : public instruction { @@ -83,8 +102,10 @@ struct instruction_alloc : public instruction { instruction_alloc(int a) : amount(a) {} + + void print(int indent, std::ostream& to) const; }; struct instruction_unwind : public instruction { - + void print(int indent, std::ostream& to) const; };