Add for expressions

This commit is contained in:
Bruno BELANYI 2022-06-01 20:23:04 +02:00
parent 26999482a3
commit bb6875a11d
5 changed files with 1777 additions and 1006 deletions

View file

@ -50,6 +50,7 @@ module.exports = grammar({
$.if_expression,
$.while_expression,
$.for_expression,
),
nil_literal: (_) => "nil",
@ -192,6 +193,17 @@ module.exports = grammar({
"do",
field("body", $._expr),
),
for_expression: ($) => seq(
"for",
field("index", $.identifier),
":=",
field("start", $._expr),
"to",
field("end", $._expr),
"do",
field("body", $._expr),
),
}
});

View file

@ -65,6 +65,10 @@
{
"type": "SYMBOL",
"name": "while_expression"
},
{
"type": "SYMBOL",
"name": "for_expression"
}
]
},
@ -880,6 +884,59 @@
}
}
]
},
"for_expression": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "for"
},
{
"type": "FIELD",
"name": "index",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "STRING",
"value": ":="
},
{
"type": "FIELD",
"name": "start",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
},
{
"type": "STRING",
"value": "to"
},
{
"type": "FIELD",
"name": "end",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
},
{
"type": "STRING",
"value": "do"
},
{
"type": "FIELD",
"name": "body",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
}
]
}
},
"extras": [

View file

@ -23,6 +23,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -89,6 +93,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -189,6 +197,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -279,6 +291,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -351,6 +367,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -427,6 +447,236 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "nil_literal",
"named": true
},
{
"type": "record_expression",
"named": true
},
{
"type": "record_value",
"named": true
},
{
"type": "sequence_expression",
"named": true
},
{
"type": "string_literal",
"named": true
},
{
"type": "unary_expression",
"named": true
},
{
"type": "while_expression",
"named": true
}
]
}
}
},
{
"type": "for_expression",
"named": true,
"fields": {
"body": {
"multiple": false,
"required": true,
"types": [
{
"type": "array_expression",
"named": true
},
{
"type": "array_value",
"named": true
},
{
"type": "assignment_expression",
"named": true
},
{
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "nil_literal",
"named": true
},
{
"type": "record_expression",
"named": true
},
{
"type": "record_value",
"named": true
},
{
"type": "sequence_expression",
"named": true
},
{
"type": "string_literal",
"named": true
},
{
"type": "unary_expression",
"named": true
},
{
"type": "while_expression",
"named": true
}
]
},
"end": {
"multiple": false,
"required": true,
"types": [
{
"type": "array_expression",
"named": true
},
{
"type": "array_value",
"named": true
},
{
"type": "assignment_expression",
"named": true
},
{
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "nil_literal",
"named": true
},
{
"type": "record_expression",
"named": true
},
{
"type": "record_value",
"named": true
},
{
"type": "sequence_expression",
"named": true
},
{
"type": "string_literal",
"named": true
},
{
"type": "unary_expression",
"named": true
},
{
"type": "while_expression",
"named": true
}
]
},
"index": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"start": {
"multiple": false,
"required": true,
"types": [
{
"type": "array_expression",
"named": true
},
{
"type": "array_value",
"named": true
},
{
"type": "assignment_expression",
"named": true
},
{
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -503,6 +753,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -585,6 +839,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -651,6 +909,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -717,6 +979,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -799,6 +1065,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -915,6 +1185,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -986,6 +1260,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -1072,6 +1350,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -1154,6 +1436,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -1220,6 +1506,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
@ -1320,6 +1610,10 @@
"type": "escape_sequence",
"named": true
},
{
"type": "for",
"named": false
},
{
"type": "identifier",
"named": true
@ -1348,6 +1642,10 @@
"type": "then",
"named": false
},
{
"type": "to",
"named": false
},
{
"type": "while",
"named": false

File diff suppressed because it is too large Load diff

View file

@ -53,3 +53,61 @@ while 12 do 27
(while_expression
condition: (integer_literal)
body: (integer_literal)))
================================================================================
For
================================================================================
for i := 12 to nil do "42"
--------------------------------------------------------------------------------
(source_file
(for_expression
index: (identifier)
start: (integer_literal)
end: (nil_literal)
body: (string_literal)))
================================================================================
For no index
================================================================================
for 12 to nil do "42"
--------------------------------------------------------------------------------
(source_file
(ERROR
(identifier)
(identifier)
(nil_literal))
(string_literal))
================================================================================
For no end
================================================================================
for 12 do "42"
--------------------------------------------------------------------------------
(source_file
(ERROR
(identifier)
(identifier))
(string_literal))
================================================================================
For no body
================================================================================
for 12 to nil
--------------------------------------------------------------------------------
(source_file
(ERROR
(identifier)
(identifier))
(nil_literal))