--- /dev/null
+print(pow(3, 4))
--- /dev/null
+81
\ No newline at end of file
)
def generate_argument(c_argument):
+ if isinstance(c_argument, transformation.CFunctionCallExpression):
+ return generate_function_call(c_argument)
+
LITERAL_TYPE_MAPPING = {
transformation.CIntegerLiteral: generate_integer_literal,
transformation.CStringLiteral: generate_string_literal,
generate_argument(c_argument.right),
)
-def generate_statement(c_function_call_statement):
- return '{}({});'.format(
- c_function_call_statement.name,
- ', '.join(generate_argument(argument) for argument in c_function_call_statement.arguments),
+def generate_function_call(c_function_call):
+ return '{}({})'.format(
+ c_function_call.name,
+ ', '.join(generate_argument(argument) for argument in c_function_call.arguments),
)
+def generate_statement(c_function_call_statement):
+ return '{};'.format(generate_function_call(c_function_call_statement))
+
def generate(c_program):
template = ENV.get_template('program.c')
return template.render(
builtins=list(sorted(c_program.builtins)),
statements=[generate_statement(statement) for statement in c_program.statements],
- standard_libraries=set(['stdio.h']),
+ standard_libraries=list(sorted(c_program.standard_libraries)),
)
if __name__ == '__main__':
def _literal_level_expression_parser(index, tokens):
return _or_parser(
+ _function_call_expression_parser,
_integer_literal_expression_parser,
_string_literal_expression_parser,
)(index, tokens)
return True, index, result
-FunctionCall = collections.namedtuple(
- 'FunctionCall',
+def _comma_separated_list_parser(index, tokens):
+ failure = (False, index, None)
+
+ expressions = []
+
+ success, index, expression = _addition_level_expression_parser(index, tokens)
+
+ if success:
+ expressions.append(expression)
+ else:
+ return failure
+
+ while success and index < len(tokens) and tokens[index].type == 'comma':
+ success = False
+
+ if index + 1 < len(tokens):
+ success, try_index, expression = _addition_level_expression_parser(index + 1, tokens)
+
+ if success:
+ expressions.append(expression)
+ index = try_index
+
+ return True, index, tuple(expressions)
+
+
+FurFunctionCallExpression = collections.namedtuple(
+ 'FurFunctionCallExpression',
[
'name',
'arguments',
],
)
-def _function_call_parser(index, tokens):
+def _function_call_expression_parser(index, tokens):
failure = (False, index, None)
if tokens[index].type != 'symbol':
return failure
index += 1
- success, index, argument = _addition_level_expression_parser(index, tokens)
+ success, index, arguments = _comma_separated_list_parser(index, tokens)
if not success:
return failure
if tokens[index].type != 'close_parenthese':
- return failure
+ raise Exception('Expected ")", found "{}" on line {}'.format(
+ tokens[index].match,
+ tokens[index].line,
+ ))
index += 1
- return True, index, FunctionCall(name=name, arguments=(argument,))
+ return True, index, FurFunctionCallExpression(name=name, arguments=arguments)
def _program_formatter(statement_list):
return FurProgram(statement_list=statement_list)
-_program_parser = _zero_or_more_parser(_program_formatter, _function_call_parser)
+_program_parser = _zero_or_more_parser(_program_formatter, _function_call_expression_parser)
def _parse(parser, tokens):
success, index, result = parser(0, tokens)
raise Exception('Unable to parse')
-
def parse(tokens):
return _parse(_program_parser, tokens)
),
)
- class FunctionCallParserTests(unittest.TestCase):
+ class FurFunctionCallExpressionParserTests(unittest.TestCase):
def test_parses_function_with_string_literal_argument(self):
self.assertEqual(
- _function_call_parser(0, tokenization.tokenize("print('Hello, world')")),
+ _function_call_expression_parser(0, tokenization.tokenize("print('Hello, world')")),
(
True,
4,
- FunctionCall(
+ FurFunctionCallExpression(
name='print',
arguments=(FurStringLiteralExpression(value='Hello, world'),),
),
return result;
}
+{% if 'pow' in builtins %}
+Object builtin$pow(Object base, Object exponent)
+{
+ assert(base.type == INTEGER);
+ assert(exponent.type == INTEGER);
+
+ Object result;
+ result.type = INTEGER;
+ result.instance.integer = pow(base.instance.integer, exponent.instance.integer);
+ return result;
+}
+{% endif %}
+
{% if 'print' in builtins %}
void builtin$print(Object output)
{
'type',
'match',
'index',
+ 'line',
],
)
name, regex = definition
regex_matcher = re.compile(regex)
- def token_matcher(index, source):
+ def token_matcher(index, source, line):
match = regex_matcher.match(source[index:])
if match is None:
return (
True,
index + len(match.group()),
- Token(type=name, match=match.group(), index=index),
+ Token(type=name, match=match.group(), index=index, line=line),
)
return token_matcher
_TOKEN_MATCHERS = [
('open_parenthese', r'\('),
('close_parenthese', r'\)'),
+ ('comma', r','),
('integer_literal', r'\d+'),
('symbol', r'[a-z]+'),
('single_quoted_string_literal', r"'.*?'"),
@util.force_generator(tuple)
def tokenize(source):
index = 0
+ line = 1
while index < len(source):
if source[index] == ' ':
success = False
for matcher in _TOKEN_MATCHERS:
- success, index, token = matcher(index, source)
+ success, index, token = matcher(index, source, line)
if success:
yield token
raise Exception('Unexpected character "{}"'.format(source[index]))
while index < len(source) and source[index] in set(['\n']):
+ line += 1
index += 1
if __name__ == '__main__':
type='open_parenthese',
match='(',
index=0,
+ line=1,
),),
)
type='close_parenthese',
match=')',
index=0,
+ line=1,
),),
)
type='symbol',
match='print',
index=0,
+ line=1,
),),
)
type='single_quoted_string_literal',
match="'Hello, world'",
index=0,
+ line=1,
),),
)
type='addition_level_operator',
match='+',
index=0,
+ line=1,
),),
)
type='addition_level_operator',
match='-',
index=0,
+ line=1,
),),
)
type='multiplication_level_operator',
match='*',
index=0,
+ line=1,
),),
)
type='multiplication_level_operator',
match='//',
index=0,
+ line=1,
),),
)
type='multiplication_level_operator',
match='%',
index=0,
+ line=1,
),),
)
+ def test_tokenizes_comma(self):
+ self.assertEqual(
+ tokenize(','),
+ (Token(
+ type='comma',
+ match=',',
+ index=0,
+ line=1,
+ ),),
+ )
+
+
def test_handles_trailing_newline(self):
self.assertEqual(
tokenize('print\n'),
type='symbol',
match='print',
index=0,
+ line=1,
),),
)
type='symbol',
match='print',
index=1,
+ line=1,
),),
)
+ def test_tokenizes_with_proper_line_numbers(self):
+ self.assertEqual(
+ tokenize('print\n('),
+ (
+ Token(
+ type='symbol',
+ match='print',
+ index=0,
+ line=1,
+ ),
+ Token(
+ type='open_parenthese',
+ match='(',
+ index=6,
+ line=2,
+ ),
+ ),
+ )
+
+
unittest.main()
],
)
-CFunctionCallStatement = collections.namedtuple(
- 'CFunctionCallStatement',
+CFunctionCallExpression = collections.namedtuple(
+ 'CFunctionCallExpression',
[
'name',
'arguments',
)
BUILTINS = {
- 'print': ['stdio.h.'],
+ 'pow': ['math.h'],
+ 'print': ['stdio.h'],
}
def transform_expression(builtin_dependencies, expression):
+ if isinstance(expression, parsing.FurFunctionCallExpression):
+ return transform_function_call_expression(builtin_dependencies, expression)
LITERAL_TYPE_MAPPING = {
parsing.FurIntegerLiteralExpression: CIntegerLiteral,
right=transform_expression(builtin_dependencies, expression.right),
)
-def transform_function_call_statement(builtin_dependencies, function_call):
+def transform_function_call_expression(builtin_dependencies, function_call):
if function_call.name in BUILTINS.keys():
builtin_dependencies.add(function_call.name)
- return CFunctionCallStatement(
+ return CFunctionCallExpression(
name='builtin$' + function_call.name,
arguments=tuple(transform_expression(builtin_dependencies, arg) for arg in function_call.arguments),
)
builtins = set()
c_statements = [
- transform_function_call_statement(builtins, statement) for statement in program.statement_list
+ transform_function_call_expression(builtins, statement) for statement in program.statement_list
]
standard_libraries = set()