Repository navigation
Expand file tree
/
Copy pathinterpreter.hpp
More file actions
64 lines (49 loc) · 1.5 KB
/
Copy pathinterpreter.hpp
File metadata and controls
64 lines (49 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
#include "ast.hpp"
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
class Interpreter {
public:
struct Value {
TypeKind type = TypeKind::Unknown;
long long intValue = 0;
bool boolValue = false;
std::string strValue;
std::vector<Value> arrayValue;
TypeKind elementType = TypeKind::Unknown;
static Value makeInt(long long v);
static Value makeBool(bool v);
static Value makeStr(std::string v);
static Value makeArray(std::size_t size);
};
class RuntimeEnvironment {
public:
RuntimeEnvironment();
void beginScope();
void endScope();
void define(const std::string &name, Value value);
Value *resolve(const std::string &name);
const Value *resolve(const std::string &name) const;
void assign(const std::string &name, Value value);
private:
std::vector<std::unordered_map<std::string, Value>> scopes_;
};
Interpreter();
void execute(const std::vector<std::unique_ptr<Stmt>> &program);
private:
struct ReturnSignal {
Value value;
};
RuntimeEnvironment env_;
std::unordered_map<std::string, const FuncStmt *> functions_;
void executeStmt(const Stmt *st);
Value evalExpr(const Expr *e);
Value callFunction(const CallExpr *call);
static std::string valueToString(const Value &v);
static void requireInts(const Value &lhs, const Value &rhs,
const std::string &op);
static bool equals(const Value &lhs, const Value &rhs);
static Value defaultValue();
};