Add support for ternary comparison operators
[fur] / templates / program.c
index 0f79b14..58ddd36 100644 (file)
@@ -33,12 +33,14 @@ const char * const SYMBOL_LIST[] = {
 
 enum Type
 {
+  BOOLEAN,
   INTEGER,
   STRING
 };
 
 union Instance
 {
+  bool boolean;
   int32_t integer;
   String* string;
 };
@@ -49,6 +51,16 @@ struct Object
   Instance instance;
 };
 
+const Object TRUE = {
+  BOOLEAN,
+  true
+};
+
+const Object FALSE = {
+  BOOLEAN,
+  false
+};
+
 struct EnvironmentNode;
 typedef struct EnvironmentNode EnvironmentNode;
 struct EnvironmentNode
@@ -252,6 +264,69 @@ 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;
+}
+
 {% if 'pow' in builtins %}
 Object builtin$pow(Object base, Object exponent)
 {
@@ -270,6 +345,10 @@ 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;