Fix tests for new grammar

This commit is contained in:
Jeff 2023-10-01 01:13:29 -04:00
parent 17ba01c0dc
commit 8c05154e43
9 changed files with 10824 additions and 7569 deletions

View File

@ -9,17 +9,15 @@ if true then "True"
(root (root
(item (item
(statement (statement
(open_statement (expression
(expression (control_flow
(control_flow (expression
(value
(boolean)))
(statement
(expression (expression
(value (value
(boolean))) (string)))))))))
(statement
(open_statement
(expression
(value
(string)))))))))))
================== ==================
@ -33,20 +31,16 @@ x = if true then 1
(root (root
(item (item
(statement (statement
(open_statement (expression
(expression (assignment
(operation (identifier)
(expression (expression
(identifier)) (control_flow
(operator) (expression
(expression (value
(control_flow (boolean)))
(statement
(expression (expression
(value (value
(boolean))) (integer)))))))))))
(statement
(open_statement
(expression
(value
(integer)))))))))))))

View File

@ -10,15 +10,13 @@ function { "Hiya" }
(root (root
(item (item
(statement (statement
(open_statement (expression
(expression (value
(value (function
(function (statement
(statement (expression
(open_statement (value
(expression (string))))))))))
(value
(string))))))))))))
================== ==================
Function Call Function Call
@ -31,12 +29,11 @@ foobar <"Hiya">
(root (root
(item (item
(statement (statement
(open_statement (expression
(expression (function_call
(function_call (identifier)
(identifier) (value
(value (string)))))))
(string))))))))
================== ==================
Complex Function Complex Function
@ -52,25 +49,20 @@ function <message, number> {
(root (root
(item (item
(statement (statement
(open_statement (expression
(expression (value
(value (function
(function (identifier)
(identifier) (identifier)
(identifier) (statement
(statement (expression
(open_statement (identifier)))
(expression (statement
(identifier)))) (expression
(statement (identifier)))
(open_statement (statement
(expression (expression
(identifier)))) (identifier)))
(statement (statement
(open_statement (expression
(expression (identifier)))))))))
(identifier))))
(statement
(open_statement
(expression
(identifier)))))))))))

View File

@ -9,14 +9,13 @@ List Declaration
(root (root
(item (item
(statement (statement
(open_statement (expression
(expression (value
(value (list
(list (value
(value (string))
(string)) (value
(value (integer))))))))
(integer)))))))))
================== ==================
List Assignment List Assignment
@ -29,76 +28,16 @@ foobar = ['answer', 42]
(root (root
(item (item
(statement (statement
(open_statement (expression
(expression (assignment
(operation (identifier)
(expression (expression
(identifier)) (value
(operator) (list
(expression (value
(value (string))
(list (value
(value (integer))))))))))
(string))
(value
(integer)))))))))))
==================
List Access
==================
x = foobar.0
---
(root
(item
(statement
(open_statement
(expression
(operation
(expression
(identifier))
(operator)
(expression
(identifier))))))))
==================
List Mutation
==================
foobar = ['answer', 42]
foobar += 'hiya'
---
(root
(item
(statement
(open_statement
(expression
(operation
(expression
(identifier))
(operator)
(expression
(value
(list
(value
(string))
(value
(integer))))))))))
(item
(statement
(open_statement
(expression
(operation
(expression
(identifier))
(operator)
(expression
(value
(string)))))))))
================== ==================
List Nesting List Nesting
@ -111,22 +50,19 @@ foobar = ['answers', [42, [666]]]
(root (root
(item (item
(statement (statement
(open_statement (expression
(expression (assignment
(operation (identifier)
(expression (expression
(identifier)) (value
(operator) (list
(expression (value
(value (string))
(list (value
(value (list
(string)) (value
(value (integer))
(list (value
(value (list
(integer)) (value
(value (integer))))))))))))))
(list
(value
(integer)))))))))))))))

View File

@ -1,5 +1,5 @@
================== ==================
Simple Maps Simple Map
================== ==================
map { map {
@ -11,13 +11,12 @@ map {
(root (root
(item (item
(statement (statement
(open_statement (expression
(expression (value
(value (map
(map (identifier)
(identifier) (value
(value (integer))))))))
(integer)))))))))
================== ==================
Map Assignment Map Assignment
@ -32,48 +31,27 @@ x = map {
(root (root
(item (item
(statement (statement
(open_statement (expression
(expression (assignment
(operation (identifier)
(expression (expression
(identifier)) (value
(operator) (map
(expression (identifier)
(value (value
(map (integer))))))))))
(identifier)
(value
(integer)))))))))))
================== ==================
Map Access Map Access
================== ==================
x = map {
answer = 42
}
x.answer x.answer
--- ---
(root (root
(item (item
(statement (statement
(open_statement
(expression (expression
(operation (identifier)))))
(expression
(identifier))
(operator)
(expression
(value
(map
(identifier)
(value
(integer))))))))))
(item
(statement
(open_statement
(expression
(identifier))))))

34
examples/fizzbuzz.ds Normal file
View File

@ -0,0 +1,34 @@
fizzbuzz_basic = function <limit> {
count = 1
while count < limit {
count += 1
if count % 3 == 0 && count % 5 == 0 {
output 'fizzbuzz'
} else {
if count % 3 == 0 {
output 'fizz'
}
if count % 5 == 0 {
output 'buzz'
}
}
}
}
fizzbuzz_match <limit> {
count = 1
while count < 1 {
output match [count % 3 == 0, count % 5 == 0] {
[true, false] => 'fizz'
[false, true] => 'buzz'
[true, true] => 'fizzbuzz'
}
}
}
fizzbuzz_basic<15>
fizzbuzz_match<15>

View File

@ -35,6 +35,8 @@ module.exports = grammar({
$.assignment, $.assignment,
$.math, $.math,
$.logic, $.logic,
$.loop,
$.match,
)), )),
identifier: $ => /[a-z|_|.]+[0-9]?/, identifier: $ => /[a-z|_|.]+[0-9]?/,
@ -56,8 +58,6 @@ module.exports = grammar({
string: $ => /(".*?")|('.*?')|(`.*?`)/, string: $ => /(".*?")|('.*?')|(`.*?`)/,
empty: $ => '()',
boolean: $ => choice( boolean: $ => choice(
'true', 'true',
'false', 'false',
@ -162,5 +162,33 @@ module.exports = grammar({
), ),
'>', '>',
)), )),
loop: $ => choice(
$.break_loop,
$.while_loop,
),
while_loop: $ => seq(
'while',
$.expression,
'{',
$.statement,
'}',
),
break_loop: $ => seq(
'loop',
'{',
$.statement,
'}',
),
match: $ => seq(
'match',
$.expression,
'{',
repeat1(seq($.expression, '=>', $.statement)),
'}',
),
} }
}); });

View File

@ -143,6 +143,14 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "logic" "name": "logic"
},
{
"type": "SYMBOL",
"name": "loop"
},
{
"type": "SYMBOL",
"name": "match"
} }
] ]
} }
@ -204,10 +212,6 @@
"type": "PATTERN", "type": "PATTERN",
"value": "(\".*?\")|('.*?')|(`.*?`)" "value": "(\".*?\")|('.*?')|(`.*?`)"
}, },
"empty": {
"type": "STRING",
"value": "()"
},
"boolean": { "boolean": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -728,6 +732,106 @@
} }
] ]
} }
},
"loop": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "break_loop"
},
{
"type": "SYMBOL",
"name": "while_loop"
}
]
},
"while_loop": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "while"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": "{"
},
{
"type": "SYMBOL",
"name": "statement"
},
{
"type": "STRING",
"value": "}"
}
]
},
"break_loop": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "loop"
},
{
"type": "STRING",
"value": "{"
},
{
"type": "SYMBOL",
"name": "statement"
},
{
"type": "STRING",
"value": "}"
}
]
},
"match": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "match"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": "=>"
},
{
"type": "SYMBOL",
"name": "statement"
}
]
}
},
{
"type": "STRING",
"value": "}"
}
]
} }
}, },
"extras": [ "extras": [

View File

@ -23,6 +23,21 @@
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{
"type": "break_loop",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "statement",
"named": true
}
]
}
},
{ {
"type": "comment", "type": "comment",
"named": true, "named": true,
@ -79,6 +94,14 @@
"type": "logic", "type": "logic",
"named": true "named": true
}, },
{
"type": "loop",
"named": true
},
{
"type": "match",
"named": true
},
{ {
"type": "math", "type": "math",
"named": true "named": true
@ -213,6 +236,25 @@
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{
"type": "loop",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "break_loop",
"named": true
},
{
"type": "while_loop",
"named": true
}
]
}
},
{ {
"type": "map", "type": "map",
"named": true, "named": true,
@ -232,6 +274,25 @@
] ]
} }
}, },
{
"type": "match",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "statement",
"named": true
}
]
}
},
{ {
"type": "math", "type": "math",
"named": true, "named": true,
@ -371,6 +432,25 @@
] ]
} }
}, },
{
"type": "while_loop",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "statement",
"named": true
}
]
}
},
{ {
"type": "yield", "type": "yield",
"named": true, "named": true,
@ -450,6 +530,10 @@
"type": "==", "type": "==",
"named": false "named": false
}, },
{
"type": "=>",
"named": false
},
{ {
"type": ">", "type": ">",
"named": false "named": false
@ -506,10 +590,18 @@
"type": "into", "type": "into",
"named": false "named": false
}, },
{
"type": "loop",
"named": false
},
{ {
"type": "map", "type": "map",
"named": false "named": false
}, },
{
"type": "match",
"named": false
},
{ {
"type": "or", "type": "or",
"named": false "named": false
@ -538,6 +630,10 @@
"type": "where", "type": "where",
"named": false "named": false
}, },
{
"type": "while",
"named": false
},
{ {
"type": "{", "type": "{",
"named": false "named": false

17819
src/parser.c

File diff suppressed because it is too large Load Diff