4 {% for standard_library in standard_libraries %}
5 #include <{{standard_library}}>
9 typedef struct String String;
11 typedef struct Runtime Runtime;
21 size_t permanentStringsLength;
22 size_t permanentStringsAllocated;
23 String** permanentStrings;
26 Runtime* Runtime_construct()
28 Runtime* result = malloc(sizeof(Runtime));
29 result->permanentStringsLength = 0;
30 result->permanentStringsAllocated = 0;
31 result->permanentStrings = NULL;
35 void Runtime_destruct(Runtime* self)
37 free(self->permanentStrings);
41 void Runtime_addPermanentString(Runtime* self, String* string)
43 // TODO Make this function thread-safe
44 if(self->permanentStringsLength == self->permanentStringsAllocated)
46 if(self->permanentStringsAllocated == 0)
48 self->permanentStringsAllocated = 8;
52 self->permanentStringsAllocated = self->permanentStringsAllocated * 2;
55 self->permanentStrings = realloc(
56 self->permanentStrings,
57 sizeof(String*) * self->permanentStringsAllocated
60 // TODO Handle realloc returning NULL
63 self->permanentStrings[self->permanentStringsLength] = string;
64 self->permanentStringsLength++;
67 String* stringLiteral(Runtime* runtime, const char* literal)
69 String* result = malloc(sizeof(String));
70 result->length = strlen(literal);
71 result->characters = malloc(result->length);
72 memcpy(result->characters, literal, result->length);
73 Runtime_addPermanentString(runtime, result);
77 {% if 'print' in builtins %}
78 void builtin$print(String* output)
80 // Using fwrite instead of printf to handle size_t length
81 fwrite(output->characters, 1, output->length, stdout);
85 int main(int argc, char** argv)
87 Runtime* runtime = Runtime_construct();
89 {% for statement in statements %}
93 Runtime_destruct(runtime);