1
0

Compare commits

..

No commits in common. "d1b116cc35a02f83e4db6e56ba7a6987ea9d1425" and "df7cd0e9721e56787be9c00d6a7c0650a2064169" have entirely different histories.

14 changed files with 54765 additions and 82451 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -14,6 +14,9 @@
"value": "[#][^#\\n]*[#|\\n]" "value": "[#][^#\\n]*[#|\\n]"
}, },
"block": { "block": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
@ -44,14 +47,12 @@
] ]
} }
] ]
}
}, },
"statement": { "statement": {
"type": "PREC_RIGHT", "type": "PREC_RIGHT",
"value": 0, "value": 0,
"content": { "content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
@ -111,24 +112,10 @@
"name": "while" "name": "while"
} }
] ]
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ";"
},
{
"type": "BLANK"
}
]
}
]
} }
}, },
"expression": { "expression": {
"type": "PREC_RIGHT", "type": "PREC_LEFT",
"value": 0, "value": 0,
"content": { "content": {
"type": "CHOICE", "type": "CHOICE",
@ -190,34 +177,6 @@
] ]
} }
}, },
"_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": { "identifier": {
"type": "PATTERN", "type": "PATTERN",
"value": "[_a-zA-Z]+[_a-zA-Z0-9]?" "value": "[_a-zA-Z]+[_a-zA-Z0-9]?"
@ -600,6 +559,60 @@
] ]
} }
}, },
"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": { "math": {
"type": "PREC_LEFT", "type": "PREC_LEFT",
"value": 0, "value": 0,
@ -739,7 +752,7 @@
] ]
}, },
"if_else": { "if_else": {
"type": "PREC_RIGHT", "type": "PREC_LEFT",
"value": 0, "value": 0,
"content": { "content": {
"type": "SEQ", "type": "SEQ",
@ -771,6 +784,9 @@
} }
}, },
"if": { "if": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -786,8 +802,12 @@
"name": "block" "name": "block"
} }
] ]
}
}, },
"else_if": { "else_if": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -803,8 +823,12 @@
"name": "block" "name": "block"
} }
] ]
}
}, },
"else": { "else": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -816,6 +840,7 @@
"name": "block" "name": "block"
} }
] ]
}
}, },
"match": { "match": {
"type": "PREC_RIGHT", "type": "PREC_RIGHT",
@ -1067,9 +1092,37 @@
"type": "STRING", "type": "STRING",
"value": "select" "value": "select"
}, },
{
"type": "STRING",
"value": "<"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier_list" "name": "identifier"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
},
{
"type": "STRING",
"value": ">"
}, },
{ {
"type": "STRING", "type": "STRING",
@ -1132,10 +1185,14 @@
} }
] ]
}, },
"identifier_list": { "function": {
"type": "PREC_RIGHT", "type": "SEQ",
"value": 0, "members": [
"content": { {
"type": "STRING",
"value": "function"
},
{
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
@ -1143,7 +1200,7 @@
"members": [ "members": [
{ {
"type": "STRING", "type": "STRING",
"value": "|" "value": "<"
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
@ -1171,65 +1228,19 @@
}, },
{ {
"type": "STRING", "type": "STRING",
"value": "|" "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": "BLANK"
} }
] ]
}
}, },
{ {
"type": "STRING",
"value": "=>"
},
{
"type": "FIELD",
"name": "body",
"content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "block" "name": "block"
} }
}
] ]
}, },
"function_call": { "function_call": {
@ -1256,11 +1267,23 @@
"name": "identifier" "name": "identifier"
}, },
{ {
"type": "CHOICE", "type": "REPEAT",
"content": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_expression_list" "name": "expression"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
}, },
{ {
"type": "BLANK" "type": "BLANK"
@ -1269,6 +1292,10 @@
} }
] ]
} }
}
}
]
}
}, },
"built_in_function": { "built_in_function": {
"type": "PREC_RIGHT", "type": "PREC_RIGHT",
@ -1281,11 +1308,23 @@
"name": "_built_in_function_name" "name": "_built_in_function_name"
}, },
{ {
"type": "CHOICE", "type": "REPEAT",
"content": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_expression_list" "name": "expression"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
}, },
{ {
"type": "BLANK" "type": "BLANK"
@ -1294,6 +1333,10 @@
} }
] ]
} }
}
}
]
}
}, },
"_built_in_function_name": { "_built_in_function_name": {
"type": "CHOICE", "type": "CHOICE",
@ -1432,9 +1475,6 @@
} }
], ],
"conflicts": [ "conflicts": [
[
"block"
],
[ [
"map", "map",
"assignment_operator" "assignment_operator"

View File

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

File diff suppressed because it is too large Load Diff