~kf5jwc/imp-parser

ref: 4a8b0d0c4a9b3e235854c13031f981f55fc7ece5 imp-parser/imp_parser/ast/statement.py -rw-r--r-- 1.0 KiB View raw
4a8b0d0c — Kyle Jones More typing! 1 year, 7 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
import attr
from .types import Statement


@attr.s
class AssignStatement(Statement):
    name = attr.ib()
    aexp = attr.ib()

    def eval(self, env):
        value = self.aexp.eval(env)
        env[self.name] = value


@attr.s
class CompoundStatement(Statement):
    first = attr.ib()
    second = attr.ib()

    def eval(self, env):
        self.first.eval(env)
        self.second.eval(env)


@attr.s
class IfStatement(Statement):
    condition = attr.ib()
    true_stmt = attr.ib()
    false_stmt = attr.ib()

    def eval(self, env):
        condition_value = self.condition.eval(env)
        if condition_value:
            self.true_stmt.eval(env)
        else:
            if self.false_stmt:
                self.false_stmt.eval(env)


@attr.s
class WhileStatement(Statement):
    condition = attr.ib()
    body = attr.ib()

    def eval(self, env):
        condition_value = self.condition.eval(env)
        while condition_value:
            self.body.env(env)
            condition_value = self.condition.eval(env)