diff options
author | Ian Johnson <ian.johnson@appliedlanguage.com> | 2013-10-09 20:18:53 +0400 |
---|---|---|
committer | Ian Johnson <ian.johnson@appliedlanguage.com> | 2013-10-09 20:18:53 +0400 |
commit | 259b829b0b89966fa3613096f5015cd997f9e9f1 (patch) | |
tree | 4f9914f5db567241fac4991dcfe9037b08b162e1 | |
parent | c25101f03aced0408ec9e5d5b8f3c1b0bad91a29 (diff) |
Started adding runtime functions.
-rw-r--r-- | src/pclc/parser/command.py | 129 | ||||
-rw-r--r-- | src/pclc/parser/pcl_parser.py | 8 | ||||
-rw-r--r-- | src/pclc/visitors/first_pass_resolver_visitor.py | 2 | ||||
-rw-r--r-- | src/runtime/pcl/__init__.py | 0 | ||||
-rw-r--r-- | src/runtime/pcl/util/__init__.py | 0 | ||||
-rw-r--r-- | src/runtime/pcl/util/list.py | 60 | ||||
-rw-r--r-- | src/runtime/pcl/util/string.py | 21 |
7 files changed, 210 insertions, 10 deletions
diff --git a/src/pclc/parser/command.py b/src/pclc/parser/command.py new file mode 100644 index 0000000..b9b3921 --- /dev/null +++ b/src/pclc/parser/command.py @@ -0,0 +1,129 @@ +# +# 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>" % \ + (self.name.__repr__(), + ", ".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>" % \ + (self.identifier.__repr__() if self.identifier else None, + self.function.__repr__(), super(Command, self).__repr__()) + +class Return(Entity): + def __init__(self, + filename, + lineno, + mappings): + Entity.__init__(self, filename, lineno) + self.mappings = mappings + + def accept(self, visitor): + visitor.visit(self) + + def __str__(self): + return "return %s" % \ + ("()" if not self.mappings \ + else ", ".join(map(lambda m: str(m), self.mappings))) + + def __repr__(self): + return "<Return:\n\tmappings = %s\n\tentity = %s>" % \ + ("()" if not self.mappings \ + else ", ".join(map(lambda m: m.__repr__(), self.mappings)), \ + super(Return, self).__repr__()) + +class IfCommand(Entity): + def __init__(self, + filename, + lineno, + condition, + then_commands, + else_commands): + Entity.__init__(self, filename, lineno) + self.condition = condition + self.then_commands = then_commands + self.else_commands = else_commands + + def accept(self, visitor): + self.condition.accept(visitor) + for then_cmd in self.then_commands: + then_cmd.accept(visitor) + for else_cmd in self.else_commands: + else_cmd.accept(visitor) + visitor.visit(self) + + def __str__(self): + f = lambda cmd: str(cmd) + l = ['if', + str(self.condition), + 'else', + " ".join(map(f, then_commands)), + " ".join(map(f, else_commands))] + return " ".join(l) + + def __repr__(self): + f = lambda cmd: cmd.__repr__() + return "<IfCommand:\n\tcondition = %s,\n\tthen_commands = %s,\n\t" \ + "else_commands = %s\n\tentity = %s>" % \ + (self.condition.__repr__(), " ".join(map(f, self.then_commands)), + " ".join(map(f, self.else_commands)), super(IfCommand, self).__repr__()) diff --git a/src/pclc/parser/pcl_parser.py b/src/pclc/parser/pcl_parser.py index d874891..f4d186e 100644 --- a/src/pclc/parser/pcl_parser.py +++ b/src/pclc/parser/pcl_parser.py @@ -169,14 +169,6 @@ def p_mapping(p): '''mapping : identifier_or_literal MAPS_TO identifier_or_qual_identifier''' p[0] = Mapping(p.parser.filename, p[1].lineno, p[1], p[3]) -##def p_body_expression_or_do_notation(p): -## '''body_expression_or_do_notation : opt_declarations AS arrow_expression -## | DO do_command_list''' -## if len(p) > 2: -## p[0] = [p[1], p[2]] -## else: -## p[0] = p[2] - def p_arrow_expression(p): '''arrow_expression : composition_expression''' p[0] = p[1] diff --git a/src/pclc/visitors/first_pass_resolver_visitor.py b/src/pclc/visitors/first_pass_resolver_visitor.py index 9ecbe24..fc6ae99 100644 --- a/src/pclc/visitors/first_pass_resolver_visitor.py +++ b/src/pclc/visitors/first_pass_resolver_visitor.py @@ -306,8 +306,6 @@ class FirstPassResolverVisitor(ResolverVisitor): # Mark the import as not used for now ;) self._module.resolution_symbols['used_imports'][an_import.alias] = (an_import, False) - print self._errors - @multimethod(Component) def visit(self, component): # Component name *must* be the same as the file name diff --git a/src/runtime/pcl/__init__.py b/src/runtime/pcl/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/runtime/pcl/__init__.py diff --git a/src/runtime/pcl/util/__init__.py b/src/runtime/pcl/util/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/runtime/pcl/util/__init__.py diff --git a/src/runtime/pcl/util/list.py b/src/runtime/pcl/util/list.py new file mode 100644 index 0000000..242439e --- /dev/null +++ b/src/runtime/pcl/util/list.py @@ -0,0 +1,60 @@ +# +# 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/>. +# + +# cons :: [] +cons = lambda : list() + +# concat :: [*] -> [*] -> [*] +concat :: lambda l, m: l + m + +# append :: [a] -> a -> a +append = lambda l, x: l.append(x) or x + +# length :: [*] -> Int +length = lambda l: len(l) + +# insert :: [a] -> Int -> a +insert = lambda l, i, x: l.insert(i, x) or x + +# head :: [a] -> a +head = lambda l: l[0] + +# tail :: [a] -> [a] +tail = lambda l: l[1:] + +# last :: [a] -> a +last = lambda l: l[-1] + +# init :: [a] -> [a] +init = lambda l: l[0:-1] + +# reverse :: [a] -> [a] +reverse = lambda l: l[::-1] + +# take :: [a] -> Int -> [a] +take = lambda l, t: l[:t] + +# drop :: [a] -> Int -> [a] +drop = lambda l, d: l[d:] + +# elem :: [a] -> a -> Boolean +elem = lambda l, e: e in l + +# index :: [a] -> Int -> a +index = lambda l, i: l[i] diff --git a/src/runtime/pcl/util/string.py b/src/runtime/pcl/util/string.py new file mode 100644 index 0000000..120c9c6 --- /dev/null +++ b/src/runtime/pcl/util/string.py @@ -0,0 +1,21 @@ +# +# 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/>. +# + +# split :: String -> String -> String +split = lambda s, ss: s.__str__().split(ss) |