blog-static/code/compiler_ast.hpp

77 lines
1.3 KiB
C++

#pragma once
#include <memory>
#include <vector>
struct ast {
virtual ~ast();
};
using ast_ptr = std::unique_ptr<ast>;
struct pattern {
virtual ~pattern();
};
struct pattern_var : public pattern {
std::string var;
pattern_var(const char* v)
: var(v) {}
};
struct pattern_constr : public pattern {
std::string constr;
std::vector<std::string> params;
pattern_constr(const char* c, std::vector<std::string>&& p)
: constr(c) {
std::swap(params, p);
}
};
using pattern_ptr = std::unique_ptr<pattern>;
struct branch {
pattern_ptr pat;
ast_ptr expr;
branch(pattern_ptr&& p, ast_ptr&& a)
: pat(std::move(p)), expr(std::move(a)) {}
};
using branch_ptr = std::unique_ptr<branch>;
enum binop {
PLUS,
MINUS,
TIMES,
DIVIDE
};
struct ast_binop : public ast {
binop op;
ast_ptr left;
ast_ptr right;
ast_binop(binop o, ast_ptr&& l, ast_ptr&& r)
: op(o), left(std::move(l)), right(std::move(r)) {}
};
struct ast_app : public ast {
ast_ptr left;
ast_ptr right;
ast_app(ast* l, ast* r)
: left(l), right(r) {}
};
struct ast_case : public ast {
ast_ptr of;
std::vector<branch_ptr> branches;
ast_case(ast_ptr&& o, std::vector<branch_ptr>&& b)
: of(std::move(o)) {
std::swap(branches, b);
}
};