Add AST and pattern classes

This commit is contained in:
Danila Fedorin 2019-06-04 18:13:39 -07:00
parent 15fda4ea97
commit a6d381b697
2 changed files with 108 additions and 0 deletions

79
src/ast.hpp Normal file
View File

@ -0,0 +1,79 @@
#pragma once
#include <string>
#include <memory>
#include "pattern.hpp"
namespace lily {
struct ast {
virtual ~ast() = default;
};
typedef std::unique_ptr<ast> ast_ptr;
struct ast_num : ast {
int num;
ast_num(int i) : num(i) {}
~ast_num() = default;
};
struct ast_var : ast {
std::string name;
ast_var(std::string n) :
name(std::move(n)) {}
~ast_var() = default;
};
struct ast_app : ast {
ast_ptr left;
ast_ptr right;
ast_app(ast_ptr l, ast_ptr r) :
left(std::move(l)), right(std::move(r)) {}
~ast_app() = default;
};
struct ast_op : ast {
enum class op {
add,
subtrct,
times,
divide
} op;
std::unique_ptr<ast> left;
std::unique_ptr<ast> right;
ast_op(enum op o, ast_ptr l, ast_ptr r) :
op(o), left(std::move(l)), right(std::move(r)) {}
~ast_op() = default;
};
struct ast_let : ast {
std::string name;
ast_ptr expr;
ast_ptr in;
ast_let(std::string n, ast_ptr e, ast_ptr i) :
name(std::move(n)), expr(std::move(e)), in(std::move(i)) {}
~ast_let() = default;
};
struct ast_letrec : ast_let {
ast_letrec(std::string n, ast_ptr e, ast_ptr i) :
ast_let(std::move(n), std::move(e), std::move(i)) {}
~ast_letrec() = default;
};
struct ast_case : ast {
struct branch {
pattern_ptr pattern;
ast_ptr expr;
};
ast_ptr of;
std::vector<branch> branches;
~ast_case() = default;
};
}

29
src/pattern.hpp Normal file
View File

@ -0,0 +1,29 @@
#pragma once
#include <string>
#include <memory>
#include <vector>
namespace lily {
struct pattern {
virtual ~pattern();
};
typedef std::unique_ptr<pattern> pattern_ptr;
struct pattern_var {
std::string name;
pattern_var(std::string n) :
name(std::move(n)) {}
~pattern_var() = default;
};
struct pattern_cons {
std::string name;
std::vector<std::string> vnames;
pattern_cons(std::string n) :
name(std::move(n)) {}
~pattern_cons() = default;
};
}