-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResolver.hpp
74 lines (69 loc) · 2.38 KB
/
Resolver.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
72
73
74
// Copyright 2020.2.22 <Copyright hulin>
#ifndef RESOLVER_HPP_
#define RESOLVER_HPP_
#include <memory>
#include <vector>
#include <string>
#include <map>
#include "./Stmt.hpp"
#include "./Expr.hpp"
#include "./Interpreter.hpp"
#include "./Token.hpp"
using std::shared_ptr;
using std::vector;
using std::string;
using std::map;
class Resolver:
public Visitor<Object>,
public Visitor_Stmt,
public std::enable_shared_from_this<Resolver> {
public:
vector<map<string, bool>> scopes;
shared_ptr<Interpreter> interpreter;
explicit Resolver(shared_ptr<Interpreter> interpreter);
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 resolve(vector<shared_ptr<Stmt>> statements);
void resolve(shared_ptr<Stmt> stmt);
void resolve(shared_ptr<Expr<Object>> expr);
private:
enum FunctionType {
FUNCTION_NONE,
FUNCTION,
METHOD,
INITIALIZER
};
enum ClassType {
CLASS_NONE,
CLASS,
SUBCLASS,
};
ClassType currentClass = CLASS_NONE;
FunctionType currentFunction = FUNCTION_NONE;
void beginScope();
void endScope();
void declare(Token name);
void define(Token name);
void resolveLocal(shared_ptr<Expr<Object>>, Token name);
void resolveFunction(shared_ptr<Function> function, FunctionType type);
};
#endif // RESOLVER_HPP_