--- /dev/null
+-1
\ No newline at end of file
''.join(c_escape(ch for ch in c_string_literal.value)),
)
-def generate_argument(c_argument):
+def generate_expression(c_argument):
+ if isinstance(c_argument, transformation.CNegationExpression):
+ return generate_negation_expression(c_argument)
+
if isinstance(c_argument, transformation.CFunctionCallExpression):
return generate_function_call(c_argument)
return 'builtin${}({}, {})'.format(
INFIX_TYPE_MAPPING[type(c_argument)],
- generate_argument(c_argument.left),
- generate_argument(c_argument.right),
+ generate_expression(c_argument.left),
+ generate_expression(c_argument.right),
+ )
+
+def generate_negation_expression(c_negation_expression):
+ return 'builtin$negate({})'.format(
+ generate_expression(c_negation_expression.value)
)
def generate_function_call(c_function_call):
return '{}({})'.format(
c_function_call.name,
- ', '.join(generate_argument(argument) for argument in c_function_call.arguments),
+ ', '.join(generate_expression(argument) for argument in c_function_call.arguments),
)
def generate_statement(c_function_call_statement):
],
)
+FurNegationExpression = collections.namedtuple(
+ 'FurNegationExpression',
+ [
+ 'value',
+ ],
+)
+
FurAdditionExpression = collections.namedtuple(
'FurAdditionExpression',
[
return True, index, FurStringLiteralExpression(value=value)
+def _negation_expression_parser(index, tokens):
+ failure = (False, index, None)
+
+ if tokens[index].match != '-':
+ return failure
+
+ success, index, value = _literal_level_expression_parser(index + 1, tokens)
+
+ if not success:
+ return failure
+
+ return (True, index, FurNegationExpression(value=value))
+
def _literal_level_expression_parser(index, tokens):
return _or_parser(
+ _negation_expression_parser,
_function_call_expression_parser,
_integer_literal_expression_parser,
_string_literal_expression_parser,
}
// TODO Make this conditionally added
+Object builtin$negate(Object input)
+{
+ assert(input.type == INTEGER);
+
+ Object result;
+ result.type = INTEGER;
+ result.instance.integer = -input.instance.integer;
+ return result;
+}
+
Object builtin$add(Object left, Object right)
{
assert(left.type == INTEGER);
],
)
+CNegationExpression = collections.namedtuple(
+ 'CNegationExpression',
+ [
+ 'value',
+ ],
+)
+
CAdditionExpression = collections.namedtuple(
'CAdditionExpression',
[
}
def transform_expression(builtin_dependencies, expression):
+ if isinstance(expression, parsing.FurNegationExpression):
+ return transform_negation_expression(builtin_dependencies, expression)
+
if isinstance(expression, parsing.FurFunctionCallExpression):
return transform_function_call_expression(builtin_dependencies, expression)
right=transform_expression(builtin_dependencies, expression.right),
)
+def transform_negation_expression(builtin_dependencies, negation_expression):
+ return CNegationExpression(value=transform_expression(builtin_dependencies, negation_expression.value))
+
def transform_function_call_expression(builtin_dependencies, function_call):
if function_call.name in BUILTINS.keys():
builtin_dependencies.add(function_call.name)