Begin implementation of new environment

This commit is contained in:
Danila Fedorin 2019-10-01 14:34:38 -07:00
parent 8c0a6c834e
commit bcaa67cc7a
7 changed files with 142 additions and 20 deletions

View File

@ -14,6 +14,7 @@ add_flex_bison_dependency(scanner parser)
add_executable(compiler
ast.cpp ast.hpp definition.cpp
type_env.cpp type_env.hpp
env.cpp env.hpp
type.cpp type.hpp
error.cpp error.hpp

View File

@ -2,6 +2,9 @@
#include <memory>
#include <vector>
#include "type.hpp"
#include "type_env.hpp"
#include "binop.hpp"
#include "instruction.hpp"
#include "env.hpp"
struct ast {
@ -9,6 +12,8 @@ struct ast {
virtual void print(int indent, std::ostream& to) const = 0;
virtual type_ptr typecheck(type_mgr& mgr, const type_env& env) const = 0;
virtual void compile(const env_ptr env,
std::vector<instruction>& into) const;
};
using ast_ptr = std::unique_ptr<ast>;
@ -51,13 +56,6 @@ struct definition {
using definition_ptr = std::unique_ptr<definition>;
enum binop {
PLUS,
MINUS,
TIMES,
DIVIDE
};
struct ast_int : public ast {
int value;

View File

@ -0,0 +1,9 @@
#pragma once
enum binop {
PLUS,
MINUS,
TIMES,
DIVIDE
};

View File

@ -1,16 +1,31 @@
#pragma once
#include <map>
#include "type.hpp"
#include <memory>
#include <string>
struct type_env {
std::map<std::string, type_ptr> names;
type_env const* parent = nullptr;
struct env {
virtual ~env() = default;
type_env(type_env const* p)
: parent(p) {}
type_env() : type_env(nullptr) {}
type_ptr lookup(const std::string& name) const;
void bind(const std::string& name, type_ptr t);
type_env scope() const;
virtual int get_offset(const std::string& name) const = 0;
};
using env_ptr = std::shared_ptr<env>;
struct env_var {
std::string name;
env_ptr parent;
env_var(std::string& n, env_ptr p)
: name(std::move(n)), parent(std::move(p)) {}
virtual int get_offset(const std::string& name) const;
};
struct env_offset {
int offset;
env_ptr parent;
env_offset(int o, env_ptr p)
: offset(o), parent(std::move(p)) {}
virtual int get_offset(const std::string& name) const;
};

View File

@ -0,0 +1,83 @@
#pragma once
#include <string>
#include <memory>
#include "binop.hpp"
struct instruction {
virtual ~instruction() = default;
};
using instruction_ptr = std::unique_ptr<instruction>;
struct instruction_pushint : public instruction {
int value;
instruction_pushint(int v)
: value(v) {}
};
struct instruction_pushglobal : public instruction {
std::string name;
instruction_pushglobal(std::string n)
: name(std::move(n)) {}
};
struct instruction_push : public instruction {
int offset;
instruction_push(int o)
: offset(o) {}
};
struct instruction_mkapp : public instruction {
};
struct instruction_update : public instruction {
int offset;
instruction_update(int o)
: offset(o) {}
};
struct instruction_pack : public instruction {
int tag;
int size;
instruction_pack(int t, int s)
: tag(t), size(s) {}
};
struct instruction_split : public instruction {
};
struct instruction_slide : public instruction {
int offset;
instruction_slide(int o)
: offset(o) {}
};
struct instruction_binop : public instruction {
binop op;
instruction_binop(binop o)
: op(o) {}
};
struct instruction_eval : public instruction {
};
struct instruction_alloc : public instruction {
int amount;
instruction_alloc(int a)
: amount(a) {}
};
struct instruction_unwind : public instruction {
};

View File

@ -1,4 +1,4 @@
#include "env.hpp"
#include "type_env.hpp"
type_ptr type_env::lookup(const std::string& name) const {
auto it = names.find(name);

View File

@ -0,0 +1,16 @@
#pragma once
#include <map>
#include "type.hpp"
struct type_env {
std::map<std::string, type_ptr> names;
type_env const* parent = nullptr;
type_env(type_env const* p)
: parent(p) {}
type_env() : type_env(nullptr) {}
type_ptr lookup(const std::string& name) const;
void bind(const std::string& name, type_ptr t);
type_env scope() const;
};