Finish conditional compilation of operators
[fur] / templates / program.c
index 0c6f8f9..897ca5e 100644 (file)
@@ -14,6 +14,10 @@ union Instance;
 typedef union Instance Instance;
 struct Object;
 typedef struct Object Object;
+struct EnvironmentNode;
+typedef struct EnvironmentNode EnvironmentNode;
+struct Environment;
+typedef struct Environment Environment;
 
 const char* const STRING_LITERAL_LIST[] = {
 {% for string_literal in string_literal_list %}
@@ -30,6 +34,7 @@ const char* const SYMBOL_LIST[] = {
 enum Type
 {
   BOOLEAN,
+  CLOSURE,
   INTEGER,
   STRING
 };
@@ -37,6 +42,7 @@ enum Type
 union Instance
 {
   bool boolean;
+  Object (*closure)(Environment*, size_t, Object*);
   int32_t integer;
   const char* string;
 };
@@ -49,16 +55,14 @@ 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;
@@ -66,29 +70,36 @@ struct EnvironmentNode
   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)
   {
-    // No objects are allocated on the heap (yet!) so we don't need to free anything else
-    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);
   }
 }
 
@@ -113,6 +124,11 @@ Object Environment_get(Environment* self, const char* const symbol)
     }
   }
 
+  if(self->parent != NULL)
+  {
+    return Environment_get(self->parent, symbol);
+  }
+
   // TODO Handle symbol errors
   assert(false);
 }
@@ -134,7 +150,7 @@ Object stringLiteral(const char* literal)
 }
 
 // TODO Make this conditionally added
-Object builtin$negate(Object input)
+Object operator$negate(Object input)
 {
   assert(input.type == INTEGER);
 
@@ -144,135 +160,21 @@ Object builtin$negate(Object input)
   return result;
 }
 
-Object builtin$add(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$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)
+{% 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$integerDivide(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$modularDivide(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$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 };
+  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 %}
 
 {% if 'pow' in builtins %}
-Object builtin$pow(size_t argc, Object args[])
+Object builtin$pow$implementation(Environment* parent, size_t argc, Object* args)
 {
   assert(argc == 2);
 
@@ -287,10 +189,12 @@ Object builtin$pow(size_t argc, Object args[])
   result.instance.integer = pow(base.instance.integer, exponent.instance.integer);
   return result;
 }
+
+Object builtin$pow = { CLOSURE, (Instance)builtin$pow$implementation };
 {% endif %}
 
 {% if 'print' in builtins %}
-void builtin$print(size_t argc, Object args[])
+Object builtin$print$implementation(Environment* parent, size_t argc, Object* args)
 {
   for(size_t i = 0; i < argc; i++)
   {
@@ -314,15 +218,42 @@ void builtin$print(size_t argc, Object args[])
         assert(false);
     }
   }
+
+  // TODO Return something better
+  return FALSE;
 }
+
+Object builtin$print = { CLOSURE, (Instance)builtin$print$implementation };
 {% endif %}
 
+{% for function_definition in function_definition_list %}
+Object user${{function_definition.name}}$implementation(Environment* parent, size_t argc, Object* args)
+{
+  Environment* environment = Environment_construct(parent);
+
+  {% for statement in function_definition.statement_list[:-1] %}
+  {{ generate_statement(statement) }}
+  {% endfor %}
+
+  Object result = {{ generate_statement(function_definition.statement_list[-1]) }}
+  Environment_destruct(environment);
+  return result;
+}
+
+Object user${{function_definition.name}} = { CLOSURE, (Instance)user${{function_definition.name}}$implementation };
+{% endfor %}
+
 int main(int argc, char** argv)
 {
-  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);