Remove redundant information
This commit is contained in:
parent
a464a849f1
commit
afd0a47bf2
7 changed files with 16 additions and 23 deletions
|
@ -31,11 +31,7 @@ pub enum UnaryOp {
|
|||
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum SimplExpr {
|
||||
// TODO figure out if that span value here is even necessary,..
|
||||
// DynVal has span information. However, keeping the span here and the span of the dynval separate
|
||||
// would allow to separate the span of where a dynval was defined and the
|
||||
// span of the use-site when a literal is used to replace a varref in the evaluation process.
|
||||
Literal(Span, DynVal),
|
||||
Literal(DynVal),
|
||||
VarRef(Span, VarName),
|
||||
BinOp(Span, Box<SimplExpr>, BinOp, Box<SimplExpr>),
|
||||
UnaryOp(Span, UnaryOp, Box<SimplExpr>),
|
||||
|
@ -47,8 +43,8 @@ pub enum SimplExpr {
|
|||
impl std::fmt::Display for SimplExpr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
SimplExpr::Literal(x) => write!(f, "\"{}\"", x),
|
||||
SimplExpr::VarRef(_, x) => write!(f, "{}", x),
|
||||
SimplExpr::Literal(_, x) => write!(f, "\"{}\"", x),
|
||||
SimplExpr::BinOp(_, l, op, r) => write!(f, "({} {} {})", l, op, r),
|
||||
SimplExpr::UnaryOp(_, op, x) => write!(f, "{}{}", op, x),
|
||||
SimplExpr::IfElse(_, a, b, c) => write!(f, "({} ? {} : {})", a, b, c),
|
||||
|
@ -61,23 +57,23 @@ impl std::fmt::Display for SimplExpr {
|
|||
}
|
||||
impl SimplExpr {
|
||||
pub fn literal(span: Span, s: String) -> Self {
|
||||
Self::Literal(span, DynVal(s, span))
|
||||
Self::Literal(DynVal(s, span))
|
||||
}
|
||||
|
||||
/// Construct a synthetic simplexpr from a literal string, without adding any relevant span information (uses [DUMMY_SPAN])
|
||||
pub fn synth_string(s: String) -> Self {
|
||||
Self::Literal(Span::DUMMY, DynVal(s, Span::DUMMY))
|
||||
Self::Literal(DynVal(s, Span::DUMMY))
|
||||
}
|
||||
|
||||
/// Construct a synthetic simplexpr from a literal dynval, without adding any relevant span information (uses [DUMMY_SPAN])
|
||||
pub fn synth_literal<T: Into<DynVal>>(s: T) -> Self {
|
||||
Self::Literal(Span::DUMMY, s.into())
|
||||
Self::Literal(s.into())
|
||||
}
|
||||
}
|
||||
impl Spanned for SimplExpr {
|
||||
fn span(&self) -> Span {
|
||||
match self {
|
||||
SimplExpr::Literal(span, _) => *span,
|
||||
SimplExpr::Literal(x) => x.span(),
|
||||
SimplExpr::VarRef(span, _) => *span,
|
||||
SimplExpr::BinOp(span, ..) => *span,
|
||||
SimplExpr::UnaryOp(span, ..) => *span,
|
||||
|
|
|
@ -92,7 +92,7 @@ impl SimplExpr {
|
|||
pub fn resolve_refs(self, variables: &HashMap<VarName, DynVal>) -> Result<Self, EvalError> {
|
||||
use SimplExpr::*;
|
||||
match self {
|
||||
Literal(span, x) => Ok(Literal(span, x)),
|
||||
Literal(x) => Ok(Literal(x)),
|
||||
BinOp(span, box a, op, box b) => Ok(BinOp(span, box a.resolve_refs(variables)?, op, box b.resolve_refs(variables)?)),
|
||||
UnaryOp(span, op, box x) => Ok(UnaryOp(span, op, box x.resolve_refs(variables)?)),
|
||||
IfElse(span, box a, box b, box c) => {
|
||||
|
@ -107,7 +107,7 @@ impl SimplExpr {
|
|||
args.into_iter().map(|a| a.resolve_refs(variables)).collect::<Result<_, EvalError>>()?,
|
||||
)),
|
||||
VarRef(span, ref name) => match variables.get(name) {
|
||||
Some(value) => Ok(Literal(span, value.clone())),
|
||||
Some(value) => Ok(Literal(value.clone())),
|
||||
None => Err(EvalError::UnknownVariable(name.clone()).at(span)),
|
||||
},
|
||||
}
|
||||
|
@ -147,7 +147,7 @@ impl SimplExpr {
|
|||
pub fn eval(&self, values: &HashMap<VarName, DynVal>) -> Result<DynVal, EvalError> {
|
||||
let span = self.span();
|
||||
let value = match self {
|
||||
SimplExpr::Literal(_, x) => Ok(x.clone()),
|
||||
SimplExpr::Literal(x) => Ok(x.clone()),
|
||||
SimplExpr::VarRef(span, ref name) => {
|
||||
Ok(values.get(name).cloned().ok_or_else(|| EvalError::UnknownVariable(name.clone()).at(*span))?.at(*span))
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ pub fn parse_stringlit(
|
|||
let span = Span(lo, hi, file_id);
|
||||
match segment {
|
||||
StrLitSegment::Literal(lit) if lit.is_empty() => None,
|
||||
StrLitSegment::Literal(lit) => Some(Ok(SimplExpr::Literal(span, DynVal(lit, span)))),
|
||||
StrLitSegment::Literal(lit) => Some(Ok(SimplExpr::Literal(DynVal(lit, span)))),
|
||||
StrLitSegment::Interp(toks) => {
|
||||
let token_stream = toks.into_iter().map(|x| Ok(x));
|
||||
Some(parser.parse(file_id, token_stream))
|
||||
|
@ -32,5 +32,5 @@ pub fn parse_stringlit(
|
|||
Some(ast) => Some(SimplExpr::BinOp(span, Box::new(ast), BinOp::Plus, Box::new(cur))),
|
||||
None => Some(cur),
|
||||
})
|
||||
.map(|ast| ast.unwrap_or_else(|| SimplExpr::Literal(span, DynVal(String::new(), span))))
|
||||
.map(|ast| ast.unwrap_or_else(|| SimplExpr::Literal(DynVal(String::new(), span))))
|
||||
}
|
||||
|
|
|
@ -76,7 +76,7 @@ pub Expr: SimplExpr = {
|
|||
<l:@L> <value:Expr> "[" <index: ExprReset> "]" <r:@R> => JsonAccess(Span(l, r, fid), b(value), b(index)),
|
||||
|
||||
<l:@L> <value:Expr> "." <lit_l:@L> <index:"identifier"> <r:@R> => {
|
||||
JsonAccess(Span(l, r, fid), b(value), b(Literal(Span(lit_l, r, fid), index.into())))
|
||||
JsonAccess(Span(l, r, fid), b(value), b(Literal(index.into())))
|
||||
},
|
||||
|
||||
#[precedence(level="2")] #[assoc(side="right")]
|
||||
|
|
|
@ -18,7 +18,7 @@ Config(
|
|||
attrs: {
|
||||
AttrName("arg"): AttrEntry(
|
||||
key_span: Span(52, 56, 0),
|
||||
value: SimplExpr(Span(57, 61, 0), Literal(Span(57, 61, 0), DynVal("hi", Span(57, 61, 0)))),
|
||||
value: SimplExpr(Span(57, 61, 0), Literal(DynVal("hi", Span(57, 61, 0)))),
|
||||
),
|
||||
},
|
||||
),
|
||||
|
@ -56,7 +56,7 @@ Config(
|
|||
attrs: {
|
||||
AttrName("arg"): AttrEntry(
|
||||
key_span: Span(468, 472, 0),
|
||||
value: SimplExpr(Span(473, 478, 0), Literal(Span(473, 478, 0), DynVal("bla", Span(473, 478, 0)))),
|
||||
value: SimplExpr(Span(473, 478, 0), Literal(DynVal("bla", Span(473, 478, 0)))),
|
||||
),
|
||||
},
|
||||
),
|
||||
|
|
|
@ -113,7 +113,7 @@ impl std::fmt::Display for Ast {
|
|||
Array(_, x) => write!(f, "({})", x.iter().map(|e| format!("{}", e)).join(" ")),
|
||||
Keyword(_, x) => write!(f, ":{}", x),
|
||||
Symbol(_, x) => write!(f, "{}", x),
|
||||
SimplExpr(_, simplexpr::SimplExpr::Literal(_, value)) => write!(f, "\"{}\"", value),
|
||||
SimplExpr(_, simplexpr::SimplExpr::Literal(value)) => write!(f, "\"{}\"", value),
|
||||
SimplExpr(_, x) => write!(f, "{{{}}}", x),
|
||||
Comment(_) => write!(f, ""),
|
||||
}
|
||||
|
|
|
@ -46,11 +46,8 @@ impl<I: Iterator<Item = Ast>> AstIterator<I> {
|
|||
pub fn expect_literal(&mut self) -> AstResult<(Span, DynVal)> {
|
||||
// TODO add some others
|
||||
match self.expect_any()? {
|
||||
// Ast::List(_, _) => todo!(),
|
||||
// Ast::Array(_, _) => todo!(),
|
||||
// Ast::Keyword(_, _) => todo!(),
|
||||
// Ast::Symbol(_, _) => todo!(),
|
||||
Ast::SimplExpr(span, expr) => Ok((span, dbg!(expr.eval_no_vars()).map_err(|e| AstError::SimplExpr(e.into()))?)),
|
||||
Ast::SimplExpr(span, expr) => Ok((span, expr.eval_no_vars().map_err(|e| AstError::SimplExpr(e.into()))?)),
|
||||
other => {
|
||||
let span = other.span();
|
||||
let actual_type = other.expr_type();
|
||||
|
|
Loading…
Add table
Reference in a new issue