1
0

Begin completing syntax revision

This commit is contained in:
Jeff 2023-10-31 18:18:39 -04:00
parent ea4ffb492c
commit d1b116cc35
12 changed files with 81768 additions and 52358 deletions

View File

@ -3,28 +3,20 @@ 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 if length suspects == 1 && length rooms == 1 && length weapons == 1
&& (length rooms) == 1
&& (length weapons) == 1
{
(output 'It was ' (output 'It was '
+ suspects:0 + suspects:0
+ ' in the ' + ' in the '
@ -32,7 +24,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 '
@ -40,7 +32,5 @@ make_guess = function <current_room> {
+ ' with the ' + ' with the '
+ (random weapons) + (random weapons)
+ '!') + '!')
}
}
(make_guess 'Library') (make_guess 'Library')

View File

@ -286,6 +286,7 @@ 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.kind() == "expression" { if child.is_named() {
let expression = Expression::from_syntax_node(source, child)?; let expression = Expression::from_syntax_node(source, child)?;
arguments.push(expression); arguments.push(expression);
@ -59,14 +59,17 @@ 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());
for (identifier, expression) in identifier_expression_pairs { if let Some(parameters) = definition.identifiers() {
let key = identifier.inner().clone(); let parameter_expression_pairs = parameters.iter().zip(arguments.iter());
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,22 +55,23 @@ impl AbstractTree for ValueNode {
ValueType::List(expressions) ValueType::List(expressions)
} }
"table" => { "table" => {
let child_count = child.child_count(); let identifier_list_node = child.child(1).unwrap();
let mut column_names = Vec::new(); 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(); for index in 0..identifier_count {
let expression = Expression::from_syntax_node(source, expression_node)?; let identifier_node = identifier_list_node.child(index).unwrap();
for index in 2..child.child_count() - 2 { if identifier_node.is_named() {
let node = child.child(index).unwrap(); let identifier = Identifier::from_syntax_node(source, identifier_node)?;
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),
@ -99,24 +100,28 @@ impl AbstractTree for ValueNode {
ValueType::Map(child_nodes) ValueType::Map(child_nodes)
} }
"function" => { "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(); for index in 0..node.child_count() {
let block = Block::from_syntax_node(source, block_node)?; let child_node = node.child(index).unwrap();
for index in 1..child.child_count() - 1 { if child_node.is_named() {
let child_node = child.child(index).unwrap(); let parameter = Identifier::from_syntax_node(source, child_node)?;
if child_node.kind() == "identifier" { parameter_list.push(parameter);
let identifier = Identifier::from_syntax_node(source, child_node)?;
identifiers.push(identifier);
} }
} }
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 { 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 = function <message> { message } foobar = |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: Vec<Identifier>, parameters: Option<Vec<Identifier>>,
body: Box<Block>, body: Box<Block>,
} }
impl Function { impl Function {
pub fn new(parameters: Vec<Identifier>, body: Block) -> Self { pub fn new(parameters: Option<Vec<Identifier>>, body: Block) -> Self {
Function { Function {
parameters, parameters,
body: Box::new(body), body: Box::new(body),
} }
} }
pub fn identifiers(&self) -> &Vec<Identifier> { pub fn identifiers(&self) -> &Option<Vec<Identifier>> {
&self.parameters &self.parameters
} }

View File

@ -2,7 +2,7 @@
Simple Function Simple Function
================================================================================ ================================================================================
function { "Hiya" } => "Hiya"
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -18,6 +18,30 @@ function { "Hiya" }
(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
================================================================================ ================================================================================
@ -40,7 +64,7 @@ Function Call
Complex Function Complex Function
================================================================================ ================================================================================
function <message number> { |message number| => {
(output message) (output message)
(output number) (output number)
} }
@ -53,7 +77,7 @@ function <message number> {
(expression (expression
(value (value
(function (function
(parameter_list (identifier_list
(identifier) (identifier)
(identifier)) (identifier))
(block (block

View File

@ -2,7 +2,7 @@
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,7 +16,7 @@ table messages numbers [
(expression (expression
(value (value
(table (table
(parameter_list (identifier_list
(identifier) (identifier)
(identifier)) (identifier))
(expression (expression
@ -54,7 +54,7 @@ table messages numbers [
Table Access Table Access
================== ==================
select <number> from foobar { select |number| from foobar {
text == 'answer' text == 'answer'
} }
@ -64,7 +64,7 @@ select <number> from foobar {
(block (block
(statement (statement
(select (select
(parameter_list (identifier_list
(identifier)) (identifier))
(expression (expression
(identifier)) (identifier))

View File

@ -6,6 +6,7 @@ module.exports = grammar({
extras: $ => [ /\s/, $.comment ], extras: $ => [ /\s/, $.comment ],
conflicts: $ => [ conflicts: $ => [
[$.block],
[$.map, $.assignment_operator], [$.map, $.assignment_operator],
], ],
@ -14,10 +15,10 @@ module.exports = grammar({
comment: $ => /[#][^#\n]*[#|\n]/, comment: $ => /[#][^#\n]*[#|\n]/,
block: $ => prec.right(choice( block: $ => choice(
repeat1($.statement), repeat1($.statement),
seq('{', repeat1($.statement), '}'), seq('{', repeat1($.statement), '}'),
)), ),
statement: $ => prec.right(seq( statement: $ => prec.right(seq(
choice( choice(
@ -116,19 +117,6 @@ module.exports = grammar({
)), )),
)), )),
_identifier_list: $ => repeat1(seq($.identifier, optional(','))),
parameter_list: $ => prec.right(choice(
$._identifier_list,
seq('<', $._identifier_list, '>'),
)),
table: $ => prec.right(seq(
'table',
$.parameter_list,
$.expression,
)),
math: $ => prec.left(seq( math: $ => prec.left(seq(
$.expression, $.expression,
$.math_operator, $.math_operator,
@ -264,7 +252,7 @@ module.exports = grammar({
select: $ => prec.right(seq( select: $ => prec.right(seq(
'select', 'select',
$.parameter_list, $.identifier_list,
'from', 'from',
$.expression, $.expression,
optional($.block), optional($.block),
@ -282,10 +270,24 @@ 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(
'function', field('parameters', optional($.identifier_list)),
optional($.parameter_list), '=>',
$.block, field('body', $.block),
), ),
function_call: $ => choice( function_call: $ => choice(

View File

@ -14,9 +14,6 @@
"value": "[#][^#\\n]*[#|\\n]" "value": "[#][^#\\n]*[#|\\n]"
}, },
"block": { "block": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
@ -47,7 +44,6 @@
] ]
} }
] ]
}
}, },
"statement": { "statement": {
"type": "PREC_RIGHT", "type": "PREC_RIGHT",
@ -604,81 +600,6 @@
] ]
} }
}, },
"_identifier_list": {
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
},
"parameter_list": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_identifier_list"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "<"
},
{
"type": "SYMBOL",
"name": "_identifier_list"
},
{
"type": "STRING",
"value": ">"
}
]
}
]
}
},
"table": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "table"
},
{
"type": "SYMBOL",
"name": "parameter_list"
},
{
"type": "SYMBOL",
"name": "expression"
}
]
}
},
"math": { "math": {
"type": "PREC_LEFT", "type": "PREC_LEFT",
"value": 0, "value": 0,
@ -1148,7 +1069,7 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "parameter_list" "name": "identifier_list"
}, },
{ {
"type": "STRING", "type": "STRING",
@ -1211,29 +1132,104 @@
} }
] ]
}, },
"function": { "identifier_list": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "CHOICE",
"members": [
{
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "STRING", "type": "STRING",
"value": "function" "value": "|"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "STRING",
"name": "parameter_list" "value": ","
}, },
{ {
"type": "BLANK" "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": "=>"
},
{
"type": "FIELD",
"name": "body",
"content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "block" "name": "block"
} }
}
] ]
}, },
"function_call": { "function_call": {
@ -1436,6 +1432,9 @@
} }
], ],
"conflicts": [ "conflicts": [
[
"block"
],
[ [
"map", "map",
"assignment_operator" "assignment_operator"

View File

@ -241,21 +241,28 @@
{ {
"type": "function", "type": "function",
"named": true, "named": true,
"fields": {}, "fields": {
"children": { "body": {
"multiple": true, "multiple": false,
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "block", "type": "block",
"named": true "named": true
}
]
}, },
"parameters": {
"multiple": false,
"required": false,
"types": [
{ {
"type": "parameter_list", "type": "identifier_list",
"named": true "named": true
} }
] ]
} }
}
}, },
{ {
"type": "function_call", "type": "function_call",
@ -280,6 +287,21 @@
] ]
} }
}, },
{
"type": "identifier_list",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
{ {
"type": "if", "type": "if",
"named": true, "named": true,
@ -457,21 +479,6 @@
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{
"type": "parameter_list",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
{ {
"type": "reduce", "type": "reduce",
"named": true, "named": true,
@ -550,7 +557,7 @@
"named": true "named": true
}, },
{ {
"type": "parameter_list", "type": "identifier_list",
"named": true "named": true
} }
] ]
@ -636,7 +643,7 @@
"named": true "named": true
}, },
{ {
"type": "parameter_list", "type": "identifier_list",
"named": true "named": true
} }
] ]
@ -895,10 +902,6 @@
"type": "from_json", "type": "from_json",
"named": false "named": false
}, },
{
"type": "function",
"named": false
},
{ {
"type": "help", "type": "help",
"named": false "named": false
@ -1055,6 +1058,10 @@
"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