import collections
+def _or_parser(*parsers):
+ def result_parser(index, tokens):
+ failure = (False, index, None)
+
+ for parser in parsers:
+ success, index, value = parser(index, tokens)
+
+ if success:
+ return (success, index, value)
+
+ return failure
+
+ return result_parser
+
+def _zero_or_more_parser(formatter, parser):
+ def result_parser(index, tokens):
+ values = []
+
+ while index < len(tokens):
+ success, index, value = parser(index, tokens)
+
+ if success:
+ values.append(value)
+ else:
+ break
+
+ return (True, index, formatter(values))
+
+ return result_parser
+
IntegerLiteral = collections.namedtuple(
'IntegerLiteral',
[
return True, index, StringLiteral(value=value)
-def _argument_parser(index, tokens):
- failure = (False, index, None)
-
- for parser in [_integer_literal_parser, _string_literal_parser]:
- success, index, value = parser(index, tokens)
-
- if success:
- return (success, index, value)
-
- return failure
-
+_argument_parser = _or_parser(_integer_literal_parser, _string_literal_parser)
FunctionCall = collections.namedtuple(
'FunctionCall',
],
)
+FurProgram = collections.namedtuple(
+ 'FurProgram',
+ [
+ 'statement_list',
+ ],
+)
+
def _function_call_parser(index, tokens):
failure = (False, index, None)
return True, index, FunctionCall(name=name, arguments=(argument,))
+def _program_formatter(statement_list):
+ return FurProgram(statement_list=statement_list)
+
+_program_parser = _zero_or_more_parser(_program_formatter, _function_call_parser)
+
def _parse(parser, tokens):
success, index, result = parser(0, tokens)
+ if index < len(tokens):
+ raise Exception('Unable to parse token {}'.format(tokens[index]))
+
if success:
return result
def parse(tokens):
- return _parse(_function_call_parser, tokens)
+ return _parse(_program_parser, tokens)
if __name__ == '__main__':
import unittest
raise Exception()
-def transform(function_call):
+def transform(program):
builtins = set()
- statement = transform_function_call_statement(builtins, function_call)
+ c_statements = [
+ transform_function_call_statement(builtins, statement) for statement in program.statement_list
+ ]
standard_libraries = set()
for builtin in builtins:
return CProgram(
builtins=builtins,
- statements=[statement],
+ statements=c_statements,
standard_libraries=standard_libraries,
)
import functools
-def force_generator(generator_function):
- @functools.wraps(generator_function)
- def forced_generator(*args, **kwargs):
- return list(generator_function(*args, **kwargs))
+def force_generator(to_type):
+ def decorator(generator_function):
+ @functools.wraps(generator_function)
+ def forced_generator(*args, **kwargs):
+ return to_type(generator_function(*args, **kwargs))
- return forced_generator
+ return forced_generator
+
+ return decorator
if __name__ == '__main__':
import unittest
class ForceGeneratorTests(unittest.TestCase):
def test_forces_generator(self):
- forced_range = force_generator(range)
+ forced_range = force_generator(list)(range)
self.assertEqual(
forced_range(10),