From: Ben Sherratt Date: Mon, 27 May 2024 17:37:02 +0000 (+0100) Subject: Groundwork for arrays X-Git-Url: https://git.bts.cx/sun.git/commitdiff_plain/b4bfa0d90e747a0f6f3080246823471423a30951?ds=sidebyside;hp=a5b6b9ea53d78b2019201a9fb75b763b7d92873c Groundwork for arrays --- diff --git a/runtime/src/sun/compiler/sun.y b/runtime/src/sun/compiler/sun.y index 2464ffa..c6acef0 100644 --- a/runtime/src/sun/compiler/sun.y +++ b/runtime/src/sun/compiler/sun.y @@ -114,7 +114,7 @@ statements_or_definitions ;*/ function_definition - : IDENTIFIER IDENTIFIER '(' optional_parameter_definitions ')' '{' optional_statements '}' { + : type IDENTIFIER '(' optional_parameter_definitions ')' '{' optional_statements '}' { $$ = sltNodeMakeFunctionDefinition($1, $2, $4, $7); } ; @@ -136,7 +136,7 @@ parameter_definitions ; parameter_definition - : IDENTIFIER IDENTIFIER { + : type IDENTIFIER { $$ = sltNodeMakeFunctionParameterDefinition($1, $2); } ; @@ -163,14 +163,23 @@ type_member_definition ;*/ variable_definition - : IDENTIFIER IDENTIFIER { + : type IDENTIFIER { $$ = sltNodeMakeVariableDefinition($1, $2, NULL); } - | IDENTIFIER IDENTIFIER '=' expression { + | type IDENTIFIER '=' expression { $$ = sltNodeMakeVariableDefinition($1, $2, $4); } ; +type + : IDENTIFIER { + $$ = $1;//sltNodeMakeTypeDefinition($1, 1); + } + | '[' type ':' INT ']' { + $$ = $2;//sltNodeMakeTypeDefinition($1, $2); + } + ; + optional_statements : /* nothing */ { $$ = NULL; @@ -414,7 +423,7 @@ postfix_expression $$ = sltNodeMakeFunctionCall($1, $3); } | postfix_expression '.' IDENTIFIER { - $$ = sltNodeMakeTypeAccess($1, $3); + $$ = sltNodeMakeMemberAccess($1, $3); } /*| postfix_expression postfix_unary_operator { $$ = sltNodeMakeUnaryOperation($1, $2); diff --git a/runtime/src/sun/compiler/vm_generator.c b/runtime/src/sun/compiler/vm_generator.c index 0309257..6a3cb19 100644 --- a/runtime/src/sun/compiler/vm_generator.c +++ b/runtime/src/sun/compiler/vm_generator.c @@ -288,7 +288,7 @@ static bool nodeTypeDefinesScope(SLTNodeType type) { switch (type) { case SLTNodeTypeModule: case SLTNodeTypeFunctionDefinition: - case SLTNodeTypeTypeDefinition: + case SLTNodeTypeUserDefinedTypeDefinition: case SLTNodeTypeForIteration: case SLTNodeTypeDoIteration: case SLTNodeTypeWhileIteration: @@ -803,7 +803,7 @@ static SVMPointer generateExpressionInstructions(VMGeneratorOutputScope *generat return r; } - case SLTNodeTypeTypeAccess: + case SLTNodeTypeMemberAccess: generator_assert_msg(false, "not implemented: %s", sltNodeToString(node)); return -1; @@ -1021,7 +1021,7 @@ static size_t generateModuleInstructions(VMGeneratorOutputScope *generator, SLTN for (SLTNode *n = node->moduleStatementsAndDefinitions; n != NULL; n = n->sequenceNext) { switch (n->sequenceNode->type) { //case SLTNodeTypeVariableDefinition: - case SLTNodeTypeTypeDefinition: + case SLTNodeTypeUserDefinedTypeDefinition: case SLTNodeTypeFunctionDefinition: // Do nothing... break; diff --git a/runtime/src/sun/tree/node.c b/runtime/src/sun/tree/node.c index ad91732..f94f6e2 100644 --- a/runtime/src/sun/tree/node.c +++ b/runtime/src/sun/tree/node.c @@ -93,8 +93,8 @@ SLTNode *sltNodeMakeArrayAccess(SLTNode *array, SLTNode *access) { return node; } -SLTNode *sltNodeMakeTypeAccess(SLTNode *type, SLTNode *access) { - SLTNode *node = node_make(SLTNodeTypeTypeAccess); +SLTNode *sltNodeMakeMemberAccess(SLTNode *type, SLTNode *access) { + SLTNode *node = node_make(SLTNodeTypeMemberAccess); node->typeAccessType = type; node->typeAccessAccess = access; return node; @@ -197,15 +197,17 @@ SLTNode *sltNodeMakeVariableDefinition(SLTNode *type, SLTNode *identifier, SLTNo return node; } -SLTNode *sltNodeMakeTypeDefinition(SLTNode *identifier, SLTNode *members) { - SLTNode *node = node_make(SLTNodeTypeTypeDefinition); +//extern SLTNode *sltNodeMakeTypeDefinition(SLTNode *identifier, SLTNode *dimension); + +SLTNode *sltNodeMakeUserDefinedTypeDefinition(SLTNode *identifier, SLTNode *members) { + SLTNode *node = node_make(SLTNodeTypeUserDefinedTypeDefinition); node->typeDefinitionIdentifier = identifier; node->typeDefinitionMembers = members; return node; } -SLTNode *sltNodeMakeTypeMemberDefinition(SLTNode *type, SLTNode *identifier) { - SLTNode *node = node_make(SLTNodeTypeTypeMemberDefinition); +SLTNode *sltNodeMakeUserDefinedTypeMemberDefinition(SLTNode *type, SLTNode *identifier) { + SLTNode *node = node_make(SLTNodeTypeUserDefinedTypeMemberDefinition); node->typeMemberDefinitionType = type; node->typeMemberDefinitionIdentifier = identifier; return node; diff --git a/runtime/src/sun/tree/node.h b/runtime/src/sun/tree/node.h index 11e4076..fe0cdf4 100644 --- a/runtime/src/sun/tree/node.h +++ b/runtime/src/sun/tree/node.h @@ -26,7 +26,7 @@ extern SLTNode *sltNodeMakeCompound(SLTNode *expression); extern SLTNode *sltNodeMakeArgument(SLTNode *expression); extern SLTNode *sltNodeMakeArrayAccess(SLTNode *array, SLTNode *access); -extern SLTNode *sltNodeMakeTypeAccess(SLTNode *type, SLTNode *access); +extern SLTNode *sltNodeMakeMemberAccess(SLTNode *type, SLTNode *access); extern SLTNode *sltNodeMakeFunctionCall(SLTNode *function, SLTNode *arguments); @@ -51,8 +51,10 @@ extern SLTNode *sltNodeMakeStatementBlock(SLTNode *contents); extern SLTNode *sltNodeMakeVariableDefinition(SLTNode *type, SLTNode *identifier, SLTNode *defaultExpression); -extern SLTNode *sltNodeMakeTypeDefinition(SLTNode *identifier, SLTNode *members); -extern SLTNode *sltNodeMakeTypeMemberDefinition(SLTNode *type, SLTNode *identifier); +//extern SLTNode *sltNodeMakeTypeDefinition(SLTNode *identifier, SLTNode *dimension); + +extern SLTNode *sltNodeMakeUserDefinedTypeDefinition(SLTNode *identifier, SLTNode *members); +extern SLTNode *sltNodeMakeUserDefinedTypeMemberDefinition(SLTNode *type, SLTNode *identifier); extern SLTNode *sltNodeMakeFunctionDefinition(SLTNode *type, SLTNode *identifier, SLTNode *parameters, SLTNode *body); extern SLTNode *sltNodeMakeFunctionParameterDefinition(SLTNode *type, SLTNode *identifier); diff --git a/runtime/src/sun/tree/node_type.c b/runtime/src/sun/tree/node_type.c index 1dacf19..d930bfe 100644 --- a/runtime/src/sun/tree/node_type.c +++ b/runtime/src/sun/tree/node_type.c @@ -33,8 +33,8 @@ const char *sltNodeTypeToString(SLTNodeType nodeType) { case SLTNodeTypeArrayAccess: return "ArrayAccess"; - case SLTNodeTypeTypeAccess: - return "TypeAccess"; + case SLTNodeTypeMemberAccess: + return "MemberAccess"; case SLTNodeTypeFunctionCall: return "FunctionCall"; @@ -75,11 +75,11 @@ const char *sltNodeTypeToString(SLTNodeType nodeType) { case SLTNodeTypeVariableDefinition: return "VariableDefinition"; - case SLTNodeTypeTypeDefinition: - return "TypeDefinition"; + case SLTNodeTypeUserDefinedTypeDefinition: + return "UserDefinedTypeDefinition"; - case SLTNodeTypeTypeMemberDefinition: - return "TypeMemberDefinition"; + case SLTNodeTypeUserDefinedTypeMemberDefinition: + return "UserDefinedTypeMemberDefinition"; case SLTNodeTypeFunctionDefinition: return "FunctionDefinition"; diff --git a/runtime/src/sun/tree/node_type.h b/runtime/src/sun/tree/node_type.h index 195d9e6..4a41ebf 100644 --- a/runtime/src/sun/tree/node_type.h +++ b/runtime/src/sun/tree/node_type.h @@ -22,7 +22,7 @@ enum SLTNodeType { SLTNodeTypeArgument, SLTNodeTypeArrayAccess, - SLTNodeTypeTypeAccess, + SLTNodeTypeMemberAccess, SLTNodeTypeFunctionCall, @@ -47,8 +47,10 @@ enum SLTNodeType { SLTNodeTypeVariableDefinition, - SLTNodeTypeTypeDefinition, - SLTNodeTypeTypeMemberDefinition, +// SLTNodeTypeArrayTypeDefinition, + + SLTNodeTypeUserDefinedTypeDefinition, + SLTNodeTypeUserDefinedTypeMemberDefinition, SLTNodeTypeFunctionDefinition, SLTNodeTypeFunctionParameterDefinition, diff --git a/runtime/src/sun/tree/tree.c b/runtime/src/sun/tree/tree.c index 118f9c6..9080d3a 100644 --- a/runtime/src/sun/tree/tree.c +++ b/runtime/src/sun/tree/tree.c @@ -58,7 +58,7 @@ SLTNode *sltTreeMap(SLTNode *root, SLTTreeMapCallback callback, SLTTreeMapCallba n->arrayAccessAccess = sltTreeMap(n->arrayAccessAccess, callback, postCallback, userParameter); break; - case SLTNodeTypeTypeAccess: + case SLTNodeTypeMemberAccess: n->typeAccessType = sltTreeMap(n->typeAccessType, callback, postCallback, userParameter); n->typeAccessAccess = sltTreeMap(n->typeAccessAccess, callback, postCallback, userParameter); break; @@ -115,12 +115,12 @@ SLTNode *sltTreeMap(SLTNode *root, SLTTreeMapCallback callback, SLTTreeMapCallba n->variableDefinitionDefaultExpression = sltTreeMap(n->variableDefinitionDefaultExpression, callback, postCallback, userParameter); break; - case SLTNodeTypeTypeDefinition: + case SLTNodeTypeUserDefinedTypeDefinition: n->typeDefinitionIdentifier = sltTreeMap(n->typeDefinitionIdentifier, callback, postCallback, userParameter); n->typeDefinitionMembers = sltTreeMap(n->typeDefinitionMembers, callback, postCallback, userParameter); break; - case SLTNodeTypeTypeMemberDefinition: + case SLTNodeTypeUserDefinedTypeMemberDefinition: n->typeMemberDefinitionType = sltTreeMap(n->typeMemberDefinitionType, callback, postCallback, userParameter); n->typeMemberDefinitionIdentifier = sltTreeMap(n->typeMemberDefinitionIdentifier, callback, postCallback, userParameter); break;