Removed not-yet-relevant string type
[fur] / templates / program.c
index c20fd45..3b65e02 100644 (file)
@@ -19,31 +19,24 @@ typedef struct Object Object;
 struct Runtime;
 typedef struct Runtime Runtime;
 
-struct String
-{
-  size_t length;
-  char* characters;
-};
-
-#define MAX_SYMBOL_LENGTH {{ MAX_SYMBOL_LENGTH }}
-struct Symbol;
-typedef struct Symbol Symbol;
-struct Symbol
-{
-  size_t length;
-  char name[MAX_SYMBOL_LENGTH];
+const char * const SYMBOL_LIST[] = {
+{% for symbol in symbol_list %}
+  "{{ symbol }}",
+{% endfor %}
 };
 
 enum Type
 {
+  BOOLEAN,
   INTEGER,
   STRING
 };
 
 union Instance
 {
+  bool boolean;
   int32_t integer;
-  String* string;
+  char* string;
 };
 
 struct Object
@@ -52,11 +45,21 @@ struct Object
   Instance instance;
 };
 
+const Object TRUE = {
+  BOOLEAN,
+  true
+};
+
+const Object FALSE = {
+  BOOLEAN,
+  false
+};
+
 struct EnvironmentNode;
 typedef struct EnvironmentNode EnvironmentNode;
 struct EnvironmentNode
 {
-  Symbol* key;
+  const char* key;
   Object value;
   EnvironmentNode* next;
 };
@@ -81,16 +84,15 @@ void Environment_destruct(Environment* self)
   EnvironmentNode* next;
   for(EnvironmentNode* node = self->root; node != NULL; node = next)
   {
-    // We don't need to destruct the keys, because those will be destructed at the end when the Runtime is destructed
     // We don't need to destruct the permanent strings, because those will be destructed at the end when the Runtime is destructed
-    // The above two comments represent all heap-allocated objects currently, so we don't need to destruct Objects (yet)
+    // The above comment represents all heap-allocated objects currently, so we don't need to destruct Objects (yet)
     next = node->next;
     free(node);
   }
 }
 
 // This need not be thread safe because environments exist on one thread only
-void Environment_set(Environment* self, Symbol* key, Object value)
+void Environment_set(Environment* self, const char* const key, Object value)
 {
   EnvironmentNode* node = malloc(sizeof(EnvironmentNode));
   node->key = key;
@@ -99,11 +101,11 @@ void Environment_set(Environment* self, Symbol* key, Object value)
   self->root = node;
 }
 
-Object Environment_get(Environment* self, Symbol* symbol)
+Object Environment_get(Environment* self, const char* const symbol)
 {
   for(EnvironmentNode* node = self->root; node != NULL; node = node->next)
   {
-    // We can compare pointers because pointers are unique within Runtime->symbols
+    // We can compare pointers because pointers are unique in the SYMBOLS_LIST
     if(node->key == symbol)
     {
       return node->value;
@@ -115,15 +117,11 @@ Object Environment_get(Environment* self, Symbol* symbol)
 }
 
 
-// TODO Allocate all symbols and strings as static constants so we can remove the level of indirection
 struct Runtime
 {
   size_t permanentStringsLength;
   size_t permanentStringsAllocated;
-  String** permanentStrings;
-  size_t symbolsLength;
-  size_t symbolsAllocated;
-  Symbol** symbols;
+  char** permanentStrings;
 };
 
 Runtime* Runtime_construct()
@@ -132,30 +130,16 @@ Runtime* Runtime_construct()
   result->permanentStringsLength = 0;
   result->permanentStringsAllocated = 0;
   result->permanentStrings = NULL;
-  result->symbolsLength = 0;
-  result->symbolsAllocated =0;
-  result->symbols = NULL;
   return result;
 }
 
 void Runtime_destruct(Runtime* self)
 {
-  for(size_t i = 0; i < self->permanentStringsLength; i++)
-  {
-    free(self->permanentStrings[i]);
-  }
-
-  for(size_t i = 0; i < self->symbolsLength; i++)
-  {
-    free(self->symbols[i]);
-  }
-
   free(self->permanentStrings);
-  free(self->symbols);
   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)
@@ -171,7 +155,7 @@ void Runtime_addPermanentString(Runtime* self, String* string)
 
     self->permanentStrings = realloc(
       self->permanentStrings,
-      sizeof(String*) * self->permanentStringsAllocated
+      sizeof(char* const) * self->permanentStringsAllocated
     );
 
     // TODO Handle realloc returning NULL
@@ -181,49 +165,6 @@ void Runtime_addPermanentString(Runtime* self, String* string)
   self->permanentStringsLength++;
 }
 
-// TODO Optimize this by sorting the symbols
-// TODO Make this function thread safe
-Symbol* Runtime_symbol(Runtime* self, const char* name)
-{
-  assert(strlen(name) <= MAX_SYMBOL_LENGTH);
-
-  for(size_t i = 0; i < self->symbolsLength; i++)
-  {
-    if(strcmp(self->symbols[i]->name, name) == 0)
-    {
-      return self->symbols[i];
-    }
-  }
-
-  if(self->symbolsLength == self->symbolsAllocated)
-  {
-    if(self->symbolsAllocated == 0)
-    {
-      self->symbolsAllocated = 8;
-    }
-    else
-    {
-      self->symbolsAllocated = self->symbolsAllocated * 2;
-    }
-
-    self->symbols = realloc(
-      self->symbols,
-      sizeof(Symbol*) * self->symbolsAllocated
-    );
-
-    // TODO Handle realloc returning NULL
-  }
-
-  Symbol* result = malloc(sizeof(Symbol));
-  result->length = strlen(name);
-  strcpy(result->name, name);
-
-  self->symbols[self->symbolsLength] = result;
-  self->symbolsLength++;
-
-  return result;
-}
-
 Object integerLiteral(int32_t literal)
 {
   Object result;
@@ -232,17 +173,13 @@ Object integerLiteral(int32_t literal)
   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;
 }
 
@@ -312,6 +249,78 @@ Object builtin$modularDivide(Object left, Object right)
   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)
 {
@@ -330,13 +339,17 @@ 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);
+      printf("%s", output.instance.string);
       break;
 
     default: