Skip to content

Commit

Permalink
refactor lib.rs to contain only the run_test function
Browse files Browse the repository at this point in the history
  • Loading branch information
oflatt committed Jan 29, 2024
1 parent f825b88 commit 9e988d9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 123 deletions.
4 changes: 3 additions & 1 deletion tree_shared/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{Expr, Expr::*, Id, Id::Shared, Id::Unique, Order};
//! Provides a set of functions for constructing `[Expr]`s.

use crate::{expr::Expr, expr::Expr::*, expr::Id, expr::Id::Shared, expr::Id::Unique, expr::Order};

pub fn give_fresh_ids(expr: &mut Expr) {
let mut id = 1;
Expand Down
9 changes: 6 additions & 3 deletions tree_shared/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
use std::collections::HashMap;

use crate::{
Expr,
Id::{self, Shared, Unique},
Order, Type, TypeError, Value,
expr::Expr,
expr::{
Id::{self, Shared, Unique},
Type, Value,
},
expr::{Order, TypeError},
};

pub fn typecheck(e: &Expr, arg_ty: &Option<Type>) -> Result<Type, TypeError> {
Expand Down
120 changes: 1 addition & 119 deletions tree_shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod ast;
pub(crate) mod deep_copy;
pub(crate) mod error_checking;
pub(crate) mod expr;
pub(crate) mod id_analysis;
pub mod interpreter;
pub(crate) mod ir;
Expand All @@ -12,125 +13,6 @@ pub(crate) mod util;

pub type Result = std::result::Result<(), egglog::Error>;

#[derive(Clone, Debug, PartialEq)]
pub enum Order {
Parallel,
Sequential,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Id {
Unique(i64),
Shared,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Expr {
Num(i64),
Boolean(bool),
Add(Box<Expr>, Box<Expr>),
Sub(Box<Expr>, Box<Expr>),
Mul(Box<Expr>, Box<Expr>),
LessThan(Box<Expr>, Box<Expr>),
And(Box<Expr>, Box<Expr>),
Or(Box<Expr>, Box<Expr>),
Not(Box<Expr>),
Get(Box<Expr>, usize),
/// Concat is a convenient built-in way
/// to put two tuples together.
/// It's not strictly necessary, but
/// doing it by constructing a new big tuple is tedius and slow.
Concat(Box<Expr>, Box<Expr>),
Print(Box<Expr>),
Read(Box<Expr>),
Write(Box<Expr>, Box<Expr>),
All(Id, Order, Vec<Expr>),
/// A pred and a list of branches
Switch(Box<Expr>, Vec<Expr>),
/// Should only be a child of `Switch`
/// Represents a single branch of a switch, giving
/// it a unique id
Branch(Id, Box<Expr>),
Loop(Id, Box<Expr>, Box<Expr>),
Let(Id, Box<Expr>, Box<Expr>),
Arg(Id),
Function(Id, Box<Expr>),
/// A list of functions, with the first
/// being the main function.
Program(Vec<Expr>),
Call(Id, Box<Expr>),
}

impl Expr {
/// Runs `func` on every child of this expression.
pub fn for_each_child(&mut self, mut func: impl FnMut(&mut Expr)) {
match self {
Expr::Num(_) | Expr::Boolean(_) | Expr::Arg(_) => {}
Expr::Add(a, b)
| Expr::Sub(a, b)
| Expr::Mul(a, b)
| Expr::LessThan(a, b)
| Expr::And(a, b)
| Expr::Or(a, b)
| Expr::Concat(a, b)
| Expr::Write(a, b) => {
func(a);
func(b);
}
Expr::Not(a) | Expr::Print(a) | Expr::Read(a) => {
func(a);
}
Expr::Get(a, _) | Expr::Function(_, a) | Expr::Call(_, a) => {
func(a);
}
Expr::All(_, _, children) => {
for child in children {
func(child);
}
}
Expr::Switch(input, children) => {
func(input);
for child in children {
func(child);
}
}
Expr::Branch(_id, child) => {
func(child);
}
Expr::Loop(_, pred, output) | Expr::Let(_, pred, output) => {
func(pred);
func(output);
}
Expr::Program(functions) => {
for function in functions {
func(function);
}
}
}
}
}

#[derive(Clone, Debug, PartialEq)]
pub enum Value {
Num(i64),
Boolean(bool),
Tuple(Vec<Value>),
}

#[derive(Clone, PartialEq)]
pub enum Type {
Num,
Boolean,
Tuple(Vec<Type>),
}

pub enum TypeError {
ExpectedType(Expr, Type, Type),
ExpectedTupleType(Expr, Type),
ExpectedLoopOutputType(Expr, Type),
NoArg(Expr),
}

pub fn run_test(build: &str, check: &str) -> Result {
let program = format!(
"{}\n{build}\n{}\n{check}\n",
Expand Down

0 comments on commit 9e988d9

Please sign in to comment.