1
0

Revise function syntax

This commit is contained in:
Jeff 2023-12-29 20:14:03 -05:00
parent e10429e1e9
commit 55de33ceb7
13 changed files with 15087 additions and 13886 deletions

View File

@ -25,7 +25,7 @@ impl AbstractTree for FunctionCall {
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, "function_call", node)?; Error::expect_syntax_node(source, "function_call", node)?;
let function_node = node.child(1).unwrap(); let function_node = node.child(0).unwrap();
let function_expression = let function_expression =
FunctionExpression::from_syntax_node(source, function_node, context)?; FunctionExpression::from_syntax_node(source, function_node, context)?;
let function_type = function_expression.expected_type(context)?; let function_type = function_expression.expected_type(context)?;
@ -170,8 +170,8 @@ mod tests {
assert_eq!( assert_eq!(
interpret( interpret(
" "
foobar = (fn message <str>) <str> { message } foobar = (message <str>) -> <str> { message }
(foobar 'Hiya') foobar('Hiya')
", ",
), ),
Ok(Value::String("Hiya".to_string())) Ok(Value::String("Hiya".to_string()))
@ -183,11 +183,10 @@ mod tests {
assert_eq!( assert_eq!(
interpret( interpret(
" "
foobar = (fn cb <() -> str>) <str> { foobar = (cb <() -> str>) <str> {
(cb) cb()
} }
foobar(() <str> { 'Hiya' })
(foobar (fn) <str> { 'Hiya' })
", ",
), ),
Ok(Value::String("Hiya".to_string())) Ok(Value::String("Hiya".to_string()))
@ -196,6 +195,6 @@ mod tests {
#[test] #[test]
fn evaluate_built_in_function_call() { fn evaluate_built_in_function_call() {
assert_eq!(interpret("(output 'Hiya')"), Ok(Value::Option(None))); assert_eq!(interpret("output('Hiya')"), Ok(Value::Option(None)));
} }
} }

View File

@ -121,8 +121,8 @@ mod tests {
let test = interpret( let test = interpret(
" "
x = [1 2 3] x = [1 2 3]
y = (fn) <int> { 0 } y = () -> <int> { 0 }
x:(y) x:y()
", ",
) )
.unwrap(); .unwrap();

View File

@ -279,10 +279,10 @@ mod tests {
fn callback_type_check() { fn callback_type_check() {
let result = interpret( let result = interpret(
" "
x = (fn cb <() -> bool>) <bool> { x = (cb <() -> bool>) <bool> {
(cb) cb()
} }
(x (fn) <int> { 1 }) x(() <int> { 1 })
", ",
); );

View File

@ -33,7 +33,7 @@ impl AbstractTree for ValueNode {
let mut parameters = Vec::new(); let mut parameters = Vec::new();
let mut parameter_types = Vec::new(); let mut parameter_types = Vec::new();
for index in 2..child_count - 2 { for index in 1..child_count - 3 {
let child = child.child(index).unwrap(); let child = child.child(index).unwrap();
if child.kind() == "identifier" { if child.kind() == "identifier" {
@ -338,14 +338,14 @@ mod tests {
#[test] #[test]
fn evaluate_function() { fn evaluate_function() {
let result = interpret("(fn) <int> { 1 }"); let result = interpret("() -> <int> { 1 }");
let value = result.unwrap(); let value = result.unwrap();
let function = value.as_function().unwrap(); let function = value.as_function().unwrap();
assert_eq!(&Vec::<Identifier>::with_capacity(0), function.parameters()); assert_eq!(&Vec::<Identifier>::with_capacity(0), function.parameters());
assert_eq!(Ok(&Type::Integer), function.return_type()); assert_eq!(Ok(&Type::Integer), function.return_type());
let result = interpret("(fn x <bool>) <bool> {true}"); let result = interpret("(x <bool>) -> <bool> {true}");
let value = result.unwrap(); let value = result.unwrap();
let function = value.as_function().unwrap(); let function = value.as_function().unwrap();

View File

@ -2,7 +2,7 @@
Simple Async Statements Simple Async Statements
================================================================================ ================================================================================
async { (output 'Whaddup') } async { output ('Whaddup') }
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -3,7 +3,7 @@ Simple For Loop
================================================================================ ================================================================================
for i in [1, 2, 3] { for i in [1, 2, 3] {
(output i) output(i)
} }
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -40,7 +40,7 @@ Nested For Loop
for list in list_of_lists { for list in list_of_lists {
for item in list { for item in list {
(output item) output(item)
} }
} }

View File

@ -2,7 +2,7 @@
Anonymous Function Anonymous Function
================================================================================ ================================================================================
(fn) <str> { "Hiya" } () <str> { "Hiya" }
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -20,10 +20,10 @@ Anonymous Function
(string)))))))))) (string))))))))))
================================================================================ ================================================================================
Function Declaration Function Assignment
================================================================================ ================================================================================
foobar = (fn x <int>, y <int>) <int> { foobar = (x <int>, y <int>) <int> {
x + y x + y
} }
@ -57,10 +57,10 @@ foobar = (fn x <int>, y <int>) <int> {
(identifier))))))))))))) (identifier)))))))))))))
================================================================================ ================================================================================
Function Call Identifier Function Call
================================================================================ ================================================================================
(foobar "Hiya") foobar("Hiya")
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -75,37 +75,36 @@ Function Call
(string))))))) (string)))))))
================================================================================ ================================================================================
Function Expressions Index Function Call
================================================================================ ================================================================================
(foobar "Hiya") foo:bar("Hiya")
(foo:bar "Hiya")
((foobar) "Hiya")
((fn msg <str>) <str> { msg } "Hiya")
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(statement (statement
(expression (expression
(function_call (index
(function_expression (expression
(identifier)) (identifier))
(expression (expression
(value (function_call
(string)))))) (function_expression
(statement
(expression
(function_call
(function_expression
(index
(expression
(identifier)) (identifier))
(expression (expression
(identifier)))) (value
(expression (string)))))))))
(value
(string)))))) ================================================================================
Double Function Call
================================================================================
foobar()("Hiya")
--------------------------------------------------------------------------------
(root
(statement (statement
(expression (expression
(function_call (function_call
@ -115,22 +114,31 @@ Function Expressions
(identifier)))) (identifier))))
(expression (expression
(value (value
(string)))))) (string)))))))
================================================================================
Anonymous Function Call
================================================================================
(msg <str>) <str> { msg } ("Hiya");
--------------------------------------------------------------------------------
(root
(statement (statement
(expression (expression
(function_call (function_call
(function_expression (function_expression
(value (function
(function (identifier)
(identifier) (type_definition
(type_definition (type))
(type)) (type_definition
(type_definition (type))
(type)) (block
(block (statement
(statement (expression
(expression (identifier))))))
(identifier)))))))
(expression (expression
(value (value
(string))))))) (string)))))))
@ -139,7 +147,7 @@ Function Expressions
Complex Function Call Complex Function Call
================================================================================ ================================================================================
(foobar foobar(
"hi" "hi"
42 42
{ {
@ -180,35 +188,11 @@ Complex Function Call
Callback Function Call Callback Function Call
================================================================================ ================================================================================
x = (fn cb <() -> bool>) <bool> { x(() <bool> { true })
(cb)
}
(x (fn) <bool> { true })
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(function
(identifier)
(type_definition
(type
(type)))
(type_definition
(type))
(block
(statement
(expression
(function_call
(function_expression
(identifier))))))))))))
(statement (statement
(expression (expression
(function_call (function_call
@ -229,7 +213,7 @@ x = (fn cb <() -> bool>) <bool> {
Nested Function Call Nested Function Call
================================================================================ ================================================================================
(from_json (read 'file.json')) from_json(read('file.json'))
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -95,7 +95,7 @@ Nested Indexes
Function Call Index Function Call Index
================================================================================ ================================================================================
x:(y):0 x:y():0
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -3,7 +3,7 @@ While Loop
================================================================================ ================================================================================
while true { while true {
(output "This is a bad idea...") output ("This is a bad idea...")
} }
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -78,7 +78,7 @@ module.exports = grammar({
$.boolean, $.boolean,
$.list, $.list,
$.map, $.map,
$.option $.option,
), ),
integer: $ => integer: $ =>
@ -177,7 +177,6 @@ module.exports = grammar({
'}', '}',
), ),
option: $ => option: $ =>
choice( choice(
'none', 'none',
@ -191,7 +190,7 @@ module.exports = grammar({
index: $ => index: $ =>
prec.left( prec.left(
1, 2,
seq( seq(
$.expression, $.expression,
':', ':',
@ -366,14 +365,13 @@ module.exports = grammar({
'(', '(',
$.type, $.type,
')', ')',
) ),
), ),
), ),
function: $ => function: $ =>
seq( seq(
'(', '(',
'fn',
repeat( repeat(
seq( seq(
$.identifier, $.identifier,
@ -385,20 +383,23 @@ module.exports = grammar({
$.type_definition, $.type_definition,
$.block, $.block,
), ),
function_expression: $ => function_expression: $ =>
choice( prec(
$.function_call, 1,
$.identifier, choice(
$.index, $.function,
$.value, $.function_call,
$.identifier,
$.index,
),
), ),
function_call: $ => function_call: $ =>
prec.right( prec.right(
seq( seq(
'(',
$.function_expression, $.function_expression,
'(',
optional($._expression_list), optional($._expression_list),
')', ')',
), ),

View File

@ -573,7 +573,7 @@
}, },
"index": { "index": {
"type": "PREC_LEFT", "type": "PREC_LEFT",
"value": 1, "value": 2,
"content": { "content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -1222,10 +1222,6 @@
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
}, },
{
"type": "STRING",
"value": "fn"
},
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
@ -1269,25 +1265,29 @@
] ]
}, },
"function_expression": { "function_expression": {
"type": "CHOICE", "type": "PREC",
"members": [ "value": 1,
{ "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "function_call" "members": [
}, {
{ "type": "SYMBOL",
"type": "SYMBOL", "name": "function"
"name": "identifier" },
}, {
{ "type": "SYMBOL",
"type": "SYMBOL", "name": "function_call"
"name": "index" },
}, {
{ "type": "SYMBOL",
"type": "SYMBOL", "name": "identifier"
"name": "value" },
} {
] "type": "SYMBOL",
"name": "index"
}
]
}
}, },
"function_call": { "function_call": {
"type": "PREC_RIGHT", "type": "PREC_RIGHT",
@ -1295,14 +1295,14 @@
"content": { "content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "STRING",
"value": "("
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "function_expression" "name": "function_expression"
}, },
{
"type": "STRING",
"value": "("
},
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [

View File

@ -202,6 +202,10 @@
"multiple": false, "multiple": false,
"required": true, "required": true,
"types": [ "types": [
{
"type": "function",
"named": true
},
{ {
"type": "function_call", "type": "function_call",
"named": true "named": true
@ -213,10 +217,6 @@
{ {
"type": "index", "type": "index",
"named": true "named": true
},
{
"type": "value",
"named": true
} }
] ]
} }
@ -784,10 +784,6 @@
"type": "float", "type": "float",
"named": false "named": false
}, },
{
"type": "fn",
"named": false
},
{ {
"type": "for", "type": "for",
"named": false "named": false

File diff suppressed because it is too large Load Diff