BOOLEAN,
CLOSURE,
INTEGER,
- STRING
+ LIST,
+ STRING,
+ VOID
};
struct Closure;
Object (*call)(EnvironmentPool*, Environment*, size_t, Object*);
};
+struct List;
+typedef struct List List;
+struct List
+{
+ size_t allocated;
+ size_t length;
+ Object* items;
+};
+
union Instance
{
bool boolean;
Closure closure;
int32_t integer;
+ List list;
const char* string;
};
Instance instance;
};
-const Object TRUE = {
- BOOLEAN,
- { true }
-};
+const Object builtin$true = { BOOLEAN, (Instance)(bool){ true } };
+const Object builtin$false = { BOOLEAN, (Instance)(bool){ false } };
+const Object builtin$nil = { VOID, { 0 } };
-const Object FALSE = {
- BOOLEAN,
- { false }
-};
+Object List_construct(size_t allocate)
+{
+ Object* items = malloc(sizeof(Object) * allocate);
+ Object result = { LIST, (Instance)(List){ allocate, 0, items } };
+ return result;
+}
+
+void List_append(Object* list, Object item)
+{
+ assert(list->type == LIST);
+
+ if(list->instance.list.allocated == list->instance.list.length)
+ {
+ list->instance.list.allocated *= 2;
+ list->instance.list.items = realloc(
+ list->instance.list.items,
+ sizeof(Object) * list->instance.list.allocated
+ );
+ }
+
+ list->instance.list.items[list->instance.list.length] = item;
+ list->instance.list.length++;
+}
+
+Object List_get(Object* list, Object index)
+{
+ assert(list->type == LIST);
+ assert(index.type == INTEGER);
+
+ return list->instance.list.items[index.instance.integer];
+}
struct EnvironmentNode
{
for(EnvironmentNode* node = self->root; node != NULL; node = next)
{
next = node->next;
+
+ switch(node->value.type)
+ {
+ case BOOLEAN:
+ case CLOSURE:
+ case INTEGER:
+ case STRING:
+ case VOID:
+ break;
+
+ case LIST:
+ free(node->value.instance.list.items);
+ break;
+
+ default:
+ assert(false);
+ }
+
free(node);
}
}
case BOOLEAN:
case INTEGER:
case STRING:
+ case VOID:
break;
case CLOSURE:
printf("%s", output.instance.string);
break;
+ case VOID:
+ printf("nil");
+ break;
+
default:
assert(false);
}
}
// TODO Return something better
- return FALSE;
+ return builtin$false;
}
Object builtin$print = { CLOSURE, (Instance)(Closure){ NULL, builtin$print$implementation } };
{% endif %}
-
{% for function_definition in function_definition_list %}
-Object user${{function_definition.name}}$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Object* args)
-{
- assert(argc == {{ function_definition.argument_name_list|length }});
-
- Environment* environment = EnvironmentPool_allocate(environmentPool);
- Environment_initialize(environment, parent);
-
- {% for argument_name in function_definition.argument_name_list %}
- Environment_set(environment, "{{ argument_name }}", args[{{ loop.index0 }}]);
- {% endfor %}
-
- {% for statement in function_definition.statement_list[:-1] %}
- {{ generate_statement(statement) }}
- {% endfor %}
-
- Object result = {{ generate_statement(function_definition.statement_list[-1]) }}
-
- Environment_setLive(environment, false);
- return result;
-}
-
+{{ function_definition }}
{% endfor %}
+
int main(int argc, char** argv)
{
EnvironmentPool* environmentPool = EnvironmentPool_construct();
{% endfor %}
{% for statement in statements %}
- {{ generate_statement(statement) }}
+ {{ statement }}
{% endfor %}
Environment_setLive(environment, false);