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 = []
while (length numbers) < count {
numbers += (random_integer)
while length(numbers) < count {
numbers += random_integer()
}
(output "Made " + (length numbers) + " numbers.")
output("Made " + length(numbers) + " numbers.")
}
(output "This will print first.")
output("This will print first.")
async {
(create_random_numbers 1000)
(create_random_numbers 100)
(create_random_numbers 10)
create_random_numbers(1000)
create_random_numbers(100)
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")
characters = (download "https://api.sampleapis.com/futurama/characters")
episodes = (download "https://api.sampleapis.com/futurama/episodes")
cast = download("https://api.sampleapis.com/futurama/cast")
characters = download("https://api.sampleapis.com/futurama/characters")
episodes = download("https://api.sampleapis.com/futurama/episodes")
async {
cast_len = (length (from_json cast))
characters_len = (length (from_json characters))
episodes_len = (length (from_json episodes))
cast_len = length(from_json(cast))
characters_len = length(from_json(characters))
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']
suspects = ['White' 'Green' 'Scarlett']
weapons = ['Rope' 'Lead_Pipe' 'Knife']
}
is_ready_to_solve = (fn cards <map>) <bool> {
((length cards:suspects) == 1)
&& ((length cards:rooms) == 1)
&& ((length cards:weapons) == 1)
is_ready_to_solve = (cards <map>) <bool> {
length(cards:suspects) == 1
&& length(cards:rooms) == 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:suspects -= opponent_card
cards:weapons -= opponent_card
cards
}
make_guess = (fn cards <map>, current_room <str>) <map> {
if (is_ready_to_solve cards) {
(output 'It was '
make_guess = (cards <map>, current_room <str>) <none> {
if is_ready_to_solve(cards) {
output(
'It was '
+ cards:suspects:0
+ ' in the '
+ cards:rooms:0
+ ' with the '
+ cards:weapons:0
+ '!')
+ '!'
)
} else {
(output 'I accuse '
+ (random cards:suspects)
output(
'I accuse '
+ random(cards:suspects)
+ ' in the '
+ current_room
+ ' with the '
+ (random cards:weapons)
+ '.')
+ random(cards:weapons)
+ '.'
)
}
cards
}
take_turn = (fn cards <map>, opponent_card <str>, current_room <str>) <map> {
cards = (remove_card cards opponent_card)
cards = (make_guess cards current_room)
cards
take_turn = (cards <map>, opponent_card <str>, current_room <str>) <none> {
remove_card(cards opponent_card)
make_guess(cards current_room)
}
all_cards
-> (take_turn 'Rope' 'Kitchen')
-> (take_turn 'Library' 'Kitchen')
-> (take_turn 'Conservatory' 'Kitchen')
-> (take_turn 'White' 'Kitchen')
-> (take_turn 'Green' 'Kitchen')
-> (take_turn 'Knife' 'Kitchen')
take_turn(cards 'Rope' 'Kitchen')
take_turn(cards 'Library' 'Kitchen')
take_turn(cards 'Conservatory' 'Kitchen')
take_turn(cards 'White' 'Kitchen')
take_turn(cards 'Green' 'Kitchen')
take_turn(cards 'Knife' 'Kitchen')

View File

@ -1,5 +1,5 @@
raw_data = (download "https://api.sampleapis.com/futurama/cast")
cast_data = (from_json raw_data)
raw_data = download("https://api.sampleapis.com/futurama/cast")
cast_data = from_json(raw_data)
names = []
@ -7,4 +7,4 @@ for cast_member in cast_data {
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 {
1
} 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
if divides_by_3 && divides_by_5 {
(output 'fizzbuzz')
output('fizzbuzz')
} else if divides_by_3 {
(output 'fizz')
output('fizz')
} else if divides_by_5 {
(output 'buzz')
output('buzz')
} else {
(output count)
output(count)
}
count += 1

View File

@ -2,5 +2,5 @@ list = [1 2 3]
for i in list {
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
z = numbers:2
(assert_equal x + y, z)
assert_equal(x + y, z)

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,4 @@
1 -> (output)
add_one = (fn numbers <[int]>) <[int]> {
add_one = (numbers <[int]>) <[int]> {
new_numbers = []
for number in numbers {
@ -10,6 +8,6 @@ add_one = (fn numbers <[int]>) <[int]> {
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::Index(index) => index.run(source, context)?,
FunctionExpression::Yield(r#yield) => r#yield.run(source, context)?,
};
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::Value(value_node) => value_node.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::{
AbstractTree, Error, FunctionCall, Identifier, Index, Map, Result, Type, Value, ValueNode,
Yield,
};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
@ -11,6 +12,7 @@ pub enum FunctionExpression {
FunctionCall(Box<FunctionCall>),
Value(ValueNode),
Index(Index),
Yield(Box<Yield>),
}
impl AbstractTree for FunctionExpression {
@ -30,6 +32,9 @@ impl AbstractTree for FunctionExpression {
FunctionExpression::Value(ValueNode::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 {
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::Value(value_node) => value_node.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::Value(value_node) => value_node.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 {
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 = Expression::from_syntax_node(source, collection_node, context)?;

View File

@ -13,6 +13,8 @@ pub struct Logic {
impl AbstractTree for Logic {
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 (left_node, operator_node, right_node) = {
if first_node.is_named() {

View File

@ -16,6 +16,8 @@ pub struct Math {
impl AbstractTree for Math {
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 = Expression::from_syntax_node(source, left_node, context)?;

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
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.
///
@ -14,7 +14,7 @@ pub struct While {
impl AbstractTree for While {
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 = Expression::from_syntax_node(source, expression_node, context)?;

View File

@ -2,8 +2,8 @@ use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{
function_expression::FunctionExpression, AbstractTree, Expression, FunctionCall, Map, Result,
Type, Value,
function_expression::FunctionExpression, AbstractTree, Error, Expression, FunctionCall, Map,
Result, Type, Value,
};
/// Abstract representation of a yield expression.
@ -16,18 +16,20 @@ pub struct Yield {
impl AbstractTree for Yield {
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 = 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 =
FunctionExpression::from_syntax_node(source, expression_node, context)?;
FunctionExpression::from_syntax_node(source, function_node, context)?;
let mut arguments = Vec::new();
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();
if child.is_named() {

View File

@ -2,7 +2,7 @@
Simple Yield
================================================================================
1 -> (output)
1 -> output
--------------------------------------------------------------------------------
@ -21,7 +21,7 @@ Simple Yield
Yield Chain
================================================================================
x -> (foo) -> (bar) -> (abc)
x -> foo -> bar -> abc
--------------------------------------------------------------------------------
@ -41,3 +41,27 @@ x -> (foo) -> (bar) -> (abc)
(identifier))))
(function_expression
(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: $ =>
prec.left(
choice(
seq(
$.expression,
$.math_operator,
$.expression,
),
seq(
'(',
$.expression,
$.math_operator,
$.expression,
')',
),
seq(
$.expression,
$.math_operator,
$.expression,
),
),
@ -224,19 +215,10 @@ module.exports = grammar({
logic: $ =>
prec.right(
choice(
seq(
$.expression,
$.logic_operator,
$.expression,
),
seq(
'(',
$.expression,
$.logic_operator,
$.expression,
')',
),
seq(
$.expression,
$.logic_operator,
$.expression,
),
),
@ -392,6 +374,7 @@ module.exports = grammar({
$.function_call,
$.identifier,
$.index,
$.yield,
),
),
@ -407,13 +390,18 @@ module.exports = grammar({
yield: $ =>
prec.left(
1,
seq(
$.expression,
'->',
'(',
$.function_expression,
optional($._expression_list),
')',
optional(
seq(
'(',
$._expression_list,
')',
),
),
),
),

View File

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

View File

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

File diff suppressed because it is too large Load Diff