Skip to content

Commit

Permalink
draft SourceSpan and SourceSpanWrapper;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Jul 19, 2023
1 parent 483062b commit 2c47b8b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ergotree-interpreter/src/eval/coll_append.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ mod tests {
fn append_byte_array_and_byte() {
let byte_coll: Constant = vec![1i8, 2i8].into();
let byte: Expr = Expr::Collection(Collection::new(SType::SByte, vec![3i8.into()]).unwrap());
let expr: Expr = Expr::Append(Append::new(byte_coll.into(), byte).unwrap());
let expr: Expr = Expr::Append(Append::new(byte_coll.into(), byte).unwrap().into());
assert_eq!(eval_out_wo_ctx::<Vec<i8>>(&expr), vec![1i8, 2, 3]);
}
}
2 changes: 1 addition & 1 deletion ergotree-interpreter/src/eval/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Evaluable for Expr {
Expr::Upcast(op) => op.eval(env, ctx),
Expr::Downcast(op) => op.eval(env, ctx),
Expr::If(op) => op.eval(env, ctx),
Expr::Append(op) => op.eval(env, ctx),
Expr::Append(op) => op.expr().eval(env, ctx),
Expr::ByIndex(op) => op.eval(env, ctx),
Expr::ExtractScriptBytes(op) => op.eval(env, ctx),
Expr::SizeOf(op) => op.eval(env, ctx),
Expand Down
7 changes: 5 additions & 2 deletions ergotree-ir/src/mir/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ use derive_more::From;
use derive_more::TryInto;
use thiserror::Error;

mod source_span;
pub use source_span::*;

#[derive(PartialEq, Eq, Debug, Clone, From, TryInto)]
/// Expression in ErgoTree
pub enum Expr {
/// Append - Concatenation of two collections
Append(Append),
Append(SourceSpanWrapper<Append>),
/// Constant value
Const(Constant),
/// Placeholder for a constant
Expand Down Expand Up @@ -227,7 +230,7 @@ impl Expr {
/// Type of the expression
pub fn tpe(&self) -> SType {
match self {
Expr::Append(ap) => ap.tpe(),
Expr::Append(ap) => ap.expr().tpe(),
Expr::Const(v) => v.tpe.clone(),
Expr::Collection(v) => v.tpe(),
Expr::SubstConstants(v) => v.tpe(),
Expand Down
57 changes: 57 additions & 0 deletions ergotree-ir/src/mir/expr/source_span.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::mir::coll_append::Append;

use super::Expr;

/// Source position for the Expr
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct SourceSpan {
/// Start position in the source code
pub start: usize,
/// End position in the source code
pub end: usize,
}

impl SourceSpan {
/// Empty span
pub fn empty() -> Self {
SourceSpan { start: 0, end: 0 }
}
}

/// Wrapper for Expr with source position
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct SourceSpanWrapper<T> {
/// Source position
pub source_span: SourceSpan,
/// Wrapped value
pub expr: T,
}

impl<T> SourceSpanWrapper<T> {
/// Expression
pub fn expr(&self) -> &T {
&self.expr
}
}

// TODO: can be a macros
impl From<Append> for Expr {
fn from(v: Append) -> Self {
Expr::Append(SourceSpanWrapper {
source_span: SourceSpan::empty(),
expr: v,
})
}
}

impl<T> From<T> for SourceSpanWrapper<T> {
fn from(v: T) -> Self {
SourceSpanWrapper {
source_span: SourceSpan::empty(),
expr: v,
}
}
}

// TODO: draft pretty printer and how it's sets source span for every expr
// TODO: draft enriching eval errors with source span and hightlight it in the source code piece
2 changes: 1 addition & 1 deletion ergotree-ir/src/serialization/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl SigmaSerializable for Expr {
}
None => c.sigma_serialize(w),
},
Expr::Append(ap) => ap.sigma_serialize_w_opcode(w),
Expr::Append(ap) => ap.expr().sigma_serialize_w_opcode(w),
Expr::Fold(op) => op.sigma_serialize_w_opcode(w),
Expr::ConstPlaceholder(cp) => cp.sigma_serialize_w_opcode(w),
Expr::SubstConstants(s) => s.sigma_serialize_w_opcode(w),
Expand Down

0 comments on commit 2c47b8b

Please sign in to comment.