Satisfied most of THE ALLMIGHTY CLIPPY's requests.

This commit is contained in:
Erin 2022-03-01 21:53:58 +01:00 committed by ondra05
parent a3c0e65f6b
commit 81f433e633
5 changed files with 27 additions and 22 deletions

View file

@ -50,19 +50,21 @@ pub enum AssignableKind {
Index { indices: Vec<Expr> },
}
pub struct InvalidAssignable;
impl Assignable {
pub fn from_expr(expr: Expr) -> Result<Assignable, ()> {
pub fn from_expr(expr: Expr) -> Result<Assignable, InvalidAssignable> {
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<Assignable, ()> {
fn from_index(mut buf: Expr, index: Expr) -> Result<Assignable, InvalidAssignable> {
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),
}
};

View file

@ -50,6 +50,15 @@ struct Scope {
variables: HashMap<String, Variable>,
}
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(),

View file

@ -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 .*")]

View file

@ -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(_) => {

View file

@ -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(),
};