-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.py
129 lines (88 loc) · 2.32 KB
/
stmt.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""Syntax tree that represents the Lox grammar"""
from abc import ABC, abstractmethod
class Stmt(ABC):
@abstractmethod
def accept(self, visitor):
pass
class Visitor(ABC):
@abstractmethod
def visit_block_stmt(self, stmt):
pass
@abstractmethod
def visit_class_stmt(self, stmt):
pass
@abstractmethod
def visit_expression_stmt(self, stmt):
pass
@abstractmethod
def visit_function_stmt(self, stmt):
pass
@abstractmethod
def visit_if_stmt(self, stmt):
pass
@abstractmethod
def visit_print_stmt(self, stmt):
pass
@abstractmethod
def visit_return_stmt(self, stmt):
pass
@abstractmethod
def visit_var_stmt(self, stmt):
pass
@abstractmethod
def visit_while_stmt(self, stmt):
pass
class Block(Stmt):
def __init__(self, statements):
self.statements = statements
def accept(self, visitor):
return visitor.visit_block_stmt(self)
class Class(Stmt):
def __init__(self, name, superclass, methods):
self.name = name
self.superclass = superclass
self.methods = methods
def accept(self, visitor):
return visitor.visit_class_stmt(self)
class Expression(Stmt):
def __init__(self, expression):
self.expression = expression
def accept(self, visitor):
return visitor.visit_expression_stmt(self)
class Function(Stmt):
def __init__(self, name, params, body):
self.name = name
self.params = params
self.body = body
def accept(self, visitor):
return visitor.visit_function_stmt(self)
class If(Stmt):
def __init__(self, condition, then_branch, else_branch):
self.condition = condition
self.then_branch = then_branch
self.else_branch = else_branch
def accept(self, visitor):
return visitor.visit_if_stmt(self)
class Print(Stmt):
def __init__(self, expression):
self.expression = expression
def accept(self, visitor):
return visitor.visit_print_stmt(self)
class Return(Stmt):
def __init__(self, keyword, value):
self.keyword = keyword
self.value = value
def accept(self, visitor):
return visitor.visit_return_stmt(self)
class Var(Stmt):
def __init__(self, name, initializer):
self.name = name
self.initializer = initializer
def accept(self, visitor):
return visitor.visit_var_stmt(self)
class While(Stmt):
def __init__(self, condition, body):
self.condition = condition
self.body = body
def accept(self, visitor):
return visitor.visit_while_stmt(self)