),
)
+CPSLambdaExpression = collections.namedtuple(
+ 'CPSLambdaExpression',
+ (
+ 'argument_name_list',
+ 'statement_list',
+ ),
+)
+
CPSListConstructExpression = collections.namedtuple(
'CPSListConstructExpression',
(
def convert_integer_literal_expression(expression):
return CPSIntegerLiteralExpression(integer=expression.integer)
+def convert_lambda_expression(expression):
+ return CPSLambdaExpression(
+ argument_name_list=expression.argument_name_list,
+ statement_list=tuple(convert_statement(s) for s in expression.statement_list),
+ )
+
def convert_list_construct_expression(expression):
return CPSListConstructExpression(allocate=expression.allocate)
return {
normalization.NormalFunctionCallExpression: convert_function_call_expression,
normalization.NormalIntegerLiteralExpression: convert_integer_literal_expression,
+ normalization.NormalLambdaExpression: convert_lambda_expression,
normalization.NormalListConstructExpression: convert_list_construct_expression,
normalization.NormalStringLiteralExpression: convert_string_literal_expression,
normalization.NormalStructureLiteralExpression: convert_structure_literal_expression,
),
)
+DesugaredLambdaExpression = collections.namedtuple(
+ 'DesugaredLambdaExpression',
+ (
+ 'argument_name_list',
+ 'statement_list',
+ ),
+)
+
DesugaredListLiteralExpression = collections.namedtuple(
'DesugaredListLiteralExpression',
(
integer=expression.integer,
)
+def desugar_lambda_expression(expression):
+ return DesugaredLambdaExpression(
+ argument_name_list=expression.argument_name_list,
+ statement_list=tuple(desugar_statement(s) for s in expression.statement_list),
+ )
+
def desugar_list_item_expression(expression):
return DesugaredFunctionCallExpression(
metadata=expression.metadata,
parsing.FurIfExpression: desugar_if_expression,
parsing.FurInfixExpression: desugar_infix_expression,
parsing.FurIntegerLiteralExpression: desugar_integer_literal_expression,
+ parsing.FurLambdaExpression: desugar_lambda_expression,
parsing.FurListItemExpression: desugar_list_item_expression,
parsing.FurListLiteralExpression: desugar_list_literal_expression,
parsing.FurNegationExpression: desugar_negation_expression,
--- /dev/null
+def incrementer(n) do
+ lambda(m) do
+ m + n
+ end
+end
+
+print(incrementer(1)(42), '\n')
+print(incrementer(2)(42), '\n')
expression.value_list_variable,
)
+def generate_lambda_expression(expression):
+ return '(Object){{ CLOSURE, (Instance)(Closure){{ environment, user${}$implementation }} }}'.format(
+ expression.name,
+ )
+
def generate_list_construct_expression(expression):
return 'List_construct({})'.format(expression.allocate)
return {
transformation.CFunctionCallExpression: generate_function_call,
transformation.CIntegerLiteral: generate_integer_literal,
+ transformation.CLambdaExpression: generate_lambda_expression,
transformation.CListConstructExpression: generate_list_construct_expression,
transformation.CStringLiteral: generate_string_literal,
transformation.CStructureLiteralExpression: generate_structure_literal_expression,
return generated_if_clause + generated_if_statement_list + generated_else_statement_list
def generate_function_declaration(statement):
- return 'Environment_set(environment, "{}", (Object){{ CLOSURE, (Instance)(Closure){{ environment, user${}$implementation }} }});'.format(statement.name, statement.name)
+ return 'Environment_set(environment, "{}", (Object){{ CLOSURE, (Instance)(Closure){{ environment, user${}$implementation }} }});'.format(
+ statement.name,
+ statement.name,
+ )
def generate_list_append_statement(statement):
return 'List_append(&{}, {});'.format(
],
)
+NormalLambdaExpression = collections.namedtuple(
+ 'NormalLambdaExpression',
+ (
+ 'argument_name_list',
+ 'statement_list',
+ ),
+)
+
NormalStringLiteralExpression = collections.namedtuple(
'NormalStringLiteralExpression',
[
NormalVariableExpression(variable=variable),
)
+def normalize_lambda_expression(counter, expression):
+ variable = '${}'.format(counter)
+
+ _, statement_list = normalize_statement_list(
+ 0,
+ expression.statement_list,
+ assign_result_to='result',
+ )
+
+ return (
+ counter + 1,
+ (
+ NormalVariableInitializationStatement(
+ variable=variable,
+ expression=NormalLambdaExpression(
+ argument_name_list=expression.argument_name_list,
+ statement_list=statement_list,
+ ),
+ ),
+ ),
+ NormalVariableExpression(variable=variable),
+ )
+
NormalListConstructExpression = collections.namedtuple(
'NormalListConstructExpression',
[
desugaring.DesugaredFunctionCallExpression: normalize_function_call_expression,
desugaring.DesugaredIfExpression: normalize_if_expression,
desugaring.DesugaredIntegerLiteralExpression: normalize_integer_literal_expression,
+ desugaring.DesugaredLambdaExpression: normalize_lambda_expression,
desugaring.DesugaredListLiteralExpression: normalize_list_literal_expression,
desugaring.DesugaredStringLiteralExpression: normalize_string_literal_expression,
desugaring.DesugaredStructureLiteralExpression: normalize_structure_literal_expression,
],
)
+FurLambdaExpression = collections.namedtuple(
+ 'FurLambdaExpression',
+ (
+ 'argument_name_list',
+ 'statement_list',
+ ),
+)
+
FurStringLiteralExpression = collections.namedtuple(
'FurStringLiteralExpression',
[
),
)
+def _lambda_expression_parser(index, tokens):
+ failure = (False, index, None)
+
+ if tokens[index].type == 'keyword' and tokens[index].match == 'lambda':
+ index += 1
+ else:
+ return failure
+
+ if tokens[index].type == 'open_parenthese':
+ index += 1
+ else:
+ raise Exception('Expected "(", found "{}" on line {}'.format(
+ tokens[index].match,
+ tokens[index].metadata.line,
+ ))
+
+ success, index, argument_name_list = _comma_separated_list_parser(_symbol_expression_parser)(
+ index,
+ tokens,
+ )
+
+ if tokens[index].type == 'close_parenthese':
+ index += 1
+ else:
+ raise Exception('Expected ")", found "{}" on line {}'.format(
+ tokens[index].match,
+ tokens[index].line,
+ ))
+
+ if tokens[index].match == 'do':
+ index += 1
+ else:
+ return failure
+
+ success, index, statement_list = _zero_or_more_parser(tuple, _statement_parser)(index, tokens)
+
+ _, index, _ = consume_newlines(index, tokens)
+
+ if tokens[index].type == 'keyword' and tokens[index].match == 'end':
+ index += 1
+ else:
+ return failure
+
+ return True, index, FurLambdaExpression(
+ argument_name_list=tuple(an.symbol for an in argument_name_list),
+ statement_list=statement_list,
+ )
+
+
+
def _list_literal_expression_parser(index, tokens):
failure = (False, index, None)
_integer_literal_expression_parser,
_string_literal_expression_parser,
_list_literal_expression_parser,
+ _lambda_expression_parser,
_symbol_expression_parser,
_structure_literal_parser,
)(index, tokens)
return True, index, FurFunctionDefinitionStatement(
name=name,
- argument_name_list=tuple(an.symbol for an in argument_name_list),
+ argument_name_list=tuple(an.symbol for an in argument_name_list),
statement_list=statement_list,
)
return token_matcher
_TOKEN_MATCHERS = [
- ('keyword', r'(def|do|else|end|if)(?![a-z_])'),
+ ('keyword', r'(def|do|else|end|if|lambda)(?![a-z_])'),
('open_bracket', r'\['),
('close_bracket', r'\]'),
('open_parenthese', r'\('),
CListConstructExpression = collections.namedtuple(
'CListConstructExpression',
- [
+ (
'allocate',
- ],
+ ),
+)
+
+CLambdaExpression = collections.namedtuple(
+ 'CLambdaExpression',
+ (
+ 'name',
+ ),
)
CListAppendStatement = collections.namedtuple(
'CListAppendStatement',
- [
+ (
'list_expression',
'item_expression',
- ],
+ ),
)
def transform_structure_literal_expression(accumulators, expression):
value_list_variable=expression.value_list_variable,
)
+def transform_lambda_expression(accumulators, expression):
+ # TODO This function feels hacky
+ if len(accumulators.lambda_number_list) == 0:
+ accumulators.lambda_number_list.append(0)
+ else:
+ accumulators.lambda_number_list.append(accumulators.lambda_number_list[-1] + 1)
+
+ name = '__lambda_{}'.format(accumulators.lambda_number_list[-1])
+
+ accumulators.function_definition_list.append(CFunctionDefinition(
+ name=name,
+ argument_name_list=expression.argument_name_list,
+ statement_list=tuple(transform_statement(accumulators, s) for s in expression.statement_list),
+ ))
+
+ return CLambdaExpression(name=name)
+
+
def transform_list_construct_expression(accumulators, expression):
return CListConstructExpression(allocate=expression.allocate)
return {
conversion.CPSFunctionCallExpression: transform_function_call_expression,
conversion.CPSIntegerLiteralExpression: transform_integer_literal_expression,
+ conversion.CPSLambdaExpression: transform_lambda_expression,
conversion.CPSListConstructExpression: transform_list_construct_expression,
conversion.CPSStructureLiteralExpression: transform_structure_literal_expression,
conversion.CPSStringLiteralExpression: transform_string_literal_expression,
[
'builtin_set',
'function_definition_list',
+ 'lambda_number_list',
'operator_set',
'symbol_list',
'string_literal_list',
accumulators = Accumulators(
builtin_set=set(),
function_definition_list=[],
+ lambda_number_list=[],
operator_set=set(),
symbol_list=[],
string_literal_list=[],