Add if expressions

This commit is contained in:
Bruno BELANYI 2022-06-01 20:18:10 +02:00
parent 93cd163707
commit 19c1f11414
5 changed files with 1661 additions and 913 deletions

View file

@ -47,6 +47,8 @@ module.exports = grammar({
$.sequence_expression,
$.assignment_expression,
$.if_expression,
),
nil_literal: (_) => "nil",
@ -167,6 +169,21 @@ module.exports = grammar({
field("right", $._expr),
),
),
if_expression: ($) => prec.right(
seq(
"if",
field("condition", $._expr),
"then",
field("consequence", $._expr),
optional(
seq(
"else",
field("alternative", $._expr),
),
),
),
),
}
});

View file

@ -57,6 +57,10 @@
{
"type": "SYMBOL",
"name": "assignment_expression"
},
{
"type": "SYMBOL",
"name": "if_expression"
}
]
},
@ -785,6 +789,64 @@
}
]
}
},
"if_expression": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "if"
},
{
"type": "FIELD",
"name": "condition",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
},
{
"type": "STRING",
"value": "then"
},
{
"type": "FIELD",
"name": "consequence",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else"
},
{
"type": "FIELD",
"name": "alternative",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
}
]
},
{
"type": "BLANK"
}
]
}
]
}
}
},
"extras": [

View file

@ -31,6 +31,10 @@
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
@ -89,6 +93,10 @@
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
@ -181,6 +189,10 @@
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
@ -263,6 +275,10 @@
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
@ -327,6 +343,10 @@
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
@ -395,6 +415,10 @@
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
@ -463,6 +487,10 @@
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
@ -505,6 +533,198 @@
}
}
},
{
"type": "if_expression",
"named": true,
"fields": {
"alternative": {
"multiple": false,
"required": false,
"types": [
{
"type": "array_expression",
"named": true
},
{
"type": "array_value",
"named": true
},
{
"type": "assignment_expression",
"named": true
},
{
"type": "binary_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
}
]
},
"condition": {
"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": "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
}
]
},
"consequence": {
"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": "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": "record_expression",
"named": true,
@ -547,6 +767,10 @@
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
@ -655,6 +879,10 @@
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
@ -718,6 +946,10 @@
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
@ -796,6 +1028,10 @@
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
@ -878,6 +1114,10 @@
"type": "]",
"named": false
},
{
"type": "else",
"named": false
},
{
"type": "escape_sequence",
"named": true
@ -886,6 +1126,10 @@
"type": "identifier",
"named": true
},
{
"type": "if",
"named": false
},
{
"type": "integer_literal",
"named": true
@ -902,6 +1146,10 @@
"type": "operator",
"named": true
},
{
"type": "then",
"named": false
},
{
"type": "{",
"named": false

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,42 @@
================================================================================
If then else
================================================================================
if 12 then 27 else 42
--------------------------------------------------------------------------------
(source_file
(if_expression
condition: (integer_literal)
consequence: (integer_literal)
alternative: (integer_literal)))
================================================================================
If then
================================================================================
if 12 then 27
--------------------------------------------------------------------------------
(source_file
(if_expression
condition: (integer_literal)
consequence: (integer_literal)))
================================================================================
Dangling else
================================================================================
if 12 then if 27 then 42 else "nope"
--------------------------------------------------------------------------------
(source_file
(if_expression
condition: (integer_literal)
consequence: (if_expression
condition: (integer_literal)
consequence: (integer_literal)
alternative: (string_literal))))