diff --git a/src/include/parser/ast.h b/src/include/parser/ast.h index 195f5be0..778d6e9b 100644 --- a/src/include/parser/ast.h +++ b/src/include/parser/ast.h @@ -23,54 +23,6 @@ /* Type Declarations */ /*********************/ -typedef enum { - AstBlank, - AstBindingStatement, - AstCallExpression, - AstDoStatement, - AstDoubleExpression, - AstEnumTypeDeclaration, - AstEnumField, - AstEnumFieldList, - AstEnumTypeExpression, - AstExpression, - AstExpressionList, - AstField, - AstFieldBinding, - AstFieldBindingList, - AstFieldList, - AstFunctionDeclaration, - AstIdentifier, - AstIdentifierExpression, - AstImportStatement, - AstIntegerExpression, - AstLetBinding, - AstListExpression, - AstMatchCase, - AstMatchCaseList, - AstMatchStatement, - AstModuleStatement, - AstModuleStatementList, - AstMutBinding, - AstMutExpression, - AstNewAlloc, - AstNumericExpression, - AstParameter, - AstParameterList, - AstProgram, - AstReturnStatement, - AstShebang, - AstStatement, - AstStatementList, - AstStringExpression, - AstStructTypeDeclaration, - AstStructuredTypeExpression, - AstTypeDeclaration, - AstTypeExpression, - AstTypeIdentifier, - AstUnionTypeDeclaration, -} AstNodeKind; - typedef enum { ImportStatement, ImportCStatement @@ -129,296 +81,243 @@ union StatementNodeUnion; union TypeDeclarationNodeUnion; union TypeExpressionNodeUnion; -union AstNodeUnion { - struct BindingStatementNode* binding_statement; - struct CallExpressionNode* call_expression; - struct DoStatementNode* do_statement; - struct DoubleExpressionNode* double_expression; - struct EnumTypeDeclarationNode* enum_type_declaration; - struct EnumFieldNode* enum_field; - struct EnumFieldListNode* enum_field_list; - struct EnumTypeExpressionNode* enum_type_expression; - struct ExpressionListNode* expression_list; - struct ExpressionNode* expression; - struct FieldBindingNode* field_binding; - struct FieldBindingListNode* field_binding_list; - struct FieldNode* field; - struct FieldListNode* field_list; - struct FunctionDeclarationNode* function_declaration; - struct IdentifierNode* identifier; - struct IdentifierExpressionNode* identifier_expression; - struct IntegerExpressionNode* integer_expression; - struct ImportStatementNode* import_statement; - struct LetBindingNode* let_binding; - struct ListExpressionNode* list_expression; - struct MatchCaseNode* match_case; - struct MatchCaseListNode* match_case_list; - struct MatchStatementNode* match_expression; - struct ModuleStatementListNode* module_statement_list; - struct ModuleStatementNode* module_statement; - struct MutBindingNode* mut_binding; - struct MutExpressionNode* mut_expression; - struct NewAllocNode* new_alloc; - struct NumericExpressionNode* numeric_expression; - struct ParameterListNode* parameter_list; - struct ParameterNode* parameter; - struct ProgramNode* program; - struct ReturnStatementNode* return_statement; - struct ShebangNode* shebang; - struct StatementNode* statement; - struct StatementListNode* statement_list; - struct StringExpressionNode* string_expression; - struct StructTypeDeclarationNode* struct_type_declaration; - struct StructuredTypeExpressionNode* structured_type_expression; - struct TypeDeclarationNode* type_declaration; - struct TypeExpressionNode* type_expression; - struct TypeIdentifierNode* type_identifier; - struct UnionTypeDeclarationNode* union_type_declaration; -}; - -union BindingStatementNodeUnion { +typedef union BindingStatementNodeUnion { struct LetBindingNode* let_binding; struct MutBindingNode* mut_binding; -}; +} BindingStatementNodeUnion; -union ExpressionNodeUnion { +typedef union ExpressionNodeUnion { struct CallExpressionNode* call_expression; struct IdentifierExpressionNode* identifier_expression; struct ListExpressionNode* list_expression; struct NumericExpressionNode* numeric_expression; struct StringExpressionNode* string_expression; struct TypeExpressionNode* type_expression; -}; +} ExpressionNodeUnion; -union ModuleStatementNodeUnion { +typedef union ModuleStatementNodeUnion { struct FunctionDeclarationNode* function_declaration; struct ImportStatementNode* import_statement; struct LetBindingNode* let_binding; struct TypeDeclarationNode* type_declaration; -}; +} ModuleStatementNodeUnion; -union MutExpressionNodeUnion { +typedef union MutExpressionNodeUnion { struct NewAllocNode* new_alloc; struct ExpressionNode* expression; -}; +} MutExpressionNodeUnion; -union NumericExpressionNodeUnion { +typedef union NumericExpressionNodeUnion { struct DoubleExpressionNode* double_expression; struct IntegerExpressionNode* integer_expression; -}; +} NumericExpressionNodeUnion; -union StatementNodeUnion { +typedef union StatementNodeUnion { struct BindingStatementNode* binding_statement; struct DoStatementNode* do_statement; struct ExpressionNode* expression; struct MatchStatementNode* match_statement; struct ReturnStatementNode* return_statement; -}; +} StatementNodeUnion; -union TypeDeclarationNodeUnion { +typedef union TypeDeclarationNodeUnion { struct EnumTypeDeclarationNode* enum_type_declaration; struct StructTypeDeclarationNode* struct_type_declaration; struct UnionTypeDeclarationNode* union_type_declaration; -}; +} TypeDeclarationNodeUnion; -union TypeExpressionNodeUnion { +typedef union TypeExpressionNodeUnion { struct EnumTypeExpressionNode* enum_type_expression; struct StructuredTypeExpressionNode* structured_type_expression; -}; +} TypeExpressionNodeUnion; /********************/ /* Type Definitions */ /********************/ - -typedef struct AstNode { - AstNodeKind kind; - union AstNodeUnion value; -} AstNode; - -struct BindingStatementNode { +typedef struct BindingStatementNode { AstNodeKind kind; union BindingStatementNodeUnion value; -}; +} BindingStatementNode; -struct CallExpressionNode { +typedef struct CallExpressionNode { AstNodeKind kind; struct IdentifierNode* function; struct ExpressionListNode* arguments; -}; +} CallExpressionNode; -struct DoStatementNode { +typedef struct DoStatementNode { AstNodeKind kind; struct StatementListNode* statements; -}; +} DoStatementNode; -struct DoubleExpressionNode { +typedef struct DoubleExpressionNode { AstNodeKind kind; double value; -}; +} DoubleExpressionNode; -struct EnumFieldListNode { +typedef struct EnumFieldListNode { AstNodeKind kind; struct EnumFieldNode** fields; size_t length; -}; +} EnumFieldListNode; -struct EnumFieldNode { +typedef struct EnumFieldNode { AstNodeKind kind; struct IdentifierNode* identifier; struct IntegerExpressionNode* integer_expression; -}; +} EnumFieldNode; -struct EnumTypeDeclarationNode { +typedef struct EnumTypeDeclarationNode { AstNodeKind kind; struct IdentifierNode* identifier; struct EnumFieldListNode* fields; -}; +} EnumTypeDeclarationNode; -struct EnumTypeExpressionNode { +typedef struct EnumTypeExpressionNode { AstNodeKind kind; struct TypeIdentifierNode* type; struct IdentifierNode* value; -}; +} EnumTypeExpressionNode; -struct ExpressionListNode { +typedef struct ExpressionListNode { AstNodeKind kind; struct ExpressionNode** expressions; size_t length; -}; +} ExpressionListNode; -struct ExpressionNode { +typedef struct ExpressionNode { AstNodeKind kind; union ExpressionNodeUnion value; -}; +} ExpressionNode; -struct FieldBindingListNode { +typedef struct FieldBindingListNode { AstNodeKind kind; struct FieldBindingNode** field_bindings; size_t length; -}; +} FieldBindingListNode; -struct FieldBindingNode { +typedef struct FieldBindingNode { AstNodeKind kind; struct IdentifierNode* identifier; struct ExpressionNode* expression; -}; +} FieldBindingNode; -struct FieldListNode { +typedef struct FieldListNode { AstNodeKind kind; struct FieldNode** fields; size_t length; -}; +} FieldListNode; -struct FieldNode { +typedef struct FieldNode { AstNodeKind kind; struct IdentifierNode* identifier; struct TypeIdentifierNode* type_identifier; -}; +} FieldNode; -struct FunctionDeclarationNode { +typedef struct FunctionDeclarationNode { AstNodeKind kind; struct IdentifierNode* identifier; struct TypeIdentifierNode* return_type; struct ParameterListNode* parameters; struct DoStatementNode* body; -}; +} FunctionDeclarationNode; -struct IdentifierNode { +typedef struct IdentifierNode { AstNodeKind kind; char* name; -}; +} IdentifierNode; -struct IdentifierExpressionNode { +typedef struct IdentifierExpressionNode { AstNodeKind kind; struct IdentifierNode* identifier; struct IdentifierExpressionNode* next; -}; +} IdentifierExpressionNode; -struct ImportStatementNode { +typedef struct ImportStatementNode { AstNodeKind kind; ImportNodeKind import_kind; struct StringExpressionNode* module_name; -}; +} ImportStatementNode; -struct IntegerExpressionNode { +typedef struct IntegerExpressionNode { AstNodeKind kind; int64_t value; -}; +} IntegerExpressionNode; -struct LetBindingNode { +typedef struct LetBindingNode { AstNodeKind kind; struct IdentifierNode* binding; struct TypeIdentifierNode* type; struct ExpressionNode* expression; -}; +} LetBindingNode; -struct ListExpressionNode { +typedef struct ListExpressionNode { AstNodeKind kind; struct ExpressionListNode* expressions; -}; +} ListExpressionNode; -struct MatchCaseNode { +typedef struct MatchCaseNode { AstNodeKind kind; struct CallExpressionNode* condition; struct StatementNode* statement; -}; +} MatchCaseNode; -struct MatchCaseListNode { +typedef struct MatchCaseListNode { AstNodeKind kind; struct MatchCaseNode** cases; size_t length; -}; +} MatchCaseListNode; -struct MatchStatementNode { +typedef struct MatchStatementNode { AstNodeKind kind; struct MatchCaseListNode* case_list; -}; +} MatchStatementNode; -struct ModuleStatementListNode { +typedef struct ModuleStatementListNode { AstNodeKind kind; struct ModuleStatementNode** module_statements; size_t length; -}; +} ModuleStatementListNode; -struct ModuleStatementNode { +typedef struct ModuleStatementNode { AstNodeKind kind; union ModuleStatementNodeUnion value; -}; +} ModuleStatementNode; -struct MutBindingNode { +typedef struct MutBindingNode { AstNodeKind kind; struct IdentifierNode* binding; struct TypeIdentifierNode* type; struct MutExpressionNode* mut_expression; -}; +} MutBindingNode; -struct MutExpressionNode { +typedef struct MutExpressionNode { AstNodeKind kind; union MutExpressionNodeUnion value; -}; +} MutExpressionNode; -struct NewAllocNode { +typedef struct NewAllocNode { AstNodeKind kind; struct StructuredTypeExpressionNode* structured_type_expression; -}; +} NewAllocNode; -struct NumericExpressionNode { +typedef struct NumericExpressionNode { AstNodeKind kind; union NumericExpressionNodeUnion value; -}; +} NumericExpressionNode; -struct ParameterListNode { +typedef struct ParameterListNode { AstNodeKind kind; struct ParameterNode** parameters; size_t length; -}; +} ParameterListNode; -struct ParameterNode { +typedef struct ParameterNode { AstNodeKind kind; struct IdentifierNode* identifier; struct TypeIdentifierNode* type_identifier; -}; +} ParameterNode; typedef struct ProgramNode { AstNodeKind kind; @@ -426,64 +325,64 @@ typedef struct ProgramNode { struct ModuleStatementListNode* module_statements; } ProgramNode; -struct ReturnStatementNode { +typedef struct ReturnStatementNode { AstNodeKind kind; struct ExpressionNode* expression; -}; +} ReturnStatementNode; typedef struct ShebangNode { AstNodeKind kind; } ShebangNode; -struct StatementNode { +typedef struct StatementNode { AstNodeKind kind; union StatementNodeUnion value; -}; +} StatementNode; -struct StatementListNode { +typedef struct StatementListNode { AstNodeKind kind; struct StatementNode** statements; size_t length; -}; +} StatementListNode; -struct StringExpressionNode { +typedef struct StringExpressionNode { AstNodeKind kind; char* value; -}; +} StringExpressionNode; -struct StructTypeDeclarationNode { +typedef struct StructTypeDeclarationNode { AstNodeKind kind; struct IdentifierNode* identifier; struct FieldListNode* fields; -}; +} StructTypeDeclarationNode; -struct StructuredTypeExpressionNode { +typedef struct StructuredTypeExpressionNode { AstNodeKind kind; struct TypeIdentifierNode* type; struct FieldBindingListNode* field_bindings; -}; +} StructuredTypeExpressionNode; -struct TypeDeclarationNode { +typedef struct TypeDeclarationNode { AstNodeKind kind; union TypeDeclarationNodeUnion value; -}; +} TypeDeclarationNode; -struct TypeExpressionNode { +typedef struct TypeExpressionNode { AstNodeKind kind; union TypeExpressionNodeUnion value; -}; +} TypeExpressionNode; -struct TypeIdentifierNode { +typedef struct TypeIdentifierNode { AstNodeKind kind; struct IdentifierNode* identifier; struct TypeIdentifierNode* contained_type; -}; +} TypeIdentifierNode; -struct UnionTypeDeclarationNode { +typedef struct UnionTypeDeclarationNode { AstNodeKind kind; struct IdentifierNode* identifier; struct FieldListNode* fields; -}; +} UnionTypeDeclarationNode; /*************************/ diff --git a/std/compiler/parser/ast/ast_node.day b/std/compiler/parser/ast/ast_node.day index 3bc0574c..3c181839 100644 --- a/std/compiler/parser/ast/ast_node.day +++ b/std/compiler/parser/ast/ast_node.day @@ -1,5 +1,101 @@ -type AstNodeUnion union is +type AstNodeKind enum is + AstBlank + AstBindingStatement + AstCallExpression + AstDoStatement + AstDoubleExpression + AstEnumTypeDeclaration + AstEnumField + AstEnumFieldList + AstEnumTypeExpression + AstExpression + AstExpressionList + AstField + AstFieldBinding + AstFieldBindingList + AstFieldList + AstFunctionDeclaration + AstIdentifier + AstIdentifierExpression + AstImportStatement + AstIntegerExpression + AstLetBinding + AstListExpression + AstMatchCase + AstMatchCaseList + AstMatchStatement + AstModuleStatement + AstModuleStatementList + AstMutBinding + AstMutExpression + AstNewAlloc + AstNumericExpression + AstParameter + AstParameterList + AstProgram + AstReturnStatement + AstShebang + AstStatement + AstStatementList + AstStringExpression + AstStructTypeDeclaration + AstStructuredTypeExpression + AstTypeDeclaration + AstTypeExpression + AstTypeIdentifier + AstUnionTypeDeclaration +end +type AstNode struct is + kind: AstNodeKind + value: AstNodeUnion +end + +type AstNodeUnion union is + binding_statement: ptr + call_expression: ptr + do_statement: ptr + double_expression: ptr + enum_type_declaration: ptr + enum_field: ptr + enum_field_list: ptr + enum_type_expression: ptr + expression_list: ptr + expression: ptr + field_binding: ptr + field_binding_list: ptr + field: ptr + field_list: ptr + function_declaration: ptr + identifier: ptr + identifier_expression: ptr + integer_expression: ptr + import_statement: ptr + let_binding: ptr + list_expression: ptr + match_case: ptr + match_case_list: ptr + match_expression: ptr + module_statement_list: ptr + module_statement: ptr + mut_binding: ptr + mut_expression: ptr + new_alloc: ptr + numeric_expression: ptr + parameter_list: ptr + parameter: ptr + program: ptr + return_statement: ptr + shebang: ptr + statement: ptr + statement_list: ptr + string_expression: ptr + struct_type_declaration: ptr + structured_type_expression: ptr + type_declaration: ptr + type_expression: ptr + type_identifier: ptr + union_type_declaration: ptr end fun ast_new_node (kind: AstNodeKind value: AstNodeUnion) ptr do