From b6f00d07e8e81310ea8f3ecaf7d395843e5c7f7b Mon Sep 17 00:00:00 2001 From: JT Date: Fri, 30 Jul 2021 19:30:11 +1200 Subject: [PATCH] Fix var decl. improve for loop --- src/eval.rs | 5 ++--- src/main.rs | 2 +- src/parser.rs | 18 ++---------------- 3 files changed, 5 insertions(+), 20 deletions(-) diff --git a/src/eval.rs b/src/eval.rs index 50a0b1241c..79b6011df4 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -157,7 +157,7 @@ fn eval_call(state: &State, stack: Stack, call: &Call) -> Result Result std::io::Result<()> { working_set.add_decl(sig.into()); let sig = Signature::build("let") - .required("var_name", SyntaxShape::Variable, "variable name") + .required("var_name", SyntaxShape::VarWithOptType, "variable name") .required( "initial_value", SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::Expression)), diff --git a/src/parser.rs b/src/parser.rs index 4fefd9cbd7..b3217a9dc4 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1212,21 +1212,19 @@ impl<'a> ParserWorkingSet<'a> { let type_bytes = self.get_span_contents(spans[*spans_idx]); let ty = self.parse_type(type_bytes); - *spans_idx += 1; let id = self.add_variable(bytes[0..(bytes.len() - 1)].to_vec(), ty.clone()); ( Expression { expr: Expr::Var(id), - span: span(&spans[*spans_idx - 2..*spans_idx]), + span: span(&spans[*spans_idx - 1..*spans_idx + 1]), ty, }, None, ) } else { let id = self.add_variable(bytes[0..(bytes.len() - 1)].to_vec(), Type::Unknown); - *spans_idx += 1; ( Expression { expr: Expr::Var(id), @@ -1238,12 +1236,11 @@ impl<'a> ParserWorkingSet<'a> { } } else { let id = self.add_variable(bytes, Type::Unknown); - *spans_idx += 1; ( Expression { expr: Expr::Var(id), - span: span(&spans[*spans_idx - 1..*spans_idx]), + span: span(&spans[*spans_idx..*spans_idx + 1]), ty: Type::Unknown, }, None, @@ -2093,17 +2090,6 @@ impl<'a> ParserWorkingSet<'a> { } } - pub fn parse_keyword(&self, span: Span, keyword: &[u8]) -> Option { - if self.get_span_contents(span) == keyword { - None - } else { - Some(ParseError::Mismatch( - String::from_utf8_lossy(keyword).to_string(), - span, - )) - } - } - pub fn parse_def(&mut self, spans: &[Span]) -> (Statement, Option) { let mut error = None; let name = self.get_span_contents(spans[0]);