Skip to content

Commit

Permalink
Merge pull request #32 from FrankBro/move-box-to-expr-in
Browse files Browse the repository at this point in the history
move box to expr
  • Loading branch information
FrankBro authored Jan 11, 2024
2 parents 41e7b42 + c064015 commit 0e2e3aa
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 149 deletions.
10 changes: 5 additions & 5 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ impl Env {
}

fn eval_pattern(&mut self, pattern: &PatternAt, value: Value) -> Result<()> {
match &pattern.expr {
match pattern.expr.as_ref() {
Pattern::Var(var) => {
self.vars.insert(var.clone(), value);
}
Pattern::RecordExtend(labels, rest) => {
match &rest.expr {
match rest.expr.as_ref() {
Expr::RecordEmpty => (),
_ => return Err(Error::PatternRecordRestNotEmpty(*rest.clone())),
_ => return Err(Error::PatternRecordRestNotEmpty(rest.clone())),
}
let labels_value = match value {
Value::Record(labels) => labels,
Expand All @@ -198,7 +198,7 @@ impl Env {
}

fn eval_inner(&mut self, expr: &ExprAt) -> Result<Wrap> {
match &expr.expr {
match expr.expr.as_ref() {
Expr::Bool(b) => Ok(Wrap::Value(Value::Bool(*b))),
Expr::Int(i) => Ok(Wrap::Value(Value::Int(*i))),
Expr::IntBinOp(op, lhs, rhs) => {
Expand Down Expand Up @@ -265,7 +265,7 @@ impl Env {
Expr::Fun(params, body) => {
let env = self.clone();
let params = params.clone();
let body = *body.clone();
let body = body.clone();
let fun = Function { env, params, body };
Ok(Wrap::Value(Value::Function(fun)))
}
Expand Down
63 changes: 30 additions & 33 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl From<Type> for TypeContext {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprIn<Context> {
pub context: Context,
pub expr: Expr<Context>,
pub expr: Box<Expr<Context>>,
}

impl<Context> fmt::Display for ExprIn<Context> {
Expand All @@ -128,7 +128,10 @@ impl<Context> fmt::Display for ExprIn<Context> {

impl From<Expr<NoContext>> for ExprOnly {
fn from(expr: Expr<NoContext>) -> Self {
ExprIn { context: (), expr }
ExprIn {
context: (),
expr: expr.into(),
}
}
}

Expand All @@ -139,7 +142,7 @@ impl From<At<Expr<PositionContext>>> for ExprAt {
start: value.start,
end: value.end,
},
expr: value.value,
expr: value.value.into(),
}
}
}
Expand All @@ -148,7 +151,7 @@ impl ExprIn<PositionContext> {
pub fn strip_context(self) -> ExprIn<NoContext> {
ExprIn {
context: (),
expr: self.expr.strip_context(),
expr: self.expr.strip_context().into(),
}
}
}
Expand All @@ -157,7 +160,7 @@ impl ExprIn<PositionTypeContext> {
pub fn strip_position(self) -> ExprIn<TypeContext> {
ExprIn {
context: self.context.ty,
expr: self.expr.strip_position(),
expr: self.expr.strip_position().into(),
}
}

Expand Down Expand Up @@ -187,34 +190,30 @@ pub type PatternTypedAt = ExprTypedAt;
pub enum Expr<Context> {
Bool(bool),
Int(i64),
IntBinOp(IntBinOp, Box<ExprIn<Context>>, Box<ExprIn<Context>>),
Negate(Box<ExprIn<Context>>),
EqualEqual(Box<ExprIn<Context>>, Box<ExprIn<Context>>),
IntBinOp(IntBinOp, ExprIn<Context>, ExprIn<Context>),
Negate(ExprIn<Context>),
EqualEqual(ExprIn<Context>, ExprIn<Context>),
Var(String),
Call(Box<ExprIn<Context>>, Vec<ExprIn<Context>>),
Fun(Vec<PatternIn<Context>>, Box<ExprIn<Context>>),
Let(
Box<PatternIn<Context>>,
Box<ExprIn<Context>>,
Box<ExprIn<Context>>,
),
RecordSelect(Box<ExprIn<Context>>, String),
RecordExtend(BTreeMap<String, ExprIn<Context>>, Box<ExprIn<Context>>),
RecordRestrict(Box<ExprIn<Context>>, String),
Call(ExprIn<Context>, Vec<ExprIn<Context>>),
Fun(Vec<PatternIn<Context>>, ExprIn<Context>),
Let(PatternIn<Context>, ExprIn<Context>, ExprIn<Context>),
RecordSelect(ExprIn<Context>, String),
RecordExtend(BTreeMap<String, ExprIn<Context>>, ExprIn<Context>),
RecordRestrict(ExprIn<Context>, String),
RecordEmpty,
Variant(String, Box<ExprIn<Context>>),
Variant(String, ExprIn<Context>),
Case(
Box<ExprIn<Context>>,
ExprIn<Context>,
Vec<(String, String, ExprIn<Context>)>,
Option<(String, Box<ExprIn<Context>>)>,
Option<(String, ExprIn<Context>)>,
),
If(
Box<ExprIn<Context>>,
Box<ExprIn<Context>>,
ExprIn<Context>,
ExprIn<Context>,
Vec<(ExprIn<Context>, ExprIn<Context>)>,
Box<ExprIn<Context>>,
ExprIn<Context>,
),
Unwrap(Box<ExprIn<Context>>),
Unwrap(ExprIn<Context>),
}

impl<Context> fmt::Display for Expr<Context> {
Expand Down Expand Up @@ -243,7 +242,7 @@ impl<Context> fmt::Display for Expr<Context> {
.iter()
.map(|(label, val)| format!("{}: {}", label, val))
.join(", ");
match rest.expr {
match rest.expr.as_ref() {
Expr::RecordEmpty => write!(f, "{{{}}}", labels),
_ => write!(f, "{{{} | {}}}", labels, rest),
}
Expand All @@ -260,9 +259,8 @@ impl<Context> fmt::Display for Expr<Context> {

impl Expr<PositionContext> {
fn strip_context(self) -> Expr<NoContext> {
#[allow(clippy::boxed_local)]
fn fix(e: Box<ExprAt>) -> Box<ExprOnly> {
e.strip_context().into()
fn fix(e: ExprAt) -> ExprOnly {
e.strip_context()
}
match self {
Expr::Bool(b) => Expr::Bool(b),
Expand Down Expand Up @@ -316,14 +314,13 @@ impl Expr<PositionTypeContext> {
let ty = TypeContext { ty };
ExprIn {
context: PositionTypeContext { position, ty },
expr: self,
expr: self.into(),
}
}

pub fn strip_position(self) -> Expr<TypeContext> {
#[allow(clippy::boxed_local)]
fn fix(e: Box<ExprTypedAt>) -> Box<ExprTyped> {
e.strip_position().into()
fn fix(e: ExprTypedAt) -> ExprTyped {
e.strip_position()
}
match self {
Expr::Bool(b) => Expr::Bool(b),
Expand Down
Loading

0 comments on commit 0e2e3aa

Please sign in to comment.