Revise function syntax
This commit is contained in:
parent
e10429e1e9
commit
55de33ceb7
@ -25,7 +25,7 @@ impl AbstractTree for FunctionCall {
|
||||
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
|
||||
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 =
|
||||
FunctionExpression::from_syntax_node(source, function_node, context)?;
|
||||
let function_type = function_expression.expected_type(context)?;
|
||||
@ -170,8 +170,8 @@ mod tests {
|
||||
assert_eq!(
|
||||
interpret(
|
||||
"
|
||||
foobar = (fn message <str>) <str> { message }
|
||||
(foobar 'Hiya')
|
||||
foobar = (message <str>) -> <str> { message }
|
||||
foobar('Hiya')
|
||||
",
|
||||
),
|
||||
Ok(Value::String("Hiya".to_string()))
|
||||
@ -183,11 +183,10 @@ mod tests {
|
||||
assert_eq!(
|
||||
interpret(
|
||||
"
|
||||
foobar = (fn cb <() -> str>) <str> {
|
||||
(cb)
|
||||
foobar = (cb <() -> str>) <str> {
|
||||
cb()
|
||||
}
|
||||
|
||||
(foobar (fn) <str> { 'Hiya' })
|
||||
foobar(() <str> { 'Hiya' })
|
||||
",
|
||||
),
|
||||
Ok(Value::String("Hiya".to_string()))
|
||||
@ -196,6 +195,6 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn evaluate_built_in_function_call() {
|
||||
assert_eq!(interpret("(output 'Hiya')"), Ok(Value::Option(None)));
|
||||
assert_eq!(interpret("output('Hiya')"), Ok(Value::Option(None)));
|
||||
}
|
||||
}
|
||||
|
@ -121,8 +121,8 @@ mod tests {
|
||||
let test = interpret(
|
||||
"
|
||||
x = [1 2 3]
|
||||
y = (fn) <int> { 0 }
|
||||
x:(y)
|
||||
y = () -> <int> { 0 }
|
||||
x:y()
|
||||
",
|
||||
)
|
||||
.unwrap();
|
||||
|
@ -279,10 +279,10 @@ mod tests {
|
||||
fn callback_type_check() {
|
||||
let result = interpret(
|
||||
"
|
||||
x = (fn cb <() -> bool>) <bool> {
|
||||
(cb)
|
||||
x = (cb <() -> bool>) <bool> {
|
||||
cb()
|
||||
}
|
||||
(x (fn) <int> { 1 })
|
||||
x(() <int> { 1 })
|
||||
",
|
||||
);
|
||||
|
||||
|
@ -33,7 +33,7 @@ impl AbstractTree for ValueNode {
|
||||
let mut parameters = 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();
|
||||
|
||||
if child.kind() == "identifier" {
|
||||
@ -338,14 +338,14 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn evaluate_function() {
|
||||
let result = interpret("(fn) <int> { 1 }");
|
||||
let result = interpret("() -> <int> { 1 }");
|
||||
let value = result.unwrap();
|
||||
let function = value.as_function().unwrap();
|
||||
|
||||
assert_eq!(&Vec::<Identifier>::with_capacity(0), function.parameters());
|
||||
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 function = value.as_function().unwrap();
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
Simple Async Statements
|
||||
================================================================================
|
||||
|
||||
async { (output 'Whaddup') }
|
||||
async { output ('Whaddup') }
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
|
@ -3,7 +3,7 @@ Simple For Loop
|
||||
================================================================================
|
||||
|
||||
for i in [1, 2, 3] {
|
||||
(output i)
|
||||
output(i)
|
||||
}
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
@ -40,7 +40,7 @@ Nested For Loop
|
||||
|
||||
for list in list_of_lists {
|
||||
for item in list {
|
||||
(output item)
|
||||
output(item)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
Anonymous Function
|
||||
================================================================================
|
||||
|
||||
(fn) <str> { "Hiya" }
|
||||
() <str> { "Hiya" }
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
@ -20,10 +20,10 @@ Anonymous Function
|
||||
(string))))))))))
|
||||
|
||||
================================================================================
|
||||
Function Declaration
|
||||
Function Assignment
|
||||
================================================================================
|
||||
|
||||
foobar = (fn x <int>, y <int>) <int> {
|
||||
foobar = (x <int>, y <int>) <int> {
|
||||
x + y
|
||||
}
|
||||
|
||||
@ -57,10 +57,10 @@ foobar = (fn x <int>, y <int>) <int> {
|
||||
(identifier)))))))))))))
|
||||
|
||||
================================================================================
|
||||
Function Call
|
||||
Identifier Function Call
|
||||
================================================================================
|
||||
|
||||
(foobar "Hiya")
|
||||
foobar("Hiya")
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
@ -75,37 +75,36 @@ Function Call
|
||||
(string)))))))
|
||||
|
||||
================================================================================
|
||||
Function Expressions
|
||||
Index Function Call
|
||||
================================================================================
|
||||
|
||||
(foobar "Hiya")
|
||||
(foo:bar "Hiya")
|
||||
((foobar) "Hiya")
|
||||
((fn msg <str>) <str> { msg } "Hiya")
|
||||
foo:bar("Hiya")
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(root
|
||||
(statement
|
||||
(expression
|
||||
(function_call
|
||||
(function_expression
|
||||
(index
|
||||
(expression
|
||||
(identifier))
|
||||
(expression
|
||||
(value
|
||||
(string))))))
|
||||
(statement
|
||||
(expression
|
||||
(function_call
|
||||
(function_expression
|
||||
(index
|
||||
(expression
|
||||
(function_call
|
||||
(function_expression
|
||||
(identifier))
|
||||
(expression
|
||||
(identifier))))
|
||||
(expression
|
||||
(value
|
||||
(string))))))
|
||||
(value
|
||||
(string)))))))))
|
||||
|
||||
================================================================================
|
||||
Double Function Call
|
||||
================================================================================
|
||||
|
||||
foobar()("Hiya")
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(root
|
||||
(statement
|
||||
(expression
|
||||
(function_call
|
||||
@ -115,22 +114,31 @@ Function Expressions
|
||||
(identifier))))
|
||||
(expression
|
||||
(value
|
||||
(string))))))
|
||||
(string)))))))
|
||||
|
||||
================================================================================
|
||||
Anonymous Function Call
|
||||
================================================================================
|
||||
|
||||
(msg <str>) <str> { msg } ("Hiya");
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(root
|
||||
(statement
|
||||
(expression
|
||||
(function_call
|
||||
(function_expression
|
||||
(value
|
||||
(function
|
||||
(identifier)
|
||||
(type_definition
|
||||
(type))
|
||||
(type_definition
|
||||
(type))
|
||||
(block
|
||||
(statement
|
||||
(expression
|
||||
(identifier)))))))
|
||||
(function
|
||||
(identifier)
|
||||
(type_definition
|
||||
(type))
|
||||
(type_definition
|
||||
(type))
|
||||
(block
|
||||
(statement
|
||||
(expression
|
||||
(identifier))))))
|
||||
(expression
|
||||
(value
|
||||
(string)))))))
|
||||
@ -139,7 +147,7 @@ Function Expressions
|
||||
Complex Function Call
|
||||
================================================================================
|
||||
|
||||
(foobar
|
||||
foobar(
|
||||
"hi"
|
||||
42
|
||||
{
|
||||
@ -180,35 +188,11 @@ Complex Function Call
|
||||
Callback Function Call
|
||||
================================================================================
|
||||
|
||||
x = (fn cb <() -> bool>) <bool> {
|
||||
(cb)
|
||||
}
|
||||
|
||||
(x (fn) <bool> { true })
|
||||
x(() <bool> { true })
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(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
|
||||
(expression
|
||||
(function_call
|
||||
@ -229,7 +213,7 @@ x = (fn cb <() -> bool>) <bool> {
|
||||
Nested Function Call
|
||||
================================================================================
|
||||
|
||||
(from_json (read 'file.json'))
|
||||
from_json(read('file.json'))
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
|
@ -95,7 +95,7 @@ Nested Indexes
|
||||
Function Call Index
|
||||
================================================================================
|
||||
|
||||
x:(y):0
|
||||
x:y():0
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
|
@ -3,7 +3,7 @@ While Loop
|
||||
================================================================================
|
||||
|
||||
while true {
|
||||
(output "This is a bad idea...")
|
||||
output ("This is a bad idea...")
|
||||
}
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
@ -78,7 +78,7 @@ module.exports = grammar({
|
||||
$.boolean,
|
||||
$.list,
|
||||
$.map,
|
||||
$.option
|
||||
$.option,
|
||||
),
|
||||
|
||||
integer: $ =>
|
||||
@ -177,7 +177,6 @@ module.exports = grammar({
|
||||
'}',
|
||||
),
|
||||
|
||||
|
||||
option: $ =>
|
||||
choice(
|
||||
'none',
|
||||
@ -191,7 +190,7 @@ module.exports = grammar({
|
||||
|
||||
index: $ =>
|
||||
prec.left(
|
||||
1,
|
||||
2,
|
||||
seq(
|
||||
$.expression,
|
||||
':',
|
||||
@ -366,14 +365,13 @@ module.exports = grammar({
|
||||
'(',
|
||||
$.type,
|
||||
')',
|
||||
)
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
function: $ =>
|
||||
seq(
|
||||
'(',
|
||||
'fn',
|
||||
repeat(
|
||||
seq(
|
||||
$.identifier,
|
||||
@ -385,20 +383,23 @@ module.exports = grammar({
|
||||
$.type_definition,
|
||||
$.block,
|
||||
),
|
||||
|
||||
|
||||
function_expression: $ =>
|
||||
choice(
|
||||
$.function_call,
|
||||
$.identifier,
|
||||
$.index,
|
||||
$.value,
|
||||
prec(
|
||||
1,
|
||||
choice(
|
||||
$.function,
|
||||
$.function_call,
|
||||
$.identifier,
|
||||
$.index,
|
||||
),
|
||||
),
|
||||
|
||||
function_call: $ =>
|
||||
prec.right(
|
||||
seq(
|
||||
'(',
|
||||
$.function_expression,
|
||||
'(',
|
||||
optional($._expression_list),
|
||||
')',
|
||||
),
|
||||
|
@ -573,7 +573,7 @@
|
||||
},
|
||||
"index": {
|
||||
"type": "PREC_LEFT",
|
||||
"value": 1,
|
||||
"value": 2,
|
||||
"content": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
@ -1222,10 +1222,6 @@
|
||||
"type": "STRING",
|
||||
"value": "("
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "fn"
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
@ -1269,25 +1265,29 @@
|
||||
]
|
||||
},
|
||||
"function_expression": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "function_call"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "identifier"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "index"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "value"
|
||||
}
|
||||
]
|
||||
"type": "PREC",
|
||||
"value": 1,
|
||||
"content": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "function"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "function_call"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "identifier"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "index"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"function_call": {
|
||||
"type": "PREC_RIGHT",
|
||||
@ -1295,14 +1295,14 @@
|
||||
"content": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "("
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "function_expression"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "("
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
|
@ -202,6 +202,10 @@
|
||||
"multiple": false,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "function",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "function_call",
|
||||
"named": true
|
||||
@ -213,10 +217,6 @@
|
||||
{
|
||||
"type": "index",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "value",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -784,10 +784,6 @@
|
||||
"type": "float",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "fn",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "for",
|
||||
"named": false
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user