1
0

Add builtin function syntax

This commit is contained in:
Jeff 2023-10-21 13:19:01 -04:00
parent 3b82c6d900
commit 02b237b11b
12 changed files with 6000 additions and 5095 deletions

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Error, Result, Value, VariableMap, BUILT_IN_FUNCTIONS};
use crate::{AbstractTree, Error, Result, Value, VariableMap};
use super::{expression::Expression, identifier::Identifier};
@ -38,12 +38,6 @@ impl AbstractTree for FunctionCall {
let definition = if let Some(value) = context.get_value(key)? {
value.as_function().cloned()?
} else {
for function in BUILT_IN_FUNCTIONS {
if key == function.name() {
return function.run(source, context);
}
}
return Err(Error::FunctionIdentifierNotFound(self.name.clone()));
};

View File

@ -8,7 +8,6 @@
pub mod assignment;
pub mod r#async;
pub mod built_in_functions;
pub mod expression;
pub mod filter;
pub mod find;
@ -28,9 +27,9 @@ pub mod value_node;
pub mod r#while;
pub use {
assignment::*, built_in_functions::*, expression::*, filter::*, find::*, function_call::*,
identifier::*, if_else::*, item::*, logic::*, math::*, r#async::*, r#for::*, r#match::*,
r#while::*, remove::*, select::*, statement::*, transform::*,
assignment::*, expression::*, filter::*, find::*, function_call::*, identifier::*, if_else::*,
item::*, logic::*, math::*, r#async::*, r#for::*, r#match::*, r#while::*, remove::*, select::*,
statement::*, transform::*,
};
use tree_sitter::Node;

View File

@ -1,24 +0,0 @@
==================
Assert Boolean
==================
assert true
---
==================
Assert Expression
==================
assert 42 % 2 == 0
---
==================
Assert Equal
==================
assert_equal 42 "the answer"
---

View File

@ -12,8 +12,7 @@ async { (output 'Whaddup') }
(async
(statement
(expression
(function_call
(tool)
(tool
(expression
(value
(string))))))))))
@ -58,23 +57,20 @@ async {
(integer)))))
(statement
(expression
(function_call
(tool)
(tool
(expression
(value
(string)))))))
(else
(statement
(expression
(function_call
(tool)
(tool
(expression
(value
(string)))))))))
(statement
(expression
(function_call
(tool)
(tool
(expression
(value
(string))))))))))
@ -148,8 +144,7 @@ x = async {
(item
(statement
(expression
(function_call
(tool)
(tool
(expression
(identifier)))))
(statement
@ -162,8 +157,7 @@ x = async {
(integer)))))))))
(statement
(expression
(function_call
(tool)
(tool
(expression
(value
(string))))))))))))

View File

@ -28,8 +28,7 @@ for i in [1, 2, 3] {
(item
(statement
(expression
(function_call
(tool)
(tool
(expression
(identifier))))))))))
@ -65,8 +64,7 @@ for list in list_of_lists {
(item
(statement
(expression
(function_call
(tool)
(tool
(expression
(identifier))))))))
(statement
@ -75,8 +73,7 @@ for list in list_of_lists {
(expression
(logic
(expression
(function_call
(tool)
(tool
(expression
(identifier))))
(logic_operator)
@ -85,8 +82,7 @@ for list in list_of_lists {
(integer)))))
(statement
(expression
(function_call
(tool)
(tool
(expression
(value
(string))))))))))))))

View File

@ -58,14 +58,12 @@ function <message number> {
(item
(statement
(expression
(function_call
(tool)
(tool
(expression
(identifier)))))
(statement
(expression
(function_call
(tool)
(tool
(expression
(identifier))))))))))))

View File

@ -28,8 +28,7 @@ transform i in [1, 2, 3] {
(item
(statement
(expression
(function_call
(tool)
(tool
(expression
(identifier))))))))))
@ -67,7 +66,6 @@ list = transform i in ["one", "two", "three"] {
(item
(statement
(expression
(function_call
(tool)
(tool
(expression
(identifier))))))))))))

View File

@ -18,8 +18,7 @@ while true {
(item
(statement
(expression
(function_call
(tool)
(tool
(expression
(value
(string)))))))))))

View File

@ -35,6 +35,7 @@ module.exports = grammar({
$.value,
$.identifier,
$.function_call,
$.tool,
$.math,
$.logic,
)),
@ -252,5 +253,25 @@ module.exports = grammar({
repeat($.statement),
'}'
),
tool: $ => prec.right(seq(
'(',
$._tool_kind,
repeat(seq($.expression, optional(','))),
')',
)),
_tool_kind: $ => choice(
'output',
'output_error',
'assert',
'assert_equal',
'length',
'read',
'write',
),
}
});

View File

@ -130,6 +130,10 @@
"type": "SYMBOL",
"name": "function_call"
},
{
"type": "SYMBOL",
"name": "tool"
},
{
"type": "SYMBOL",
"name": "math"
@ -1038,35 +1042,81 @@
}
]
},
"assert": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "assert"
},
{
"type": "REPEAT",
"content": {
"tool": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SYMBOL",
"name": "expression"
"name": "_tool_kind"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
},
{
"type": "STRING",
"value": ")"
}
}
]
]
}
},
"assert_equal": {
"type": "SEQ",
"_tool_kind": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "output"
},
{
"type": "STRING",
"value": "output_error"
},
{
"type": "STRING",
"value": "assert"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "expression"
}
"type": "STRING",
"value": "assert_equal"
},
{
"type": "STRING",
"value": "length"
},
{
"type": "STRING",
"value": "read"
},
{
"type": "STRING",
"value": "write"
}
]
}

View File

@ -110,6 +110,10 @@
"type": "math",
"named": true
},
{
"type": "tool",
"named": true
},
{
"type": "value",
"named": true
@ -529,6 +533,21 @@
]
}
},
{
"type": "tool",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "expression",
"named": true
}
]
}
},
{
"type": "transform",
"named": true,
@ -698,6 +717,10 @@
"type": "assert",
"named": false
},
{
"type": "assert_equal",
"named": false
},
{
"type": "async",
"named": false
@ -762,6 +785,22 @@
"type": "into",
"named": false
},
{
"type": "length",
"named": false
},
{
"type": "output",
"named": false
},
{
"type": "output_error",
"named": false
},
{
"type": "read",
"named": false
},
{
"type": "remove",
"named": false
@ -794,6 +833,10 @@
"type": "while",
"named": false
},
{
"type": "write",
"named": false
},
{
"type": "{",
"named": false

File diff suppressed because it is too large Load Diff