Add let expressions
This commit is contained in:
parent
c04041712d
commit
44ea60a191
@ -138,6 +138,23 @@ or = x \/ y;
|
||||
(assignment (identifier) (infix_operator (identifier) (identifier)))
|
||||
(assignment (identifier) (infix_operator (identifier) (identifier))))
|
||||
|
||||
==============
|
||||
Let Expression
|
||||
==============
|
||||
|
||||
empty = let {} in 1;
|
||||
var_offset = let {var int: a;} in a + 3;
|
||||
constrained = let {constraint x = y} in x;
|
||||
multiple = let {var int: a, var int: b} in a + b;
|
||||
|
||||
---
|
||||
|
||||
(source_file
|
||||
(assignment (identifier) (let_expression (integer_literal)))
|
||||
(assignment (identifier) (let_expression (declaration (base_type (primitive_type)) (identifier)) (infix_operator (identifier) (integer_literal))))
|
||||
(assignment (identifier) (let_expression (constraint (infix_operator (identifier) (identifier))) (identifier)))
|
||||
(assignment (identifier) (let_expression (declaration (base_type (primitive_type)) (identifier)) (declaration (base_type (primitive_type)) (identifier)) (infix_operator (identifier) (identifier)))))
|
||||
|
||||
===========
|
||||
Precedences
|
||||
===========
|
||||
|
14
grammar.js
14
grammar.js
@ -89,6 +89,7 @@ module.exports = grammar({
|
||||
$.if_then_else,
|
||||
$.indexed_access,
|
||||
$.infix_operator,
|
||||
$.let_expression,
|
||||
$.prefix_operator,
|
||||
$.set_comprehension,
|
||||
$.string_interpolation,
|
||||
@ -193,6 +194,19 @@ module.exports = grammar({
|
||||
);
|
||||
},
|
||||
|
||||
let_expression: ($) =>
|
||||
seq(
|
||||
"let",
|
||||
"{",
|
||||
field(
|
||||
"let",
|
||||
sepBy(choice(",", ";"), choice($.declaration, $.constraint))
|
||||
),
|
||||
"}",
|
||||
"in",
|
||||
field("in", $._expression)
|
||||
),
|
||||
|
||||
prefix_operator: ($) =>
|
||||
prec(
|
||||
PREC.unary,
|
||||
|
97
src/grammar.json
vendored
97
src/grammar.json
vendored
@ -266,6 +266,10 @@
|
||||
"type": "SYMBOL",
|
||||
"name": "infix_operator"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "let_expression"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "prefix_operator"
|
||||
@ -1245,6 +1249,99 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"let_expression": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "let"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "{"
|
||||
},
|
||||
{
|
||||
"type": "FIELD",
|
||||
"name": "let",
|
||||
"content": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "declaration"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "constraint"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": ","
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": ";"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "declaration"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "constraint"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "}"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "in"
|
||||
},
|
||||
{
|
||||
"type": "FIELD",
|
||||
"name": "in",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "_expression"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"prefix_operator": {
|
||||
"type": "PREC",
|
||||
"value": 13,
|
||||
|
46
src/node-types.json
vendored
46
src/node-types.json
vendored
@ -51,6 +51,10 @@
|
||||
"type": "integer_literal",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "let_expression",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "parenthesised_expression",
|
||||
"named": true
|
||||
@ -640,6 +644,44 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "let_expression",
|
||||
"named": true,
|
||||
"fields": {
|
||||
"in": {
|
||||
"multiple": false,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "_expression",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"let": {
|
||||
"multiple": true,
|
||||
"required": false,
|
||||
"types": [
|
||||
{
|
||||
"type": ",",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": ";",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "constraint",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "declaration",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "output",
|
||||
"named": true,
|
||||
@ -1012,6 +1054,10 @@
|
||||
"type": "intersect",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "let",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "maximize",
|
||||
"named": false
|
||||
|
15130
src/parser.c
vendored
15130
src/parser.c
vendored
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user