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

View File

@ -8,7 +8,6 @@
pub mod assignment; pub mod assignment;
pub mod r#async; pub mod r#async;
pub mod built_in_functions;
pub mod expression; pub mod expression;
pub mod filter; pub mod filter;
pub mod find; pub mod find;
@ -28,9 +27,9 @@ pub mod value_node;
pub mod r#while; pub mod r#while;
pub use { pub use {
assignment::*, built_in_functions::*, expression::*, filter::*, find::*, function_call::*, assignment::*, expression::*, filter::*, find::*, function_call::*, identifier::*, if_else::*,
identifier::*, if_else::*, item::*, logic::*, math::*, r#async::*, r#for::*, r#match::*, item::*, logic::*, math::*, r#async::*, r#for::*, r#match::*, r#while::*, remove::*, select::*,
r#while::*, remove::*, select::*, statement::*, transform::*, statement::*, transform::*,
}; };
use tree_sitter::Node; 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 (async
(statement (statement
(expression (expression
(function_call (tool
(tool)
(expression (expression
(value (value
(string)))))))))) (string))))))))))
@ -58,23 +57,20 @@ async {
(integer))))) (integer)))))
(statement (statement
(expression (expression
(function_call (tool
(tool)
(expression (expression
(value (value
(string))))))) (string)))))))
(else (else
(statement (statement
(expression (expression
(function_call (tool
(tool)
(expression (expression
(value (value
(string))))))))) (string)))))))))
(statement (statement
(expression (expression
(function_call (tool
(tool)
(expression (expression
(value (value
(string)))))))))) (string))))))))))
@ -148,8 +144,7 @@ x = async {
(item (item
(statement (statement
(expression (expression
(function_call (tool
(tool)
(expression (expression
(identifier))))) (identifier)))))
(statement (statement
@ -162,8 +157,7 @@ x = async {
(integer))))))))) (integer)))))))))
(statement (statement
(expression (expression
(function_call (tool
(tool)
(expression (expression
(value (value
(string)))))))))))) (string))))))))))))

View File

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

View File

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

View File

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

View File

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

View File

@ -35,6 +35,7 @@ module.exports = grammar({
$.value, $.value,
$.identifier, $.identifier,
$.function_call, $.function_call,
$.tool,
$.math, $.math,
$.logic, $.logic,
)), )),
@ -252,5 +253,25 @@ module.exports = grammar({
repeat($.statement), 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", "type": "SYMBOL",
"name": "function_call" "name": "function_call"
}, },
{
"type": "SYMBOL",
"name": "tool"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "math" "name": "math"
@ -1038,35 +1042,81 @@
} }
] ]
}, },
"assert": { "tool": {
"type": "SEQ", "type": "PREC_RIGHT",
"members": [ "value": 0,
{ "content": {
"type": "STRING", "type": "SEQ",
"value": "assert" "members": [
}, {
{ "type": "STRING",
"type": "REPEAT", "value": "("
"content": { },
{
"type": "SYMBOL", "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": { "_tool_kind": {
"type": "SEQ", "type": "CHOICE",
"members": [ "members": [
{
"type": "STRING",
"value": "output"
},
{
"type": "STRING",
"value": "output_error"
},
{ {
"type": "STRING", "type": "STRING",
"value": "assert" "value": "assert"
}, },
{ {
"type": "REPEAT", "type": "STRING",
"content": { "value": "assert_equal"
"type": "SYMBOL", },
"name": "expression" {
} "type": "STRING",
"value": "length"
},
{
"type": "STRING",
"value": "read"
},
{
"type": "STRING",
"value": "write"
} }
] ]
} }

View File

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

File diff suppressed because it is too large Load Diff