Add print methods to instructions

This commit is contained in:
Danila Fedorin 2019-10-09 22:46:17 -07:00
parent d9486d08ae
commit 3901c9b115
4 changed files with 104 additions and 6 deletions

View File

@ -19,6 +19,7 @@ add_executable(compiler
type.cpp type.hpp type.cpp type.hpp
error.cpp error.hpp error.cpp error.hpp
binop.cpp binop.hpp binop.cpp binop.hpp
instruction.cpp instruction.hpp
${BISON_parser_OUTPUTS} ${BISON_parser_OUTPUTS}
${FLEX_scanner_OUTPUTS} ${FLEX_scanner_OUTPUTS}
main.cpp main.cpp

View File

@ -2,7 +2,7 @@
#include <ostream> #include <ostream>
#include "error.hpp" #include "error.hpp"
void print_indent(int n, std::ostream& to) { static void print_indent(int n, std::ostream& to) {
while(n--) to << " "; while(n--) to << " ";
} }

View File

@ -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;
}

View File

@ -1,12 +1,15 @@
#pragma once #pragma once
#include <string> #include <string>
#include <memory> #include <memory>
#include "binop.hpp"
#include <vector> #include <vector>
#include <map> #include <map>
#include <ostream>
#include "binop.hpp"
struct instruction { struct instruction {
virtual ~instruction() = default; virtual ~instruction() = default;
virtual void print(int indent, std::ostream& to) const = 0;
}; };
using instruction_ptr = std::unique_ptr<instruction>; using instruction_ptr = std::unique_ptr<instruction>;
@ -16,6 +19,8 @@ struct instruction_pushint : public instruction {
instruction_pushint(int v) instruction_pushint(int v)
: value(v) {} : value(v) {}
void print(int indent, std::ostream& to) const;
}; };
struct instruction_pushglobal : public instruction { struct instruction_pushglobal : public instruction {
@ -23,6 +28,8 @@ struct instruction_pushglobal : public instruction {
instruction_pushglobal(std::string n) instruction_pushglobal(std::string n)
: name(std::move(n)) {} : name(std::move(n)) {}
void print(int indent, std::ostream& to) const;
}; };
struct instruction_push : public instruction { struct instruction_push : public instruction {
@ -30,10 +37,12 @@ struct instruction_push : public instruction {
instruction_push(int o) instruction_push(int o)
: offset(o) {} : offset(o) {}
void print(int indent, std::ostream& to) const;
}; };
struct instruction_mkapp : public instruction { struct instruction_mkapp : public instruction {
void print(int indent, std::ostream& to) const;
}; };
struct instruction_update : public instruction { struct instruction_update : public instruction {
@ -41,6 +50,8 @@ struct instruction_update : public instruction {
instruction_update(int o) instruction_update(int o)
: offset(o) {} : offset(o) {}
void print(int indent, std::ostream& to) const;
}; };
struct instruction_pack : public instruction { struct instruction_pack : public instruction {
@ -49,15 +60,19 @@ struct instruction_pack : public instruction {
instruction_pack(int t, int s) instruction_pack(int t, int s)
: tag(t), size(s) {} : tag(t), size(s) {}
void print(int indent, std::ostream& to) const;
}; };
struct instruction_split : public instruction { struct instruction_split : public instruction {
void print(int indent, std::ostream& to) const;
}; };
struct instruction_jump : public instruction { struct instruction_jump : public instruction {
std::vector<std::vector<instruction_ptr>> branches; std::vector<std::vector<instruction_ptr>> branches;
std::map<int, int> tag_mappings; std::map<int, int> tag_mappings;
void print(int indent, std::ostream& to) const;
}; };
struct instruction_slide : public instruction { struct instruction_slide : public instruction {
@ -65,6 +80,8 @@ struct instruction_slide : public instruction {
instruction_slide(int o) instruction_slide(int o)
: offset(o) {} : offset(o) {}
void print(int indent, std::ostream& to) const;
}; };
struct instruction_binop : public instruction { struct instruction_binop : public instruction {
@ -72,10 +89,12 @@ struct instruction_binop : public instruction {
instruction_binop(binop o) instruction_binop(binop o)
: op(o) {} : op(o) {}
void print(int indent, std::ostream& to) const;
}; };
struct instruction_eval : public instruction { struct instruction_eval : public instruction {
void print(int indent, std::ostream& to) const;
}; };
struct instruction_alloc : public instruction { struct instruction_alloc : public instruction {
@ -83,8 +102,10 @@ struct instruction_alloc : public instruction {
instruction_alloc(int a) instruction_alloc(int a)
: amount(a) {} : amount(a) {}
void print(int indent, std::ostream& to) const;
}; };
struct instruction_unwind : public instruction { struct instruction_unwind : public instruction {
void print(int indent, std::ostream& to) const;
}; };