1
0

Allow wrapping expressions in parentheses

This commit is contained in:
Jeff 2023-12-29 21:53:26 -05:00
parent f3921ba87c
commit f486d87976
9 changed files with 13991 additions and 12805 deletions

View File

@ -5,9 +5,9 @@ cards = {
} }
is_ready_to_solve = (cards <map>) <bool> { is_ready_to_solve = (cards <map>) <bool> {
length(cards:suspects) == 1 (length(cards:suspects) == 1)
&& length(cards:rooms) == 1 && (length(cards:rooms) == 1)
&& length(cards:weapons) == 1 && (length(cards:weapons) == 1)
} }
remove_card = (cards <map>, opponent_card <str>) <none> { remove_card = (cards <map>, opponent_card <str>) <none> {
@ -19,7 +19,7 @@ remove_card = (cards <map>, opponent_card <str>) <none> {
make_guess = (cards <map>, current_room <str>) <none> { make_guess = (cards <map>, current_room <str>) <none> {
if is_ready_to_solve(cards) { if is_ready_to_solve(cards) {
output( output(
'It was ' 'I accuse '
+ cards:suspects:0 + cards:suspects:0
+ ' in the ' + ' in the '
+ cards:rooms:0 + cards:rooms:0
@ -29,7 +29,7 @@ make_guess = (cards <map>, current_room <str>) <none> {
) )
} else { } else {
output( output(
'I accuse ' 'I question '
+ random(cards:suspects) + random(cards:suspects)
+ ' in the ' + ' in the '
+ current_room + current_room

View File

@ -1,4 +1,4 @@
data = (from_json (read 'examples/assets/jq_data.json')) data = from_json(read('examples/assets/jq_data.json'))
new_data = [] new_data = []

View File

@ -34,27 +34,11 @@ if 1 == 1 && 2 == 2 && 3 == 3 { "True" }
(expression (expression
(logic (logic
(expression (expression
(value (logic
(integer)))
(logic_operator)
(expression (expression
(logic (logic
(expression
(value
(integer)))
(logic_operator)
(expression (expression
(logic (logic
(expression
(value
(integer)))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression (expression
(logic (logic
(expression (expression
@ -63,7 +47,23 @@ if 1 == 1 && 2 == 2 && 3 == 3 { "True" }
(logic_operator) (logic_operator)
(expression (expression
(value (value
(integer))))))))))))) (integer)))))
(logic_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(value
(integer)))))
(block (block
(statement (statement
(expression (expression

View File

@ -0,0 +1,199 @@
================================================================================
Simple Logic
================================================================================
true && false
--------------------------------------------------------------------------------
(root
(statement
(expression
(logic
(expression
(value
(boolean)))
(logic_operator)
(expression
(value
(boolean)))))))
================================================================================
Logic Sequence
================================================================================
1 == 2 || false
--------------------------------------------------------------------------------
(root
(statement
(expression
(logic
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(value
(boolean)))))))
================================================================================
Complex Logic Sequence
================================================================================
(length(suspects) == 1)
&& (length(rooms) == 1)
&& (length(weapons) == 1)
--------------------------------------------------------------------------------
(root
(statement
(expression
(logic
(expression
(logic
(expression
(logic
(expression
(function_call
(function_expression
(identifier
(built_in_function)))
(expression
(identifier))))
(logic_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(logic
(expression
(function_call
(function_expression
(identifier
(built_in_function)))
(expression
(identifier))))
(logic_operator)
(expression
(value
(integer)))))))
(logic_operator)
(expression
(logic
(expression
(function_call
(function_expression
(identifier
(built_in_function)))
(expression
(identifier))))
(logic_operator)
(expression
(value
(integer)))))))))
================================================================================
\==
================================================================================
3 == 1 + 1 + 1
--------------------------------------------------------------------------------
(root
(statement
(expression
(math
(expression
(math
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(integer)))))
(math_operator)
(expression
(value
(integer)))))
(math_operator)
(expression
(value
(integer)))))))
================================================================================
&&
================================================================================
4 + 2 == 42 && true
--------------------------------------------------------------------------------
(root
(statement
(expression
(logic
(expression
(logic
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(value
(boolean)))))))
================================================================================
\||
================================================================================
4 + 2 == 42 || true
--------------------------------------------------------------------------------
(root
(statement
(expression
(logic
(expression
(logic
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(value
(boolean)))))))

View File

@ -1,95 +0,0 @@
================================================================================
\==
================================================================================
3 == 1 + 1 + 1
--------------------------------------------------------------------------------
(root
(statement
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(math
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(math_operator)
(expression
(value
(integer)))))))))
================================================================================
&&
================================================================================
4 + 2 == 42 && true
--------------------------------------------------------------------------------
(root
(statement
(expression
(logic
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(boolean)))))))))
================================================================================
\||
================================================================================
4 + 2 == 42 || true
--------------------------------------------------------------------------------
(root
(statement
(expression
(logic
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(boolean)))))))))

View File

@ -109,19 +109,19 @@ x = 3 == 1 + 2 + 2
(identifier) (identifier)
(assignment_operator) (assignment_operator)
(statement (statement
(expression
(math
(expression
(math
(expression (expression
(logic (logic
(expression (expression
(value (value
(integer))) (integer)))
(logic_operator) (logic_operator)
(expression
(math
(expression
(math
(expression (expression
(value (value
(integer))) (integer)))))
(math_operator) (math_operator)
(expression (expression
(value (value
@ -129,4 +129,4 @@ x = 3 == 1 + 2 + 2
(math_operator) (math_operator)
(expression (expression
(value (value
(integer))))))))))) (integer)))))))))

View File

@ -38,6 +38,16 @@ module.exports = grammar({
), ),
expression: $ => expression: $ =>
choice(
$._expression_kind,
seq(
'(',
$._expression_kind,
')',
),
),
_expression_kind: $ =>
prec.right( prec.right(
choice( choice(
$.function_call, $.function_call,
@ -214,7 +224,7 @@ module.exports = grammar({
choice('+', '-', '*', '/', '%'), choice('+', '-', '*', '/', '%'),
logic: $ => logic: $ =>
prec.right( prec.left(
seq( seq(
$.expression, $.expression,
$.logic_operator, $.logic_operator,

View File

@ -112,6 +112,32 @@
} }
}, },
"expression": { "expression": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression_kind"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SYMBOL",
"name": "_expression_kind"
},
{
"type": "STRING",
"value": ")"
}
]
}
]
},
"_expression_kind": {
"type": "PREC_RIGHT", "type": "PREC_RIGHT",
"value": 0, "value": 0,
"content": { "content": {
@ -660,7 +686,7 @@
] ]
}, },
"logic": { "logic": {
"type": "PREC_RIGHT", "type": "PREC_LEFT",
"value": 0, "value": 0,
"content": { "content": {
"type": "SEQ", "type": "SEQ",

File diff suppressed because it is too large Load Diff