Expr::Literal contains Literal type which contains only possible types instead of any Value
This commit is contained in:
parent
eac86b5e0b
commit
0eb2698242
|
@ -153,7 +153,7 @@ pub enum Expr {
|
|||
kind: BinOpKind,
|
||||
},
|
||||
Aint(Box<Spanned<Expr>>),
|
||||
Literal(Value),
|
||||
Literal(Literal),
|
||||
Cart(Vec<(Spanned<Expr>, Spanned<Expr>)>),
|
||||
Index {
|
||||
expr: Box<Spanned<Expr>>,
|
||||
|
@ -163,6 +163,21 @@ pub enum Expr {
|
|||
Variable(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Hash)]
|
||||
pub enum Literal {
|
||||
Int(isize),
|
||||
Str(String),
|
||||
}
|
||||
|
||||
impl From<Literal> for Value {
|
||||
fn from(lit: Literal) -> Self {
|
||||
match lit {
|
||||
Literal::Int(i) => Self::Int(i),
|
||||
Literal::Str(s) => Self::Str(s),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Hash)]
|
||||
pub enum BinOpKind {
|
||||
Add,
|
||||
|
|
|
@ -174,7 +174,7 @@ impl ExecEnv {
|
|||
}
|
||||
}
|
||||
Aint(expr) => !self.eval_expr(expr)?,
|
||||
Literal(value) => value.clone(),
|
||||
Literal(lit) => lit.clone().into(),
|
||||
Expr::Cart(members) => Value::Cart(
|
||||
members
|
||||
.iter()
|
||||
|
@ -599,9 +599,8 @@ impl ExecEnv {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::ast::Expr;
|
||||
|
||||
use super::*;
|
||||
use crate::ast::{Expr, Literal};
|
||||
|
||||
#[test]
|
||||
fn basic_expression_test() {
|
||||
|
@ -611,11 +610,11 @@ mod tests {
|
|||
env.eval_expr(&Spanned {
|
||||
item: Expr::BinOp {
|
||||
lhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Int(2)),
|
||||
item: Expr::Literal(Literal::Int(2)),
|
||||
span: 1..1,
|
||||
}),
|
||||
rhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Int(2)),
|
||||
item: Expr::Literal(Literal::Int(2)),
|
||||
span: 1..1,
|
||||
}),
|
||||
kind: crate::ast::BinOpKind::Add,
|
||||
|
@ -631,18 +630,17 @@ mod tests {
|
|||
fn type_coercions() {
|
||||
// The sum of an integer and an aboolean causes an aboolean
|
||||
// coercion.
|
||||
use crate::variables::Abool;
|
||||
|
||||
let env = ExecEnv::new();
|
||||
assert_eq!(
|
||||
env.eval_expr(&Spanned {
|
||||
item: Expr::BinOp {
|
||||
lhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Int(2)),
|
||||
item: Expr::Literal(Literal::Int(2)),
|
||||
span: 1..1,
|
||||
}),
|
||||
rhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Abool(Abool::Always)),
|
||||
item: Expr::Variable("always".to_owned()),
|
||||
span: 1..1,
|
||||
}),
|
||||
kind: crate::ast::BinOpKind::Add,
|
||||
|
@ -663,11 +661,11 @@ mod tests {
|
|||
env.eval_expr(&Spanned {
|
||||
item: Expr::BinOp {
|
||||
lhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Int(isize::MAX)),
|
||||
item: Expr::Literal(Literal::Int(isize::MAX)),
|
||||
span: 1..1,
|
||||
}),
|
||||
rhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Int(1)),
|
||||
item: Expr::Literal(Literal::Int(1)),
|
||||
span: 1..1,
|
||||
}),
|
||||
kind: crate::ast::BinOpKind::Add,
|
||||
|
@ -683,11 +681,11 @@ mod tests {
|
|||
env.eval_expr(&Spanned {
|
||||
item: Expr::BinOp {
|
||||
lhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Int(84)),
|
||||
item: Expr::Literal(Literal::Int(84)),
|
||||
span: 1..1,
|
||||
}),
|
||||
rhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Int(0)),
|
||||
item: Expr::Literal(Literal::Int(0)),
|
||||
span: 1..1,
|
||||
}),
|
||||
kind: crate::ast::BinOpKind::Divide,
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
use crate::ast::*;
|
||||
use crate::error::{Error, ErrorKind};
|
||||
use crate::lexer::Token;
|
||||
use crate::variables::Value;
|
||||
use logos::{Lexer, Logos};
|
||||
|
||||
/// Parser structure which holds lexer and metadata
|
||||
|
@ -173,11 +172,11 @@ impl<'source> Parser<'source> {
|
|||
start..self.lexer.span().end,
|
||||
)),
|
||||
Token::Integer(i) => Ok(Spanned::new(
|
||||
Expr::Literal(Value::Int(i)),
|
||||
Expr::Literal(Literal::Int(i)),
|
||||
start..self.lexer.span().end,
|
||||
)),
|
||||
Token::String(s) => Ok(Spanned::new(
|
||||
Expr::Literal(Value::Str(if self.tdark {
|
||||
Expr::Literal(Literal::Str(if self.tdark {
|
||||
s.replace("lang", "script")
|
||||
} else {
|
||||
s
|
||||
|
@ -614,7 +613,7 @@ mod tests {
|
|||
lhs: Box::new(Spanned {
|
||||
item: Expr::BinOp {
|
||||
lhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Int(1)),
|
||||
item: Expr::Literal(Literal::Int(1)),
|
||||
span: 0..1,
|
||||
}),
|
||||
rhs: Box::new(Spanned {
|
||||
|
@ -624,7 +623,7 @@ mod tests {
|
|||
span: 5..6,
|
||||
}),
|
||||
rhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Int(3)),
|
||||
item: Expr::Literal(Literal::Int(3)),
|
||||
span: 9..10,
|
||||
}),
|
||||
kind: BinOpKind::Add,
|
||||
|
@ -636,7 +635,7 @@ mod tests {
|
|||
span: 0..11,
|
||||
}),
|
||||
rhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Int(666)),
|
||||
item: Expr::Literal(Literal::Int(666)),
|
||||
span: 14..17,
|
||||
}),
|
||||
kind: BinOpKind::Divide,
|
||||
|
@ -660,7 +659,7 @@ mod tests {
|
|||
span: 4..5,
|
||||
},
|
||||
init: Some(Spanned {
|
||||
item: Expr::Literal(Value::Int(42)),
|
||||
item: Expr::Literal(Literal::Int(42)),
|
||||
span: 8..10,
|
||||
}),
|
||||
},
|
||||
|
@ -692,7 +691,7 @@ mod tests {
|
|||
},
|
||||
body: vec![Spanned {
|
||||
item: Stmt::Print(Spanned {
|
||||
item: Expr::Literal(Value::Str("Buy Able products!".to_owned())),
|
||||
item: Expr::Literal(Literal::Str("Buy Able products!".to_owned())),
|
||||
span: 19..39,
|
||||
}),
|
||||
span: 19..46,
|
||||
|
@ -717,7 +716,7 @@ mod tests {
|
|||
init: Some(Spanned {
|
||||
item: Expr::BinOp {
|
||||
lhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Str("script".to_owned())),
|
||||
item: Expr::Literal(Literal::Str("script".to_owned())),
|
||||
span: 20..26,
|
||||
}),
|
||||
rhs: Box::new(Spanned {
|
||||
|
@ -744,28 +743,28 @@ mod tests {
|
|||
item: Expr::Cart(vec![
|
||||
(
|
||||
Spanned {
|
||||
item: Expr::Literal(Value::Str("able".to_owned())),
|
||||
item: Expr::Literal(Literal::Str("able".to_owned())),
|
||||
span: 1..7,
|
||||
},
|
||||
Spanned {
|
||||
item: Expr::Literal(Value::Int(1)),
|
||||
item: Expr::Literal(Literal::Int(1)),
|
||||
span: 11..12,
|
||||
},
|
||||
),
|
||||
(
|
||||
Spanned {
|
||||
item: Expr::Literal(Value::Str("script".to_owned())),
|
||||
item: Expr::Literal(Literal::Str("script".to_owned())),
|
||||
span: 14..22,
|
||||
},
|
||||
Spanned {
|
||||
item: Expr::BinOp {
|
||||
kind: BinOpKind::Subtract,
|
||||
lhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Int(3)),
|
||||
item: Expr::Literal(Literal::Int(3)),
|
||||
span: 26..27,
|
||||
}),
|
||||
rhs: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Int(1)),
|
||||
item: Expr::Literal(Literal::Int(1)),
|
||||
span: 30..31,
|
||||
}),
|
||||
},
|
||||
|
@ -791,18 +790,18 @@ mod tests {
|
|||
expr: Box::new(Spanned {
|
||||
item: Expr::Cart(vec![(
|
||||
Spanned {
|
||||
item: Expr::Literal(Value::Str("able".to_owned())),
|
||||
item: Expr::Literal(Literal::Str("able".to_owned())),
|
||||
span: 1..7,
|
||||
},
|
||||
Spanned {
|
||||
item: Expr::Literal(Value::Str("ablecorp".to_owned())),
|
||||
item: Expr::Literal(Literal::Str("ablecorp".to_owned())),
|
||||
span: 11..21,
|
||||
},
|
||||
)]),
|
||||
span: 0..22,
|
||||
}),
|
||||
index: Box::new(Spanned {
|
||||
item: Expr::Literal(Value::Str("ablecorp".to_owned())),
|
||||
item: Expr::Literal(Literal::Str("ablecorp".to_owned())),
|
||||
span: 23..33,
|
||||
}),
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue