From 3a97ba76a017e06ff7d687e6d838f0439b821921 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 17 Mar 2024 21:42:53 -0400 Subject: [PATCH] Get read_line built-in working --- src/error.rs | 6 +++++- src/parser.rs | 44 ++++++++++++++++++++------------------------ src/value.rs | 12 +++++++++++- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/error.rs b/src/error.rs index 06f562e..f41512a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -117,7 +117,11 @@ impl Error { collection_type, index_type, position, - } => todo!(), + } => builder.add_label(Label::new(position.0..position.1).with_message(format!( + "Cannot index into a {} with a {}.", + collection_type.fg(type_color), + index_type.fg(type_color) + ))), ValidationError::InterpreterExpectedReturn => todo!(), ValidationError::ExpectedFunction { .. } => todo!(), ValidationError::ExpectedValue => todo!(), diff --git a/src/parser.rs b/src/parser.rs index 40578ce..0ee413a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -191,26 +191,8 @@ pub fn parser<'src>() -> DustParser<'src> { .with_position(state.span()) }); - let function_expression = choice((identifier_expression.clone(), function.clone())); - - let function_call = function_expression - .then( - positioned_expression - .clone() - .separated_by(just(Token::Control(Control::Comma))) - .collect() - .delimited_by( - just(Token::Control(Control::ParenOpen)), - just(Token::Control(Control::ParenClose)), - ), - ) - .map_with(|(function, arguments), state| { - Expression::FunctionCall(FunctionCall::new(function, arguments)) - .with_position(state.span()) - }); - let atom = choice(( - function_call.clone(), + function.clone(), identifier_expression.clone(), basic_value.clone(), list.clone(), @@ -223,7 +205,7 @@ pub fn parser<'src>() -> DustParser<'src> { use Operator::*; - let logic_math_and_indexes = atom.pratt(( + let logic_math_indexes_and_function_calls = atom.pratt(( prefix(2, just(Token::Operator(Not)), |_, expression, span| { Expression::Logic(Box::new(Logic::Not(expression))).with_position(span) }), @@ -233,13 +215,28 @@ pub fn parser<'src>() -> DustParser<'src> { just(Token::Control(Control::SquareOpen)), just(Token::Control(Control::SquareClose)), ), + |left, right, span| { + Expression::ListIndex(Box::new(ListIndex::new(left, right))) + .with_position(span) + }, + ), + postfix( + 3, + positioned_expression + .clone() + .separated_by(just(Token::Control(Control::Comma))) + .collect() + .delimited_by( + just(Token::Control(Control::ParenOpen)), + just(Token::Control(Control::ParenClose)), + ), |op, expression, span| { - Expression::ListIndex(Box::new(ListIndex::new(op, expression))) + Expression::FunctionCall(FunctionCall::new(op, expression)) .with_position(span) }, ), infix( - left(3), + left(4), just(Token::Control(Control::Dot)), |left, _, right, span| { Expression::MapIndex(Box::new(MapIndex::new(left, right))) @@ -344,9 +341,8 @@ pub fn parser<'src>() -> DustParser<'src> { choice(( range, - logic_math_and_indexes, + logic_math_indexes_and_function_calls, function, - function_call, list, map, basic_value, diff --git a/src/value.rs b/src/value.rs index 9acf729..fd012d1 100644 --- a/src/value.rs +++ b/src/value.rs @@ -90,7 +90,17 @@ impl Value { ValueInner::Map(_) => Type::Map, ValueInner::Range(_) => Type::Range, ValueInner::String(_) => Type::String, - ValueInner::Function(_) => todo!(), + ValueInner::Function(function) => match function { + Function::Parsed(parsed_function) => Type::Function { + parameter_types: parsed_function + .parameters + .iter() + .map(|(_, r#type)| r#type.node.clone()) + .collect(), + return_type: Box::new(parsed_function.return_type.node.clone()), + }, + Function::BuiltIn(built_in_function) => built_in_function.r#type(), + }, } }