-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.py
77 lines (51 loc) · 1.69 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
from abc import abstractmethod
from typing import Any, Optional
from expr import Expr
from tokens import Token
class Stmt:
expression = None
@abstractmethod
def accept(self, expr: Any):
pass
class Expression(Stmt):
def __init__(self, expression: Expr):
self.expression = expression
def accept(self, visitor):
return visitor.visit_expression_stmt(self)
class Print(Stmt):
def __init__(self, expression):
self.expression = expression
def accept(self, visitor):
return visitor.visit_print_stmt(self)
class Var(Stmt):
initializer: Expr = None
name: Token = None
def __init__(self, name: Token, initializer: Expr):
self.name = name
self.initializer = initializer
def accept(self, visitor):
return visitor.visit_var_stmt(self)
class Block(Stmt):
statements: list = None
def __init__(self, statements):
self.statements = statements
def accept(self, visitor):
return visitor.visit_block_stmt(self)
class If(Stmt):
condition: Optional[Expr] = None
then_branch: Optional[Stmt] = None
else_branch: Optional[Stmt] = None
def __init__(self, condition: Expr, then_branch: Stmt, else_branch: Stmt):
self.condition = condition
self.then_branch = then_branch
self.else_branch = else_branch
def accept(self, visitor):
return visitor.visit_if_stmt(self)
class While(Stmt):
condition: Optional[Expr] = None
body: Optional[Stmt] = None
def __init__(self, condition: Expr, body: Stmt):
self.condition = condition
self.body = body
def accept(self, visitor):
return visitor.visit_while_stmt(self)