Adding floating point

This commit is contained in:
JT 2021-08-09 08:21:21 +12:00
parent cb11f042ab
commit d92e661253
7 changed files with 56 additions and 1 deletions

View File

@ -16,6 +16,7 @@ pub enum ShellError {
pub enum Value {
Bool { val: bool, span: Span },
Int { val: i64, span: Span },
Float { val: f64, span: Span },
String { val: String, span: Span },
List(Vec<Value>),
Block(BlockId),
@ -27,6 +28,7 @@ impl PartialEq for Value {
match (self, other) {
(Value::Bool { val: lhs, .. }, Value::Bool { val: rhs, .. }) => lhs == rhs,
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => lhs == rhs,
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => lhs == rhs,
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => lhs == rhs,
(Value::List(l1), Value::List(l2)) => l1 == l2,
(Value::Block(b1), Value::Block(b2)) => b1 == b2,
@ -44,6 +46,9 @@ impl Display for Value {
Value::Int { val, .. } => {
write!(f, "{}", val)
}
Value::Float { val, .. } => {
write!(f, "{}", val)
}
Value::String { val, .. } => write!(f, "{}", val),
Value::List(..) => write!(f, "<list>"),
Value::Block(..) => write!(f, "<block>"),
@ -59,6 +64,18 @@ impl Value {
val: lhs + rhs,
span: Span::unknown(),
}),
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
val: *lhs as f64 + *rhs,
span: Span::unknown(),
}),
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Float {
val: *lhs + *rhs as f64,
span: Span::unknown(),
}),
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
val: lhs + rhs,
span: Span::unknown(),
}),
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::String {
val: lhs.to_string() + rhs,
span: Span::unknown(),
@ -287,6 +304,10 @@ pub fn eval_expression(
val: *i,
span: expr.span,
}),
Expr::Float(f) => Ok(Value::Float {
val: *f,
span: expr.span,
}),
Expr::Var(var_id) => stack
.get_var(*var_id)
.map_err(move |_| ShellError::VariableNotFound(expr.span)),

View File

@ -5,6 +5,7 @@ pub enum FlatShape {
Garbage,
Bool,
Int,
Float,
InternalCall,
External,
Literal,
@ -57,6 +58,9 @@ impl<'a> ParserWorkingSet<'a> {
Expr::Int(_) => {
vec![(expr.span, FlatShape::Int)]
}
Expr::Float(_) => {
vec![(expr.span, FlatShape::Float)]
}
Expr::Bool(_) => {
vec![(expr.span, FlatShape::Bool)]
}

View File

@ -162,6 +162,7 @@ impl Call {
pub enum Expr {
Bool(bool),
Int(i64),
Float(f64),
Var(VarId),
Call(Box<Call>),
ExternalCall(Vec<u8>, Vec<Vec<u8>>),
@ -905,9 +906,29 @@ impl<'a> ParserWorkingSet<'a> {
}
}
pub fn parse_float(&mut self, token: &str, span: Span) -> (Expression, Option<ParseError>) {
if let Ok(x) = token.parse::<f64>() {
(
Expression {
expr: Expr::Float(x),
span,
ty: Type::Int,
},
None,
)
} else {
(
garbage(span),
Some(ParseError::Mismatch("int".into(), span)),
)
}
}
pub fn parse_number(&mut self, token: &str, span: Span) -> (Expression, Option<ParseError>) {
if let (x, None) = self.parse_int(token, span) {
(x, None)
} else if let (x, None) = self.parse_float(token, span) {
(x, None)
} else {
(
garbage(span),

View File

@ -9,7 +9,7 @@ pub struct ParserState {
vars: Vec<Type>,
decls: Vec<Declaration>,
blocks: Vec<Block>,
scope: Vec<ScopeFrame>,
scope: Vec<ScopeFrame>, // REMOVE
}
#[derive(Clone, Debug, PartialEq, Eq)]

View File

@ -54,6 +54,9 @@ impl Highlighter for NuHighlighter {
FlatShape::Int => {
output.push((Style::new().fg(nu_ansi_term::Color::Green), next_token))
}
FlatShape::Float => {
output.push((Style::new().fg(nu_ansi_term::Color::Green), next_token))
}
FlatShape::Bool => {
output.push((Style::new().fg(nu_ansi_term::Color::LightCyan), next_token))
}

View File

@ -128,3 +128,8 @@ fn def_with_no_dollar() -> TestResult {
fn env_shorthand() -> TestResult {
run_test("FOO=BAR if $false { 3 } else { 4 }", "4")
}
#[test]
fn floating_add() -> TestResult {
run_test("10.1 + 0.8", "10.9")
}

View File

@ -9,6 +9,7 @@ impl<'a> ParserWorkingSet<'a> {
) -> (Type, Option<ParseError>) {
match &op.expr {
Expr::Operator(operator) => match operator {
Operator::Equal => (Type::Bool, None),
Operator::Multiply => match (&lhs.ty, &rhs.ty) {
(Type::Int, Type::Int) => (Type::Int, None),
(Type::Unknown, _) => (Type::Unknown, None),