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, $.if_expression,
$.while_expression, $.while_expression,
$.for_expression,
), ),
nil_literal: (_) => "nil", nil_literal: (_) => "nil",
@ -192,6 +193,17 @@ module.exports = grammar({
"do", "do",
field("body", $._expr), 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", "type": "SYMBOL",
"name": "while_expression" "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": [ "extras": [

View file

@ -23,6 +23,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -89,6 +93,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -189,6 +197,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -279,6 +291,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -351,6 +367,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -427,6 +447,236 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "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", "type": "function_call",
"named": true "named": true
@ -503,6 +753,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -585,6 +839,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -651,6 +909,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -717,6 +979,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -799,6 +1065,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -915,6 +1185,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -986,6 +1260,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -1072,6 +1350,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -1154,6 +1436,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -1220,6 +1506,10 @@
"type": "binary_expression", "type": "binary_expression",
"named": true "named": true
}, },
{
"type": "for_expression",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -1320,6 +1610,10 @@
"type": "escape_sequence", "type": "escape_sequence",
"named": true "named": true
}, },
{
"type": "for",
"named": false
},
{ {
"type": "identifier", "type": "identifier",
"named": true "named": true
@ -1348,6 +1642,10 @@
"type": "then", "type": "then",
"named": false "named": false
}, },
{
"type": "to",
"named": false
},
{ {
"type": "while", "type": "while",
"named": false "named": false

File diff suppressed because it is too large Load diff

View file

@ -53,3 +53,61 @@ while 12 do 27
(while_expression (while_expression
condition: (integer_literal) condition: (integer_literal)
body: (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))