add datetime expression, remove todo compileerror

This commit is contained in:
Devyn Cairns 2024-07-05 05:17:49 -07:00
parent c7a86cf75e
commit 4b56fba7d6
7 changed files with 11 additions and 14 deletions

View File

@ -137,6 +137,7 @@ impl BlockBuilder {
| Literal::String(_)
| Literal::RawString(_)
| Literal::CellPath(_)
| Literal::Date(_)
| Literal::Nothing => (),
}
}

View File

@ -46,11 +46,6 @@ pub(crate) fn compile_expression(
builder.load_empty(out_reg)
};
let todo = |msg: &str| CompileError::Todo {
msg: msg.into(),
span: Some(expr.span),
};
let unexpected = |expr_name: &str| CompileError::UnexpectedExpression {
expr_name: expr_name.into(),
span: expr.span,
@ -375,7 +370,7 @@ pub(crate) fn compile_expression(
Expr::ValueWithUnit(value_with_unit) => {
lit(builder, literal_from_value_with_unit(value_with_unit)?)
}
Expr::DateTime(_) => Err(todo("DateTime")),
Expr::DateTime(dt) => lit(builder, Literal::Date(Box::new(*dt))),
Expr::Filepath(path, no_expand) => {
let val = builder.data(path)?;
lit(

View File

@ -745,6 +745,7 @@ fn literal_value(
Literal::String(s) => Value::string(ctx.get_str(*s, span)?, span),
Literal::RawString(s) => Value::string(ctx.get_str(*s, span)?, span),
Literal::CellPath(path) => Value::cell_path(CellPath::clone(&path), span),
Literal::Date(dt) => Value::date(**dt, span),
Literal::Nothing => Value::nothing(span),
})
}

View File

@ -128,12 +128,4 @@ pub enum CompileError {
#[label("{msg}")]
span: Span,
},
#[error("TODO: {msg}")]
#[diagnostic(code(nu::compile::todo), help("IR compilation is a work in progress"))]
Todo {
msg: String,
#[label("not yet implemented")]
span: Option<Span>,
},
}

View File

@ -323,6 +323,7 @@ impl<'a> fmt::Display for FmtLiteral<'a> {
Literal::String(s) => write!(f, "string({})", FmtData(self.data, *s)),
Literal::RawString(rs) => write!(f, "raw-string({})", FmtData(self.data, *rs)),
Literal::CellPath(p) => write!(f, "cell-path({p})"),
Literal::Date(dt) => write!(f, "date({dt})"),
Literal::Nothing => write!(f, "nothing"),
}
}

View File

@ -6,6 +6,7 @@ use crate::{
BlockId, DeclId, RegId, Span, Value, VarId,
};
use chrono::{DateTime, FixedOffset};
use serde::{Deserialize, Serialize};
mod call;
@ -278,6 +279,7 @@ pub enum Literal {
String(DataSlice),
RawString(DataSlice),
CellPath(Box<CellPath>),
Date(Box<DateTime<FixedOffset>>),
Nothing,
}

View File

@ -164,6 +164,11 @@ fn literal_raw_string() {
test_eval(r#"r#'bazquux'#"#, Eq("bazquux"))
}
#[test]
fn literal_date() {
test_eval("2020-01-01T00:00:00Z", Matches("2020"))
}
#[test]
fn literal_nothing() {
test_eval("null", Eq(""))