Use snapshots of the stack to restore stack to its previous state
[fur] / templates / program.c
index 6b49881..25f9acb 100644 (file)
@@ -163,6 +163,18 @@ void Stack_initialize(Stack* self)
   self->length = 0;
 }
 
+Stack* Stack_construct()
+{
+  Stack* result = malloc(sizeof(Stack));
+  Stack_initialize(result);
+  return result;
+}
+
+void Stack_destruct(Stack* self)
+{
+  free(self);
+}
+
 bool Stack_any(Stack* self)
 {
   return self->length > 0;
@@ -321,6 +333,22 @@ void Object_deinitialize(Object* self)
   }
 }
 
+typedef uint32_t StackSnapshot;
+
+StackSnapshot Stack_takeSnapshot(Stack* self)
+{
+  return (StackSnapshot) self->length;
+}
+
+void Stack_rewind(Stack* self, StackSnapshot snapshot)
+{
+  while(self->length > snapshot)
+  {
+    Object item = Stack_pop(self);
+    Object_deinitialize(&item);
+  }
+}
+
 void Environment_deinitialize(Environment* self)
 {
   EnvironmentNode* next;
@@ -522,6 +550,13 @@ Environment* EnvironmentPool_allocate(EnvironmentPool* self)
   return EnvironmentPool_allocate(previous->overflow);
 }
 
+Environment* Environment_construct(EnvironmentPool* environmentPool, Environment* parent)
+{
+  Environment* environment = EnvironmentPool_allocate(environmentPool);
+  Environment_initialize(environment, parent);
+  return environment;
+}
+
 Object integerLiteral(int32_t literal)
 {
   Object result;