Simplify indexer parsing for variables with lowered part

This commit is contained in:
PaddiM8 2022-01-06 01:36:13 +01:00
parent 92ba6c5677
commit 09348ba726
2 changed files with 11 additions and 16 deletions

View File

@ -14,7 +14,6 @@ pub const DEFAULT_ANGLE_UNIT: &'static str = "rad";
/// Struct containing the current state of the parser. It stores user-defined functions and variables.
#[wasm_bindgen]
#[derive(Clone)]
pub struct Context {
tokens: Vec<Token>,
pos: usize,
@ -725,21 +724,17 @@ fn parse_identifier(context: &mut Context) -> Result<Expr, CalcError> {
let underscore_pos = identifier.pure_name.find('_').unwrap();
let var_name = &identifier.pure_name[0..underscore_pos];
let lowered = &identifier.pure_name[underscore_pos + 1..];
let lowered_expr =
if lowered.len() > 0 && lowered.chars().nth(0).unwrap_or('\0').is_digit(10) {
Expr::Literal(lowered.parse::<f64>().unwrap_or(f64::NAN))
} else {
Expr::Var(Identifier::from_full_name(lowered))
};
// Create a new identical context to avoid changing the current one
let mut lowered_context = context.clone();
let mut parsed_stmts = parse(&mut lowered_context, &lowered)?;
let parsed_lowered = parsed_stmts
.pop()
.unwrap_or(Stmt::Expr(Box::new(Expr::Literal(0f64))));
if let Stmt::Expr(lowered_expr) = parsed_lowered {
Ok(Expr::Indexer(
Box::new(Expr::Var(Identifier::from_full_name(&var_name))),
lowered_expr,
Box::new(lowered_expr),
))
} else {
Err(CalcError::UnableToParseExpression)
}
} else if context.parsing_unit_decl {
context.unit_decl_base_unit = Some(identifier.full_name);
Ok(Expr::Var(Identifier::from_full_name(DECL_UNIT)))

View File

@ -1,7 +1,7 @@
use crate::{ast::Expr, ast::Identifier, ast::Stmt, prelude};
use std::collections::HashMap;
#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct SymbolTable {
pub(crate) hashmap: HashMap<String, Stmt>,
pub(crate) unit_types: HashMap<String, ()>,