Skip to content

Commit

Permalink
LEM tag own file and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-barrett committed Jan 4, 2024
1 parent c13c502 commit 4b40af9
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 218 deletions.
2 changes: 1 addition & 1 deletion src/cli/repl/meta_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
multiframe::MultiFrame,
pointers::{Ptr, RawPtr, ZPtr},
store::expect_ptrs,
Tag,
tag::Tag,
},
package::{Package, SymbolRef},
proof::{
Expand Down
2 changes: 1 addition & 1 deletion src/cli/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
multiframe::MultiFrame,
pointers::{Ptr, RawPtr},
store::Store,
Tag,
tag::Tag,
},
parser,
proof::{nova::NovaProver, Prover, RecursiveSNARKTrait},
Expand Down
2 changes: 1 addition & 1 deletion src/cli/zstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ mod tests {
lem::{
pointers::Ptr,
store::{intern_ptrs, Store},
Tag,
tag::Tag,
},
tag::{ContTag, ExprTag, Op1, Op2},
};
Expand Down
2 changes: 1 addition & 1 deletion src/coprocessor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub(crate) mod test {

use super::*;
use crate::circuit::gadgets::constraints::{alloc_equal, mul};
use crate::lem::{pointers::RawPtr, Tag as LEMTag};
use crate::lem::{pointers::RawPtr, tag::Tag as LEMTag};
use crate::tag::{ExprTag, Tag};
use std::marker::PhantomData;

Expand Down
3 changes: 2 additions & 1 deletion src/lem/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use super::{
pointers::{Ptr, RawPtr},
slot::{SlotData, Val},
store::{fetch_ptrs, intern_ptrs, Store},
tag::Tag,
var_map::VarMap,
Block, Ctrl, Func, Op, Tag, Var,
Block, Ctrl, Func, Op, Var,
};

use crate::{
Expand Down
8 changes: 4 additions & 4 deletions src/lem/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ macro_rules! lit {
#[macro_export]
macro_rules! tag {
( Expr::$tag:ident ) => {
$crate::lem::Tag::Expr($crate::tag::ExprTag::$tag)
$crate::lem::tag::Tag::Expr($crate::tag::ExprTag::$tag)
};
( Cont::$tag:ident ) => {
$crate::lem::Tag::Cont($crate::tag::ContTag::$tag)
$crate::lem::tag::Tag::Cont($crate::tag::ContTag::$tag)
};
( Op1::$tag:ident ) => {
$crate::lem::Tag::Op1($crate::tag::Op1::$tag)
$crate::lem::tag::Tag::Op1($crate::tag::Op1::$tag)
};
( Op2::$tag:ident ) => {
$crate::lem::Tag::Op2($crate::tag::Op2::$tag)
$crate::lem::tag::Tag::Op2($crate::tag::Op2::$tag)
};
}

Expand Down
207 changes: 3 additions & 204 deletions src/lem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,19 @@ mod path;
pub mod pointers;
mod slot;
pub mod store;
pub mod tag;
mod var_map;

use anyhow::{bail, Result};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[cfg(test)]
mod tests;

use crate::{
field::LurkField,
symbol::Symbol,
tag::{ContTag, ExprTag, Op1, Op2, Tag as TagTrait},
};
use crate::{field::LurkField, symbol::Symbol};

use self::{pointers::Ptr, slot::SlotsCounter, store::Store, var_map::VarMap};
use self::{pointers::Ptr, slot::SlotsCounter, store::Store, tag::Tag, var_map::VarMap};

pub type AString = Arc<str>;

Expand All @@ -103,203 +99,6 @@ pub struct Func {
#[derive(Debug, PartialEq, Clone, Eq, Hash)]
pub struct Var(AString);

/// The LEM `Tag` is a wrapper around other types that are used as tags
#[derive(Copy, Debug, PartialEq, Clone, Eq, Hash, Serialize, Deserialize)]
pub enum Tag {
Expr(ExprTag),
Cont(ContTag),
Op1(Op1),
Op2(Op2),
}

impl TryFrom<u16> for Tag {
type Error = anyhow::Error;

fn try_from(val: u16) -> Result<Self, Self::Error> {
if let Ok(tag) = ExprTag::try_from(val) {
Ok(Tag::Expr(tag))
} else if let Ok(tag) = ContTag::try_from(val) {
Ok(Tag::Cont(tag))
} else if let Ok(tag) = Op1::try_from(val) {
Ok(Tag::Op1(tag))
} else if let Ok(tag) = Op2::try_from(val) {
Ok(Tag::Op2(tag))
} else {
bail!("Invalid u16 for Tag: {val}")
}
}
}

impl From<Tag> for u16 {
fn from(val: Tag) -> Self {
match val {
Tag::Expr(tag) => tag.into(),
Tag::Cont(tag) => tag.into(),
Tag::Op1(tag) => tag.into(),
Tag::Op2(tag) => tag.into(),
}
}
}

impl TagTrait for Tag {
fn from_field<F: LurkField>(f: &F) -> Option<Self> {
Self::try_from(f.to_u16()?).ok()
}

fn to_field<F: LurkField>(&self) -> F {
Tag::to_field(self)
}
}

impl Tag {
#[inline]
pub fn to_field<F: LurkField>(&self) -> F {
match self {
Self::Expr(tag) => tag.to_field(),
Self::Cont(tag) => tag.to_field(),
Self::Op1(tag) => tag.to_field(),
Self::Op2(tag) => tag.to_field(),
}
}

pub fn pos(i: usize) -> Option<Self> {
match i {
0 => Some(Self::Expr(ExprTag::Nil)),
1 => Some(Self::Expr(ExprTag::Cons)),
2 => Some(Self::Expr(ExprTag::Sym)),
3 => Some(Self::Expr(ExprTag::Fun)),
4 => Some(Self::Expr(ExprTag::Num)),
5 => Some(Self::Expr(ExprTag::Thunk)),
6 => Some(Self::Expr(ExprTag::Str)),
7 => Some(Self::Expr(ExprTag::Char)),
8 => Some(Self::Expr(ExprTag::Comm)),
9 => Some(Self::Expr(ExprTag::U64)),
10 => Some(Self::Expr(ExprTag::Key)),
11 => Some(Self::Expr(ExprTag::Cproc)),
12 => Some(Self::Cont(ContTag::Outermost)),
13 => Some(Self::Cont(ContTag::Call0)),
14 => Some(Self::Cont(ContTag::Call)),
15 => Some(Self::Cont(ContTag::Call2)),
16 => Some(Self::Cont(ContTag::Tail)),
17 => Some(Self::Cont(ContTag::Error)),
18 => Some(Self::Cont(ContTag::Lookup)),
19 => Some(Self::Cont(ContTag::Unop)),
20 => Some(Self::Cont(ContTag::Binop)),
21 => Some(Self::Cont(ContTag::Binop2)),
22 => Some(Self::Cont(ContTag::If)),
23 => Some(Self::Cont(ContTag::Let)),
24 => Some(Self::Cont(ContTag::LetRec)),
25 => Some(Self::Cont(ContTag::Dummy)),
26 => Some(Self::Cont(ContTag::Terminal)),
27 => Some(Self::Cont(ContTag::Emit)),
28 => Some(Self::Cont(ContTag::Cproc)),
29 => Some(Self::Op1(Op1::Car)),
30 => Some(Self::Op1(Op1::Cdr)),
31 => Some(Self::Op1(Op1::Atom)),
32 => Some(Self::Op1(Op1::Emit)),
33 => Some(Self::Op1(Op1::Open)),
34 => Some(Self::Op1(Op1::Secret)),
35 => Some(Self::Op1(Op1::Commit)),
36 => Some(Self::Op1(Op1::Num)),
37 => Some(Self::Op1(Op1::Comm)),
38 => Some(Self::Op1(Op1::Char)),
39 => Some(Self::Op1(Op1::Eval)),
40 => Some(Self::Op1(Op1::U64)),
41 => Some(Self::Op2(Op2::Sum)),
42 => Some(Self::Op2(Op2::Diff)),
43 => Some(Self::Op2(Op2::Product)),
44 => Some(Self::Op2(Op2::Quotient)),
45 => Some(Self::Op2(Op2::Equal)),
46 => Some(Self::Op2(Op2::NumEqual)),
47 => Some(Self::Op2(Op2::Less)),
48 => Some(Self::Op2(Op2::Greater)),
49 => Some(Self::Op2(Op2::LessEqual)),
50 => Some(Self::Op2(Op2::GreaterEqual)),
51 => Some(Self::Op2(Op2::Cons)),
52 => Some(Self::Op2(Op2::StrCons)),
53 => Some(Self::Op2(Op2::Begin)),
54 => Some(Self::Op2(Op2::Hide)),
55 => Some(Self::Op2(Op2::Modulo)),
56 => Some(Self::Op2(Op2::Eval)),
_ => None,
}
}

pub fn index(&self) -> usize {
match self {
Self::Expr(ExprTag::Nil) => 0,
Self::Expr(ExprTag::Cons) => 1,
Self::Expr(ExprTag::Sym) => 2,
Self::Expr(ExprTag::Fun) => 3,
Self::Expr(ExprTag::Num) => 4,
Self::Expr(ExprTag::Thunk) => 5,
Self::Expr(ExprTag::Str) => 6,
Self::Expr(ExprTag::Char) => 7,
Self::Expr(ExprTag::Comm) => 8,
Self::Expr(ExprTag::U64) => 9,
Self::Expr(ExprTag::Key) => 10,
Self::Expr(ExprTag::Cproc) => 11,
Self::Cont(ContTag::Outermost) => 12,
Self::Cont(ContTag::Call0) => 13,
Self::Cont(ContTag::Call) => 14,
Self::Cont(ContTag::Call2) => 15,
Self::Cont(ContTag::Tail) => 16,
Self::Cont(ContTag::Error) => 17,
Self::Cont(ContTag::Lookup) => 18,
Self::Cont(ContTag::Unop) => 19,
Self::Cont(ContTag::Binop) => 20,
Self::Cont(ContTag::Binop2) => 21,
Self::Cont(ContTag::If) => 22,
Self::Cont(ContTag::Let) => 23,
Self::Cont(ContTag::LetRec) => 24,
Self::Cont(ContTag::Dummy) => 25,
Self::Cont(ContTag::Terminal) => 26,
Self::Cont(ContTag::Emit) => 27,
Self::Cont(ContTag::Cproc) => 28,
Self::Op1(Op1::Car) => 29,
Self::Op1(Op1::Cdr) => 30,
Self::Op1(Op1::Atom) => 31,
Self::Op1(Op1::Emit) => 32,
Self::Op1(Op1::Open) => 33,
Self::Op1(Op1::Secret) => 34,
Self::Op1(Op1::Commit) => 35,
Self::Op1(Op1::Num) => 36,
Self::Op1(Op1::Comm) => 37,
Self::Op1(Op1::Char) => 38,
Self::Op1(Op1::Eval) => 39,
Self::Op1(Op1::U64) => 40,
Self::Op2(Op2::Sum) => 41,
Self::Op2(Op2::Diff) => 42,
Self::Op2(Op2::Product) => 43,
Self::Op2(Op2::Quotient) => 44,
Self::Op2(Op2::Equal) => 45,
Self::Op2(Op2::NumEqual) => 46,
Self::Op2(Op2::Less) => 47,
Self::Op2(Op2::Greater) => 48,
Self::Op2(Op2::LessEqual) => 49,
Self::Op2(Op2::GreaterEqual) => 50,
Self::Op2(Op2::Cons) => 51,
Self::Op2(Op2::StrCons) => 52,
Self::Op2(Op2::Begin) => 53,
Self::Op2(Op2::Hide) => 54,
Self::Op2(Op2::Modulo) => 55,
Self::Op2(Op2::Eval) => 56,
}
}
}

impl std::fmt::Display for Tag {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Tag::{Cont, Expr, Op1, Op2};
match self {
Expr(tag) => write!(f, "expr.{}", tag),
Cont(tag) => write!(f, "cont.{}", tag),
Op1(tag) => write!(f, "op1.{}", tag),
Op2(tag) => write!(f, "op2.{}", tag),
}
}
}

/// LEM literals
#[derive(Debug, PartialEq, Clone, Eq, Hash)]
pub enum Lit {
Expand Down
Loading

0 comments on commit 4b40af9

Please sign in to comment.