-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpreter.hpp
71 lines (65 loc) · 2.45 KB
/
Interpreter.hpp
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
65
66
67
68
69
70
71
// Copyright 2020 <Copyright hulin>
#ifndef INTERPRETER_HPP_
#define INTERPRETER_HPP_
#include <memory>
#include <utility>
#include <string>
#include <vector>
#include <map>
#include "./Expr.hpp"
#include "./Stmt.hpp"
#include "./Token.hpp"
#include "./Environment.hpp"
using std::shared_ptr;
using std::string;
using std::vector;
using std::map;
class Interpreter:
public Visitor<Object>,
public Visitor_Stmt,
public std::enable_shared_from_this<Interpreter>
{
public:
Interpreter();
void interpret(vector<shared_ptr<Stmt>> statements);
Object visitLiteralExpr(shared_ptr<Literal<Object>> expr);
Object visitAssignExpr(shared_ptr<Assign<Object>> expr);
Object visitBinaryExpr(shared_ptr<Binary<Object>> expr);
Object visitGroupingExpr(shared_ptr<Grouping<Object>> expr);
Object visitUnaryExpr(shared_ptr<Unary<Object>> expr);
Object visitVariableExpr(shared_ptr<Variable<Object>> expr);
Object visitLogicalExpr(shared_ptr<Logical<Object>> expr);
Object visitCallExpr(shared_ptr<Call<Object>> expr);
Object visitGetExpr(shared_ptr<Get<Object>> expr);
Object visitSetExpr(shared_ptr<Set<Object>> expr);
Object visitThisExpr(shared_ptr<This<Object>> expr);
Object visitSuperExpr(shared_ptr<Super<Object>> expr);
void visitExpressionStmt(const Expression& stmt);
void visitPrintStmt(const Print& stmt);
void visitVarStmt(const Var& stmt);
void visitBlockStmt(const Block& stmt);
void visitClassStmt(const Class& stmt);
void visitIfStmt(const If& stmt);
void visitWhileStmt(const While& stmt);
void visitFunctionStmt(shared_ptr<Function> stmt);
void visitReturnStmt(const Return& stmt);
void executeBlock(
vector<shared_ptr<Stmt>> statements,
shared_ptr<Environment> environment);
shared_ptr<Environment> globals
= shared_ptr<Environment>(new Environment());
void resolve(shared_ptr<Expr<Object>> expr, int depth);
private:
shared_ptr<Environment> environment
= globals;
map<shared_ptr<Expr<Object>>, int> locals;
Object evaluate(shared_ptr<Expr<Object>> expr);
void execute(shared_ptr<Stmt> stmt);
bool isTruthy(Object object);
bool isEqual(Object a, Object b);
void checkNumberOperand(Token operation, Object operand);
void checkNumberOperands(Token operation, Object left, Object right);
string stringify(Object object);
Object lookUpVariable(Token name, shared_ptr<Expr<Object>> expr);
};
#endif // INTERPRETER_HPP_