1
0

Revise function and yield syntax

This commit is contained in:
Jeff 2023-12-29 21:15:03 -05:00
parent 55de33ceb7
commit f3921ba87c
26 changed files with 13237 additions and 14700 deletions

View File

@ -1,19 +1,19 @@
create_random_numbers = (fn count <int>) <none> { create_random_numbers = (count <int>) <none> {
numbers = [] numbers = []
while (length numbers) < count { while length(numbers) < count {
numbers += (random_integer) numbers += random_integer()
} }
(output "Made " + (length numbers) + " numbers.") output("Made " + length(numbers) + " numbers.")
} }
(output "This will print first.") output("This will print first.")
async { async {
(create_random_numbers 1000) create_random_numbers(1000)
(create_random_numbers 100) create_random_numbers(100)
(create_random_numbers 10) create_random_numbers(10)
} }
(output "This will print last.") output("This will print last.")

View File

@ -1,11 +1,11 @@
cast = (download "https://api.sampleapis.com/futurama/cast") cast = download("https://api.sampleapis.com/futurama/cast")
characters = (download "https://api.sampleapis.com/futurama/characters") characters = download("https://api.sampleapis.com/futurama/characters")
episodes = (download "https://api.sampleapis.com/futurama/episodes") episodes = download("https://api.sampleapis.com/futurama/episodes")
async { async {
cast_len = (length (from_json cast)) cast_len = length(from_json(cast))
characters_len = (length (from_json characters)) characters_len = length(from_json(characters))
episodes_len = (length (from_json episodes)) episodes_len = length(from_json(episodes))
} }
(output [cast_len, characters_len, episodes_len]) output ([cast_len, characters_len, episodes_len])

View File

@ -1,54 +1,54 @@
all_cards = { cards = {
rooms = ['Library' 'Kitchen' 'Conservatory'] rooms = ['Library' 'Kitchen' 'Conservatory']
suspects = ['White' 'Green' 'Scarlett'] suspects = ['White' 'Green' 'Scarlett']
weapons = ['Rope' 'Lead_Pipe' 'Knife'] weapons = ['Rope' 'Lead_Pipe' 'Knife']
} }
is_ready_to_solve = (fn 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 = (fn cards <map>, opponent_card <str>) <map> { remove_card = (cards <map>, opponent_card <str>) <none> {
cards:rooms -= opponent_card cards:rooms -= opponent_card
cards:suspects -= opponent_card cards:suspects -= opponent_card
cards:weapons -= opponent_card cards:weapons -= opponent_card
cards
} }
make_guess = (fn cards <map>, current_room <str>) <map> { make_guess = (cards <map>, current_room <str>) <none> {
if (is_ready_to_solve cards) { if is_ready_to_solve(cards) {
(output 'It was ' output(
'It was '
+ cards:suspects:0 + cards:suspects:0
+ ' in the ' + ' in the '
+ cards:rooms:0 + cards:rooms:0
+ ' with the ' + ' with the '
+ cards:weapons:0 + cards:weapons:0
+ '!') + '!'
)
} else { } else {
(output 'I accuse ' output(
+ (random cards:suspects) 'I accuse '
+ random(cards:suspects)
+ ' in the ' + ' in the '
+ current_room + current_room
+ ' with the ' + ' with the '
+ (random cards:weapons) + random(cards:weapons)
+ '.') + '.'
)
} }
cards
} }
take_turn = (fn cards <map>, opponent_card <str>, current_room <str>) <map> { take_turn = (cards <map>, opponent_card <str>, current_room <str>) <none> {
cards = (remove_card cards opponent_card) remove_card(cards opponent_card)
cards = (make_guess cards current_room) make_guess(cards current_room)
cards
} }
all_cards take_turn(cards 'Rope' 'Kitchen')
-> (take_turn 'Rope' 'Kitchen') take_turn(cards 'Library' 'Kitchen')
-> (take_turn 'Library' 'Kitchen') take_turn(cards 'Conservatory' 'Kitchen')
-> (take_turn 'Conservatory' 'Kitchen') take_turn(cards 'White' 'Kitchen')
-> (take_turn 'White' 'Kitchen') take_turn(cards 'Green' 'Kitchen')
-> (take_turn 'Green' 'Kitchen') take_turn(cards 'Knife' 'Kitchen')
-> (take_turn 'Knife' 'Kitchen')

View File

@ -1,5 +1,5 @@
raw_data = (download "https://api.sampleapis.com/futurama/cast") raw_data = download("https://api.sampleapis.com/futurama/cast")
cast_data = (from_json raw_data) cast_data = from_json(raw_data)
names = [] names = []
@ -7,4 +7,4 @@ for cast_member in cast_data {
names += cast_member:name names += cast_member:name
} }
(assert_equal "Billy West", names:0) assert_equal("Billy West", names:0)

View File

@ -1,9 +1,9 @@
fib = (fn i <int>) <int> { fib = (i <int>) <int> {
if i <= 1 { if i <= 1 {
1 1
} else { } else {
(fib i - 1) + (fib i - 2) fib(i - 1) + fib(i - 2)
} }
} }
(fib 5) fib(5)

View File

@ -5,13 +5,13 @@ while count <= 15 {
divides_by_5 = count % 5 == 0 divides_by_5 = count % 5 == 0
if divides_by_3 && divides_by_5 { if divides_by_3 && divides_by_5 {
(output 'fizzbuzz') output('fizzbuzz')
} else if divides_by_3 { } else if divides_by_3 {
(output 'fizz') output('fizz')
} else if divides_by_5 { } else if divides_by_5 {
(output 'buzz') output('buzz')
} else { } else {
(output count) output(count)
} }
count += 1 count += 1

View File

@ -2,5 +2,5 @@ list = [1 2 3]
for i in list { for i in list {
i += 1 i += 1
(output i) output(i)
} }

View File

@ -1 +1 @@
(output 'Hello, world!') output('Hello, world!')

View File

@ -4,4 +4,4 @@ x = numbers:0
y = numbers:1 y = numbers:1
z = numbers:2 z = numbers:2
(assert_equal x + y, z) assert_equal(x + y, z)

View File

@ -3,7 +3,7 @@ dictionary = {
answer = 42 answer = 42
} }
(output output(
'Dust is ' 'Dust is '
+ dictionary:dust + dictionary:dust
+ '! The answer is ' + '! The answer is '

View File

@ -1,13 +1,13 @@
stuff = [ stuff = [
(random_integer) random_integer()
(random_integer) random_integer()
(random_integer) random_integer()
(random_float) random_float()
(random_float) random_float()
(random_float) random_float()
(random_boolean) random_boolean()
(random_boolean) random_boolean()
(random_boolean) random_boolean()
] ]
(random stuff) random(stuff)

View File

@ -1,5 +1,5 @@
raw_data = (read 'examples/assets/seaCreatures.json') raw_data = read('examples/assets/seaCreatures.json')
sea_creatures = (from_json raw_data) sea_creatures = from_json(raw_data)
data = { data = {
creatures = [] creatures = []

View File

@ -1,6 +1,6 @@
i = 0 i = 0
while i < 10 { while i < 10 {
(output i) output(i)
i += 1 i += 1
} }

View File

@ -1,6 +1,4 @@
1 -> (output) add_one = (numbers <[int]>) <[int]> {
add_one = (fn numbers <[int]>) <[int]> {
new_numbers = [] new_numbers = []
for number in numbers { for number in numbers {
@ -10,6 +8,6 @@ add_one = (fn numbers <[int]>) <[int]> {
new_numbers new_numbers
} }
foo = [1, 2, 3] -> (add_one) foo = [1, 2, 3] -> add_one
(assert_equal [2 3 4] foo) assert_equal([2 3 4] foo)

View File

@ -114,6 +114,7 @@ impl AbstractTree for FunctionCall {
} }
FunctionExpression::Value(value_node) => value_node.run(source, context)?, FunctionExpression::Value(value_node) => value_node.run(source, context)?,
FunctionExpression::Index(index) => index.run(source, context)?, FunctionExpression::Index(index) => index.run(source, context)?,
FunctionExpression::Yield(r#yield) => r#yield.run(source, context)?,
}; };
let mut arguments = Vec::with_capacity(self.arguments.len()); let mut arguments = Vec::with_capacity(self.arguments.len());
@ -157,6 +158,7 @@ impl AbstractTree for FunctionCall {
FunctionExpression::FunctionCall(function_call) => function_call.expected_type(context), FunctionExpression::FunctionCall(function_call) => function_call.expected_type(context),
FunctionExpression::Value(value_node) => value_node.expected_type(context), FunctionExpression::Value(value_node) => value_node.expected_type(context),
FunctionExpression::Index(index) => index.expected_type(context), FunctionExpression::Index(index) => index.expected_type(context),
FunctionExpression::Yield(r#yield) => r#yield.expected_type(context),
} }
} }
} }

View File

@ -3,6 +3,7 @@ use tree_sitter::Node;
use crate::{ use crate::{
AbstractTree, Error, FunctionCall, Identifier, Index, Map, Result, Type, Value, ValueNode, AbstractTree, Error, FunctionCall, Identifier, Index, Map, Result, Type, Value, ValueNode,
Yield,
}; };
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
@ -11,6 +12,7 @@ pub enum FunctionExpression {
FunctionCall(Box<FunctionCall>), FunctionCall(Box<FunctionCall>),
Value(ValueNode), Value(ValueNode),
Index(Index), Index(Index),
Yield(Box<Yield>),
} }
impl AbstractTree for FunctionExpression { impl AbstractTree for FunctionExpression {
@ -30,6 +32,9 @@ impl AbstractTree for FunctionExpression {
FunctionExpression::Value(ValueNode::from_syntax_node(source, child, context)?) FunctionExpression::Value(ValueNode::from_syntax_node(source, child, context)?)
} }
"index" => FunctionExpression::Index(Index::from_syntax_node(source, child, context)?), "index" => FunctionExpression::Index(Index::from_syntax_node(source, child, context)?),
"yield" => FunctionExpression::Yield(Box::new(Yield::from_syntax_node(
source, child, context,
)?)),
_ => { _ => {
return Err(Error::UnexpectedSyntaxNode { return Err(Error::UnexpectedSyntaxNode {
expected: "identifier, function call, value or index", expected: "identifier, function call, value or index",
@ -49,6 +54,7 @@ impl AbstractTree for FunctionExpression {
FunctionExpression::FunctionCall(function_call) => function_call.run(source, context), FunctionExpression::FunctionCall(function_call) => function_call.run(source, context),
FunctionExpression::Value(value_node) => value_node.run(source, context), FunctionExpression::Value(value_node) => value_node.run(source, context),
FunctionExpression::Index(index) => index.run(source, context), FunctionExpression::Index(index) => index.run(source, context),
FunctionExpression::Yield(r#yield) => r#yield.run(source, context),
} }
} }
@ -58,6 +64,7 @@ impl AbstractTree for FunctionExpression {
FunctionExpression::FunctionCall(function_call) => function_call.expected_type(context), FunctionExpression::FunctionCall(function_call) => function_call.expected_type(context),
FunctionExpression::Value(value_node) => value_node.expected_type(context), FunctionExpression::Value(value_node) => value_node.expected_type(context),
FunctionExpression::Index(index) => index.expected_type(context), FunctionExpression::Index(index) => index.expected_type(context),
FunctionExpression::Yield(r#yield) => r#yield.expected_type(context),
} }
} }
} }

View File

@ -15,6 +15,8 @@ pub struct Index {
impl AbstractTree for Index { impl AbstractTree for Index {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "index", node)?;
let collection_node = node.child(0).unwrap(); let collection_node = node.child(0).unwrap();
let collection = Expression::from_syntax_node(source, collection_node, context)?; let collection = Expression::from_syntax_node(source, collection_node, context)?;

View File

@ -13,6 +13,8 @@ pub struct Logic {
impl AbstractTree for Logic { impl AbstractTree for Logic {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "logic", node)?;
let first_node = node.child(0).unwrap(); let first_node = node.child(0).unwrap();
let (left_node, operator_node, right_node) = { let (left_node, operator_node, right_node) = {
if first_node.is_named() { if first_node.is_named() {

View File

@ -16,6 +16,8 @@ pub struct Math {
impl AbstractTree for Math { impl AbstractTree for Math {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "math", node)?;
let left_node = node.child(0).unwrap(); let left_node = node.child(0).unwrap();
let left = Expression::from_syntax_node(source, left_node, context)?; let left = Expression::from_syntax_node(source, left_node, context)?;

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Block, Expression, Map, Result, Type, Value}; use crate::{AbstractTree, Block, Error, Expression, Map, Result, Type, Value};
/// Abstract representation of a while loop. /// Abstract representation of a while loop.
/// ///
@ -14,7 +14,7 @@ pub struct While {
impl AbstractTree for While { impl AbstractTree for While {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> crate::Result<Self> { fn from_syntax_node(source: &str, node: Node, context: &Map) -> crate::Result<Self> {
debug_assert_eq!("while", node.kind()); Error::expect_syntax_node(source, "while", node)?;
let expression_node = node.child(1).unwrap(); let expression_node = node.child(1).unwrap();
let expression = Expression::from_syntax_node(source, expression_node, context)?; let expression = Expression::from_syntax_node(source, expression_node, context)?;

View File

@ -2,8 +2,8 @@ use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{ use crate::{
function_expression::FunctionExpression, AbstractTree, Expression, FunctionCall, Map, Result, function_expression::FunctionExpression, AbstractTree, Error, Expression, FunctionCall, Map,
Type, Value, Result, Type, Value,
}; };
/// Abstract representation of a yield expression. /// Abstract representation of a yield expression.
@ -16,18 +16,20 @@ pub struct Yield {
impl AbstractTree for Yield { impl AbstractTree for Yield {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "yield", node)?;
let input_node = node.child(0).unwrap(); let input_node = node.child(0).unwrap();
let input = Expression::from_syntax_node(source, input_node, context)?; let input = Expression::from_syntax_node(source, input_node, context)?;
let expression_node = node.child(3).unwrap(); let function_node = node.child(2).unwrap();
let function_expression = let function_expression =
FunctionExpression::from_syntax_node(source, expression_node, context)?; FunctionExpression::from_syntax_node(source, function_node, context)?;
let mut arguments = Vec::new(); let mut arguments = Vec::new();
arguments.push(input); arguments.push(input);
for index in 4..node.child_count() - 1 { for index in 3..node.child_count() - 1 {
let child = node.child(index).unwrap(); let child = node.child(index).unwrap();
if child.is_named() { if child.is_named() {

View File

@ -2,7 +2,7 @@
Simple Yield Simple Yield
================================================================================ ================================================================================
1 -> (output) 1 -> output
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -21,7 +21,7 @@ Simple Yield
Yield Chain Yield Chain
================================================================================ ================================================================================
x -> (foo) -> (bar) -> (abc) x -> foo -> bar -> abc
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -41,3 +41,27 @@ x -> (foo) -> (bar) -> (abc)
(identifier)))) (identifier))))
(function_expression (function_expression
(identifier)))))) (identifier))))))
================================================================================
Yielded Function Call
================================================================================
x -> foo(1)()
--------------------------------------------------------------------------------
(root
(statement
(expression
(function_call
(function_expression
(function_call
(function_expression
(yield
(expression
(identifier))
(function_expression
(identifier))))
(expression
(value
(integer)))))))))

View File

@ -203,19 +203,10 @@ module.exports = grammar({
math: $ => math: $ =>
prec.left( prec.left(
choice( seq(
seq( $.expression,
$.expression, $.math_operator,
$.math_operator, $.expression,
$.expression,
),
seq(
'(',
$.expression,
$.math_operator,
$.expression,
')',
),
), ),
), ),
@ -224,19 +215,10 @@ module.exports = grammar({
logic: $ => logic: $ =>
prec.right( prec.right(
choice( seq(
seq( $.expression,
$.expression, $.logic_operator,
$.logic_operator, $.expression,
$.expression,
),
seq(
'(',
$.expression,
$.logic_operator,
$.expression,
')',
),
), ),
), ),
@ -392,6 +374,7 @@ module.exports = grammar({
$.function_call, $.function_call,
$.identifier, $.identifier,
$.index, $.index,
$.yield,
), ),
), ),
@ -407,13 +390,18 @@ module.exports = grammar({
yield: $ => yield: $ =>
prec.left( prec.left(
1,
seq( seq(
$.expression, $.expression,
'->', '->',
'(',
$.function_expression, $.function_expression,
optional($._expression_list), optional(
')', seq(
'(',
$._expression_list,
')',
),
),
), ),
), ),

View File

@ -617,49 +617,19 @@
"type": "PREC_LEFT", "type": "PREC_LEFT",
"value": 0, "value": 0,
"content": { "content": {
"type": "CHOICE", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SEQ", "type": "SYMBOL",
"members": [ "name": "expression"
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "math_operator"
},
{
"type": "SYMBOL",
"name": "expression"
}
]
}, },
{ {
"type": "SEQ", "type": "SYMBOL",
"members": [ "name": "math_operator"
{ },
"type": "STRING", {
"value": "(" "type": "SYMBOL",
}, "name": "expression"
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "math_operator"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": ")"
}
]
} }
] ]
} }
@ -693,49 +663,19 @@
"type": "PREC_RIGHT", "type": "PREC_RIGHT",
"value": 0, "value": 0,
"content": { "content": {
"type": "CHOICE", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SEQ", "type": "SYMBOL",
"members": [ "name": "expression"
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "logic_operator"
},
{
"type": "SYMBOL",
"name": "expression"
}
]
}, },
{ {
"type": "SEQ", "type": "SYMBOL",
"members": [ "name": "logic_operator"
{ },
"type": "STRING", {
"value": "(" "type": "SYMBOL",
}, "name": "expression"
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "SYMBOL",
"name": "logic_operator"
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": ")"
}
]
} }
] ]
} }
@ -1285,6 +1225,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "index" "name": "index"
},
{
"type": "SYMBOL",
"name": "yield"
} }
] ]
} }
@ -1324,7 +1268,7 @@
}, },
"yield": { "yield": {
"type": "PREC_LEFT", "type": "PREC_LEFT",
"value": 0, "value": 1,
"content": { "content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -1336,10 +1280,6 @@
"type": "STRING", "type": "STRING",
"value": "->" "value": "->"
}, },
{
"type": "STRING",
"value": "("
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "function_expression" "name": "function_expression"
@ -1348,17 +1288,26 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SEQ",
"name": "_expression_list" "members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SYMBOL",
"name": "_expression_list"
},
{
"type": "STRING",
"value": ")"
}
]
}, },
{ {
"type": "BLANK" "type": "BLANK"
} }
] ]
},
{
"type": "STRING",
"value": ")"
} }
] ]
} }

View File

@ -217,6 +217,10 @@
{ {
"type": "index", "type": "index",
"named": true "named": true
},
{
"type": "yield",
"named": true
} }
] ]
} }

File diff suppressed because it is too large Load Diff