Big Tabbing Change
[satune.git] / src / AST / element.c
1 #include "element.h"
2 #include "structs.h"
3 #include "set.h"
4 #include "constraint.h"
5 #include "function.h"
6 #include "table.h"
7
8 Element *allocElementSet(Set *s) {
9         ElementSet *This = (ElementSet *)ourmalloc(sizeof(ElementSet));
10         GETELEMENTTYPE(This) = ELEMSET;
11         This->set = s;
12         initDefVectorASTNode(GETELEMENTPARENTS(This));
13         initElementEncoding(&This->encoding, (Element *) This);
14         return &This->base;
15 }
16
17 Element *allocElementFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
18         ElementFunction *This = (ElementFunction *) ourmalloc(sizeof(ElementFunction));
19         GETELEMENTTYPE(This) = ELEMFUNCRETURN;
20         This->function = function;
21         ASSERT(GETBOOLEANTYPE(overflowstatus) == BOOLEANVAR);
22         This->overflowstatus = overflowstatus;
23         initArrayInitElement(&This->inputs, array, numArrays);
24         initDefVectorASTNode(GETELEMENTPARENTS(This));
25         for (uint i = 0; i < numArrays; i++)
26                 pushVectorASTNode(GETELEMENTPARENTS(array[i]), (ASTNode *) This);
27         initElementEncoding(&This->rangeencoding, (Element *) This);
28         initFunctionEncoding(&This->functionencoding, (Element *) This);
29         return &This->base;
30 }
31
32 Element *allocElementConst(uint64_t value, VarType type) {
33         ElementConst *This = (ElementConst *)ourmalloc(sizeof(ElementConst));
34         GETELEMENTTYPE(This) = ELEMCONST;
35         This->value = value;
36         This->set = allocSet(type, (uint64_t[]) {value}, 1);
37         initDefVectorASTNode(GETELEMENTPARENTS(This));
38         initElementEncoding(&This->encoding, (Element *) This);
39         return &This->base;
40 }
41
42 Set *getElementSet(Element *This) {
43         switch (GETELEMENTTYPE(This)) {
44         case ELEMSET:
45                 return ((ElementSet *)This)->set;
46         case ELEMCONST:
47                 return ((ElementConst *)This)->set;
48         case ELEMFUNCRETURN: {
49                 Function *func = ((ElementFunction *)This)->function;
50                 switch (GETFUNCTIONTYPE(func)) {
51                 case TABLEFUNC:
52                         return ((FunctionTable *)func)->table->range;
53                 case OPERATORFUNC:
54                         return ((FunctionOperator *)func)->range;
55                 default:
56                         ASSERT(0);
57                 }
58         }
59         default:
60                 ASSERT(0);
61         }
62         ASSERT(0);
63         return NULL;
64 }
65
66 void deleteElement(Element *This) {
67         switch (GETELEMENTTYPE(This)) {
68         case ELEMFUNCRETURN: {
69                 ElementFunction *ef = (ElementFunction *) This;
70                 deleteInlineArrayElement(&ef->inputs);
71                 deleteElementEncoding(&ef->rangeencoding);
72                 deleteFunctionEncoding(&ef->functionencoding);
73                 break;
74         }
75         case ELEMSET: {
76                 ElementSet *es = (ElementSet *) This;
77                 deleteElementEncoding(&es->encoding);
78                 break;
79         }
80         case ELEMCONST: {
81                 ElementConst *ec = (ElementConst *) This;
82                 deleteSet(ec->set);//Client did not create, so we free it
83                 deleteElementEncoding(&ec->encoding);
84                 break;
85         }
86         default:
87                 ASSERT(0);
88         }
89         deleteVectorArrayASTNode(GETELEMENTPARENTS(This));
90         ourfree(This);
91 }