1
0

Compare commits

..

2 Commits

Author SHA1 Message Date
d1b116cc35 Begin completing syntax revision 2023-10-31 18:18:39 -04:00
ea4ffb492c Revise syntax 2023-10-31 16:25:13 -04:00
14 changed files with 83954 additions and 56268 deletions

View File

@ -3,28 +3,20 @@ suspects = ['White' 'Green']
weapons = ['Rope' 'Lead_Pipe']
cards = [rooms suspects weapons]
take_turn = function <current_room opponent_card> {
(remove_card opponent_card)
(make_guess current_room)
}
take_turn = function current_room opponent_card
remove_card opponent_card
make_guess current_room
remove_card = function <opponent_card> {
for card_list in cards {
# removed = remove card from card_list {
remove_card = function opponent_card
for card_list in cards
removed = remove card from card_list
card == opponent_card
}
}
if (type removed) == 'empty' {
(output 'Card not found.')
}
}
if type removed == 'empty'
output 'Card not found.'
make_guess = function <current_room> {
if (length suspects) == 1
&& (length rooms) == 1
&& (length weapons) == 1
{
make_guess = function current_room
if length suspects == 1 && length rooms == 1 && length weapons == 1
(output 'It was '
+ suspects:0
+ ' in the '
@ -32,7 +24,7 @@ make_guess = function <current_room> {
+ ' with the '
+ weapons:0
+ '!')
} else {
else
(output 'I accuse '
+ (random suspects)
+ ' in the '
@ -40,7 +32,5 @@ make_guess = function <current_room> {
+ ' with the '
+ (random weapons)
+ '!')
}
}
(make_guess 'Library')

View File

@ -286,6 +286,7 @@ impl AbstractTree for BuiltInFunction {
}
BuiltInFunction::AssertEqual(expressions) => {
let mut prev_value = None;
for expression in expressions {
let value = expression.run(source, context)?;

View File

@ -24,7 +24,7 @@ impl AbstractTree for FunctionCall {
for index in 1..node.child_count() {
let child = node.child(index).unwrap();
if child.kind() == "expression" {
if child.is_named() {
let expression = Expression::from_syntax_node(source, child)?;
arguments.push(expression);
@ -59,13 +59,16 @@ impl AbstractTree for FunctionCall {
return Err(Error::FunctionIdentifierNotFound(name.clone()));
};
let mut function_context = Map::clone_from(context);
let identifier_expression_pairs = definition.identifiers().iter().zip(arguments.iter());
for (identifier, expression) in identifier_expression_pairs {
let key = identifier.inner().clone();
let value = expression.run(source, context)?;
if let Some(parameters) = definition.identifiers() {
let parameter_expression_pairs = parameters.iter().zip(arguments.iter());
function_context.variables_mut().insert(key, value);
for (identifier, expression) in parameter_expression_pairs {
let key = identifier.clone().take_inner();
let value = expression.run(source, context)?;
function_context.variables_mut().insert(key, value);
}
}
definition.body().run(source, &mut function_context)

View File

@ -55,22 +55,23 @@ impl AbstractTree for ValueNode {
ValueType::List(expressions)
}
"table" => {
let child_count = child.child_count();
let mut column_names = Vec::new();
let identifier_list_node = child.child(1).unwrap();
let identifier_count = identifier_list_node.child_count();
let mut column_names = Vec::with_capacity(identifier_count);
let expression_node = child.child(child_count - 1).unwrap();
let expression = Expression::from_syntax_node(source, expression_node)?;
for index in 0..identifier_count {
let identifier_node = identifier_list_node.child(index).unwrap();
for index in 2..child.child_count() - 2 {
let node = child.child(index).unwrap();
if node.is_named() {
let identifier = Identifier::from_syntax_node(source, node)?;
if identifier_node.is_named() {
let identifier = Identifier::from_syntax_node(source, identifier_node)?;
column_names.push(identifier)
}
}
let expression_node = child.child(2).unwrap();
let expression = Expression::from_syntax_node(source, expression_node)?;
ValueType::Table {
column_names,
rows: Box::new(expression),
@ -99,24 +100,28 @@ impl AbstractTree for ValueNode {
ValueType::Map(child_nodes)
}
"function" => {
let mut identifiers = Vec::new();
let parameters_node = child.child_by_field_name("parameters");
let parameters = if let Some(node) = parameters_node {
let mut parameter_list = Vec::new();
let block_node = child.child(child.child_count() - 1).unwrap();
let block = Block::from_syntax_node(source, block_node)?;
for index in 0..node.child_count() {
let child_node = node.child(index).unwrap();
for index in 1..child.child_count() - 1 {
let child_node = child.child(index).unwrap();
if child_node.is_named() {
let parameter = Identifier::from_syntax_node(source, child_node)?;
if child_node.kind() == "identifier" {
let identifier = Identifier::from_syntax_node(source, child_node)?;
identifiers.push(identifier);
parameter_list.push(parameter);
}
}
}
let function = Function::new(identifiers, block);
Some(parameter_list)
} else {
None
};
let body_node = child.child_by_field_name("body").unwrap();
let body = Block::from_syntax_node(source, body_node)?;
ValueType::Function(function)
ValueType::Function(Function::new(parameters, body))
}
_ => {
return Err(Error::UnexpectedSyntaxNode {

View File

@ -174,7 +174,7 @@ mod tests {
assert_eq!(
evaluate(
"
table <messages, numbers> [
table |messages numbers| [
['hiya', 42]
['foo', 57]
['bar', 99.99]
@ -247,7 +247,7 @@ mod tests {
assert_eq!(
evaluate(
"
foobar = function <message> { message }
foobar = |message| => message
(foobar 'Hiya')
",
),

View File

@ -6,19 +6,19 @@ use crate::{Block, Identifier};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Function {
parameters: Vec<Identifier>,
parameters: Option<Vec<Identifier>>,
body: Box<Block>,
}
impl Function {
pub fn new(parameters: Vec<Identifier>, body: Block) -> Self {
pub fn new(parameters: Option<Vec<Identifier>>, body: Block) -> Self {
Function {
parameters,
body: Box::new(body),
}
}
pub fn identifiers(&self) -> &Vec<Identifier> {
pub fn identifiers(&self) -> &Option<Vec<Identifier>> {
&self.parameters
}

View File

@ -2,7 +2,7 @@
Simple Function
================================================================================
function { "Hiya" }
=> "Hiya"
--------------------------------------------------------------------------------
@ -18,6 +18,30 @@ function { "Hiya" }
(value
(string)))))))))))
================================================================================
Function Assignment
================================================================================
x = => "Hiya"
--------------------------------------------------------------------------------
(root
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(function
(block
(statement
(expression
(value
(string)))))))))))))
================================================================================
Function Call
================================================================================
@ -40,7 +64,7 @@ Function Call
Complex Function
================================================================================
function <message number> {
|message number| => {
(output message)
(output number)
}
@ -53,8 +77,9 @@ function <message number> {
(expression
(value
(function
(identifier)
(identifier)
(identifier_list
(identifier)
(identifier))
(block
(statement
(expression

View File

@ -24,7 +24,7 @@ if true { "True" }
Complex If
==================
if 1 == 1 && 2 == 2 && 3 == 3 { "True" }
if 1 == 1 && 2 == 2 && 3 == 3 "True"
---
@ -76,13 +76,11 @@ if 1 == 1 && 2 == 2 && 3 == 3 { "True" }
Nested If
==================
if true {
if 42 == 12 {
if true
if 42 == 12
'hiya'
} else {
else
'bye'
}
}
---

View File

@ -3,7 +3,7 @@ Simple Statements
==================
1
"one"
"one";
x
---
@ -26,7 +26,7 @@ x
Simple Assignment
==================
x = 1
x = 1;
y = "one"
---

View File

@ -2,22 +2,23 @@
Table Declaration
==================
table <messages, numbers> [
['hiya', 42]
['foo', 57]
['bar', 99.99]
table |messages numbers| [
['hiya' 42]
['foo' 57]
['bar' 99.99]
]
---
(root
(block
(statement
(statement
(expression
(value
(table
(identifier)
(identifier)
(identifier_list
(identifier)
(identifier))
(expression
(value
(list
@ -53,7 +54,7 @@ table <messages, numbers> [
Table Access
==================
select <number> from foobar {
select |number| from foobar {
text == 'answer'
}
@ -63,7 +64,8 @@ select <number> from foobar {
(block
(statement
(select
(identifier)
(identifier_list
(identifier))
(expression
(identifier))
(block

View File

@ -6,6 +6,7 @@ module.exports = grammar({
extras: $ => [ /\s/, $.comment ],
conflicts: $ => [
[$.block],
[$.map, $.assignment_operator],
],
@ -14,29 +15,32 @@ module.exports = grammar({
comment: $ => /[#][^#\n]*[#|\n]/,
block: $ => prec.right(choice(
block: $ => choice(
repeat1($.statement),
seq('{', repeat1($.statement), '}'),
),
statement: $ => prec.right(seq(
choice(
$.assignment,
$.async,
$.expression,
$.filter,
$.find,
$.for,
$.if_else,
$.insert,
$.match,
$.reduce,
$.remove,
$.select,
$.transform,
$.while,
),
optional(';'),
)),
statement: $ => prec.right(choice(
$.assignment,
$.async,
$.expression,
$.filter,
$.find,
$.for,
$.if_else,
$.insert,
$.match,
$.reduce,
$.remove,
$.select,
$.transform,
$.while,
)),
expression: $ => prec.left(choice(
expression: $ => prec.right(choice(
$._expression_kind,
seq('(', $._expression_kind, ')'),
)),
@ -50,6 +54,8 @@ module.exports = grammar({
$.value,
)),
_expression_list: $ => repeat1(prec.right(seq($.expression, optional(',')))),
identifier: $ => /[_a-zA-Z]+[_a-zA-Z0-9]?/,
value: $ => choice(
@ -111,12 +117,6 @@ module.exports = grammar({
)),
)),
table: $ => prec.left(seq(
'table',
seq('<', repeat1(seq($.identifier, optional(','))), '>'),
$.expression,
)),
math: $ => prec.left(seq(
$.expression,
$.math_operator,
@ -160,28 +160,28 @@ module.exports = grammar({
"-=",
),
if_else: $ => prec.left(seq(
if_else: $ => prec.right(seq(
$.if,
repeat($.else_if),
optional($.else),
)),
if: $ => prec.left(seq(
if: $ => seq(
'if',
$.expression,
$.block,
)),
),
else_if: $ => prec.left(seq(
else_if: $ => seq(
'else if',
$.expression,
$.block,
)),
),
else: $ => prec.left(seq(
else: $ => seq(
'else',
$.block,
)),
),
match: $ => prec.right(seq(
'match',
@ -252,9 +252,7 @@ module.exports = grammar({
select: $ => prec.right(seq(
'select',
'<',
repeat(seq($.identifier, optional(','))),
'>',
$.identifier_list,
'from',
$.expression,
optional($.block),
@ -272,10 +270,24 @@ module.exports = grammar({
$.block,
),
identifier_list: $ => prec.right(choice(
seq(
'|',
repeat(seq($.identifier, optional(','))),
'|',
),
)),
table: $ => prec.right(seq(
'table',
$.identifier_list,
$.expression,
)),
function: $ => seq(
'function',
optional(seq('<', repeat(seq($.identifier, optional(','))), '>')),
$.block,
field('parameters', optional($.identifier_list)),
'=>',
field('body', $.block),
),
function_call: $ => choice(
@ -285,12 +297,12 @@ module.exports = grammar({
_context_defined_function: $ => prec.right(seq(
$.identifier,
repeat(prec.right(seq($.expression, optional(',')))),
optional($._expression_list),
)),
built_in_function: $ => prec.right(seq(
$._built_in_function_name,
repeat(prec.right(seq($.expression, optional(',')))),
optional($._expression_list),
)),
_built_in_function_name: $ => choice(

View File

@ -14,108 +14,121 @@
"value": "[#][^#\\n]*[#|\\n]"
},
"block": {
"type": "CHOICE",
"members": [
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "statement"
}
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "statement"
}
},
{
"type": "STRING",
"value": "}"
}
]
}
]
},
"statement": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "CHOICE",
"type": "SEQ",
"members": [
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "statement"
}
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "assignment"
},
{
"type": "SYMBOL",
"name": "async"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "filter"
},
{
"type": "SYMBOL",
"name": "find"
},
{
"type": "SYMBOL",
"name": "for"
},
{
"type": "SYMBOL",
"name": "if_else"
},
{
"type": "SYMBOL",
"name": "insert"
},
{
"type": "SYMBOL",
"name": "match"
},
{
"type": "SYMBOL",
"name": "reduce"
},
{
"type": "SYMBOL",
"name": "remove"
},
{
"type": "SYMBOL",
"name": "select"
},
{
"type": "SYMBOL",
"name": "transform"
},
{
"type": "SYMBOL",
"name": "while"
}
]
},
{
"type": "SEQ",
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "{"
"value": ";"
},
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "statement"
}
},
{
"type": "STRING",
"value": "}"
"type": "BLANK"
}
]
}
]
}
},
"statement": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "assignment"
},
{
"type": "SYMBOL",
"name": "async"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "filter"
},
{
"type": "SYMBOL",
"name": "find"
},
{
"type": "SYMBOL",
"name": "for"
},
{
"type": "SYMBOL",
"name": "if_else"
},
{
"type": "SYMBOL",
"name": "insert"
},
{
"type": "SYMBOL",
"name": "match"
},
{
"type": "SYMBOL",
"name": "reduce"
},
{
"type": "SYMBOL",
"name": "remove"
},
{
"type": "SYMBOL",
"name": "select"
},
{
"type": "SYMBOL",
"name": "transform"
},
{
"type": "SYMBOL",
"name": "while"
}
]
}
},
"expression": {
"type": "PREC_LEFT",
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "CHOICE",
@ -177,6 +190,34 @@
]
}
},
"_expression_list": {
"type": "REPEAT1",
"content": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
}
},
"identifier": {
"type": "PATTERN",
"value": "[_a-zA-Z]+[_a-zA-Z0-9]?"
@ -559,60 +600,6 @@
]
}
},
"table": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "table"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "<"
},
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
},
{
"type": "STRING",
"value": ">"
}
]
},
{
"type": "SYMBOL",
"name": "expression"
}
]
}
},
"math": {
"type": "PREC_LEFT",
"value": 0,
@ -752,7 +739,7 @@
]
},
"if_else": {
"type": "PREC_LEFT",
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
@ -784,63 +771,51 @@
}
},
"if": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "if"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "block"
}
]
}
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "if"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "block"
}
]
},
"else_if": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else if"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "block"
}
]
}
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else if"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "block"
}
]
},
"else": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else"
},
{
"type": "SYMBOL",
"name": "block"
}
]
}
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else"
},
{
"type": "SYMBOL",
"name": "block"
}
]
},
"match": {
"type": "PREC_RIGHT",
@ -1093,36 +1068,8 @@
"value": "select"
},
{
"type": "STRING",
"value": "<"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
},
{
"type": "STRING",
"value": ">"
"type": "SYMBOL",
"name": "identifier_list"
},
{
"type": "STRING",
@ -1185,61 +1132,103 @@
}
]
},
"identifier_list": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "|"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
},
{
"type": "STRING",
"value": "|"
}
]
}
]
}
},
"table": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "table"
},
{
"type": "SYMBOL",
"name": "identifier_list"
},
{
"type": "SYMBOL",
"name": "expression"
}
]
}
},
"function": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "parameters",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier_list"
},
{
"type": "BLANK"
}
]
}
},
{
"type": "STRING",
"value": "function"
"value": "=>"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "<"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
},
{
"type": "STRING",
"value": ">"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "block"
"type": "FIELD",
"name": "body",
"content": {
"type": "SYMBOL",
"name": "block"
}
}
]
},
@ -1267,32 +1256,16 @@
"name": "identifier"
},
{
"type": "REPEAT",
"content": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression_list"
},
{
"type": "BLANK"
}
}
]
}
]
}
@ -1308,32 +1281,16 @@
"name": "_built_in_function_name"
},
{
"type": "REPEAT",
"content": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression_list"
},
{
"type": "BLANK"
}
}
]
}
]
}
@ -1475,6 +1432,9 @@
}
],
"conflicts": [
[
"block"
],
[
"map",
"assignment_operator"

View File

@ -241,20 +241,27 @@
{
"type": "function",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "block",
"named": true
},
{
"type": "identifier",
"named": true
}
]
"fields": {
"body": {
"multiple": false,
"required": true,
"types": [
{
"type": "block",
"named": true
}
]
},
"parameters": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier_list",
"named": true
}
]
}
}
},
{
@ -280,6 +287,21 @@
]
}
},
{
"type": "identifier_list",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
{
"type": "if",
"named": true,
@ -535,7 +557,7 @@
"named": true
},
{
"type": "identifier",
"type": "identifier_list",
"named": true
}
]
@ -621,7 +643,7 @@
"named": true
},
{
"type": "identifier",
"type": "identifier_list",
"named": true
}
]
@ -768,6 +790,10 @@
"type": ":",
"named": false
},
{
"type": ";",
"named": false
},
{
"type": "<",
"named": false
@ -876,10 +902,6 @@
"type": "from_json",
"named": false
},
{
"type": "function",
"named": false
},
{
"type": "help",
"named": false
@ -1036,6 +1058,10 @@
"type": "{",
"named": false
},
{
"type": "|",
"named": false
},
{
"type": "||",
"named": false

File diff suppressed because it is too large Load Diff