--- /dev/null
+def return_zero() do
+ print('This should not print\n')
+ 2
+end
+
+def return_one() do
+ print('This should only print once.\n')
+ 1
+end
+
+def return_true() do
+ print('This should print exactly twice.\n')
+ true
+end
+
+def return_false() do
+ print('This should print exactly twice.\n')
+ false
+end
+
+0 < return_one() < 2
+0 < 0 < return_zero()
+return_true() or return_true()
+return_false() and return_false()
--- /dev/null
+This should only print once.
+This should print exactly twice.
+This should print exactly twice.
return CONSTANT_EXPRESSION_MAPPING[c_constant_expression.value]
def generate_symbol_expression(c_symbol_expression):
- return 'Environment_get(environment, SYMBOL_LIST[{}] /* symbol: {} */)'.format(
+ return 'Environment_get(&environment, SYMBOL_LIST[{}] /* symbol: {} */)'.format(
c_symbol_expression.symbol_list_index,
c_symbol_expression.symbol,
)
)
def generate_function_call(function_call):
- return 'Environment_get(environment, "{}").instance.closure({}, {})'.format(
+ return 'Environment_get(&environment, "{}").instance.closure(&environment, {}, {})'.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
)
def generate_expression_statement(statement):
+ # TODO Do this at an earlier pass
+ if isinstance(statement.expression, transformation.CVariableExpression):
+ return '';
+
# TODO Do we need to garbage collect the results of arbitrary statements?
return '{};'.format(generate_expression(statement.expression))
def generate_symbol_assignment_statement(c_assignment_statement):
- return 'Environment_set(environment, SYMBOL_LIST[{}] /* symbol: {} */, {});'.format(
+ return 'Environment_set(&environment, SYMBOL_LIST[{}] /* symbol: {} */, {});'.format(
c_assignment_statement.target_symbol_list_index,
c_assignment_statement.target,
generate_expression(c_assignment_statement.expression),
def generate_if_else_statement(statement):
# TODO Check that the argument is boolean
- return 'if({}.instance.boolean)\n{{\n{}\n}}\nelse\n{{\n{}\n}}'.format(
+ condition_expression = '{}.instance.boolean'.format(
generate_expression(statement.condition_expression),
- indent('\n'.join(generate_statement(s) for s in statement.if_statements)),
- indent('\n'.join(generate_statement(s) for s in statement.else_statements)),
)
+ if len(statement.if_statements) == 0:
+ condition_expression = '!({})'.format(condition_expression)
+ if_statements = statement.else_statements
+ else_statements = ()
+ else:
+ if_statements = statement.if_statements
+ else_statements = statement.else_statements
+
+ generated_if_clause = 'if({})'.format(condition_expression)
+
+ if len(if_statements) == 0:
+ generated_if_statements = ';'
+ else:
+ generated_if_statements = indent('\n{{\n{}\n}}'.format(
+ indent('\n'.join(generate_statement(s) for s in if_statements)),
+ ))
+
+ if len(else_statements) == 0:
+ generated_else_statements = ''
+ else:
+ generated_else_statements = indent('\nelse\n{{\n{}\n}}'.format(
+ indent('\n'.join(generate_statement(s) for s in else_statements)),
+ ))
+
+ return generated_if_clause + generated_if_statements + generated_else_statements
+
def generate_function_declaration(statement):
- return 'Environment_set(environment, "{}", user${});'.format(statement.name, statement.name)
+ return 'Environment_set(&environment, "{}", user${});'.format(statement.name, statement.name)
def generate_statement(statement):
return {
}[type(expression)](counter, expression)
def normalize_expression_statement(counter, statement):
+ # TODO Verify all expression types are supported and just call normalize_expression
counter, prestatements, normalized = {
parsing.FurFunctionCallExpression: normalize_function_call_expression,
+ parsing.FurSymbolExpression: normalize_expression,
+ parsing.FurInfixExpression: normalize_expression,
parsing.FurIntegerLiteralExpression: normalize_expression,
}[type(statement.expression)](counter, statement.expression)
typedef union Instance Instance;
struct Object;
typedef struct Object Object;
+struct EnvironmentNode;
+typedef struct EnvironmentNode EnvironmentNode;
+struct Environment;
+typedef struct Environment Environment;
const char* const STRING_LITERAL_LIST[] = {
{% for string_literal in string_literal_list %}
union Instance
{
bool boolean;
- Object (*closure)(size_t, Object*);
+ Object (*closure)(Environment*, size_t, Object*);
int32_t integer;
const char* string;
};
false
};
-struct EnvironmentNode;
-typedef struct EnvironmentNode EnvironmentNode;
struct EnvironmentNode
{
const char* key;
EnvironmentNode* next;
};
-struct Environment;
-typedef struct Environment Environment;
struct Environment
{
+ Environment* parent;
EnvironmentNode* root;
};
-Environment* Environment_construct()
+void Environment_initialize(Environment* self, Environment* parent)
{
- // TODO Handle malloc returning NULL
- Environment* result = malloc(sizeof(Environment));
- result->root = NULL;
- return result;
+ self->parent = parent;
+ self->root = NULL;
}
-void Environment_destruct(Environment* self)
+void Environment_deinitialize(Environment* self)
{
EnvironmentNode* next;
for(EnvironmentNode* node = self->root; node != NULL; node = next)
}
}
+ if(self->parent != NULL)
+ {
+ return Environment_get(self->parent, symbol);
+ }
+
// TODO Handle symbol errors
assert(false);
}
}
{% if 'pow' in builtins %}
-Object builtin$pow$implementation(size_t argc, Object* args)
+Object builtin$pow$implementation(Environment* parent, size_t argc, Object* args)
{
assert(argc == 2);
{% endif %}
{% if 'print' in builtins %}
-Object builtin$print$implementation(size_t argc, Object* args)
+Object builtin$print$implementation(Environment* parent, size_t argc, Object* args)
{
for(size_t i = 0; i < argc; i++)
{
{% endif %}
{% for function_definition in function_definition_list %}
-Object user${{function_definition.name}}$implementation(size_t argc, Object* args)
+Object user${{function_definition.name}}$implementation(Environment* parent, size_t argc, Object* args)
{
- Environment* environment = Environment_construct();
+ Environment environment;
+ Environment_initialize(&environment, parent);
- {% for statement in function_definition.statement_list %}
+ {% for statement in function_definition.statement_list[:-1] %}
{{ generate_statement(statement) }}
{% endfor %}
Object result = {{ generate_statement(function_definition.statement_list[-1]) }}
- Environment_destruct(environment);
+ Environment_deinitialize(&environment);
return result;
}
int main(int argc, char** argv)
{
- Environment* environment = Environment_construct();
+ Environment environment;
+ Environment_initialize(&environment, NULL);
// TODO Use the symbol from SYMBOL_LIST
{% for builtin in builtins %}
- Environment_set(environment, "{{ builtin }}", builtin${{ builtin }});
+ Environment_set(&environment, "{{ builtin }}", builtin${{ builtin }});
{% endfor %}
{% for statement in statements %}
{{ generate_statement(statement) }}
{% endfor %}
- Environment_destruct(environment);
+ Environment_deinitialize(&environment);
return 0;
}
)
def transform_expression_statement(accumulators, statement):
+ # TODO At some point we can verify that all expression types are supported and just call transform_expression
expression = {
parsing.FurFunctionCallExpression: transform_function_call_expression,
+ parsing.FurInfixExpression: transform_expression,
parsing.FurIntegerLiteralExpression: transform_expression,
+ parsing.FurSymbolExpression: transform_expression,
normalization.NormalFunctionCallExpression: transform_function_call_expression,
+ normalization.NormalVariableExpression: transform_expression,
}[type(statement.expression)](accumulators, statement.expression)
return CExpressionStatement(