--- /dev/null
+print('Hello' ++ ', ' ++ 'world' ++ '\n')
--- /dev/null
+Hello, world
return 'integerLiteral({})'.format(c_integer_literal.value)
def generate_string_literal(c_string_literal):
- return 'stringLiteral(STRING_LITERAL_LIST[{}])'.format(c_string_literal.index)
+ return 'stringLiteral(STRING_LITERAL_LIST[{}] /* string: {} */)'.format(
+ c_string_literal.index,
+ repr(c_string_literal.value),
+ )
def generate_symbol_expression(symbol_expression):
return 'Environment_get(environment, SYMBOL_LIST[{}] /* symbol: {} */)'.format(
)
def generate_expression_statement(statement):
- # TODO Do we need to garbage collect the results of arbitrary statements?
- return '{};'.format(generate_expression(statement.expression))
+ return 'Object_deinitialize(&({}));'.format(generate_expression(statement.expression))
def generate_symbol_assignment_statement(statement):
return 'Environment_set(environment, SYMBOL_LIST[{}] /* symbol: {} */, {});'.format(
function_expression = NormalVariableExpression(variable=function_variable)
counter += 1
+ result_variable = '${}'.format(counter)
+
+ prestatements.append(
+ NormalVariableInitializationStatement(
+ variable=result_variable,
+ expression=NormalFunctionCallExpression(
+ function_expression=function_expression,
+ argument_count=len(arguments),
+ argument_items=NormalVariableExpression(variable=arguments_variable),
+ ),
+ )
+ )
+
return (
- counter,
+ counter + 1,
tuple(prestatements),
- NormalFunctionCallExpression(
- function_expression=function_expression,
- argument_count=len(arguments),
- argument_items=NormalVariableExpression(variable=arguments_variable),
- ),
+ NormalVariableExpression(variable=result_variable),
)
def normalize_basic_infix_operation(counter, expression):
CLOSURE,
INTEGER,
LIST,
- STRING,
+ STRING_CONCATENATION,
+ STRING_LITERAL,
VOID
};
Object* items;
};
+struct StringConcatenation;
+typedef struct StringConcatenation StringConcatenation;
+
union Instance
{
bool boolean;
Closure closure;
int32_t integer;
List list;
- const char* string;
+ StringConcatenation* string_concatenation;
+ const char* string_literal;
};
struct Object
const Object builtin$false = { BOOLEAN, (Instance)(bool){ false } };
const Object builtin$nil = { VOID, { 0 } };
+struct StringConcatenation
+{
+ size_t referenceCount;
+ Object left;
+ Object right;
+};
+
Object List_construct(size_t allocate)
{
Object* items = malloc(sizeof(Object) * allocate);
self->live = true;
}
-void Environment_deinitialize(Environment* self)
+void Object_deinitialize(Object* self)
{
- EnvironmentNode* next;
- for(EnvironmentNode* node = self->root; node != NULL; node = next)
+ switch(self->type)
{
- next = node->next;
+ case BOOLEAN:
+ break;
+ case CLOSURE:
+ break;
+ case INTEGER:
+ break;
+ case STRING_LITERAL:
+ break;
+ case VOID:
+ break;
+
+ case LIST:
+ for(size_t i = 0; i < self->instance.list.length; i++) {
+ Object_deinitialize(&(self->instance.list.items[i]));
+ }
- switch(node->value.type)
- {
- case BOOLEAN:
- case CLOSURE:
- case INTEGER:
- case STRING:
- case VOID:
- break;
+ free(self->instance.list.items);
+ break;
- case LIST:
- free(node->value.instance.list.items);
- break;
+ case STRING_CONCATENATION:
+ self->instance.string_concatenation->referenceCount--;
- default:
- assert(false);
- }
+ if(self->instance.string_concatenation->referenceCount == 0)
+ {
+ Object_deinitialize(&(self->instance.string_concatenation->left));
+ Object_deinitialize(&(self->instance.string_concatenation->right));
+ free(self->instance.string_concatenation);
+ }
+ break;
+ default:
+ assert(false);
+ }
+}
+
+void Environment_deinitialize(Environment* self)
+{
+ EnvironmentNode* next;
+ for(EnvironmentNode* node = self->root; node != NULL; node = next)
+ {
+ next = node->next;
+ Object_deinitialize(&(node->value));
free(node);
}
}
{
case BOOLEAN:
case INTEGER:
- case STRING:
+ case STRING_LITERAL:
case VOID:
break;
Object stringLiteral(const char* literal)
{
Object result;
- result.type = STRING;
- result.instance.string = literal;
+ result.type = STRING_LITERAL;
+ result.instance.string_literal = literal;
return result;
}
return result;
}
+// TODO Make this conditionally added
+Object operator$concatenate(Object left, Object right)
+{
+ switch(left.type) {
+ case STRING_CONCATENATION:
+ left.instance.string_concatenation->referenceCount++;
+ break;
+
+ case STRING_LITERAL:
+ break;
+
+ default:
+ assert(false);
+ }
+
+ switch(right.type) {
+ case STRING_CONCATENATION:
+ right.instance.string_concatenation->referenceCount++;
+ break;
+
+ case STRING_LITERAL:
+ break;
+
+ default:
+ assert(false);
+ }
+
+ StringConcatenation* concatenation = malloc(sizeof(StringConcatenation));
+ concatenation->referenceCount = 1;
+ concatenation->left = left;
+ concatenation->right = right;
+
+ Object result = { STRING_CONCATENATION, (Instance)concatenation };
+ return result;
+}
+
{% for id in infix_declarations %}
Object operator${{ id.name }}(Object left, Object right)
{
printf("%" PRId32, output.instance.integer);
break;
- case STRING:
+ case STRING_CONCATENATION:
+ builtin$print$implementation(NULL, NULL, 1, &(output.instance.string_concatenation->left));
+ builtin$print$implementation(NULL, NULL, 1, &(output.instance.string_concatenation->right));
+ break;
+
+ case STRING_LITERAL:
// Using fwrite instead of printf to handle size_t length
- printf("%s", output.instance.string);
+ printf("%s", output.instance.string_literal);
break;
case VOID:
default:
assert(false);
}
+ Object_deinitialize(&output);
}
// TODO Return something better
('single_quoted_string_literal', r"'.*?'"),
('comparison_level_operator', r'(<=|>=|==|!=|<|>)'),
('assignment_operator', r'='),
- ('addition_level_operator', r'(\+|-)'),
+ ('addition_level_operator', r'(\+\+|\+|-)'),
('multiplication_level_operator', r'(\*|//|%)'),
('newline', r'\n'),
]
)
BUILTINS = {
+ 'concatenate': [],
'false': [],
'pow': ['math.h'],
'print': ['stdio.h'],
],
)
-INFIX_OPERATOR_TO_DECLARATION = {
+FUR_INFIX_OPERATOR_TO_C_FUNCTION = {
+ '++': 'concatenate',
+}
+
+FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR = {
'+': CInfixDeclaration(name='add', in_type='integer', out_type='integer', operator='+'),
'-': CInfixDeclaration(name='subtract', in_type='integer', out_type='integer', operator='-'),
'*': CInfixDeclaration(name='multiply', in_type='integer', out_type='integer', operator='*'),
}
def transform_comparison_level_expression(accumulators, expression):
- accumulators.operator_set.add(INFIX_OPERATOR_TO_DECLARATION[expression.operator])
+ accumulators.operator_set.add(FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR[expression.operator])
# Transform expressions like 1 < 2 < 3 into expressions like 1 < 2 && 2 < 3
if isinstance(expression.left, parsing.FurInfixExpression) and expression.left.order == 'comparison_level':
name='and',
left=left,
right=CFunctionCallForFurInfixOperator(
- name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
+ name=FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR[expression.operator].name,
left=middle,
right=right,
),
)
return CFunctionCallForFurInfixOperator(
- name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
+ name=FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR[expression.operator].name,
left=transform_expression(accumulators, expression.left),
right=transform_expression(accumulators, expression.right),
)
+def transform_infix_operator_without_c_equivalent(accumulators, expression):
+ return CFunctionCallForFurInfixOperator(
+ name='concatenate',
+ left=transform_expression(accumulators, expression.left),
+ right=transform_expression(accumulators, expression.right),
+ )
def transform_infix_expression(accumulators, expression):
+ if expression.operator in FUR_INFIX_OPERATOR_TO_C_FUNCTION:
+ return transform_infix_operator_without_c_equivalent(accumulators, expression)
+
if expression.order == 'comparison_level':
return transform_comparison_level_expression(accumulators, expression)
- accumulators.operator_set.add(INFIX_OPERATOR_TO_DECLARATION[expression.operator])
+ accumulators.operator_set.add(FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR[expression.operator])
return CFunctionCallForFurInfixOperator(
- name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
+ name=FUR_INFIX_OPERATOR_TO_C_INFIX_OPERATOR[expression.operator].name,
left=transform_expression(accumulators, expression.left),
right=transform_expression(accumulators, expression.right),
)