Welcome to mirror list, hosted at ThFree Co, Russian Federation.

command.py « parser « pclc « src - github.com/ianj-als/pcl.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dc8a036bcf85f7a3de3c35b61dfc1d02f365f893 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#
# Copyright Capita Translation and Interpreting 2013
#
# This file is part of Pipeline Creation Language (PCL).
# 
# Pipeline Creation Language (PCL) is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# Pipeline Creation Language (PCL) is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Pipeline Creation Language (PCL).  If not, see <http://www.gnu.org/licenses/>.
#
from entity import Entity


class Function(Entity):
    def __init__(self,
                 filename,
                 lineno,
                 name,
                 arguments):
        Entity.__init__(self, filename, lineno)
        self.name = name
        self.arguments = arguments

    def accept(self, visitor):
        visitor.visit(self)

    def __str__(self):
        return "%s(%s)" % \
               (str(self.name),
                ", ".join(map(lambda arg: str(arg), self.arguments)))

    def __repr__(self):
        return "<Function:\n\tname = %s\n\targuments = %s\n\tentity = %s>" % \
               (repr(self.name),
                ", ".join(map(lambda arg: arg.__repr__(), self.arguments)),
                super(Function, self).__repr__())

class Command(Entity):
    def __init__(self,
                 filename,
                 lineno,
                 identifier,
                 function):
        Entity.__init__(self, filename, lineno)
        self.identifier = identifier
        self.function = function

    def accept(self, visitor):
        self.function.accept(visitor)
        visitor.visit(self)

    def __str__(self):
        l = [str(self.function)]
        if self.identifier:
            l.insert(0, "<-")
            l.insert(0, str(self.identifier))
        return " ".join(l)
                    
    def __repr__(self):
        return "<Command:\n\tidentifier = %s,\n\tfunction = %s,\n\t" \
               "entity = %s>" % \
               (repr(self.identifier),
                repr(self.function),
                super(Command, self).__repr__())

class Return(Entity):
    def __init__(self,
                 filename,
                 lineno,
                 value = None,
                 mappings = list()):
        Entity.__init__(self, filename, lineno)
        self.value = value
        self.mappings = mappings

    def accept(self, visitor):
        visitor.visit(self)

    def __str__(self):
        def value_str():
            if self.value is not None:
                return str(self.value)
            elif not self.mappings:
                return "()"
            else:
                return ", ".join(map(lambda m: str(m), self.mappings))
        return "return %s" % value_str()

    def __repr__(self):
        return "<Return:\n\tvalue = %s\n\tmappings = %s\n\tentity = %s>" % \
               (repr(self.value),
                "()" if not self.mappings \
                     else ", ".join(map(lambda m: repr(m), self.mappings)), \
                super(Return, self).__repr__())

class IfCommand(Entity):
    class Block(Entity):
        def __init__(self, filename, lineno, commands, if_command):
            Entity.__init__(self, filename, lineno)
            self.commands = commands
            self.if_command = if_command

        def __getitem__(self, idx):
            return self.commands[idx]

        def __iter__(self):
            return self.commands.__iter__()

        def append(self, cmd):
            self.commands.append(cmd)

        def accept(self, visitor):
            visitor.visit(self)
            for cmd in self.commands:
                cmd.accept(visitor)

    class ThenBlock(Block):
        def __init__(self, filename, lineno, commands, if_command):
            IfCommand.Block.__init__(self, filename, lineno, commands, if_command)

    class ElseBlock(Block):
        def __init__(self, filename, lineno, commands, if_command):
            IfCommand.Block.__init__(self, filename, lineno, commands, if_command)

    def __init__(self,
                 filename,
                 lineno,
                 identifier,
                 condition,
                 then_commands,
                 else_commands):
        Entity.__init__(self, filename, lineno)
        self.identifier = identifier
        self.condition = condition
        self.then_commands = IfCommand.ThenBlock(filename, then_commands[0].lineno, then_commands, self)
        self.else_commands = IfCommand.ElseBlock(filename, else_commands[0].lineno, else_commands, self)

    def accept(self, visitor):
        self.condition.accept(visitor)
        self.then_commands.accept(visitor)
        self.else_commands.accept(visitor)
        visitor.visit(self)

    def __str__(self):
        f = lambda cmd: str(cmd)
        l = list()
        if self.identifier:
            l.extend([str(self.identifier), '<-'])
        l.extend(['if',
                  str(self.condition),
                  'then',
                  " ".join(map(f, self.then_commands)),
                  'else',
                  " ".join(map(f, self.else_commands)),
                  'endif'])
        return " ".join(l)

    def __repr__(self):
        f = lambda cmd: repr(cmd)
        return "<IfCommand:\n\tidentifier = %s\n\tcondition = %s,\n\tthen_commands = %s,\n\t" \
               "else_commands = %s\n\tentity = %s>" % \
               (repr(self.identifier),
                repr(self.condition),
                " ".join(map(f, self.then_commands)),
                " ".join(map(f, self.else_commands)),
                super(IfCommand, self).__repr__())

class LetCommand(Entity):
    class LetBindings(Entity):
        def __init__(self,
                     filename,
                     lineno,
                     bindings):
            Entity.__init__(self, filename, lineno)
            self.bindings = bindings

        def __getitem__(self, idx):
            return self.bindings[idx]

        def __iter__(self):
            return self.bindings.__iter__()

        def accept(self, visitor):
            visitor.visit(self)
            for binding in self.bindings:
                binding.accept(visitor)

    class LetEnd(Entity):
        def __init__(self, filename, lineno):
            Entity.__init__(self, filename, lineno)

        def accept(self, visitor):
            visitor.visit(self)

    def __init__(self,
                 filename,
                 lineno,
                 identifier,
                 bindings,
                 expression):
        Entity.__init__(self, filename, lineno)
        self.identifier = identifier
        self.bindings = LetCommand.LetBindings(filename, bindings[0].lineno, bindings)
        self.expression = expression
        self.let_end = LetCommand.LetEnd(filename, expression.lineno)

    def accept(self, visitor):
        visitor.visit(self)
        self.bindings.accept(visitor)
        self.expression.accept(visitor)
        self.let_end.accept(visitor)

    def __str__(self):
        l = ['let']
        l.extend([str(b) for b in self.bindings])
        l.append('in')
        l.append(str(self.expression))
        return ' '.join(l)

    def __repr__(self):
        f = lambda t: repr(t)
        return "<LetCommand:\n\tidentifier = %s\n\tbindings = %s\n\t" \
               "expression = %s\n\tentity = %s>" % \
               (repr(self.identifier),
                " ".join(map(f, self.bindings)),
                repr(self.expression),
                super(LetCommand, self).__repr__())