struct Runtime;
typedef struct Runtime Runtime;
-struct String
-{
- size_t length;
- char* characters;
-};
-
const char * const SYMBOL_LIST[] = {
{% for symbol in symbol_list %}
"{{ symbol }}",
enum Type
{
+ BOOLEAN,
INTEGER,
STRING
};
union Instance
{
+ bool boolean;
int32_t integer;
- String* string;
+ char* string;
};
struct Object
Instance instance;
};
+const Object TRUE = {
+ BOOLEAN,
+ true
+};
+
+const Object FALSE = {
+ BOOLEAN,
+ false
+};
+
struct EnvironmentNode;
typedef struct EnvironmentNode EnvironmentNode;
struct EnvironmentNode
{
size_t permanentStringsLength;
size_t permanentStringsAllocated;
- String** permanentStrings;
+ char** permanentStrings;
};
Runtime* Runtime_construct()
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)
+void Runtime_addPermanentString(Runtime* self, char* const string)
{
// TODO Make this function thread-safe
if(self->permanentStringsLength == self->permanentStringsAllocated)
self->permanentStrings = realloc(
self->permanentStrings,
- sizeof(String*) * self->permanentStringsAllocated
+ sizeof(char* const) * self->permanentStringsAllocated
);
// TODO Handle realloc returning NULL
return result;
}
-Object stringLiteral(Runtime* runtime, const char* literal)
+Object stringLiteral(Runtime* runtime, 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);
+ Runtime_addPermanentString(runtime, literal);
Object result;
result.type = STRING;
- result.instance.string = resultString;
+ result.instance.string = literal;
return result;
}
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$notEquals(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$greaterThan(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$lessThan(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$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$lessThanOrEqual(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$and(Object left, Object right)
+{
+ assert(left.type == BOOLEAN);
+ assert(right.type == BOOLEAN);
+
+ Object result = { BOOLEAN, left.instance.boolean && right.instance.boolean };
+ return result;
+}
+
+Object builtin$or(Object left, Object right)
+{
+ assert(left.type == BOOLEAN);
+ assert(right.type == BOOLEAN);
+
+ Object result = { BOOLEAN, left.instance.boolean || right.instance.boolean };
+ return result;
+}
+
{% if 'pow' in builtins %}
Object builtin$pow(Object base, Object exponent)
{
{
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);
+ printf("%s", output.instance.string);
break;
default: