Keeping the type of Boolean/Element struct, as well as keeping backward edges for...
authorHamed <hamed.gorjiara@gmail.com>
Wed, 21 Jun 2017 21:12:04 +0000 (14:12 -0700)
committerHamed <hamed.gorjiara@gmail.com>
Wed, 21 Jun 2017 21:12:04 +0000 (14:12 -0700)
src/AST/boolean.c
src/AST/boolean.h
src/AST/element.c
src/AST/element.h
src/AST/ops.h
src/AST/predicate.c
src/AST/predicate.h
src/AST/structtype.h [new file with mode: 0644]
src/classlist.h
src/csolver.c

index 831a20042cb0f507a48e28e81dc285449fa08455..d0cfdddbc131a5be29be790eeed249ca13e6be53 100644 (file)
@@ -5,6 +5,8 @@
 Boolean* allocBoolean(VarType t) {
        BooleanVar* tmp=(BooleanVar *) ourmalloc(sizeof (BooleanVar));
        GETBOOLEANTYPE(tmp)=BOOLEANVAR;
+       GETSTRUCTTYPE(tmp) = _BOOLEAN;
+       GETPARENTSVECTOR(tmp) = allocDefVectorVoid();
        tmp->vtype=t;
        tmp->var=NULL;
        return & tmp->base;
@@ -13,6 +15,8 @@ Boolean* allocBoolean(VarType t) {
 Boolean* allocBooleanOrder(Order* order, uint64_t first, uint64_t second) {
        BooleanOrder* tmp=(BooleanOrder *) ourmalloc(sizeof (BooleanOrder));
        GETBOOLEANTYPE(tmp)=ORDERCONST;
+       GETSTRUCTTYPE(tmp) = _BOOLEAN;
+       GETPARENTSVECTOR(tmp) = allocDefVectorVoid();
        tmp->order=order;
        tmp->first=first;
        tmp->second=second;
@@ -20,32 +24,49 @@ Boolean* allocBooleanOrder(Order* order, uint64_t first, uint64_t second) {
 }
 
 Boolean * allocBooleanPredicate(Predicate * predicate, Element ** inputs, uint numInputs){
-    BooleanPredicate* bp = (BooleanPredicate*) ourmalloc(sizeof(BooleanPredicate));
-    GETBOOLEANTYPE(bp)= PREDICATEOP;
-    bp->predicate=predicate;
-    bp->inputs= allocVectorArrayElement (numInputs,inputs);
-    return & bp->base;
+       BooleanPredicate* tmp = (BooleanPredicate*) ourmalloc(sizeof(BooleanPredicate));
+       GETBOOLEANTYPE(tmp)= PREDICATEOP;
+       GETSTRUCTTYPE(tmp) = _BOOLEAN;
+       GETPARENTSVECTOR(tmp) = allocDefVectorVoid();
+       tmp->predicate=predicate;
+       tmp->inputs= allocVectorArrayElement (numInputs,inputs);
+       return & tmp->base;
 }
 
 Boolean * allocBooleanLogic(LogicOp op, Boolean * left, Boolean* right){
-    BooleanLogic* bl = (BooleanLogic*) ourmalloc(sizeof(BooleanLogic));
-    GETBOOLEANTYPE(bl) = LOGICOP;
-    bl->op=op;
-    bl->left=left;
-    bl->right=right;
-    return &bl->base;
+       BooleanLogic* tmp = (BooleanLogic*) ourmalloc(sizeof(BooleanLogic));
+       GETBOOLEANTYPE(tmp) = LOGICOP;
+       GETSTRUCTTYPE(tmp) = _BOOLEAN;
+       GETPARENTSVECTOR(tmp) = allocDefVectorVoid();
+       tmp->op=op;
+       tmp->left=left;
+       tmp->right=right;
+       return &tmp->base;
 }
 Boolean * allocBooleanLogicArray(CSolver *solver, LogicOp op, Boolean ** array, uint asize){
-    ASSERT(asize>=2);
-    Boolean* boolean = allocBooleanLogic(op,array[0], array[1]);
-    pushVectorBoolean(solver->allBooleans,boolean);
-    for(uint i=2; i<asize; i++){
-       boolean=allocBooleanLogic(op,boolean, array[i]);
+       ASSERT(asize>=2);
+       Boolean* boolean = allocBooleanLogic(op,array[0], array[1]);
+       ADDNEWPARENT(array[0], boolean);
+       ADDNEWPARENT(array[1], boolean);
        pushVectorBoolean(solver->allBooleans,boolean);
-    }
-    return boolean;
+       for(uint i=2; i<asize; i++){
+               Boolean* oldBoolean = boolean;
+               boolean=allocBooleanLogic(op,oldBoolean, array[i]);
+               ADDNEWPARENT(oldBoolean, boolean);
+               ADDNEWPARENT(array[i], boolean);
+               pushVectorBoolean(solver->allBooleans,boolean);
+       }
+       return boolean;
 }
 
 void deleteBoolean(Boolean * This) {
+       switch(GETBOOLEANTYPE(This)){
+               case PREDICATEOP:
+                       deleteVectorArrayElement( ((BooleanPredicate*)This)->inputs );
+                       break;
+               default:
+                       break;
+       }
+       DELETEPARENTSVECTOR(This);
        ourfree(This);
 }
index 15ebd52b9a3532ff0a13c5e6ca1465f8fdc87dcb..cd44c3ca651604a7b6c2345b75baadda4b425ae7 100644 (file)
@@ -4,7 +4,7 @@
 #include "mymemory.h"
 #include "ops.h"
 #include "structs.h"
-
+#include "structtype.h"
 /**
     This is a little sketchy, but apparently legit.
     https://www.python.org/dev/peps/pep-3123/ */
@@ -12,6 +12,7 @@
 #define GETBOOLEANTYPE(o) (((Boolean *)(o))->btype)
 
 struct Boolean {
+       Struct stype;
        BooleanType btype;
 };
 
index 1fb49aacc63f0f8087b5b1ad5d8284ff4cea4e45..b4b51471b480f1d4767df2b13b29bc18097b5772 100644 (file)
@@ -4,20 +4,25 @@
 Element *allocElementSet(Set * s) {
        ElementSet * tmp=(ElementSet *)ourmalloc(sizeof(ElementSet));
        GETELEMENTTYPE(tmp)= ELEMSET;
+       GETPARENTSVECTOR(tmp) = allocDefVectorVoid();
+       GETSTRUCTTYPE(tmp) = _ELEMENT;
        tmp->set=s;
        tmp->encoding=NULL;
        return &tmp->base;
 }
 
 Element* allocElementFunction(Function * function, Element ** array, uint numArrays, Boolean * overflowstatus){
-    ElementFunction* ef = (ElementFunction*) ourmalloc(sizeof(ElementFunction));
-    GETELEMENTTYPE(ef)= ELEMFUNCRETURN;
-    ef->function=function;
-    ef->overflowstatus = overflowstatus;
-    ef->Elements = allocVectorArrayElement(numArrays, array);
-    return &ef->base;
+       ElementFunction* tmp = (ElementFunction*) ourmalloc(sizeof(ElementFunction));
+       GETELEMENTTYPE(tmp)= ELEMFUNCRETURN;
+       GETPARENTSVECTOR(tmp) = allocDefVectorVoid();
+       GETSTRUCTTYPE(tmp) = _ELEMENT;
+       tmp->function=function;
+       tmp->overflowstatus = overflowstatus;
+       tmp->Elements = allocVectorArrayElement(numArrays, array);
+       return &tmp->base;
 }
 
 void deleteElement(Element *This) {
+       DELETEPARENTSVECTOR(This);
        ourfree(This);
 }
index da9b994e7df39219659b087c2631d9d7ff39c99b..d1dabe0b08c09d3f8b8f6c7757a16cff6eafb7ee 100644 (file)
@@ -4,11 +4,12 @@
 #include "mymemory.h"
 #include "ops.h"
 #include "structs.h"
+#include "structtype.h"
 
 #define GETELEMENTTYPE(o) (((Element*)o)->type)
 
-//FIXME:TALK ABOUT ELEMENT
 struct Element {
+       Struct stype;
        ElementType type;
 };
 
index 123617b4e80ccd29f5428e96e73d3ad8bae42c78..e5ccdeb6aead885246081c05eb08cbd291a21099 100644 (file)
@@ -37,4 +37,8 @@ typedef enum PredicateType PredicateType;
 
 enum ElementType {ELEMSET, ELEMFUNCRETURN};
 typedef enum ElementType ElementType;
+
+enum StructType {_BOOLEAN, _ELEMENT};
+typedef enum StructType StructType;
+
 #endif
index 17376812a7d8d84979878b2631546633614147bf..904fe224740e511d1fbe84f2275c484482e124fb 100644 (file)
@@ -23,7 +23,6 @@ void deletePredicate(Predicate* predicate){
                break;
        }
        }
-
        //need to handle freeing array...
        ourfree(predicate);
 }
index f9af5af66b2a937861431ebb0ae8d057e1da50fc..c6dd02b6fcb1d27c68ff4be9d65b703432f3aba7 100644 (file)
@@ -3,7 +3,7 @@
 #include "classlist.h"
 #include "mymemory.h"
 #include "ops.h"
-#include "structs.h"
+
 
 #define GETPREDICATETYPE(o) (((Predicate *)(o))->type)
 
diff --git a/src/AST/structtype.h b/src/AST/structtype.h
new file mode 100644 (file)
index 0000000..d58d844
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+/* 
+ * File:   structtype.h
+ * Author: hamed
+ *
+ * Created on June 21, 2017, 10:37 AM
+ */
+
+#ifndef STRUCTTYPE_H
+#define STRUCTTYPE_H
+#include "ops.h"
+#include "structs.h"
+#define GETSTRUCTTYPE(s) (((Struct*)s)->stype)
+#define GETPARENTSVECTOR(s) (((Struct*)s)->parents)
+#define ADDNEWPARENT(obj,par) pushVectorVoid(((Struct*)obj)->parents,(void*) par)
+#define ADDNEWPARENTTOOBJECTARRAY(array,size,par) \
+       do{     \
+               for(int i=0; i<size; i++){      \
+                       ADDNEWPARENT(array[i], par);    \
+               }       \
+       }while(0)
+#define DELETEPARENTSVECTOR(obj) deleteVectorArrayVoid(((Struct*)obj)->parents)
+
+struct Struct {
+       StructType stype;
+       VectorVoid* parents;
+};
+
+#endif /* STRUCTTYPE_H */
+
index d32af3faa81e5a714d502675d34038e31eae8d4c..c07a428fcd48a6a1b5091f9ba99d11454726c08c 100644 (file)
@@ -79,6 +79,9 @@ typedef struct OrderEncoding OrderEncoding;
 struct TableEntry;
 typedef struct TableEntry TableEntry;
 
+struct Struct;
+typedef struct Struct Struct;
+
 typedef unsigned int uint;
 typedef uint64_t VarType;
 #endif
index 5f330fd6112c70074522c17ac116739bf35746d6..eac2b05efc8aa61e90138f69b21a0a4e0a5b535c 100644 (file)
@@ -140,12 +140,14 @@ Function * completeTable(CSolver *solver, Table * table) {
 
 Element * applyFunction(CSolver *solver, Function * function, Element ** array, uint numArrays, Boolean * overflowstatus) {
        Element* element= allocElementFunction(function,array,numArrays,overflowstatus);
+       ADDNEWPARENTTOOBJECTARRAY(array, numArrays, element);
        pushVectorElement(solver->allElements, element);
        return element;
 }
 
 Boolean * applyPredicate(CSolver *solver, Predicate * predicate, Element ** inputs, uint numInputs) {
        Boolean* boolean= allocBooleanPredicate(predicate, inputs, numInputs);
+       ADDNEWPARENTTOOBJECTARRAY(inputs, numInputs, boolean);
        pushVectorBoolean(solver->allBooleans, boolean);
        return boolean;
 }