From 81f433e6331458f9ab86d5f489404603083da053 Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 1 Mar 2022 21:53:58 +0100 Subject: [PATCH] Satisfied most of THE ALLMIGHTY CLIPPY's requests. --- ablescript/src/ast.rs | 10 ++++++---- ablescript/src/interpret.rs | 21 +++++++++++++-------- ablescript/src/lexer.rs | 6 ++---- ablescript/src/variables.rs | 10 +++++----- ablescript_cli/src/repl.rs | 2 +- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/ablescript/src/ast.rs b/ablescript/src/ast.rs index 3a7941e7..447fd45b 100644 --- a/ablescript/src/ast.rs +++ b/ablescript/src/ast.rs @@ -50,19 +50,21 @@ pub enum AssignableKind { Index { indices: Vec }, } +pub struct InvalidAssignable; + impl Assignable { - pub fn from_expr(expr: Expr) -> Result { + pub fn from_expr(expr: Expr) -> Result { match expr.kind { ExprKind::Variable(ident) => Ok(Assignable { ident: Ident::new(ident, expr.span), kind: AssignableKind::Variable, }), ExprKind::Index { expr, index } => Self::from_index(*expr, *index), - _ => Err(()), + _ => Err(InvalidAssignable), } } - fn from_index(mut buf: Expr, index: Expr) -> Result { + fn from_index(mut buf: Expr, index: Expr) -> Result { let mut indices = vec![index]; let ident = loop { match buf.kind { @@ -71,7 +73,7 @@ impl Assignable { indices.push(*index); buf = *expr; } - _ => return Err(()), + _ => return Err(InvalidAssignable), } }; diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index 0d0b1499..2a38632f 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -50,6 +50,15 @@ struct Scope { variables: HashMap, } +impl Default for ExecEnv { + fn default() -> Self { + Self { + stack: vec![Default::default()], + read_buf: Default::default(), + } + } +} + impl Default for Scope { fn default() -> Self { Self { @@ -80,11 +89,7 @@ impl ExecEnv { /// Create a new Scope with no predefined variable definitions or /// other information. pub fn new() -> Self { - Self { - // We always need at least one stackframe. - stack: vec![Default::default()], - read_buf: Default::default(), - } + Self::default() } /// Create a new Scope with predefined variables @@ -190,7 +195,7 @@ impl ExecEnv { .map(|x| x.borrow().clone()) .unwrap_or(Value::Nul) } - Len(expr) => Value::Int(self.eval_expr(expr)?.len()), + Len(expr) => Value::Int(self.eval_expr(expr)?.length()), // TODO: not too happy with constructing an artificial // Ident here. @@ -476,7 +481,7 @@ impl ExecEnv { .chain( args[2 * n_alternations..] .iter() - .map(|r| Rc::clone(r)) + .map(Rc::clone) .take(extra_l), ) .collect(), @@ -486,7 +491,7 @@ impl ExecEnv { .chain( args[2 * n_alternations..] .iter() - .map(|r| Rc::clone(r)) + .map(Rc::clone) .take(extra_r), ) .collect(), diff --git a/ablescript/src/lexer.rs b/ablescript/src/lexer.rs index 1ae88ab2..95cc355b 100644 --- a/ablescript/src/lexer.rs +++ b/ablescript/src/lexer.rs @@ -1,7 +1,5 @@ use logos::{Lexer, Logos}; -use crate::variables::Abool; - #[derive(Logos, Debug, PartialEq, Clone)] pub enum Token { // Symbols @@ -121,8 +119,8 @@ pub enum Token { #[regex(r"-?[0-9]+", get_value)] Integer(isize), - /// A C-complaint identifier - #[regex(r"[a-zA-Z_][a-zA-Z_0-9]*", get_ident)] + /// An identifier + #[regex(r"\p{XID_Start}\p{XID_Continue}*", get_ident)] Identifier(String), #[regex(r"owo .*")] diff --git a/ablescript/src/variables.rs b/ablescript/src/variables.rs index 552c0675..992b88d5 100644 --- a/ablescript/src/variables.rs +++ b/ablescript/src/variables.rs @@ -104,7 +104,7 @@ impl std::fmt::Debug for BuiltinFunctio { impl PartialEq for BuiltinFunctio { fn eq(&self, other: &Self) -> bool { - Rc::ptr_eq(&self.function, &other.function) && self.arity == other.arity + self.fn_addr() == other.fn_addr() && self.arity == other.arity } } @@ -264,7 +264,7 @@ impl Value { .into_abool(), Functio::Able { params, body } => { let str_to_isize = |x: String| -> isize { - x.as_bytes().into_iter().map(|x| *x as isize).sum() + x.as_bytes().iter().map(|x| *x as isize).sum() }; let params: isize = params.into_iter().map(str_to_isize).sum(); @@ -482,8 +482,8 @@ impl Value { } } - /// Get a lenght of a value - pub fn len(&self) -> isize { + /// Get a length of a value + pub fn length(&self) -> isize { match self { Value::Nul => 0, Value::Str(s) => s.len() as _, @@ -547,7 +547,7 @@ impl ops::Add for Value { Value::Functio(_) => Value::Functio(self.into_functio()) + rhs, Value::Cart(_) => Value::Cart(self.into_cart()) + rhs, }, - Value::Str(s) => Value::Str(format!("{}{}", s, rhs.to_string())), + Value::Str(s) => Value::Str(format!("{s}{rhs}")), Value::Int(i) => Value::Int(i.wrapping_add(rhs.into_isize())), Value::Bool(b) => Value::Bool(b || rhs.into_bool()), Value::Abool(_) => { diff --git a/ablescript_cli/src/repl.rs b/ablescript_cli/src/repl.rs index 5959302a..eca64b91 100644 --- a/ablescript_cli/src/repl.rs +++ b/ablescript_cli/src/repl.rs @@ -18,7 +18,7 @@ pub fn repl(ast_print: bool) { rl.add_history_entry(readline); let partial_data = match partial { - Some(line) => line + &readline, + Some(line) => line + readline, None => readline.to_owned(), };