#include <{{standard_library}}>
{% endfor %}
-struct String;
-typedef struct String String;
enum Type;
typedef enum Type Type;
union Instance;
typedef union Instance Instance;
struct Object;
typedef struct Object Object;
-struct Runtime;
-typedef struct Runtime Runtime;
+struct EnvironmentNode;
+typedef struct EnvironmentNode EnvironmentNode;
+struct Environment;
+typedef struct Environment Environment;
-struct String
-{
- size_t length;
- char* characters;
+const char* const STRING_LITERAL_LIST[] = {
+{% for string_literal in string_literal_list %}
+ "{{ string_literal }}",
+{% endfor %}
};
-const char * const SYMBOL_LIST[] = {
+const char* const SYMBOL_LIST[] = {
{% for symbol in symbol_list %}
"{{ symbol }}",
{% endfor %}
enum Type
{
BOOLEAN,
+ CLOSURE,
INTEGER,
STRING
};
union Instance
{
bool boolean;
+ Object (*closure)(Environment*, size_t, Object*);
int32_t integer;
- String* string;
+ const char* string;
};
struct Object
const Object TRUE = {
BOOLEAN,
- true
+ { true }
};
const Object FALSE = {
BOOLEAN,
- false
+ { false }
};
-struct EnvironmentNode;
-typedef struct EnvironmentNode EnvironmentNode;
struct EnvironmentNode
{
const char* key;
EnvironmentNode* next;
};
-struct Environment;
-typedef struct Environment Environment;
struct Environment
{
+ size_t referenceCount;
+ Environment* parent;
EnvironmentNode* root;
};
-Environment* Environment_construct()
+Environment* Environment_construct(Environment* parent)
{
- // TODO Handle malloc returning NULL
Environment* result = malloc(sizeof(Environment));
+ result->referenceCount = 1;
+ result->parent = parent;
result->root = NULL;
return result;
}
void Environment_destruct(Environment* self)
{
- EnvironmentNode* next;
- for(EnvironmentNode* node = self->root; node != NULL; node = next)
+ self->referenceCount--;
+
+ if(self->referenceCount == 0)
{
- // We don't need to destruct the permanent strings, because those will be destructed at the end when the Runtime is destructed
- // The above comment represents all heap-allocated objects currently, so we don't need to destruct Objects (yet)
- next = node->next;
- free(node);
+ EnvironmentNode* next;
+ for(EnvironmentNode* node = self->root; node != NULL; node = next)
+ {
+ // No objects are allocated on the heap (yet!) so we don't need to free anything else
+ next = node->next;
+ free(node);
+ }
+ free(self);
}
}
{
for(EnvironmentNode* node = self->root; node != NULL; node = node->next)
{
- // We can compare pointers because pointers are unique in the SYMBOLS_LIST
+ // We can compare pointers because pointers are unique in the SYMBOL_LIST
if(node->key == symbol)
{
return node->value;
}
}
- // TODO Handle symbol errors
- assert(false);
-}
-
-
-struct Runtime
-{
- size_t permanentStringsLength;
- size_t permanentStringsAllocated;
- String** permanentStrings;
-};
-
-Runtime* Runtime_construct()
-{
- Runtime* result = malloc(sizeof(Runtime));
- result->permanentStringsLength = 0;
- result->permanentStringsAllocated = 0;
- result->permanentStrings = NULL;
- return result;
-}
-
-void Runtime_destruct(Runtime* self)
-{
- for(size_t i = 0; i < self->permanentStringsLength; i++)
- {
- free(self->permanentStrings[i]);
- }
-
- free(self->permanentStrings);
- free(self);
-}
-
-void Runtime_addPermanentString(Runtime* self, String* string)
-{
- // TODO Make this function thread-safe
- if(self->permanentStringsLength == self->permanentStringsAllocated)
+ if(self->parent != NULL)
{
- if(self->permanentStringsAllocated == 0)
- {
- self->permanentStringsAllocated = 8;
- }
- else
- {
- self->permanentStringsAllocated = self->permanentStringsAllocated * 2;
- }
-
- self->permanentStrings = realloc(
- self->permanentStrings,
- sizeof(String*) * self->permanentStringsAllocated
- );
-
- // TODO Handle realloc returning NULL
+ return Environment_get(self->parent, symbol);
}
- self->permanentStrings[self->permanentStringsLength] = string;
- self->permanentStringsLength++;
+ // TODO Handle symbol errors
+ assert(false);
}
Object integerLiteral(int32_t literal)
return result;
}
-Object stringLiteral(Runtime* runtime, const char* literal)
+Object stringLiteral(const char* literal)
{
- String* resultString = malloc(sizeof(String));
- resultString->length = strlen(literal);
- resultString->characters = malloc(resultString->length);
- memcpy(resultString->characters, literal, resultString->length);
- Runtime_addPermanentString(runtime, resultString);
-
Object result;
result.type = STRING;
- result.instance.string = resultString;
+ result.instance.string = literal;
return result;
}
// TODO Make this conditionally added
-Object builtin$negate(Object input)
+Object operator$negate(Object input)
{
assert(input.type == INTEGER);
return result;
}
-Object builtin$add(Object left, Object right)
+{% for id in infix_declarations %}
+Object operator${{ id.name }}(Object left, Object right)
{
- assert(left.type == INTEGER);
- assert(right.type == INTEGER);
+ assert(left.type == {{ id.in_type.upper() }});
+ assert(right.type == {{ id.in_type.upper() }});
Object result;
- result.type = INTEGER;
- result.instance.integer = left.instance.integer + right.instance.integer;
- return result;
-}
-
-Object builtin$subtract(Object left, Object right)
-{
- assert(left.type == INTEGER);
- assert(right.type == INTEGER);
-
- Object result;
- result.type = INTEGER;
- result.instance.integer = left.instance.integer - right.instance.integer;
- return result;
-}
-
-Object builtin$multiply(Object left, Object right)
-{
- assert(left.type == INTEGER);
- assert(right.type == INTEGER);
-
- Object result;
- result.type = INTEGER;
- result.instance.integer = left.instance.integer * right.instance.integer;
+ result.type = {{ id.out_type.upper() }};
+ result.instance.{{ id.out_type.lower() }} = left.instance.{{ id.in_type.lower() }} {{ id.operator }} right.instance.{{ id.in_type.lower() }};
return result;
}
+{% endfor %}
-Object builtin$integerDivide(Object left, Object right)
+{% if 'pow' in builtins %}
+Object builtin$pow$implementation(Environment* parent, size_t argc, Object* args)
{
- assert(left.type == INTEGER);
- assert(right.type == INTEGER);
+ assert(argc == 2);
- Object result;
- result.type = INTEGER;
- result.instance.integer = left.instance.integer / right.instance.integer;
- return result;
-}
+ Object base = args[0];
+ Object exponent = args[1];
-Object builtin$modularDivide(Object left, Object right)
-{
- assert(left.type == INTEGER);
- assert(right.type == INTEGER);
+ assert(base.type == INTEGER);
+ assert(exponent.type == INTEGER);
Object result;
result.type = INTEGER;
- result.instance.integer = left.instance.integer % right.instance.integer;
+ result.instance.integer = pow(base.instance.integer, exponent.instance.integer);
return result;
}
-Object builtin$equals(Object left, Object right)
-{
- assert(left.type == INTEGER);
- assert(right.type == INTEGER);
-
- Object result = { BOOLEAN, left.instance.integer == right.instance.integer };
- return result;
-}
+Object builtin$pow = { CLOSURE, (Instance)builtin$pow$implementation };
+{% endif %}
-Object builtin$notEquals(Object left, Object right)
+{% if 'print' in builtins %}
+Object builtin$print$implementation(Environment* parent, size_t argc, Object* args)
{
- assert(left.type == INTEGER);
- assert(right.type == INTEGER);
-
- Object result = { BOOLEAN, left.instance.integer != right.instance.integer };
- return result;
-}
+ for(size_t i = 0; i < argc; i++)
+ {
+ Object output = args[i];
+ switch(output.type)
+ {
+ case BOOLEAN:
+ fputs(output.instance.boolean ? "true" : "false", stdout);
+ break;
-Object builtin$greaterThan(Object left, Object right)
-{
- assert(left.type == INTEGER);
- assert(right.type == INTEGER);
+ case INTEGER:
+ printf("%" PRId32, output.instance.integer);
+ break;
- Object result = { BOOLEAN, left.instance.integer > right.instance.integer };
- return result;
-}
+ case STRING:
+ // Using fwrite instead of printf to handle size_t length
+ printf("%s", output.instance.string);
+ break;
-Object builtin$lessThan(Object left, Object right)
-{
- assert(left.type == INTEGER);
- assert(right.type == INTEGER);
+ default:
+ assert(false);
+ }
+ }
- Object result = { BOOLEAN, left.instance.integer < right.instance.integer };
- return result;
+ // TODO Return something better
+ return FALSE;
}
-Object builtin$greaterThanOrEqual(Object left, Object right)
-{
- assert(left.type == INTEGER);
- assert(right.type == INTEGER);
-
- Object result = { BOOLEAN, left.instance.integer >= right.instance.integer };
- return result;
-}
+Object builtin$print = { CLOSURE, (Instance)builtin$print$implementation };
+{% endif %}
-Object builtin$lessThanOrEqual(Object left, Object right)
+{% for function_definition in function_definition_list %}
+Object user${{function_definition.name}}$implementation(Environment* parent, size_t argc, Object* args)
{
- assert(left.type == INTEGER);
- assert(right.type == INTEGER);
+ Environment* environment = Environment_construct(parent);
- Object result = { BOOLEAN, left.instance.integer <= right.instance.integer };
- return result;
-}
-
-{% if 'pow' in builtins %}
-Object builtin$pow(Object base, Object exponent)
-{
- assert(base.type == INTEGER);
- assert(exponent.type == INTEGER);
+ {% for statement in function_definition.statement_list[:-1] %}
+ {{ generate_statement(statement) }}
+ {% endfor %}
- Object result;
- result.type = INTEGER;
- result.instance.integer = pow(base.instance.integer, exponent.instance.integer);
+ Object result = {{ generate_statement(function_definition.statement_list[-1]) }}
+ Environment_destruct(environment);
return result;
}
-{% endif %}
-
-{% if 'print' in builtins %}
-void builtin$print(Object output)
-{
- switch(output.type)
- {
- case BOOLEAN:
- fputs(output.instance.boolean ? "true" : "false", stdout);
- break;
-
- case INTEGER:
- printf("%" PRId32, output.instance.integer);
- break;
- case STRING:
- // Using fwrite instead of printf to handle size_t length
- fwrite(output.instance.string->characters, 1, output.instance.string->length, stdout);
- break;
-
- default:
- assert(false);
- }
-}
-{% endif %}
+Object user${{function_definition.name}} = { CLOSURE, (Instance)user${{function_definition.name}}$implementation };
+{% endfor %}
int main(int argc, char** argv)
{
- Runtime* runtime = Runtime_construct();
- Environment* environment = Environment_construct();
+ Environment* environment = Environment_construct(NULL);
+
+ // TODO Use the symbol from SYMBOL_LIST
+ {% for builtin in builtins %}
+ Environment_set(environment, "{{ builtin }}", builtin${{ builtin }});
+ {% endfor %}
{% for statement in statements %}
- {{ statement }}
+ {{ generate_statement(statement) }}
{% endfor %}
Environment_destruct(environment);
- Runtime_destruct(runtime);
return 0;
}