1 #include "node_internal.h"
3 #include <sun/tree/crc32.h>
8 const char *sltNodeToString(SLTNode
*node
) {
9 return sltNodeTypeToString(node
->type
);
12 static SLTNode
*node_make_typed(SLTNodeType type
, SLTDataType dataType
) {
13 SLTNode
*node
= (SLTNode
*)malloc(sizeof(SLTNode
));
15 node
->dataType
= dataType
;
20 static SLTNode
*node_make(SLTNodeType type
) {
21 return node_make_typed(type
, SLTDataTypeUnknown
);
24 SLTNode
*sltNodeMakeIdentifier(const char *name
) {
25 SLTNode
*node
= node_make(SLTNodeTypeIdentifier
);
26 node
->identifierStringValue
= strdup(name
); // FIXME
27 node
->identifierStringKeyValue
= crc32c(name
);
31 SLTNode
*sltNodeMakeBoolean(bool value
) {
32 SLTNode
*node
= node_make_typed(SLTNodeTypeBoolean
, SLTDataTypeBoolean
);
33 node
->booleanValue
= value
;
37 SLTNode
*sltNodeMakeInteger(int value
) {
38 SLTNode
*node
= node_make_typed(SLTNodeTypeInteger
, SLTDataTypeInteger
);
39 node
->integerValue
= value
;
43 SLTNode
*sltNodeMakeRawInteger(int value
) {
44 SLTNode
*node
= node_make_typed(SLTNodeTypeRawInteger
, SLTDataTypeInteger
);
45 node
->integerValue
= value
;
49 SLTNode
*sltNodeMakeFloat(float value
) {
50 SLTNode
*node
= node_make_typed(SLTNodeTypeFloat
, SLTDataTypeFloat
);
51 node
->floatValue
= value
;
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';
64 SLTNode
*sltNodeMakeKey(const char *value
) {
65 SLTNode
*node
= node_make_typed(SLTNodeTypeKey
, SLTDataTypeString
);
67 size_t strLen
= strlen(value
) - 3; // -3 to remove quotes
68 unsigned char *key
= alloca(strLen
+ 1);
69 strncpy(key
, value
+ 2, strLen
);
72 node
->keyValue
= crc32c(key
);
77 SLTNode
*sltNodeMakeCompound(SLTNode
*expression
) {
78 SLTNode
*node
= node_make(SLTNodeTypeCompound
);
79 node
->compoundSubexpression
= expression
;
83 SLTNode
*sltNodeMakeArgument(SLTNode
*expression
) {
84 SLTNode
*node
= node_make(SLTNodeTypeArgument
);
85 node
->argumentExpression
= expression
;
89 SLTNode
*sltNodeMakeArrayAccess(SLTNode
*array
, SLTNode
*access
) {
90 SLTNode
*node
= node_make(SLTNodeTypeArrayAccess
);
91 node
->arrayAccessArray
= array
;
92 node
->arrayAccessAccess
= access
;
96 SLTNode
*sltNodeMakeTypeAccess(SLTNode
*type
, SLTNode
*access
) {
97 SLTNode
*node
= node_make(SLTNodeTypeTypeAccess
);
98 node
->typeAccessType
= type
;
99 node
->typeAccessAccess
= access
;
103 SLTNode
*sltNodeMakeFunctionCall(SLTNode
*function
, SLTNode
*arguments
) {
104 SLTNode
*node
= node_make(SLTNodeTypeFunctionCall
);
105 node
->functionCallFunction
= function
;
106 node
->functionCallArguments
= arguments
;
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
;
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
;
126 SLTNode
*sltNodeMakeOperator(SLTOperatorType type
) {
127 SLTNode
*node
= node_make(SLTNodeTypeOperator
);
128 node
->operatorType
= type
;
132 SLTNode
*sltNodeMakeAssignment(SLTNode
*target
, SLTNode
*source
) {
133 SLTNode
*node
= node_make(SLTNodeTypeAssignment
);
134 node
->assignmentTarget
= target
;
135 node
->assignmentSource
= source
;
139 SLTNode
*sltNodeMakeReturn(SLTNode
*expression
) {
140 SLTNode
*node
= node_make(SLTNodeTypeReturn
);
141 node
->returnExpression
= expression
;
145 SLTNode
*sltNodeMakeSleep() {
146 SLTNode
*node
= node_make(SLTNodeTypeSleep
);
150 SLTNode
*sltNodeMakeBreak() {
151 SLTNode
*node
= node_make(SLTNodeTypeBreak
);
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
;
164 SLTNode
*sltNodeMakeDoIteration(SLTNode
*postValidExpression
, SLTNode
*statement
) {
165 SLTNode
*node
= node_make(SLTNodeTypeDoIteration
);
166 node
->iterationPostValidExpression
= postValidExpression
;
167 node
->iterationStatement
= statement
;
171 SLTNode
*sltNodeMakeWhileIteration(SLTNode
*preValidExpression
, SLTNode
*statement
) {
172 SLTNode
*node
= node_make(SLTNodeTypeWhileIteration
);
173 node
->iterationPreValidExpression
= preValidExpression
;
174 node
->iterationStatement
= statement
;
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
;
186 SLTNode
*sltNodeMakeStatementBlock(SLTNode
*contents
) {
187 SLTNode
*node
= node_make(SLTNodeTypeStatementBlock
);
188 node
->statementBlockContents
= contents
;
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
;
200 SLTNode
*sltNodeMakeTypeDefinition(SLTNode
*identifier
, SLTNode
*members
) {
201 SLTNode
*node
= node_make(SLTNodeTypeTypeDefinition
);
202 node
->typeDefinitionIdentifier
= identifier
;
203 node
->typeDefinitionMembers
= members
;
207 SLTNode
*sltNodeMakeTypeMemberDefinition(SLTNode
*type
, SLTNode
*identifier
) {
208 SLTNode
*node
= node_make(SLTNodeTypeTypeMemberDefinition
);
209 node
->typeMemberDefinitionType
= type
;
210 node
->typeMemberDefinitionIdentifier
= identifier
;
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
;
223 SLTNode
*sltNodeMakeFunctionParameterDefinition(SLTNode
*type
, SLTNode
*identifier
) {
224 SLTNode
*node
= node_make(SLTNodeTypeFunctionParameterDefinition
);
225 node
->functionParameterDefinitionType
= type
;
226 node
->functionParameterDefinitionIdentifier
= identifier
;
230 SLTNode
*sltNodeMakeModule(SLTNode
*statementsAndDefinitions
) {
231 SLTNode
*node
= node_make(SLTNodeTypeModule
);
232 node
->moduleStatementsAndDefinitions
= statementsAndDefinitions
;
236 SLTNode
*sltNodeMakeSequence(SLTNode
*head
, SLTNode
*item
) {
237 assert(head
== NULL
|| head
->type
== SLTNodeTypeSequence
);
240 SLTNode
*node
= node_make(SLTNodeTypeSequence
);
241 node
->sequenceNode
= item
;
242 node
->sequenceNext
= NULL
;
248 while (pos
->sequenceNext
!= NULL
) pos
= pos
->sequenceNext
;
249 pos
->sequenceNext
= node
;