--- /dev/null
+print('Hello, world\n')
+print(42, '\n')
--- /dev/null
+Hello, world
+42
generate_expression(c_negation_expression.value)
)
-def generate_function_call(c_function_call):
- return '{}({})'.format(
- c_function_call.name,
- ', '.join(generate_expression(argument) for argument in c_function_call.arguments),
+def generate_function_call(function_call):
+ return '{}({}, {})'.format(
+ function_call.name,
+ function_call.argument_count,
+ # TODO This is just a single item containing a reference to the items list--make that clearer
+ generate_expression(function_call.argument_items),
)
def generate_expression_statement(statement):
generate_expression(c_assignment_statement.expression),
)
+def generate_array_variable_initialization_statement(statement):
+ return 'Object {}[] = {{ {} }};'.format(
+ statement.variable,
+ ', '.join(generate_expression(i) for i in statement.items),
+ )
+
def generate_variable_initialization_statement(statement):
return 'Object {} = {};'.format(
statement.variable,
transformation.CExpressionStatement: generate_expression_statement,
transformation.CIfElseStatement: generate_if_else_statement,
transformation.CSymbolAssignmentStatement: generate_symbol_assignment_statement,
+ transformation.CArrayVariableInitializationStatement: generate_array_variable_initialization_statement,
transformation.CVariableInitializationStatement: generate_variable_initialization_statement,
transformation.CVariableReassignmentStatement: generate_variable_reassignment_statement,
}[type(statement)](statement)
'NormalFunctionCallExpression',
[
'function',
- 'arguments',
+ 'argument_count',
+ 'argument_items',
+ ],
+)
+
+NormalArrayVariableInitializationStatement = collections.namedtuple(
+ 'NormalArrayVariableInitializationStatement',
+ [
+ 'variable',
+ 'items',
],
)
))
counter += 1
+ arguments_variable = '${}'.format(counter)
+ counter += 1
+
+ prestatements.append(NormalArrayVariableInitializationStatement(
+ variable=arguments_variable,
+ items=tuple(arguments),
+ ))
+
return (
counter,
tuple(prestatements),
NormalFunctionCallExpression(
- expression.function, # TODO Normalize the function
- arguments=tuple(arguments),
+ function=expression.function, # TODO Normalize the function
+ argument_count=len(arguments),
+ argument_items=NormalVariableExpression(variable=arguments_variable),
),
)
}
{% if 'pow' in builtins %}
-Object builtin$pow(Object base, Object exponent)
+Object builtin$pow(size_t argc, Object args[])
{
+ assert(argc == 2);
+
+ Object base = args[0];
+ Object exponent = args[1];
+
assert(base.type == INTEGER);
assert(exponent.type == INTEGER);
{% endif %}
{% if 'print' in builtins %}
-void builtin$print(Object output)
+void builtin$print(size_t argc, Object args[])
{
- switch(output.type)
+ for(size_t i = 0; i < argc; i++)
{
- case BOOLEAN:
- fputs(output.instance.boolean ? "true" : "false", stdout);
- break;
+ Object output = args[i];
+ switch(output.type)
+ {
+ case BOOLEAN:
+ fputs(output.instance.boolean ? "true" : "false", stdout);
+ break;
- case INTEGER:
- printf("%" PRId32, output.instance.integer);
- break;
+ case INTEGER:
+ printf("%" PRId32, output.instance.integer);
+ break;
- case STRING:
- // Using fwrite instead of printf to handle size_t length
- printf("%s", output.instance.string);
- break;
+ case STRING:
+ // Using fwrite instead of printf to handle size_t length
+ printf("%s", output.instance.string);
+ break;
- default:
- assert(false);
+ default:
+ assert(false);
+ }
}
}
{% endif %}
'CFunctionCallExpression',
[
'name',
- 'arguments',
+ 'argument_count',
+ 'argument_items',
],
)
],
)
+CArrayVariableInitializationStatement = collections.namedtuple(
+ 'CArrayVariableInitializationStatement',
+ [
+ 'variable',
+ 'items',
+ ],
+)
+
CVariableInitializationStatement = collections.namedtuple(
'CVariableInitializationStatement',
[
)
def transform_function_call_expression(accumulators, function_call):
- # TODO Function should be a full expression
if function_call.function.value in BUILTINS.keys():
# TODO Check that the builtin is actually callable
accumulators.builtin_set.add(function_call.function.value)
return CFunctionCallExpression(
name='builtin$' + function_call.function.value,
- arguments=tuple(
- transform_expression(accumulators, arg)
- for arg in function_call.arguments
- ),
+ argument_count=function_call.argument_count,
+ argument_items=transform_expression(accumulators, function_call.argument_items),
)
raise Exception()
else_statements=tuple(transform_statement(accumulators, s) for s in statement.else_statements),
)
+def transform_array_variable_initialization_statement(accumulators, statement):
+ return CArrayVariableInitializationStatement(
+ variable=statement.variable,
+ items=tuple(transform_expression(accumulators, i) for i in statement.items),
+ )
+
def transform_variable_initialization_statement(accumulators, statement):
return CVariableInitializationStatement(
variable=statement.variable,
parsing.FurExpressionStatement: transform_expression_statement,
normalization.NormalExpressionStatement: transform_expression_statement,
normalization.NormalIfElseStatement: transform_if_else_statement,
+ normalization.NormalArrayVariableInitializationStatement: transform_array_variable_initialization_statement,
normalization.NormalVariableInitializationStatement: transform_variable_initialization_statement,
normalization.NormalVariableReassignmentStatement: transform_variable_reassignment_statement,
}[type(statement)](accumulators, statement)