Add let expressions

This commit is contained in:
Bruno BELANYI 2022-06-01 20:36:14 +02:00
parent 68aac9a0ae
commit 1449aa939f
5 changed files with 2843 additions and 1754 deletions

View file

@ -54,6 +54,7 @@ module.exports = grammar({
$.while_expression,
$.for_expression,
$.break_expression,
$.let_expression,
),
nil_literal: (_) => "nil",
@ -210,6 +211,29 @@ module.exports = grammar({
break_expression: (_) => "break",
let_expression: ($) => seq(
"let",
field("declarations", optional($._declaration_chunks)),
"in",
field("body", sepBy(";", $._expr)),
"end",
),
// }}}
// Declarations {{{
_declaration_chunks: ($) => repeat1($._declaration_chunk),
_declaration_chunk: ($) => choice(
$.import_declaration,
),
import_declaration: ($) => seq(
"import",
field("file", $.string_literal),
),
// }}}
}
});

View file

@ -73,6 +73,10 @@
{
"type": "SYMBOL",
"name": "break_expression"
},
{
"type": "SYMBOL",
"name": "let_expression"
}
]
},
@ -945,6 +949,109 @@
"break_expression": {
"type": "STRING",
"value": "break"
},
"let_expression": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "let"
},
{
"type": "FIELD",
"name": "declarations",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_declaration_chunks"
},
{
"type": "BLANK"
}
]
}
},
{
"type": "STRING",
"value": "in"
},
{
"type": "FIELD",
"name": "body",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_expr"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ";"
},
{
"type": "SYMBOL",
"name": "_expr"
}
]
}
}
]
},
{
"type": "BLANK"
}
]
}
},
{
"type": "STRING",
"value": "end"
}
]
},
"_declaration_chunks": {
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "_declaration_chunk"
}
},
"_declaration_chunk": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "import_declaration"
}
]
},
"import_declaration": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "import"
},
{
"type": "FIELD",
"name": "file",
"content": {
"type": "SYMBOL",
"name": "string_literal"
}
}
]
}
},
"extras": [

View file

@ -47,6 +47,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -121,6 +125,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -229,6 +237,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -327,6 +339,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -407,6 +423,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -491,6 +511,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -571,6 +595,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -645,6 +673,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -729,6 +761,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -813,6 +849,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -903,6 +943,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -977,6 +1021,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -1051,6 +1099,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -1083,6 +1135,120 @@
}
}
},
{
"type": "import_declaration",
"named": true,
"fields": {
"file": {
"multiple": false,
"required": true,
"types": [
{
"type": "string_literal",
"named": true
}
]
}
}
},
{
"type": "let_expression",
"named": true,
"fields": {
"body": {
"multiple": true,
"required": false,
"types": [
{
"type": ";",
"named": false
},
{
"type": "array_expression",
"named": true
},
{
"type": "array_value",
"named": true
},
{
"type": "assignment_expression",
"named": true
},
{
"type": "binary_expression",
"named": true
},
{
"type": "break_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": "let_expression",
"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
}
]
},
"declarations": {
"multiple": true,
"required": false,
"types": [
{
"type": "import_declaration",
"named": true
}
]
}
}
},
{
"type": "record_expression",
"named": true,
@ -1141,6 +1307,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -1265,6 +1435,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -1344,6 +1518,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -1438,6 +1616,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -1528,6 +1710,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -1602,6 +1788,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
@ -1686,6 +1876,10 @@
"type": "else",
"named": false
},
{
"type": "end",
"named": false
},
{
"type": "escape_sequence",
"named": true
@ -1702,10 +1896,22 @@
"type": "if",
"named": false
},
{
"type": "import",
"named": false
},
{
"type": "in",
"named": false
},
{
"type": "integer_literal",
"named": true
},
{
"type": "let",
"named": false
},
{
"type": "nil_literal",
"named": true

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,123 @@
================================================================================
Let expression
================================================================================
let
import "a.tih"
in
12
end
--------------------------------------------------------------------------------
(source_file
(let_expression
declarations: (import_declaration
file: (string_literal))
body: (integer_literal)))
================================================================================
Let multiple declarations
================================================================================
let
import "a.tih"
import "b.tih"
in
12
end
--------------------------------------------------------------------------------
(source_file
(let_expression
declarations: (import_declaration
file: (string_literal))
declarations: (import_declaration
file: (string_literal))
body: (integer_literal)))
================================================================================
Let multiple expressions
================================================================================
let
import "a.tih"
in
12;
27
end
--------------------------------------------------------------------------------
(source_file
(let_expression
declarations: (import_declaration
file: (string_literal))
body: (integer_literal)
body: (integer_literal)))
================================================================================
Let multiple expressions and declarations
================================================================================
let
import "a.tih"
import "b.tih"
in
12;
27
end
--------------------------------------------------------------------------------
(source_file
(let_expression
declarations: (import_declaration
file: (string_literal))
declarations: (import_declaration
file: (string_literal))
body: (integer_literal)
body: (integer_literal)))
================================================================================
Let empty declarations
================================================================================
let
in
42
end
--------------------------------------------------------------------------------
(source_file
(let_expression
body: (integer_literal)))
================================================================================
Let empty expressions
================================================================================
let
import "a.tih"
in
end
--------------------------------------------------------------------------------
(source_file
(let_expression
declarations: (import_declaration
file: (string_literal))))
================================================================================
Let empty
================================================================================
let in end
--------------------------------------------------------------------------------
(source_file
(let_expression))