Rework structs; Add enums; Remove yield statements

This commit is contained in:
Jeff 2024-02-14 20:23:33 -05:00
parent 390d1aa504
commit 4323c50d32
7 changed files with 30618 additions and 31123 deletions

View File

@ -0,0 +1,86 @@
================================================================================
Simple Enum
================================================================================
enum Foobar {
Foo,
Bar,
}
--------------------------------------------------------------------------------
(root
(statement
(type_definition
(enum_definition
(identifier)
(identifier)
(identifier)))))
================================================================================
Nested Enum
================================================================================
enum Foobar {
Foo(str),
Bar(enum BazBuff {
Baz,
Buff,
})
}
--------------------------------------------------------------------------------
(root
(statement
(type_definition
(enum_definition
(identifier)
(identifier)
(type)
(identifier)
(type_definition
(enum_definition
(identifier)
(identifier)
(identifier)))))))
================================================================================
Simple Enum Instance
================================================================================
new Foobar:Foo
--------------------------------------------------------------------------------
(root
(statement
(expression
(value
(enum_instance
(identifier)
(identifier))))))
================================================================================
Nested Enum Instance
================================================================================
new Foobar:Bar(new BazBuf:Baz(123))
--------------------------------------------------------------------------------
(root
(statement
(expression
(value
(enum_instance
(identifier)
(identifier)
(expression
(value
(enum_instance
(identifier)
(identifier)
(expression
(value
(integer)))))))))))

View File

@ -2,7 +2,7 @@
Simple Structure Simple Structure
================================================================================ ================================================================================
struct { struct Foo {
x <int> x <int>
y <float> y <float>
} }
@ -11,21 +11,21 @@ struct {
(root (root
(statement (statement
(expression (type_definition
(value (struct_definition
(structure (identifier)
(identifier) (identifier)
(type_specification (type_specification
(type)) (type))
(identifier) (identifier)
(type_specification (type_specification
(type))))))) (type))))))
================================================================================ ================================================================================
Complex Structure Nested Structure
================================================================================ ================================================================================
Foo = struct { struct Foo {
x <int> x <int>
y <float> = 0.0 y <float> = 0.0
@ -38,13 +38,9 @@ Foo = struct {
(root (root
(statement (statement
(assignment (type_definition
(struct_definition
(identifier) (identifier)
(assignment_operator)
(statement
(expression
(value
(structure
(identifier) (identifier)
(type_specification (type_specification
(type)) (type))
@ -61,10 +57,12 @@ Foo = struct {
(identifier))) (identifier)))
(statement (statement
(expression (expression
(new (value
(struct_instance
(identifier) (identifier)
(map
(identifier) (identifier)
(statement (statement
(expression (expression
(value (value
(integer)))))))))))))) (integer)))))))))))))

View File

@ -1,67 +0,0 @@
================================================================================
Simple Yield
================================================================================
1 -> output
--------------------------------------------------------------------------------
(root
(statement
(expression
(yield
(expression
(value
(integer)))
(function_expression
(value
(built_in_value)))))))
================================================================================
Yield Chain
================================================================================
x -> foo -> bar -> abc
--------------------------------------------------------------------------------
(root
(statement
(expression
(yield
(expression
(yield
(expression
(yield
(expression
(identifier))
(function_expression
(identifier))))
(function_expression
(identifier))))
(function_expression
(identifier))))))
================================================================================
Yielded Function Call
================================================================================
x -> foo(1)()
--------------------------------------------------------------------------------
(root
(statement
(expression
(function_call
(function_expression
(function_call
(function_expression
(yield
(expression
(identifier))
(function_expression
(identifier))))
(expression
(value
(integer)))))))))

View File

@ -25,6 +25,7 @@ module.exports = grammar({
$.return, $.return,
$.pipe, $.pipe,
$.while, $.while,
$.type_definition,
), ),
optional(';'), optional(';'),
), ),
@ -50,8 +51,6 @@ module.exports = grammar({
$.logic, $.logic,
$.math, $.math,
$.value, $.value,
$.yield,
$.new,
$.command, $.command,
), ),
), ),
@ -125,61 +124,13 @@ module.exports = grammar({
$.map, $.map,
$.option, $.option,
$.built_in_value, $.built_in_value,
$.structure,
$.range, $.range,
$.struct_instance,
$.enum_instance,
), ),
range: $ => /\d+[.]{2}\d+/, range: $ => /\d+[.]{2}\d+/,
structure: $ =>
seq(
'struct',
'{',
repeat(
choice(
seq(
$.identifier,
$.type_specification,
),
seq(
$.identifier,
'=',
$.statement,
),
seq(
$.identifier,
$.type_specification,
'=',
$.statement,
),
),
),
'}',
),
new: $ =>
seq(
'new',
$.identifier,
'{',
repeat(
choice(
seq(
$.identifier,
'=',
$.statement,
),
seq(
$.identifier,
$.type_specification,
'=',
$.statement,
),
),
),
'}',
),
integer: $ => /[-]?\d+/, integer: $ => /[-]?\d+/,
float: $ => float: $ =>
@ -453,7 +404,6 @@ module.exports = grammar({
$.identifier, $.identifier,
$.index, $.index,
$.value, $.value,
$.yield,
), ),
), ),
@ -467,22 +417,85 @@ module.exports = grammar({
), ),
), ),
yield: $ => type_definition: $ =>
prec.left( choice(
1, $.enum_definition,
$.struct_definition,
),
enum_definition: $ =>
prec.right(
seq( seq(
$.expression, 'enum',
'->', $.identifier,
$.function_expression, repeat(
seq(
'{',
repeat1(
seq(
$.identifier,
optional( optional(
seq( seq(
'(', '(',
$._expression_list, choice(
$.type,
$.type_definition,
),
')', ')',
), ),
), ),
optional(','),
), ),
), ),
'}',
),
),
),
),
enum_instance: $ =>
prec.right(
seq(
'new',
$.identifier,
':',
$.identifier,
optional(
seq('(', $.expression, ')'),
),
),
),
struct_definition: $ =>
seq(
'struct',
$.identifier,
'{',
repeat(
choice(
seq(
$.identifier,
$.type_specification,
),
seq(
$.identifier,
'=',
$.statement,
),
seq(
$.identifier,
$.type_specification,
'=',
$.statement,
),
),
),
'}',
),
struct_instance: $ =>
seq('new', $.identifier, $.map),
built_in_value: $ => built_in_value: $ =>
choice( choice(

View File

@ -65,6 +65,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "while" "name": "while"
},
{
"type": "SYMBOL",
"name": "type_definition"
} }
] ]
}, },
@ -143,14 +147,6 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "value" "name": "value"
}, },
{
"type": "SYMBOL",
"name": "yield"
},
{
"type": "SYMBOL",
"name": "new"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "command" "name": "command"
@ -364,11 +360,15 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "structure" "name": "range"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "range" "name": "struct_instance"
},
{
"type": "SYMBOL",
"name": "enum_instance"
} }
] ]
}, },
@ -376,149 +376,6 @@
"type": "PATTERN", "type": "PATTERN",
"value": "\\d+[.]{2}\\d+" "value": "\\d+[.]{2}\\d+"
}, },
"structure": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "struct"
},
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "type_specification"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": "="
},
{
"type": "SYMBOL",
"name": "statement"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "type_specification"
},
{
"type": "STRING",
"value": "="
},
{
"type": "SYMBOL",
"name": "statement"
}
]
}
]
}
},
{
"type": "STRING",
"value": "}"
}
]
},
"new": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "new"
},
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": "="
},
{
"type": "SYMBOL",
"name": "statement"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "type_specification"
},
{
"type": "STRING",
"value": "="
},
{
"type": "SYMBOL",
"name": "statement"
}
]
}
]
}
},
{
"type": "STRING",
"value": "}"
}
]
},
"integer": { "integer": {
"type": "PATTERN", "type": "PATTERN",
"value": "[-]?\\d+" "value": "[-]?\\d+"
@ -1406,10 +1263,6 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "value" "name": "value"
},
{
"type": "SYMBOL",
"name": "yield"
} }
] ]
} }
@ -1447,23 +1300,131 @@
] ]
} }
}, },
"yield": { "type_definition": {
"type": "PREC_LEFT", "type": "CHOICE",
"value": 1, "members": [
{
"type": "SYMBOL",
"name": "enum_definition"
},
{
"type": "SYMBOL",
"name": "struct_definition"
}
]
},
"enum_definition": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "enum"
},
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT1",
"content": { "content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "expression" "name": "identifier"
}, },
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{ {
"type": "STRING", "type": "STRING",
"value": "->" "value": "("
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "type"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "function_expression" "name": "type_definition"
}
]
},
{
"type": "STRING",
"value": ")"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
},
{
"type": "STRING",
"value": "}"
}
]
}
}
]
}
},
"enum_instance": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "new"
},
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": ":"
},
{
"type": "SYMBOL",
"name": "identifier"
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
@ -1477,7 +1438,7 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_expression_list" "name": "expression"
}, },
{ {
"type": "STRING", "type": "STRING",
@ -1493,6 +1454,103 @@
] ]
} }
}, },
"struct_definition": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "struct"
},
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "type_specification"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": "="
},
{
"type": "SYMBOL",
"name": "statement"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "type_specification"
},
{
"type": "STRING",
"value": "="
},
{
"type": "SYMBOL",
"name": "statement"
}
]
}
]
}
},
{
"type": "STRING",
"value": "}"
}
]
},
"struct_instance": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "new"
},
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "map"
}
]
},
"built_in_value": { "built_in_value": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [

View File

@ -133,6 +133,48 @@
] ]
} }
}, },
{
"type": "enum_definition",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "identifier",
"named": true
},
{
"type": "type",
"named": true
},
{
"type": "type_definition",
"named": true
}
]
}
},
{
"type": "enum_instance",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "identifier",
"named": true
}
]
}
},
{ {
"type": "expression", "type": "expression",
"named": true, "named": true,
@ -169,17 +211,9 @@
"type": "math", "type": "math",
"named": true "named": true
}, },
{
"type": "new",
"named": true
},
{ {
"type": "value", "type": "value",
"named": true "named": true
},
{
"type": "yield",
"named": true
} }
] ]
} }
@ -277,10 +311,6 @@
{ {
"type": "value", "type": "value",
"named": true "named": true
},
{
"type": "yield",
"named": true
} }
] ]
} }
@ -501,29 +531,6 @@
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{
"type": "new",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "identifier",
"named": true
},
{
"type": "statement",
"named": true
},
{
"type": "type_specification",
"named": true
}
]
}
},
{ {
"type": "option", "type": "option",
"named": true, "named": true,
@ -636,6 +643,10 @@
"type": "return", "type": "return",
"named": true "named": true
}, },
{
"type": "type_definition",
"named": true
},
{ {
"type": "while", "type": "while",
"named": true "named": true
@ -649,12 +660,12 @@
"fields": {} "fields": {}
}, },
{ {
"type": "structure", "type": "struct_definition",
"named": true, "named": true,
"fields": {}, "fields": {},
"children": { "children": {
"multiple": true, "multiple": true,
"required": false, "required": true,
"types": [ "types": [
{ {
"type": "identifier", "type": "identifier",
@ -671,6 +682,25 @@
] ]
} }
}, },
{
"type": "struct_instance",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "identifier",
"named": true
},
{
"type": "map",
"named": true
}
]
}
},
{ {
"type": "type", "type": "type",
"named": true, "named": true,
@ -690,6 +720,25 @@
] ]
} }
}, },
{
"type": "type_definition",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "enum_definition",
"named": true
},
{
"type": "struct_definition",
"named": true
}
]
}
},
{ {
"type": "type_specification", "type": "type_specification",
"named": true, "named": true,
@ -721,6 +770,10 @@
"type": "built_in_value", "type": "built_in_value",
"named": true "named": true
}, },
{
"type": "enum_instance",
"named": true
},
{ {
"type": "float", "type": "float",
"named": true "named": true
@ -754,7 +807,7 @@
"named": true "named": true
}, },
{ {
"type": "structure", "type": "struct_instance",
"named": true "named": true
} }
] ]
@ -779,25 +832,6 @@
] ]
} }
}, },
{
"type": "yield",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "function_expression",
"named": true
}
]
}
},
{ {
"type": "!=", "type": "!=",
"named": false "named": false
@ -950,6 +984,10 @@
"type": "else if", "type": "else if",
"named": false "named": false
}, },
{
"type": "enum",
"named": false
},
{ {
"type": "env", "type": "env",
"named": false "named": false

File diff suppressed because it is too large Load Diff