Most coercions implemented

This commit is contained in:
Erin 2021-08-28 23:27:35 +02:00 committed by ondra05
parent 2c36b2a5f7
commit 8b57cbe5a5
4 changed files with 298 additions and 127 deletions

View file

@ -142,7 +142,7 @@ pub enum ExprKind {
Literal(Value), Literal(Value),
Cart(Vec<(Expr, Expr)>), Cart(Vec<(Expr, Expr)>),
Index { Index {
cart: Box<Expr>, expr: Box<Expr>,
index: Box<Expr>, index: Box<Expr>,
}, },
Variable(String), Variable(String),

View file

@ -141,59 +141,19 @@ impl ExecEnv {
let lhs = self.eval_expr(lhs)?; let lhs = self.eval_expr(lhs)?;
let rhs = self.eval_expr(rhs)?; let rhs = self.eval_expr(rhs)?;
match kind { match kind {
// Arithmetic operators. Add => lhs + rhs,
Add | Subtract | Multiply | Divide => { Subtract => todo!(),
let lhs = lhs.to_i32(); Multiply => todo!(),
let rhs = rhs.to_i32(); Divide => todo!(),
Greater => Value::Bool(lhs > rhs),
let res = match kind { Less => Value::Bool(lhs < rhs),
Add => lhs.checked_add(rhs), Equal => Value::Bool(lhs == rhs),
Subtract => lhs.checked_sub(rhs), NotEqual => Value::Bool(lhs != rhs),
Multiply => lhs.checked_mul(rhs), And => todo!(),
Divide => lhs.checked_div(rhs), Or => todo!(),
_ => unreachable!(),
}
.unwrap_or(consts::ANSWER);
Int(res)
}
// Numeric comparisons.
Less | Greater => {
let lhs = lhs.to_i32();
let rhs = rhs.to_i32();
let res = match kind {
Less => lhs < rhs,
Greater => lhs > rhs,
_ => unreachable!(),
};
Bool(res)
}
// General comparisons.
Equal | NotEqual => {
let res = match kind {
Equal => lhs == rhs,
NotEqual => lhs != rhs,
_ => unreachable!(),
};
Bool(res)
}
// Logical connectives.
And | Or => {
let lhs = lhs.to_bool();
let rhs = rhs.to_bool();
let res = match kind {
And => lhs && rhs,
Or => lhs || rhs,
_ => unreachable!(),
};
Bool(res)
} }
} }
} Not(expr) => Bool(!self.eval_expr(expr)?.into_bool()),
Not(expr) => Bool(!self.eval_expr(expr)?.to_bool()),
Literal(value) => value.clone(), Literal(value) => value.clone(),
ExprKind::Cart(members) => Value::Cart( ExprKind::Cart(members) => Value::Cart(
members members
@ -206,12 +166,15 @@ impl ExecEnv {
}) })
.collect::<Result<HashMap<_, _>, _>>()?, .collect::<Result<HashMap<_, _>, _>>()?,
), ),
Index { cart, index } => { Index { expr, index } => {
let cart = self.eval_expr(cart)?; let value = self.eval_expr(expr)?;
let index = self.eval_expr(index)?; let index = self.eval_expr(index)?;
// TODO: this probably shouldn't be cloned value
cart.index(&index).borrow().clone() .into_cart()
.get(&index)
.map(|x| x.borrow().clone())
.unwrap_or(Value::Nul)
} }
// TODO: not too happy with constructing an artificial // TODO: not too happy with constructing an artificial
@ -257,24 +220,20 @@ impl ExecEnv {
instructions: code.to_owned(), instructions: code.to_owned(),
tape_len: tape_len tape_len: tape_len
.as_ref() .as_ref()
.map(|tape_len| self.eval_expr(tape_len).map(|v| v.to_i32() as usize)) .map(|tape_len| self.eval_expr(tape_len).map(|v| v.into_i32() as usize))
.unwrap_or(Ok(crate::brian::DEFAULT_TAPE_SIZE_LIMIT))?, .unwrap_or(Ok(crate::brian::DEFAULT_TAPE_SIZE_LIMIT))?,
}), }),
); );
} }
StmtKind::If { cond, body } => { StmtKind::If { cond, body } => {
if self.eval_expr(cond)?.to_bool() { if self.eval_expr(cond)?.into_bool() {
return self.eval_stmts_hs(&body.block, true); return self.eval_stmts_hs(&body.block, true);
} }
} }
StmtKind::Call { expr, args } => { StmtKind::Call { expr, args } => {
let func = self.eval_expr(expr)?; let func = self.eval_expr(expr)?.into_functio();
if let Value::Functio(func) = func {
self.fn_call(func, args, &stmt.span)?; self.fn_call(func, args, &stmt.span)?;
} else {
// Fail silently for now.
}
} }
StmtKind::Loop { body } => loop { StmtKind::Loop { body } => loop {
let res = self.eval_stmts_hs(&body.block, true)?; let res = self.eval_stmts_hs(&body.block, true)?;
@ -387,6 +346,17 @@ impl ExecEnv {
self.stack.pop(); self.stack.pop();
res?; res?;
} }
Functio::Eval(code) => {
if args.len() != 0 {
return Err(Error {
kind: ErrorKind::MismatchedArgumentError,
span: span.to_owned(),
});
}
let stmts = crate::parser::Parser::new(&code).init()?;
self.eval_stmts(&stmts)?;
}
} }
Ok(()) Ok(())
} }

View file

@ -98,6 +98,7 @@ impl<'source> Parser<'source> {
| Token::Integer(_) | Token::Integer(_)
| Token::Abool(_) | Token::Abool(_)
| Token::Bool(_) | Token::Bool(_)
| Token::Nul
| Token::LeftBracket | Token::LeftBracket
| Token::LeftParen => Ok(Stmt::new( | Token::LeftParen => Ok(Stmt::new(
self.value_flow(token)?, self.value_flow(token)?,
@ -192,7 +193,7 @@ impl<'source> Parser<'source> {
Token::LeftBracket => match buf.take() { Token::LeftBracket => match buf.take() {
Some(buf) => Ok(Expr::new( Some(buf) => Ok(Expr::new(
ExprKind::Index { ExprKind::Index {
cart: Box::new(buf), expr: Box::new(buf),
index: Box::new(self.expr_flow(Token::RightBracket)?), index: Box::new(self.expr_flow(Token::RightBracket)?),
}, },
start..self.lexer.span().end, start..self.lexer.span().end,
@ -728,7 +729,7 @@ mod tests {
let expected = &[Stmt { let expected = &[Stmt {
kind: StmtKind::Print(Expr { kind: StmtKind::Print(Expr {
kind: ExprKind::Index { kind: ExprKind::Index {
cart: Box::new(Expr { expr: Box::new(Expr {
kind: ExprKind::Cart(vec![( kind: ExprKind::Cart(vec![(
Expr { Expr {
kind: ExprKind::Literal(Value::Str("able".to_string())), kind: ExprKind::Literal(Value::Str("able".to_string())),

View file

@ -1,6 +1,6 @@
use std::{ use std::{
cell::RefCell, collections::HashMap, convert::TryFrom, fmt::Display, hash::Hash, io::Write, cell::RefCell, collections::HashMap, convert::TryFrom, fmt::Display, hash::Hash, io::Write,
mem::discriminant, rc::Rc, mem::discriminant, ops, rc::Rc, vec,
}; };
use rand::Rng; use rand::Rng;
@ -44,8 +44,11 @@ pub enum Functio {
params: Vec<String>, params: Vec<String>,
body: Vec<Stmt>, body: Vec<Stmt>,
}, },
Eval(String),
} }
pub type Cart = HashMap<Value, Rc<RefCell<Value>>>;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Value { pub enum Value {
Nul, Nul,
@ -54,7 +57,7 @@ pub enum Value {
Bool(bool), Bool(bool),
Abool(Abool), Abool(Abool),
Functio(Functio), Functio(Functio),
Cart(HashMap<Value, Rc<RefCell<Value>>>), Cart(Cart),
} }
impl Hash for Value { impl Hash for Value {
@ -72,24 +75,6 @@ impl Hash for Value {
} }
} }
impl PartialEq for Value {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Value::Nul, Value::Nul) => true,
(Value::Str(left), Value::Str(right)) => left == right,
(Value::Int(left), Value::Int(right)) => left == right,
(Value::Bool(left), Value::Bool(right)) => left == right,
(Value::Abool(left), Value::Abool(right)) => left == right,
(Value::Functio(left), Value::Functio(right)) => left == right,
(Value::Cart(_), Value::Cart(_)) => self.to_string() == other.to_string(),
(_, _) => false,
// TODO: do more coercions!
}
}
}
impl Eq for Value {}
impl Value { impl Value {
/// Write an AbleScript value to a Brainfuck input stream by /// Write an AbleScript value to a Brainfuck input stream by
/// coercing the value to an integer, then truncating that integer /// coercing the value to an integer, then truncating that integer
@ -98,23 +83,24 @@ impl Value {
/// any IO errors will cause a panic. /// any IO errors will cause a panic.
pub fn bf_write(&self, stream: &mut impl Write) { pub fn bf_write(&self, stream: &mut impl Write) {
stream stream
.write_all(&[self.clone().to_i32() as u8]) .write_all(&[self.clone().into_i32() as u8])
.expect("Failed to write to Brainfuck input"); .expect("Failed to write to Brainfuck input");
} }
/// Coerce a value to an integer. /// Coerce a value to an integer.
pub fn to_i32(&self) -> i32 { pub fn into_i32(self) -> i32 {
match self { match self {
Value::Abool(a) => *a as _, Value::Abool(a) => a as _,
Value::Bool(b) => *b as _, Value::Bool(b) => b as _,
Value::Functio(func) => match func { Value::Functio(func) => match func {
Functio::BfFunctio { Functio::BfFunctio {
instructions, instructions,
tape_len, tape_len,
} => (instructions.len() + tape_len) as _, } => (instructions.len() + tape_len) as _,
Functio::AbleFunctio { params, body } => (params.len() + body.len()) as _, Functio::AbleFunctio { params, body } => (params.len() + body.len()) as _,
Functio::Eval(s) => s.parse().unwrap_or(consts::ANSWER),
}, },
Value::Int(i) => *i, Value::Int(i) => i,
Value::Nul => consts::ANSWER, Value::Nul => consts::ANSWER,
Value::Str(text) => text.parse().unwrap_or(consts::ANSWER), Value::Str(text) => text.parse().unwrap_or(consts::ANSWER),
Value::Cart(c) => c.len() as _, Value::Cart(c) => c.len() as _,
@ -122,49 +108,262 @@ impl Value {
} }
/// Coerce a Value to a boolean. The conversion cannot fail. /// Coerce a Value to a boolean. The conversion cannot fail.
pub fn to_bool(&self) -> bool { pub fn into_bool(self) -> bool {
match self { match self {
Value::Abool(b) => (*b).into(), Value::Abool(b) => b.into(),
Value::Bool(b) => *b, Value::Bool(b) => b,
Value::Functio(_) => true, Value::Functio(_) => true,
Value::Int(x) => *x != 0, Value::Int(x) => x != 0,
Value::Nul => true, Value::Nul => false,
Value::Str(s) => !s.is_empty(), Value::Str(s) => match s.to_lowercase().as_str() {
"false" | "no" | "🇳🇴" => false,
"true" | "yes" => true,
s => !s.is_empty(),
},
Value::Cart(c) => !c.is_empty(), Value::Cart(c) => !c.is_empty(),
} }
} }
/// Index a value with another value, as in the "a[b]" syntax. /// Coerce a Value to an aboolean
pub fn index(&self, index: &Value) -> Rc<RefCell<Value>> { pub fn into_abool(self) -> Abool {
Rc::new(RefCell::new(match self { match self {
Value::Nul => Value::Nul, Value::Nul => Abool::Never,
Value::Str(s) => Value::Int( Value::Str(s) => match s.to_lowercase().as_str() {
usize::try_from(index.to_i32() - 1) "never" => Abool::Never,
.ok() "sometimes" => Abool::Sometimes,
.and_then(|idx| s.as_bytes().get(idx).cloned()) "always" => Abool::Always,
.map(|value| value as i32) s => {
.unwrap_or(0), if s.is_empty() {
), Abool::Never
Value::Int(i) => Value::Int( } else {
usize::try_from(index.to_i32() - 1) Abool::Always
.ok() }
.and_then(|idx| format!("{}", i).as_bytes().get(idx).cloned()) }
.map(|value| value as i32) },
.unwrap_or(0), Value::Int(x) => match x.cmp(&0) {
), std::cmp::Ordering::Less => Abool::Never,
Value::Bool(b) => Value::Int( std::cmp::Ordering::Equal => Abool::Sometimes,
usize::try_from(index.to_i32() - 1) std::cmp::Ordering::Greater => Abool::Always,
.ok() },
.and_then(|idx| format!("{}", b).as_bytes().get(idx).cloned()) Value::Bool(b) => {
.map(|value| value as i32) if b {
.unwrap_or(0), Abool::Always
), } else {
Value::Abool(b) => Value::Int(*b as i32), Abool::Never
Value::Functio(_) => Value::Int(42), }
Value::Cart(c) => { }
return (c.get(index).cloned()).unwrap_or_else(|| Rc::new(RefCell::new(Value::Nul))) Value::Abool(a) => a,
Value::Functio(_) => todo!(),
Value::Cart(c) => {
if c.is_empty() {
Abool::Never
} else {
Abool::Always
}
}
}
}
/// Coerce a Value to a functio
pub fn into_functio(self) -> Functio {
match self {
Value::Nul => Functio::AbleFunctio {
body: vec![],
params: vec![],
},
Value::Str(s) => Functio::Eval(s),
Value::Int(i) => todo!(),
Value::Bool(_) => todo!(),
Value::Abool(_) => todo!(),
Value::Functio(f) => f,
Value::Cart(_) => todo!(),
}
}
pub fn into_cart(self) -> Cart {
match self {
Value::Nul => HashMap::new(),
Value::Str(s) => s
.chars()
.enumerate()
.map(|(i, x)| {
(
Value::Int(i as i32 + 1),
Rc::new(RefCell::new(Value::Str(x.to_string()))),
)
})
.collect(),
Value::Int(i) => Value::Str(i.to_string()).into_cart(),
Value::Bool(b) => Value::Str(b.to_string()).into_cart(),
Value::Abool(a) => Value::Str(a.to_string()).into_cart(),
Value::Functio(f) => match f {
Functio::BfFunctio {
instructions,
tape_len,
} => {
let mut cart: Cart = instructions
.into_iter()
.enumerate()
.map(|(i, x)| {
(
Value::Int(i as i32 + 1),
Rc::new(RefCell::new(
char::from_u32(x as u32)
.map(|x| Value::Str(x.to_string()))
.unwrap_or(Value::Nul),
)),
)
})
.collect();
cart.insert(
Value::Str("tapelen".to_owned()),
Rc::new(RefCell::new(Value::Int(tape_len as _))),
);
cart
}
Functio::AbleFunctio { params, body } => {
let params: Cart = params
.into_iter()
.enumerate()
.map(|(i, x)| {
(
Value::Int(i as i32 + 1),
Rc::new(RefCell::new(Value::Str(x))),
)
})
.collect();
let body: Cart = body
.into_iter()
.enumerate()
.map(|(i, x)| {
(
Value::Int(i as i32 + 1),
Rc::new(RefCell::new(Value::Str(format!("{:?}", x)))),
)
})
.collect();
let mut cart = HashMap::new();
cart.insert(
Value::Str("params".to_owned()),
Rc::new(RefCell::new(Value::Cart(params))),
);
cart.insert(
Value::Str("body".to_owned()),
Rc::new(RefCell::new(Value::Cart(body))),
);
cart
}
Functio::Eval(s) => Value::Str(s).into_cart(),
},
Value::Cart(c) => c,
}
}
}
impl ops::Add for Value {
type Output = Value;
fn add(self, rhs: Self) -> Self::Output {
match self {
Value::Nul => todo!(),
Value::Str(s) => Value::Str(format!("{}{}", s, rhs.to_string())),
Value::Int(i) => Value::Int(i + rhs.into_i32()),
Value::Bool(b) => Value::Bool(b ^ rhs.into_bool()),
Value::Abool(a) => Value::Abool({
let rhs = rhs.into_abool();
if a == rhs {
a
} else if a == Abool::Sometimes {
if rand::thread_rng().gen() {
Abool::Sometimes
} else {
rhs
}
} else if rhs == Abool::Sometimes {
if rand::thread_rng().gen() {
Abool::Sometimes
} else {
a
}
} else {
Abool::Sometimes
}
}),
Value::Functio(f) => Value::Functio(todo!()),
Value::Cart(c) => {
Value::Cart(c.into_iter().chain(rhs.into_cart().into_iter()).collect())
}
}
}
}
impl ops::Sub for Value {
type Output = Value;
fn sub(self, rhs: Self) -> Self::Output {
todo!()
}
}
impl ops::Mul for Value {
type Output = Value;
fn mul(self, rhs: Self) -> Self::Output {
todo!()
}
}
impl ops::Div for Value {
type Output = Value;
fn div(self, rhs: Self) -> Self::Output {
todo!()
}
}
impl PartialEq for Value {
fn eq(&self, other: &Self) -> bool {
let other = other.clone();
match self {
Value::Nul => other == Value::Nul,
Value::Str(s) => *s == other.to_string(),
Value::Int(i) => *i == other.into_i32(),
Value::Bool(b) => *b == other.into_bool(),
Value::Abool(a) => *a == other.into_abool(),
Value::Functio(f) => *f == other.into_functio(),
Value::Cart(c) => *c == other.into_cart(),
}
}
}
impl Eq for Value {}
impl PartialOrd for Value {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
use std::cmp::Ordering::*;
let other = other.clone();
match self {
Value::Nul => {
if other == Value::Nul {
Some(Equal)
} else {
None
}
}
Value::Str(_) => todo!(),
Value::Int(_) => todo!(),
Value::Bool(_) => todo!(),
Value::Abool(_) => todo!(),
Value::Functio(_) => todo!(),
Value::Cart(_) => todo!(),
} }
}))
} }
} }
@ -199,6 +398,7 @@ impl Display for Value {
body, body,
) )
} }
Functio::Eval(s) => write!(f, "{}", s),
}, },
Value::Cart(c) => { Value::Cart(c) => {
write!(f, "[")?; write!(f, "[")?;