]> git.bts.cx Git - sun.git/blob - runtime/src/sun/tree/node.c
ad91732e504e514f01a83b4012bdca69a15dcbc9
[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 *sltNodeMakeTypeAccess(SLTNode *type, SLTNode *access) {
97 SLTNode *node = node_make(SLTNodeTypeTypeAccess);
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 SLTNode *sltNodeMakeTypeDefinition(SLTNode *identifier, SLTNode *members) {
201 SLTNode *node = node_make(SLTNodeTypeTypeDefinition);
202 node->typeDefinitionIdentifier = identifier;
203 node->typeDefinitionMembers = members;
204 return node;
205 }
206
207 SLTNode *sltNodeMakeTypeMemberDefinition(SLTNode *type, SLTNode *identifier) {
208 SLTNode *node = node_make(SLTNodeTypeTypeMemberDefinition);
209 node->typeMemberDefinitionType = type;
210 node->typeMemberDefinitionIdentifier = identifier;
211 return node;
212 }
213
214 SLTNode *sltNodeMakeFunctionDefinition(SLTNode *type, SLTNode *identifier, SLTNode *parameters, SLTNode *body) {
215 SLTNode *node = node_make(SLTNodeTypeFunctionDefinition);
216 node->functionDefinitionType = type;
217 node->functionDefinitionIdentifier = identifier;
218 node->functionDefinitionParameters = parameters;
219 node->functionDefinitionBody = body;
220 return node;
221 }
222
223 SLTNode *sltNodeMakeFunctionParameterDefinition(SLTNode *type, SLTNode *identifier) {
224 SLTNode *node = node_make(SLTNodeTypeFunctionParameterDefinition);
225 node->functionParameterDefinitionType = type;
226 node->functionParameterDefinitionIdentifier = identifier;
227 return node;
228 }
229
230 SLTNode *sltNodeMakeModule(SLTNode *statementsAndDefinitions) {
231 SLTNode *node = node_make(SLTNodeTypeModule);
232 node->moduleStatementsAndDefinitions = statementsAndDefinitions;
233 return node;
234 }
235
236 SLTNode *sltNodeMakeSequence(SLTNode *head, SLTNode *item) {
237 assert(head == NULL || head->type == SLTNodeTypeSequence);
238
239 if (item != NULL) {
240 SLTNode *node = node_make(SLTNodeTypeSequence);
241 node->sequenceNode = item;
242 node->sequenceNext = NULL;
243
244 if (head == NULL) {
245 head = node;
246 } else {
247 SLTNode *pos = head;
248 while (pos->sequenceNext != NULL) pos = pos->sequenceNext;
249 pos->sequenceNext = node;
250 }
251 }
252
253 return head;
254 }