mirror of
https://github.com/nushell/nushell.git
synced 2025-04-23 20:58:19 +02:00
Adding floating point
This commit is contained in:
parent
cb11f042ab
commit
d92e661253
21
src/eval.rs
21
src/eval.rs
@ -16,6 +16,7 @@ pub enum ShellError {
|
|||||||
pub enum Value {
|
pub enum Value {
|
||||||
Bool { val: bool, span: Span },
|
Bool { val: bool, span: Span },
|
||||||
Int { val: i64, span: Span },
|
Int { val: i64, span: Span },
|
||||||
|
Float { val: f64, span: Span },
|
||||||
String { val: String, span: Span },
|
String { val: String, span: Span },
|
||||||
List(Vec<Value>),
|
List(Vec<Value>),
|
||||||
Block(BlockId),
|
Block(BlockId),
|
||||||
@ -27,6 +28,7 @@ impl PartialEq for Value {
|
|||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Value::Bool { val: lhs, .. }, Value::Bool { val: rhs, .. }) => lhs == rhs,
|
(Value::Bool { val: lhs, .. }, Value::Bool { val: rhs, .. }) => lhs == rhs,
|
||||||
(Value::Int { val: lhs, .. }, Value::Int { 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::String { val: lhs, .. }, Value::String { val: rhs, .. }) => lhs == rhs,
|
||||||
(Value::List(l1), Value::List(l2)) => l1 == l2,
|
(Value::List(l1), Value::List(l2)) => l1 == l2,
|
||||||
(Value::Block(b1), Value::Block(b2)) => b1 == b2,
|
(Value::Block(b1), Value::Block(b2)) => b1 == b2,
|
||||||
@ -44,6 +46,9 @@ impl Display for Value {
|
|||||||
Value::Int { val, .. } => {
|
Value::Int { val, .. } => {
|
||||||
write!(f, "{}", val)
|
write!(f, "{}", val)
|
||||||
}
|
}
|
||||||
|
Value::Float { val, .. } => {
|
||||||
|
write!(f, "{}", val)
|
||||||
|
}
|
||||||
Value::String { val, .. } => write!(f, "{}", val),
|
Value::String { val, .. } => write!(f, "{}", val),
|
||||||
Value::List(..) => write!(f, "<list>"),
|
Value::List(..) => write!(f, "<list>"),
|
||||||
Value::Block(..) => write!(f, "<block>"),
|
Value::Block(..) => write!(f, "<block>"),
|
||||||
@ -59,6 +64,18 @@ impl Value {
|
|||||||
val: lhs + rhs,
|
val: lhs + rhs,
|
||||||
span: Span::unknown(),
|
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 {
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::String {
|
||||||
val: lhs.to_string() + rhs,
|
val: lhs.to_string() + rhs,
|
||||||
span: Span::unknown(),
|
span: Span::unknown(),
|
||||||
@ -287,6 +304,10 @@ pub fn eval_expression(
|
|||||||
val: *i,
|
val: *i,
|
||||||
span: expr.span,
|
span: expr.span,
|
||||||
}),
|
}),
|
||||||
|
Expr::Float(f) => Ok(Value::Float {
|
||||||
|
val: *f,
|
||||||
|
span: expr.span,
|
||||||
|
}),
|
||||||
Expr::Var(var_id) => stack
|
Expr::Var(var_id) => stack
|
||||||
.get_var(*var_id)
|
.get_var(*var_id)
|
||||||
.map_err(move |_| ShellError::VariableNotFound(expr.span)),
|
.map_err(move |_| ShellError::VariableNotFound(expr.span)),
|
||||||
|
@ -5,6 +5,7 @@ pub enum FlatShape {
|
|||||||
Garbage,
|
Garbage,
|
||||||
Bool,
|
Bool,
|
||||||
Int,
|
Int,
|
||||||
|
Float,
|
||||||
InternalCall,
|
InternalCall,
|
||||||
External,
|
External,
|
||||||
Literal,
|
Literal,
|
||||||
@ -57,6 +58,9 @@ impl<'a> ParserWorkingSet<'a> {
|
|||||||
Expr::Int(_) => {
|
Expr::Int(_) => {
|
||||||
vec![(expr.span, FlatShape::Int)]
|
vec![(expr.span, FlatShape::Int)]
|
||||||
}
|
}
|
||||||
|
Expr::Float(_) => {
|
||||||
|
vec![(expr.span, FlatShape::Float)]
|
||||||
|
}
|
||||||
Expr::Bool(_) => {
|
Expr::Bool(_) => {
|
||||||
vec![(expr.span, FlatShape::Bool)]
|
vec![(expr.span, FlatShape::Bool)]
|
||||||
}
|
}
|
||||||
|
@ -162,6 +162,7 @@ impl Call {
|
|||||||
pub enum Expr {
|
pub enum Expr {
|
||||||
Bool(bool),
|
Bool(bool),
|
||||||
Int(i64),
|
Int(i64),
|
||||||
|
Float(f64),
|
||||||
Var(VarId),
|
Var(VarId),
|
||||||
Call(Box<Call>),
|
Call(Box<Call>),
|
||||||
ExternalCall(Vec<u8>, Vec<Vec<u8>>),
|
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>) {
|
pub fn parse_number(&mut self, token: &str, span: Span) -> (Expression, Option<ParseError>) {
|
||||||
if let (x, None) = self.parse_int(token, span) {
|
if let (x, None) = self.parse_int(token, span) {
|
||||||
(x, None)
|
(x, None)
|
||||||
|
} else if let (x, None) = self.parse_float(token, span) {
|
||||||
|
(x, None)
|
||||||
} else {
|
} else {
|
||||||
(
|
(
|
||||||
garbage(span),
|
garbage(span),
|
||||||
|
@ -9,7 +9,7 @@ pub struct ParserState {
|
|||||||
vars: Vec<Type>,
|
vars: Vec<Type>,
|
||||||
decls: Vec<Declaration>,
|
decls: Vec<Declaration>,
|
||||||
blocks: Vec<Block>,
|
blocks: Vec<Block>,
|
||||||
scope: Vec<ScopeFrame>,
|
scope: Vec<ScopeFrame>, // REMOVE
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
@ -54,6 +54,9 @@ impl Highlighter for NuHighlighter {
|
|||||||
FlatShape::Int => {
|
FlatShape::Int => {
|
||||||
output.push((Style::new().fg(nu_ansi_term::Color::Green), next_token))
|
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 => {
|
FlatShape::Bool => {
|
||||||
output.push((Style::new().fg(nu_ansi_term::Color::LightCyan), next_token))
|
output.push((Style::new().fg(nu_ansi_term::Color::LightCyan), next_token))
|
||||||
}
|
}
|
||||||
|
@ -128,3 +128,8 @@ fn def_with_no_dollar() -> TestResult {
|
|||||||
fn env_shorthand() -> TestResult {
|
fn env_shorthand() -> TestResult {
|
||||||
run_test("FOO=BAR if $false { 3 } else { 4 }", "4")
|
run_test("FOO=BAR if $false { 3 } else { 4 }", "4")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn floating_add() -> TestResult {
|
||||||
|
run_test("10.1 + 0.8", "10.9")
|
||||||
|
}
|
||||||
|
@ -9,6 +9,7 @@ impl<'a> ParserWorkingSet<'a> {
|
|||||||
) -> (Type, Option<ParseError>) {
|
) -> (Type, Option<ParseError>) {
|
||||||
match &op.expr {
|
match &op.expr {
|
||||||
Expr::Operator(operator) => match operator {
|
Expr::Operator(operator) => match operator {
|
||||||
|
Operator::Equal => (Type::Bool, None),
|
||||||
Operator::Multiply => match (&lhs.ty, &rhs.ty) {
|
Operator::Multiply => match (&lhs.ty, &rhs.ty) {
|
||||||
(Type::Int, Type::Int) => (Type::Int, None),
|
(Type::Int, Type::Int) => (Type::Int, None),
|
||||||
(Type::Unknown, _) => (Type::Unknown, None),
|
(Type::Unknown, _) => (Type::Unknown, None),
|
||||||
|
Loading…
Reference in New Issue
Block a user