Skip to content
Permalink
Browse files

Reorganize

  • Loading branch information...
kevinmehall committed Mar 25, 2018
1 parent 06e4fa6 commit 15c172dd58f8a4131fc641ed95814bc0a12dff83
@@ -35,7 +35,7 @@ fn main() {
}

if let Some(path) = test {
let success = signalspec::run_test(&*path);
let success = signalspec::run_tests_in_file(&*path);
process::exit( if success { 0 } else { 1 } );
} else {
let loader = signalspec::Ctxt::new(signalspec::Config {
@@ -1,16 +1,6 @@
use std::collections::HashSet;
use std::cmp::{min, max};
use num_complex::Complex;
use std::fmt;

#[derive(PartialEq, Clone)]
pub enum Value {
Number(f64),
Integer(i64),
Complex(Complex<f64>),
Symbol(String),
Vector(Vec<Value>),
}
use syntax::Value;

impl Value {
pub fn get_type(&self) -> Type {
@@ -25,37 +15,6 @@ impl Value {
}
}

impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Value::Number(n) => write!(f, "{}", n),
Value::Complex(c) => write!(f, "{}+{}i", c.re, c.im),
Value::Integer(n) => write!(f, "#{}", n),
Value::Symbol(ref s) => write!(f, "#{}", *s),
Value::Vector(ref n) => write!(f, "{:?}", n),
}
}
}

impl fmt::Debug for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}

impl ::std::hash::Hash for Value {
fn hash<H>(&self, state: &mut H) where H: ::std::hash::Hasher {
match *self {
Value::Number(_) | Value::Complex(_) => (),
Value::Integer(n) => n.hash(state),
Value::Symbol(ref s) => s.hash(state),
Value::Vector(ref n) => n.hash(state),
}
}
}

impl ::std::cmp::Eq for Value {}

/// A type represents a set of possible values
#[derive(Debug, PartialEq, Clone)]
pub enum Type {
@@ -1,9 +1,5 @@
use bit_set::BitSet;
use language::ValueID;
use super::eval::Expr;
use super::step::{ Step, StepInfo, Message };
use protocol::Fields;
use data::DataMode;
use core::{ ValueId, Expr, Step, StepInfo, Message, Fields, DataMode };

/// Summary of the usage of values within an block and its children
#[derive(Clone, Eq, PartialEq, Debug)]
@@ -48,7 +44,7 @@ impl ResolveInfo {
ri
}

pub fn mode_of(&self, id: ValueID) -> DataMode {
pub fn mode_of(&self, id: ValueId) -> DataMode {
DataMode { up: self.vars_up.contains(id), down: self.vars_down.contains(id)}
}

@@ -79,7 +75,7 @@ impl ResolveInfo {
ri
}

pub fn foreach(&self, inner_vars: &Vec<(ValueID, Expr)>) -> ResolveInfo {
pub fn foreach(&self, inner_vars: &Vec<(ValueId, Expr)>) -> ResolveInfo {
let mut ri = self.clone();
for &(id, ref e) in inner_vars {
let dir = ri.mode_of(id);
@@ -1,9 +1,7 @@
use super::Item;
use data::{ Value, Type };
use language::ValueID;
use std::fmt;
use std::ops::{Add, Sub, Mul, Div};
use num_complex::Complex;
use syntax::{ Value, BinOp };
use super::{ Item, Type, ValueId };

/// Element of Expr::Concat
#[derive(PartialEq, Debug, Clone)]
@@ -49,7 +47,7 @@ pub enum SignMode {
pub enum Expr {
Ignored,
Const(Value),
Variable(ValueID, Type),
Variable(ValueId, Type),

Range(f64, f64),
RangeInt(i64, i64),
@@ -67,7 +65,7 @@ pub enum Expr {

impl Expr {
/// Call `f` with the ID of each runtime variable referenced in the expression
pub fn each_var(&self, f: &mut FnMut(ValueID)) {
pub fn each_var(&self, f: &mut FnMut(ValueId)) {
use self::Expr::*;
match *self {
Variable(id, _) => f(id),
@@ -240,7 +238,7 @@ impl Expr {
}

/// Down-evaluate the expression with variables from the given value function.
pub fn eval_down(&self, state: &Fn(ValueID)->Value) -> Value {
pub fn eval_down(&self, state: &Fn(ValueId)->Value) -> Value {
match *self {
Expr::Ignored | Expr::Range(..) | Expr::RangeInt(..) | Expr::Union(..) => {
panic!("{:?} can't be down-evaluated", self)
@@ -304,7 +302,7 @@ impl Expr {

/// Up-evaluate a value. This accepts a value and may write variables
/// via the passed function. It returns whether the expression matched the value.
pub fn eval_up(&self, state: &mut FnMut(ValueID, Value) -> bool, v: Value) -> bool {
pub fn eval_up(&self, state: &mut FnMut(ValueId, Value) -> bool, v: Value) -> bool {
match *self {
Expr::Ignored => true,
Expr::Range(a, b) => match v {
@@ -461,67 +459,6 @@ impl fmt::Display for Expr {
}
}

/// Binary numeric operators
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum BinOp {
/// a + b
Add,
/// a - b
Sub,
/// b - a
SubSwap,
/// a * b
Mul,
/// a / b
Div,
/// b / a
DivSwap,
}

impl BinOp {
/// a `op` b
pub fn eval<A, B, C>(&self, a: A, b: B) -> C where
A: Add<B, Output=C>,
A: Sub<B, Output=C>,
B: Sub<A, Output=C>,
A: Mul<B, Output=C>,
A: Div<B, Output=C>,
B: Div<A, Output=C> {
match *self {
BinOp::Add => a + b,
BinOp::Sub => a - b,
BinOp::SubSwap => b - a,
BinOp::Mul => a * b,
BinOp::Div => a / b,
BinOp::DivSwap => b / a,
}
}

/// (a `op` b) == (b `op.swap()` a)
pub fn swap(&self) -> BinOp {
match *self {
BinOp::Add => BinOp::Add,
BinOp::Sub => BinOp::SubSwap,
BinOp::SubSwap => BinOp::Sub,
BinOp::Mul => BinOp::Mul,
BinOp::Div => BinOp::DivSwap,
BinOp::DivSwap => BinOp::Div,
}
}

/// ((a `op` b) `op.invert()` b) == a
pub fn invert(&self) -> BinOp {
match *self {
BinOp::Add => BinOp::Sub,
BinOp::Sub => BinOp::Add,
BinOp::SubSwap => BinOp::SubSwap,
BinOp::Mul => BinOp::Div,
BinOp::Div => BinOp::Mul,
BinOp::DivSwap => BinOp::DivSwap,
}
}
}

fn fn_int(arg: Item) -> Result<Item, &'static str> {
match arg {
Item::Value(v) => {
@@ -599,8 +536,8 @@ pub fn add_primitive_fns(loader: &super::Ctxt) {

#[test]
fn exprs() {
use super::Ctxt;
use super::grammar;
use syntax::parse_valexpr;
use core::{Ctxt, value};

let ctxt = Ctxt::new(Default::default());

@@ -609,8 +546,8 @@ fn exprs() {

let expr = |e: &str| {
let file = ctxt.codemap.borrow_mut().add_file("<expr>".into(), e.into());
let ast = grammar::valexpr(&file.source(), file.span).unwrap();
super::expr::value(&ctxt, &scope, &ast)
let ast = parse_valexpr(&file.source(), file.span).unwrap();
value(&ctxt, &scope, &ast)
};

let two = expr("2");
@@ -1,10 +1,5 @@
use super::ast;
use super::Ctxt;
use super::eval::{ Expr, ConcatElem };
use super::scope::{ Scope, Item };
use super::function::{FunctionDef, Func};
use data::Value;
use protocol::Shape;
use syntax::{ast, Value};
use super::{ Ctxt, Expr, ConcatElem, Scope, Item , FunctionDef, Func, Shape };

fn resolve(ctx: &Ctxt, scope: Option<&Scope>, var_handler: &mut FnMut(&str) -> Expr, e: &ast::Expr) -> Expr {
match *e {
@@ -61,7 +56,7 @@ fn resolve(ctx: &Ctxt, scope: Option<&Scope>, var_handler: &mut FnMut(&str) -> E
}

ast::Expr::Bin(box ref a, op, box ref b) => {
use super::eval::Expr::Const;
use self::Expr::Const;
let lhs = resolve(ctx, scope, var_handler, a);
let rhs = resolve(ctx, scope, var_handler, b);
match (lhs, rhs) {
@@ -1,4 +1,5 @@
use super::{ expr, ast, Item, Scope, Ctxt };
use syntax::ast;
use super::{ rexpr, lexpr, Item, Scope, Ctxt };

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct FunctionId(pub usize);
@@ -44,7 +45,7 @@ pub struct Func {
impl Func {
pub fn apply(&self, ctx: &Ctxt, arg: Item) -> Item {
let mut scope = self.scope.child();
expr::lexpr(ctx, &mut scope, &self.args, arg).expect("failed to match function argument");
expr::rexpr(ctx, &scope, &self.body)
lexpr(ctx, &mut scope, &self.args, arg).expect("failed to match function argument");
rexpr(ctx, &scope, &self.body)
}
}
@@ -1,4 +1,4 @@
use super::step::Message;
use super::Message;

#[derive(Clone, Debug)]
pub struct MatchSet {
@@ -0,0 +1,29 @@
mod scope;
mod expr;
mod expr_resolve;
mod step;
mod direction_infer;
mod module_loader;
mod protocol;
mod function;
mod matchset;
mod primitive;
mod process;
mod data;
mod shape;

pub use self::module_loader::{ Config, Ctxt, Module, Test };
pub use self::scope::{ Item, Scope };
pub use self::expr::{ Expr, ConcatElem, add_primitive_fns};
pub use self::expr_resolve::{ rexpr, lexpr, on_expr_message, value, pattern_match };
pub use self::function::{ PrimitiveFn, FunctionDef, Func, FunctionId };
pub use self::primitive::{ PrimitiveDef, PrimitiveDefFields, call_primitive };
pub use self::process::{ Process, ProcessInfo, ProcessChain, resolve_process };
pub use self::protocol::{ ProtocolScope, ProtocolId, DefImpl, resolve_protocol_invoke };
pub use self::step::{ Step, StepInfo, Message, make_literal_process, resolve_token, compile_block };
pub use self::data::{ Type, DataMode };
pub use self::shape::{ Shape, ShapeVariant, Fields, Field };
pub use self::direction_infer::ResolveInfo;
pub use self::matchset::MatchSet;

pub type ValueId = usize;
@@ -4,13 +4,11 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::path::PathBuf;
use std::fs;

use super::{ ast, grammar, Item, PrimitiveDef };
use super::scope::Scope;
use super::function::{ FunctionId, FunctionDef, PrimitiveFn };
use process::{ ProcessChain };
use language::protocol::ProtocolScope;
use protocol::{ ProtocolId, Shape, Fields };
use util::Index;
use syntax::{ ast, parse_module, parse_process_chain, ParseError};
use super::{ Item, PrimitiveDef, Scope, FunctionId, FunctionDef, PrimitiveFn, ProcessChain, ProtocolScope, ProtocolId, Shape, Fields };
use super::{ resolve_process };


#[derive(Clone, Default, Debug)]
pub struct Config {
@@ -76,7 +74,7 @@ impl Ctxt {

pub fn define_primitive(&self, header_src: &str, implementations: Vec<PrimitiveDef>) {
let file = self.codemap.borrow_mut().add_file("<primitive>".into(), header_src.into());
let header = grammar::primitive_header(&file.source(), file.span).expect("failed to parse primitive header");
let header = ::syntax::parse_primitive_header(&file.source(), file.span).expect("failed to parse primitive header");
self.protocol_scope.borrow_mut().add_primitive(self, &*self.prelude.borrow(), header, implementations);
}

@@ -96,14 +94,14 @@ impl Ctxt {
self.functions.get(id)
}

pub fn parse_process(&self, source: &str, shape_below: &Shape, fields_below: &Fields) -> Result<ProcessChain, grammar::ParseError> {
pub fn parse_process(&self, source: &str, shape_below: &Shape, fields_below: &Fields) -> Result<ProcessChain, ParseError> {
let file = self.codemap.borrow_mut().add_file("<process>".into(), source.into());
let ast = try!(grammar::process_chain(&file.source(), file.span));
Ok(super::program::resolve_process(self, &*self.prelude.borrow(), &*self.protocol_scope.borrow(), shape_below, fields_below, &ast))
let ast = try!(parse_process_chain(&file.source(), file.span));
Ok(resolve_process(self, &*self.prelude.borrow(), &*self.protocol_scope.borrow(), shape_below, fields_below, &ast))
}

pub fn parse_module(&self, file: &File) -> Result<Module, grammar::ParseError> {
let ast = grammar::module(&file.source(), file.span)?;
pub fn parse_module(&self, file: &File) -> Result<Module, ParseError> {
let ast = parse_module(&file.source(), file.span)?;

let mut scope = self.prelude.borrow().child();
let mut with_blocks = vec![];
@@ -156,14 +154,10 @@ impl Module {
}

pub struct Test<'m> {
scope: &'m Scope,
ast: &'m ast::Test,
pub scope: &'m Scope,
pub ast: &'m ast::Test,
}

impl<'m> Test<'m> {
pub fn run<'s>(&self, ctx: &'s Ctxt) -> super::program::TestResult {
super::program::run_test(ctx, &self.scope, &*ctx.protocol_scope.borrow(), &self.ast)
}

pub fn should_fail(&self) -> bool { self.ast.should_fail }
}
@@ -1,7 +1,5 @@
use super::{ Ctxt, Scope };
use data::DataMode;
use protocol::{ Fields, Shape };
use process::PrimitiveProcess;
use super::{ Ctxt, Scope, DataMode, Fields, Shape };
use runtime::PrimitiveProcess;

pub enum PrimitiveDefFields {
Explicit(Fields),
Oops, something went wrong.

0 comments on commit 15c172d

Please sign in to comment.
You can’t perform that action at this time.