new operators for array manipulation

This commit is contained in:
2024-10-28 21:30:58 -04:00
parent 9e709c4cfc
commit 4c4c69d40b
3 changed files with 77 additions and 24 deletions

View File

@@ -102,6 +102,12 @@ impl Executor {
[Value::Int(x), Value::Float(y)] => Ok(Value::Float(*x as f64 + y)),
[Value::Float(x), Value::Float(y)] => Ok(Value::Float(x + y)),
[Value::String(x), Value::String(y)] => Ok(Value::String(format!("{x}{y}"))),
[Value::Nil, x] => Ok(x.clone()),
[x, Value::Nil] => Ok(x.clone()),
[x, y] => Err(Error::new(format!("no overload of + exists for types {} and {}", x.get_type(), y.get_type()))),
_ => unreachable!(),
}
Op::Concat => match &args[..] {
[Value::Array(xtype, x), Value::Array(ytype, y)] => {
if xtype != ytype {
return Err(Error::new(format!("expected type {} but found {}", xtype, ytype)));
@@ -109,20 +115,9 @@ impl Executor {
Ok(Value::Array(xtype.clone(), [x.clone(), y.clone()].concat()))
},
[Value::Nil, x] => Ok(x.clone()),
[x, Value::Nil] => Ok(x.clone()),
[Value::Array(t, x), y] => {
let ytype = y.get_type();
if *t != ytype {
return Err(Error::new(format!("expected type {} but found {}", t, ytype)));
_ => Err(Error::new("++".into())),
}
// NOTE: use y's type instead of the arrays type.
// an `empty` array has Any type, but any value will have a fixed type.
// this converts the empty array into a typed array.
Ok(Value::Array(ytype, [x.clone(), vec![y.clone()]].concat()))
},
Op::Prepend => match &args[..] {
[x, Value::Array(t, y)] => {
let xtype = x.get_type();
@@ -130,10 +125,41 @@ impl Executor {
return Err(Error::new(format!("expected type {} but found {}", t, xtype)));
}
// NOTE: read above
Ok(Value::Array(xtype, [vec![x.clone()], y.clone()].concat()))
},
_ => Err(Error::new("todo: add".into())),
[x, y] => Err(Error::new(format!("no overload of [+ exists for types {} and {}", x.get_type(), y.get_type()))),
_ => unreachable!(),
}
Op::Append => match &args[..] {
[Value::Array(t, y), x] => {
let xtype = x.get_type();
if *t != xtype {
return Err(Error::new(format!("expected type {} but found {}", t, xtype)));
}
Ok(Value::Array(xtype, [y.clone(), vec![x.clone()]].concat()))
},
_ => Err(Error::new("+]".into())),
}
Op::Insert => match &args[..] {
[Value::Int(idx), x, Value::Array(t, y)] => {
let mut y = y.clone();
let xtype = x.get_type();
if *t != xtype {
return Err(Error::new(format!("expected type {} but found {}", t, xtype)));
}
if *idx as usize > y.len() {
return Err(Error::new("attempt to insert out of array len".into()));
}
y.insert(*idx as usize, x.clone());
Ok(Value::Array(t.clone(), y))
},
_ => Err(Error::new("[+]".into())),
}
Op::Sub => match &args[..] {
[Value::Int(x), Value::Int(y)] => Ok(Value::Int(x - y)),

View File

@@ -125,6 +125,10 @@ impl Parser {
(Op::And, FunctionType(Box::new(Type::Bool), vec![Type::Bool, Type::Bool])),
(Op::Or, FunctionType(Box::new(Type::Bool), vec![Type::Bool, Type::Bool])),
(Op::Head, FunctionType(Box::new(Type::Any), vec![Type::Array(Box::new(Type::Any))])),
(Op::Concat, FunctionType(Box::new(Type::Array(Box::new(Type::Any))), vec![Type::Array(Box::new(Type::Any)), Type::Array(Box::new(Type::Any))])),
(Op::Prepend, FunctionType(Box::new(Type::Array(Box::new(Type::Any))), vec![Type::Any, Type::Array(Box::new(Type::Any))])),
(Op::Append, FunctionType(Box::new(Type::Array(Box::new(Type::Any))), vec![Type::Array(Box::new(Type::Any)), Type::Any])),
(Op::Insert, FunctionType(Box::new(Type::Array(Box::new(Type::Any))), vec![Type::Int, Type::Any, Type::Array(Box::new(Type::Any))])),
(Op::Tail, FunctionType(Box::new(Type::Array(Box::new(Type::Any))), vec![Type::Array(Box::new(Type::Any))])),
(Op::Init, FunctionType(Box::new(Type::Array(Box::new(Type::Any))), vec![Type::Array(Box::new(Type::Any))])),
(Op::Fini, FunctionType(Box::new(Type::Any), vec![Type::Array(Box::new(Type::Any))])),
@@ -238,7 +242,7 @@ impl Parser {
let tree = trees.into_iter().fold(
ParseTree::Value(Value::Array(Type::Any, vec![])),
|acc, x| ParseTree::Operator(Op::Add, vec![acc, x.clone()]),
|acc, x| ParseTree::Operator(Op::Append, vec![acc, x.clone()]),
);
Ok(Some(tree))
@@ -386,7 +390,7 @@ impl Parser {
},
Op::Export => {
let token = tokens.next()
.ok_or(Error::new("export expects one argument of [String], but found nothing".into())
.ok_or(Error::new("export expects an identifer or multiple inside of parens".into())
.location(token.line, token.location.clone()))??;
let names = match token.token() {

View File

@@ -42,6 +42,10 @@ pub enum Op {
Print,
OpenArray,
CloseArray,
Concat,
Prepend,
Append,
Insert,
OpenStatement,
CloseStatement,
Empty,
@@ -207,6 +211,10 @@ impl<R: BufRead> Tokenizer<R> {
("!=", Op::NotEqualTo),
("[", Op::OpenArray),
("]", Op::CloseArray),
("++", Op::Concat),
("[+", Op::Prepend),
("+]", Op::Append),
("[+]", Op::Insert),
("(", Op::OpenStatement),
(")", Op::CloseStatement),
("!", Op::Not),
@@ -366,3 +374,18 @@ impl<R: BufRead> Iterator for Tokenizer<R> {
self.tokenize().transpose()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn meow() {
let program = "[+] 0 1 [2 3]";
let tokens: Vec<_> = Tokenizer::new(Arc::new(Mutex::new(CodeIter::new(Cursor::new(program))))).collect();
println!("{tokens:#?}");
}
}