]> git.bts.cx Git - sun.git/blob - runtime/src/sun/tree/node.c
Groundwork for arrays
[sun.git] / runtime / src / sun / tree / node.c
1 #include "node_internal.h"
2
3 #include <sun/tree/crc32.h>
4 #include <assert.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 const char *sltNodeToString(SLTNode *node) {
9 return sltNodeTypeToString(node->type);
10 }
11
12 static SLTNode *node_make_typed(SLTNodeType type, SLTDataType dataType) {
13 SLTNode *node = (SLTNode *)malloc(sizeof(SLTNode));
14 node->type = type;
15 node->dataType = dataType;
16 //node->next = NULL;
17 return node;
18 }
19
20 static SLTNode *node_make(SLTNodeType type) {
21 return node_make_typed(type, SLTDataTypeUnknown);
22 }
23
24 SLTNode *sltNodeMakeIdentifier(const char *name) {
25 SLTNode *node = node_make(SLTNodeTypeIdentifier);
26 node->identifierStringValue = strdup(name); // FIXME
27 node->identifierStringKeyValue = crc32c(name);
28 return node;
29 }
30
31 SLTNode *sltNodeMakeBoolean(bool value) {
32 SLTNode *node = node_make_typed(SLTNodeTypeBoolean, SLTDataTypeBoolean);
33 node->booleanValue = value;
34 return node;
35 }
36
37 SLTNode *sltNodeMakeInteger(int value) {
38 SLTNode *node = node_make_typed(SLTNodeTypeInteger, SLTDataTypeInteger);
39 node->integerValue = value;
40 return node;
41 }
42
43 SLTNode *sltNodeMakeRawInteger(int value) {
44 SLTNode *node = node_make_typed(SLTNodeTypeRawInteger, SLTDataTypeInteger);
45 node->integerValue = value;
46 return node;
47 }
48
49 SLTNode *sltNodeMakeFloat(float value) {
50 SLTNode *node = node_make_typed(SLTNodeTypeFloat, SLTDataTypeFloat);
51 node->floatValue = value;
52 return node;
53 }
54
55 SLTNode *sltNodeMakeString(const char *value) {
56 SLTNode *node = node_make_typed(SLTNodeTypeString, SLTDataTypeString);
57 size_t strLen = strlen(value) - 2; // -2 to remove quotes
58 node->stringValue = malloc(strLen + 1);
59 strncpy(node->stringValue, value + 1, strLen);
60 node->stringValue[strLen] = '\0';
61 return node;
62 }
63
64 SLTNode *sltNodeMakeKey(const char *value) {
65 SLTNode *node = node_make_typed(SLTNodeTypeKey, SLTDataTypeString);
66
67 size_t strLen = strlen(value) - 3; // -3 to remove quotes
68 unsigned char *key = alloca(strLen + 1);
69 strncpy(key, value + 2, strLen);
70 key[strLen] = '\0';
71
72 node->keyValue = crc32c(key);
73
74 return node;
75 }
76
77 SLTNode *sltNodeMakeCompound(SLTNode *expression) {
78 SLTNode *node = node_make(SLTNodeTypeCompound);
79 node->compoundSubexpression = expression;
80 return node;
81 }
82
83 SLTNode *sltNodeMakeArgument(SLTNode *expression) {
84 SLTNode *node = node_make(SLTNodeTypeArgument);
85 node->argumentExpression = expression;
86 return node;
87 }
88
89 SLTNode *sltNodeMakeArrayAccess(SLTNode *array, SLTNode *access) {
90 SLTNode *node = node_make(SLTNodeTypeArrayAccess);
91 node->arrayAccessArray = array;
92 node->arrayAccessAccess = access;
93 return node;
94 }
95
96 SLTNode *sltNodeMakeMemberAccess(SLTNode *type, SLTNode *access) {
97 SLTNode *node = node_make(SLTNodeTypeMemberAccess);
98 node->typeAccessType = type;
99 node->typeAccessAccess = access;
100 return node;
101 }
102
103 SLTNode *sltNodeMakeFunctionCall(SLTNode *function, SLTNode *arguments) {
104 SLTNode *node = node_make(SLTNodeTypeFunctionCall);
105 node->functionCallFunction = function;
106 node->functionCallArguments = arguments;
107 return node;
108 }
109
110
111 SLTNode *sltNodeMakeUnaryOperation(SLTNode *op, SLTNode *operand) { // Can't use "operator" apparently...
112 SLTNode *node = node_make(SLTNodeTypeUnaryOperation);
113 node->unaryOperationOperator = op;
114 node->unaryOperationOperand = operand;
115 return node;
116 }
117
118 SLTNode *sltNodeMakeBinaryOperation(SLTNode *op, SLTNode *lhs, SLTNode *rhs) {
119 SLTNode *node = node_make(SLTNodeTypeBinaryOperation);
120 node->binaryOperationOperator = op;
121 node->binaryOperationLHS = lhs;
122 node->binaryOperationRHS = rhs;
123 return node;
124 }
125
126 SLTNode *sltNodeMakeOperator(SLTOperatorType type) {
127 SLTNode *node = node_make(SLTNodeTypeOperator);
128 node->operatorType = type;
129 return node;
130 }
131
132 SLTNode *sltNodeMakeAssignment(SLTNode *target, SLTNode *source) {
133 SLTNode *node = node_make(SLTNodeTypeAssignment);
134 node->assignmentTarget = target;
135 node->assignmentSource = source;
136 return node;
137 }
138
139 SLTNode *sltNodeMakeReturn(SLTNode *expression) {
140 SLTNode *node = node_make(SLTNodeTypeReturn);
141 node->returnExpression = expression;
142 return node;
143 }
144
145 SLTNode *sltNodeMakeSleep() {
146 SLTNode *node = node_make(SLTNodeTypeSleep);
147 return node;
148 }
149
150 SLTNode *sltNodeMakeBreak() {
151 SLTNode *node = node_make(SLTNodeTypeBreak);
152 return node;
153 }
154
155 SLTNode *sltNodeMakeForIteration(SLTNode *initialExpression, SLTNode *preValidExpression, SLTNode *loopExpression, SLTNode *statement) {
156 SLTNode *node = node_make(SLTNodeTypeForIteration);
157 node->iterationInitialExpression = initialExpression;
158 node->iterationPreValidExpression = preValidExpression;
159 node->iterationLoopExpression = loopExpression;
160 node->iterationStatement = statement;
161 return node;
162 }
163
164 SLTNode *sltNodeMakeDoIteration(SLTNode *postValidExpression, SLTNode *statement) {
165 SLTNode *node = node_make(SLTNodeTypeDoIteration);
166 node->iterationPostValidExpression = postValidExpression;
167 node->iterationStatement = statement;
168 return node;
169 }
170
171 SLTNode *sltNodeMakeWhileIteration(SLTNode *preValidExpression, SLTNode *statement) {
172 SLTNode *node = node_make(SLTNodeTypeWhileIteration);
173 node->iterationPreValidExpression = preValidExpression;
174 node->iterationStatement = statement;
175 return node;
176 }
177
178 SLTNode *sltNodeMakeSelection(SLTNode *validExpression, SLTNode *validStatement, SLTNode *invalidStatement) {
179 SLTNode *node = node_make(SLTNodeTypeSelection);
180 node->selectionValidExpression = validExpression;
181 node->selectionValidStatement = validStatement;
182 node->selectionInvalidStatement = invalidStatement;
183 return node;
184 }
185
186 SLTNode *sltNodeMakeStatementBlock(SLTNode *contents) {
187 SLTNode *node = node_make(SLTNodeTypeStatementBlock);
188 node->statementBlockContents = contents;
189 return node;
190 }
191
192 SLTNode *sltNodeMakeVariableDefinition(SLTNode *type, SLTNode *identifier, SLTNode *defaultExpression) {
193 SLTNode *node = node_make(SLTNodeTypeVariableDefinition);
194 node->variableDefinitionType = type;
195 node->variableDefinitionIdentifier = identifier;
196 node->variableDefinitionDefaultExpression = defaultExpression;
197 return node;
198 }
199
200 //extern SLTNode *sltNodeMakeTypeDefinition(SLTNode *identifier, SLTNode *dimension);
201
202 SLTNode *sltNodeMakeUserDefinedTypeDefinition(SLTNode *identifier, SLTNode *members) {
203 SLTNode *node = node_make(SLTNodeTypeUserDefinedTypeDefinition);
204 node->typeDefinitionIdentifier = identifier;
205 node->typeDefinitionMembers = members;
206 return node;
207 }
208
209 SLTNode *sltNodeMakeUserDefinedTypeMemberDefinition(SLTNode *type, SLTNode *identifier) {
210 SLTNode *node = node_make(SLTNodeTypeUserDefinedTypeMemberDefinition);
211 node->typeMemberDefinitionType = type;
212 node->typeMemberDefinitionIdentifier = identifier;
213 return node;
214 }
215
216 SLTNode *sltNodeMakeFunctionDefinition(SLTNode *type, SLTNode *identifier, SLTNode *parameters, SLTNode *body) {
217 SLTNode *node = node_make(SLTNodeTypeFunctionDefinition);
218 node->functionDefinitionType = type;
219 node->functionDefinitionIdentifier = identifier;
220 node->functionDefinitionParameters = parameters;
221 node->functionDefinitionBody = body;
222 return node;
223 }
224
225 SLTNode *sltNodeMakeFunctionParameterDefinition(SLTNode *type, SLTNode *identifier) {
226 SLTNode *node = node_make(SLTNodeTypeFunctionParameterDefinition);
227 node->functionParameterDefinitionType = type;
228 node->functionParameterDefinitionIdentifier = identifier;
229 return node;
230 }
231
232 SLTNode *sltNodeMakeModule(SLTNode *statementsAndDefinitions) {
233 SLTNode *node = node_make(SLTNodeTypeModule);
234 node->moduleStatementsAndDefinitions = statementsAndDefinitions;
235 return node;
236 }
237
238 SLTNode *sltNodeMakeSequence(SLTNode *head, SLTNode *item) {
239 assert(head == NULL || head->type == SLTNodeTypeSequence);
240
241 if (item != NULL) {
242 SLTNode *node = node_make(SLTNodeTypeSequence);
243 node->sequenceNode = item;
244 node->sequenceNext = NULL;
245
246 if (head == NULL) {
247 head = node;
248 } else {
249 SLTNode *pos = head;
250 while (pos->sequenceNext != NULL) pos = pos->sequenceNext;
251 pos->sequenceNext = node;
252 }
253 }
254
255 return head;
256 }