~kf5jwc/imp-parser

ref: 9a694fa0d8b73f01beb051b54cdab8a2b000be09 imp-parser/imp_parser/ast/statement.py -rw-r--r-- 1.1 KiB View raw
9a694fa0 — Kyle Jones I've done more type annotating, which.... helped? 1 year, 6 months ago
                                                                                
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
from dataclasses import dataclass
from .types import Aexp, Statement
from .boolean import RelopBexp

@dataclass
class AssignStatement(Statement):
    name: str
    aexp: Aexp

    def eval(self, env: dict) -> None:
        value = self.aexp.eval(env)
        env[self.name] = value


@dataclass
class CompoundStatement(Statement):
    first: Statement
    second: Statement

    def eval(self, env: dict) -> None:
        self.first.eval(env)
        self.second.eval(env)


@dataclass
class IfStatement(Statement):
    condition: RelopBexp
    true_stmt: Statement
    false_stmt: Statement

    def eval(self, env: dict) -> None:
        condition_value: bool = self.condition.eval(env)
        if condition_value:
            self.true_stmt.eval(env)
        else:
            if self.false_stmt:
                self.false_stmt.eval(env)


@dataclass
class WhileStatement(Statement):
    condition: RelopBexp
    body: CompoundStatement

    def eval(self, env: dict) -> None:
        condition_value = self.condition.eval(env)
        while condition_value:
            self.body.eval(env)
            condition_value = self.condition.eval(env)