Improve index parsing
This commit is contained in:
parent
5be7b9b73a
commit
346ff1c0da
@ -19,7 +19,12 @@ impl AbstractTree for FunctionExpression {
|
|||||||
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_expression", node)?;
|
Error::expect_syntax_node(source, "function_expression", node)?;
|
||||||
|
|
||||||
let child = node.child(0).unwrap();
|
let first_child = node.child(0).unwrap();
|
||||||
|
let child = if first_child.is_named() {
|
||||||
|
first_child
|
||||||
|
} else {
|
||||||
|
node.child(1).unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
let function_expression = match child.kind() {
|
let function_expression = match child.kind() {
|
||||||
"identifier" => FunctionExpression::Identifier(Identifier::from_syntax_node(
|
"identifier" => FunctionExpression::Identifier(Identifier::from_syntax_node(
|
||||||
|
@ -94,7 +94,7 @@ impl AbstractTree for Index {
|
|||||||
Type::List(item_type) => Ok(*item_type.clone()),
|
Type::List(item_type) => Ok(*item_type.clone()),
|
||||||
Type::Map => Ok(Type::Any),
|
Type::Map => Ok(Type::Any),
|
||||||
Type::None => Ok(Type::None),
|
Type::None => Ok(Type::None),
|
||||||
_ => todo!(),
|
r#type => Ok(r#type),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ impl BuiltInFunction for Std {
|
|||||||
Error::expect_argument_amount(self, 0, arguments.len())?;
|
Error::expect_argument_amount(self, 0, arguments.len())?;
|
||||||
|
|
||||||
let std_context = STD.get_or_init(|| {
|
let std_context = STD.get_or_init(|| {
|
||||||
let std_source = "say_hi = () <none> { output(hi) }";
|
let std_source = "say_hi = () <none> { output('hi') }";
|
||||||
let std_context = Map::new();
|
let std_context = Map::new();
|
||||||
|
|
||||||
interpret_with_context(std_source, std_context.clone()).unwrap();
|
interpret_with_context(std_source, std_context.clone()).unwrap();
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
//!
|
//!
|
||||||
//! Using this library is simple and straightforward, see the [inferface] module for instructions on
|
//! Using this library is simple and straightforward, see the [inferface] module for instructions on
|
||||||
//! interpreting Dust code. Most of the language's features are implemented in the [tools] module.
|
//! interpreting Dust code. Most of the language's features are implemented in the [tools] module.
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
abstract_tree::*,
|
abstract_tree::*,
|
||||||
built_in_functions::{BuiltInFunction, BUILT_IN_FUNCTIONS},
|
built_in_functions::{BuiltInFunction, BUILT_IN_FUNCTIONS},
|
||||||
|
@ -338,17 +338,29 @@ mod index {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn complex_index() {
|
fn index_function_calls() {
|
||||||
let test = interpret(
|
assert_eq!(
|
||||||
|
interpret(
|
||||||
"
|
"
|
||||||
x = [1 2 3]
|
x = [1 2 3]
|
||||||
y = () <int> { 2 }
|
y = () <int> { 2 }
|
||||||
x:y()
|
x:y()
|
||||||
",
|
",
|
||||||
)
|
),
|
||||||
.unwrap();
|
Ok(Value::Integer(3))
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(Value::Integer(3), test);
|
assert_eq!(
|
||||||
|
interpret(
|
||||||
|
"
|
||||||
|
x = {
|
||||||
|
y = () <int> { 2 }
|
||||||
|
}
|
||||||
|
(x:y)()
|
||||||
|
",
|
||||||
|
),
|
||||||
|
Ok(Value::Integer(2))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,7 +200,7 @@ module.exports = grammar({
|
|||||||
|
|
||||||
index: $ =>
|
index: $ =>
|
||||||
prec.left(
|
prec.left(
|
||||||
2,
|
1,
|
||||||
seq(
|
seq(
|
||||||
$.expression,
|
$.expression,
|
||||||
':',
|
':',
|
||||||
@ -377,6 +377,16 @@ module.exports = grammar({
|
|||||||
),
|
),
|
||||||
|
|
||||||
function_expression: $ =>
|
function_expression: $ =>
|
||||||
|
choice(
|
||||||
|
$._function_expression_kind,
|
||||||
|
seq(
|
||||||
|
'(',
|
||||||
|
$._function_expression_kind,
|
||||||
|
')',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
_function_expression_kind: $ =>
|
||||||
prec(
|
prec(
|
||||||
1,
|
1,
|
||||||
choice(
|
choice(
|
||||||
@ -387,7 +397,6 @@ module.exports = grammar({
|
|||||||
$.yield,
|
$.yield,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
function_call: $ =>
|
function_call: $ =>
|
||||||
prec.right(
|
prec.right(
|
||||||
seq(
|
seq(
|
||||||
|
@ -599,7 +599,7 @@
|
|||||||
},
|
},
|
||||||
"index": {
|
"index": {
|
||||||
"type": "PREC_LEFT",
|
"type": "PREC_LEFT",
|
||||||
"value": 2,
|
"value": 1,
|
||||||
"content": {
|
"content": {
|
||||||
"type": "SEQ",
|
"type": "SEQ",
|
||||||
"members": [
|
"members": [
|
||||||
@ -1231,6 +1231,32 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"function_expression": {
|
"function_expression": {
|
||||||
|
"type": "CHOICE",
|
||||||
|
"members": [
|
||||||
|
{
|
||||||
|
"type": "SYMBOL",
|
||||||
|
"name": "_function_expression_kind"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "SEQ",
|
||||||
|
"members": [
|
||||||
|
{
|
||||||
|
"type": "STRING",
|
||||||
|
"value": "("
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "SYMBOL",
|
||||||
|
"name": "_function_expression_kind"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "STRING",
|
||||||
|
"value": ")"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"_function_expression_kind": {
|
||||||
"type": "PREC",
|
"type": "PREC",
|
||||||
"value": 1,
|
"value": 1,
|
||||||
"content": {
|
"content": {
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user