diff --git a/code/compiler/08/ast.hpp b/code/compiler/08/ast.hpp index c536d17..c88fc99 100644 --- a/code/compiler/08/ast.hpp +++ b/code/compiler/08/ast.hpp @@ -43,27 +43,6 @@ struct branch { using branch_ptr = std::unique_ptr; -struct constructor { - std::string name; - std::vector types; - - constructor(std::string n, std::vector ts) - : name(std::move(n)), types(std::move(ts)) {} -}; - -using constructor_ptr = std::unique_ptr; - -struct definition { - virtual ~definition() = default; - - virtual void typecheck_first(type_mgr& mgr, type_env& env) = 0; - virtual void typecheck_second(type_mgr& mgr, const type_env& env) const = 0; - virtual void resolve(const type_mgr& mgr) = 0; - virtual void compile() = 0; -}; - -using definition_ptr = std::unique_ptr; - struct ast_int : public ast { int value; @@ -160,37 +139,3 @@ struct pattern_constr : public pattern { void print(std::ostream &to) const; void match(type_ptr t, type_mgr&, type_env& env) const; }; - -struct definition_defn : public definition { - std::string name; - std::vector params; - ast_ptr body; - - type_ptr return_type; - std::vector param_types; - - std::vector instructions; - - definition_defn(std::string n, std::vector p, ast_ptr b) - : name(std::move(n)), params(std::move(p)), body(std::move(b)) { - - } - - void typecheck_first(type_mgr& mgr, type_env& env); - void typecheck_second(type_mgr& mgr, const type_env& env) const; - void resolve(const type_mgr& mgr); - void compile(); -}; - -struct definition_data : public definition { - std::string name; - std::vector constructors; - - definition_data(std::string n, std::vector cs) - : name(std::move(n)), constructors(std::move(cs)) {} - - void typecheck_first(type_mgr& mgr, type_env& env); - void typecheck_second(type_mgr& mgr, const type_env& env) const; - void resolve(const type_mgr& mgr); - void compile(); -}; diff --git a/code/compiler/08/definition.cpp b/code/compiler/08/definition.cpp index 34c6f66..b3fb4df 100644 --- a/code/compiler/08/definition.cpp +++ b/code/compiler/08/definition.cpp @@ -1,5 +1,6 @@ -#include "ast.hpp" +#include "definition.hpp" #include "error.hpp" +#include "ast.hpp" void definition_defn::typecheck_first(type_mgr& mgr, type_env& env) { return_type = mgr.new_type(); diff --git a/code/compiler/08/definition.hpp b/code/compiler/08/definition.hpp new file mode 100644 index 0000000..1431b8b --- /dev/null +++ b/code/compiler/08/definition.hpp @@ -0,0 +1,63 @@ +#pragma once +#include +#include +#include "instruction.hpp" +#include "type_env.hpp" + +struct ast; +using ast_ptr = std::unique_ptr; + +struct definition { + virtual ~definition() = default; + + virtual void typecheck_first(type_mgr& mgr, type_env& env) = 0; + virtual void typecheck_second(type_mgr& mgr, const type_env& env) const = 0; + virtual void resolve(const type_mgr& mgr) = 0; + virtual void compile() = 0; +}; + +using definition_ptr = std::unique_ptr; + +struct constructor { + std::string name; + std::vector types; + + constructor(std::string n, std::vector ts) + : name(std::move(n)), types(std::move(ts)) {} +}; + +using constructor_ptr = std::unique_ptr; + +struct definition_defn : public definition { + std::string name; + std::vector params; + ast_ptr body; + + type_ptr return_type; + std::vector param_types; + + std::vector instructions; + + definition_defn(std::string n, std::vector p, ast_ptr b) + : name(std::move(n)), params(std::move(p)), body(std::move(b)) { + + } + + void typecheck_first(type_mgr& mgr, type_env& env); + void typecheck_second(type_mgr& mgr, const type_env& env) const; + void resolve(const type_mgr& mgr); + void compile(); +}; + +struct definition_data : public definition { + std::string name; + std::vector constructors; + + definition_data(std::string n, std::vector cs) + : name(std::move(n)), constructors(std::move(cs)) {} + + void typecheck_first(type_mgr& mgr, type_env& env); + void typecheck_second(type_mgr& mgr, const type_env& env) const; + void resolve(const type_mgr& mgr); + void compile(); +}; diff --git a/code/compiler/08/main.cpp b/code/compiler/08/main.cpp index ffbd4e5..728e2a6 100644 --- a/code/compiler/08/main.cpp +++ b/code/compiler/08/main.cpp @@ -1,5 +1,6 @@ #include "ast.hpp" #include +#include "definition.hpp" #include "parser.hpp" #include "error.hpp" #include "type.hpp" diff --git a/code/compiler/08/parser.y b/code/compiler/08/parser.y index 3874aca..088648d 100644 --- a/code/compiler/08/parser.y +++ b/code/compiler/08/parser.y @@ -2,6 +2,7 @@ #include #include #include "ast.hpp" +#include "definition.hpp" #include "parser.hpp" std::vector program; diff --git a/code/compiler/08/scanner.l b/code/compiler/08/scanner.l index 683deeb..c8a4429 100644 --- a/code/compiler/08/scanner.l +++ b/code/compiler/08/scanner.l @@ -3,6 +3,7 @@ %{ #include #include "ast.hpp" +#include "definition.hpp" #include "parser.hpp" #define YY_DECL yy::parser::symbol_type yylex()