Implement negatives, use typedef
[fur] / templates / program2.c
index 8abe2a2..8602054 100644 (file)
@@ -17,6 +17,7 @@ enum Builtin;
 typedef enum Builtin Builtin;
 enum Builtin {
   NIL,
+  POW,
   PRINT
 };
 
@@ -71,13 +72,38 @@ union Argument {
   int32_t integer;
 };
 
-void call(struct Thread* thread, const union Argument argument) {
+void call(struct Thread* thread, Argument argument) {
   assert(!Stack_isEmpty(&(thread->stack)));
   Object f = Stack_pop(&(thread->stack));
+  size_t argumentCount = argument.label;
 
   switch(f.type) {
     case BUILTIN:
       switch(f.value.builtin) {
+        case POW:
+          {
+            assert(argumentCount == 2);
+            assert(!Stack_isEmpty(&(thread->stack)));
+            Object exponent = Stack_pop(&(thread->stack));
+            assert(exponent.type == INTEGER);
+            assert(exponent.value.integer >= 0);
+
+            assert(!Stack_isEmpty(&(thread->stack)));
+            Object base = Stack_pop(&(thread->stack));
+            assert(base.type == INTEGER);
+
+            Object result;
+            result.type = INTEGER;
+            result.value.integer = 1;
+
+            while(exponent.value.integer > 0) {
+              result.value.integer *= base.value.integer;
+              exponent.value.integer--;
+            }
+
+            Stack_push(&(thread->stack), result);
+          }
+          break;
         case PRINT:
           {
             // TODO Handle multiple arguments
@@ -116,13 +142,13 @@ void call(struct Thread* thread, const union Argument argument) {
 {% endwith %}
 
 
-void drop(struct Thread* thread, const union Argument argument) {
+void drop(struct Thread* thread, Argument argument) {
   assert(!Stack_isEmpty(&(thread->stack)));
   Object result = Stack_pop(&(thread->stack));
   Object_deinitialize(&result);
 }
 
-void end(struct Thread* thread, const union Argument argument) {
+void end(struct Thread* thread, Argument argument) {
 }
 
 {% with name='idiv', operation='/' %}
@@ -137,7 +163,17 @@ void end(struct Thread* thread, const union Argument argument) {
   {% include "arithmetic_instruction.c" %}
 {% endwith %}
 
-void pop(struct Thread* thread, const union Argument argument) {
+void neg(struct Thread* thread, Argument argument) {
+  assert(!Stack_isEmpty(&(thread->stack)));
+  Object result = Stack_pop(&(thread->stack));
+  assert(result.type == INTEGER);
+
+  result.value.integer = -(result.value.integer);
+
+  Stack_push(&(thread->stack), result);
+}
+
+void pop(struct Thread* thread, Argument argument) {
   char* argumentString = argument.string;
 
   assert(!Stack_isEmpty(&(thread->stack)));
@@ -145,12 +181,15 @@ void pop(struct Thread* thread, const union Argument argument) {
 
   if(strcmp(argumentString, "print") == 0) {
     assert(false);
+  } else if(strcmp(argumentString, "pow") == 0) {
+    assert(false);
   }
 
+
   Environment_set(thread->environment, argumentString, result);
 }
 
-void push(struct Thread* thread, const union Argument argument) {
+void push(struct Thread* thread, Argument argument) {
   char* argumentString = argument.string;
 
   if(strcmp(argumentString, "print") == 0) {
@@ -158,6 +197,11 @@ void push(struct Thread* thread, const union Argument argument) {
     result.type = BUILTIN;
     result.value.builtin = PRINT;
     Stack_push(&(thread->stack), result);
+  } else if(strcmp(argumentString, "pow") == 0) {
+    Object result;
+    result.type = BUILTIN;
+    result.value.builtin = POW;
+    Stack_push(&(thread->stack), result);
   } else {
     Environment_get_Result result = Environment_get(thread->environment, argumentString);
     if(!result.found) {
@@ -168,7 +212,7 @@ void push(struct Thread* thread, const union Argument argument) {
   }
 }
 
-void push_integer(struct Thread* thread, const union Argument argument) {
+void push_integer(struct Thread* thread, Argument argument) {
   Object result;
   result.type = INTEGER;
   result.value.integer = argument.integer;
@@ -176,7 +220,7 @@ void push_integer(struct Thread* thread, const union Argument argument) {
   Stack_push(&(thread->stack), result);
 }
 
-void push_string(struct Thread* thread, const union Argument argument) {
+void push_string(struct Thread* thread, Argument argument) {
   Object result;
   result.type = STRING;
   result.value.string = argument.string;
@@ -191,7 +235,7 @@ void push_string(struct Thread* thread, const union Argument argument) {
 struct Instruction;
 typedef const struct Instruction Instruction;
 struct Instruction {
-  void (*instruction)(struct Thread*,const union Argument);
+  void (*instruction)(struct Thread*,Argument);
   Argument argument;
 };