From fca3a6b75e5c71594becb6bcfe38d6674b79f277 Mon Sep 17 00:00:00 2001 From: JT Date: Sat, 24 Jul 2021 09:46:55 +1200 Subject: [PATCH] Support adding variables --- src/eval.rs | 15 ++++++++++++++- src/main.rs | 9 +++++---- src/parse_error.rs | 2 ++ src/parser.rs | 22 ++++++++++++++++++++-- src/parser_state.rs | 12 +++++++++++- 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/eval.rs b/src/eval.rs index b6bcc5e00b..13888be44d 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -87,7 +87,20 @@ fn eval_call(state: &State, stack: &mut Stack, call: &Call) -> Result std::io::Result<()> { let prompt = DefaultPrompt::new(1); let mut current_line = 1; + let mut stack = Stack { + vars: HashMap::new(), + }; loop { let input = line_editor.read_line(&prompt)?; @@ -158,9 +161,10 @@ fn main() -> std::io::Result<()> { s.as_bytes(), false, ); - println!("{:?}", output); + // println!("{:?}", output); if let Some(err) = err { println!("Error: {:?}", err); + continue; } // println!("Error: {:?}", err); (output, working_set.render()) @@ -168,9 +172,6 @@ fn main() -> std::io::Result<()> { ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta); - let mut stack = Stack { - vars: HashMap::new(), - }; let state = State { parser_state: &*parser_state.borrow(), }; diff --git a/src/parse_error.rs b/src/parse_error.rs index 5ff90ff60b..fe0683532c 100644 --- a/src/parse_error.rs +++ b/src/parse_error.rs @@ -1,3 +1,4 @@ +use crate::parser_state::Type; pub use crate::Span; #[derive(Debug)] @@ -18,6 +19,7 @@ pub enum ParseError { ShortFlagBatchCantTakeArg(Span), MissingPositional(String, Span), MissingType(Span), + TypeMismatch(Type, Span), MissingRequiredFlag(String, Span), IncompleteMathExpression(Span), UnknownState(String, Span), diff --git a/src/parser.rs b/src/parser.rs index 2346c06efc..d5b340ccf1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -666,12 +666,30 @@ impl<'a> ParserWorkingSet<'a> { }; // println!("end: {}", end); - let (arg, err) = - self.parse_multispan_value(&spans[..end], &mut spans_idx, positional.shape); + let orig_idx = spans_idx; + let (arg, err) = self.parse_multispan_value( + &spans[..end], + &mut spans_idx, + positional.shape.clone(), + ); error = error.or(err); + + let arg = if positional.shape.to_type() != Type::Unknown + && arg.ty != positional.shape.to_type() + { + let span = span(&spans[orig_idx..spans_idx + 1]); + error = error.or(Some(ParseError::TypeMismatch( + positional.shape.to_type(), + span, + ))); + Expression::garbage(span) + } else { + arg + }; call.positional.push(arg); positional_idx += 1; } else { + call.positional.push(Expression::garbage(arg_span)); error = error.or(Some(ParseError::ExtraPositional(arg_span))) } diff --git a/src/parser_state.rs b/src/parser_state.rs index 8d73e7528d..d634f28c6f 100644 --- a/src/parser_state.rs +++ b/src/parser_state.rs @@ -12,7 +12,7 @@ pub struct ParserState { scope: Vec, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] pub enum Type { Int, Bool, @@ -118,6 +118,16 @@ impl ParserState { } } + pub fn find_decl(&self, name: &[u8]) -> Option { + for scope in self.scope.iter().rev() { + if let Some(decl_id) = scope.decls.get(name) { + return Some(*decl_id); + } + } + + None + } + pub fn get_var(&self, var_id: VarId) -> &Type { self.vars .get(var_id)