Commit 7581e405 authored by Jeff Smits's avatar Jeff Smits

WIP: new aterm version

parent 90076d94
......@@ -2,7 +2,7 @@
name = "strs"
version = "0.1.0"
dependencies = [
"aterm 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
"aterm 0.13.0",
"fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -30,10 +30,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "aterm"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"typed-arena 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -250,6 +250,11 @@ name = "try_from"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "typed-arena"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-segmentation"
version = "1.1.0"
......@@ -301,7 +306,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"
"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
"checksum aterm 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a239eea0367b4e6aecc626d45cfc9a893e24398c9837124a3ea2a1606d0ac033"
"checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159"
"checksum bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1370e9fc2a6ae53aea8b7a5110edbd08836ed87c88736dfabccade1c2b44bff4"
"checksum clap 2.23.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f57e9b63057a545ad2ecd773ea61e49422ed1b1d63d74d5da5ecaee55b3396cd"
......@@ -330,6 +334,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a"
"checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7"
"checksum try_from 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "48eb1fb21b59755547de28b0d7c0d67ab0b3f504efae762e2478663665965f53"
"checksum typed-arena 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5934776c3ac1bea4a9d56620d6bf2d483b20d394e49581db40f187e1118ff667"
"checksum unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18127285758f0e2c6cf325bb3f3d138a12fee27de4f23e146cd6a179f26c2cf3"
"checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f"
"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
......
......@@ -13,7 +13,6 @@ version = "0.1.0"
phf_codegen = "0.7.21"
[dependencies]
aterm = "0.13.0"
fnv = "1.0.5"
linked-hash-map = "0.4.2"
phf = "0.7.21"
......@@ -23,6 +22,9 @@ structopt = "0.0.3"
structopt-derive = "0.0.3"
try_from = "0.2.1"
[dependencies.aterm]
path = "../aterm"
[profile]
[profile.release]
......
......@@ -202,9 +202,9 @@ fn write_primitive_libs(
pub fn eval_prim_ref<'d, 'f : 'd>(prim_ref: (Libs, usize),
context: &MutContext<'d, 'f>,
sargs: Vec<StrategyDef<'d>>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef>
targs: Vec<ATermRef<'f>>,
current: ATermRef<'f>)
-> Result<ATermRef<'f>>
{{
match prim_ref.0 {{\
"
......@@ -269,9 +269,9 @@ fn write_primitives(
fn {}<'d, 'f : 'd>(offset: usize,
context: &MutContext<'d, 'f>,
sargs: Vec<StrategyDef<'d>>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef>
targs: Vec<ATermRef<'f>>,
current: ATermRef<'f>)
-> Result<ATermRef<'f>>
{{
match offset {{
\
......
......@@ -15,16 +15,16 @@ use std::cell::RefCell;
/// thing, there is observable effect, so the name of the context includes `Mut`.
pub struct MutContext<'d, 'f : 'd> {
pub stack_tracer: RefCell<StackTracer>,
pub scopes: RefCell<Vec<Scope<'d, ATermRef>>>,
pub factory: &'f ATermFactory,
pub scopes: RefCell<Vec<Scope<'d, ATermRef<'f>>>>,
pub factory: &'f ATermFactory<'f>,
pub primitives: Vec<&'static Primitives>,
pub ssl_state: RefCell<State>,
pub ssl_state: RefCell<State<'f>>,
}
impl<'d, 'f : 'd> MutContext<'d, 'f> {
pub fn new(
factory: &'f ATermFactory,
scopes: Vec<Scope<'d, ATermRef>>,
factory: &'f ATermFactory<'f>,
scopes: Vec<Scope<'d, ATermRef<'f>>>,
primitives: Vec<&'static Primitives>,
) -> MutContext<'d, 'f> {
MutContext {
......@@ -40,9 +40,9 @@ impl<'d, 'f : 'd> MutContext<'d, 'f> {
&self,
prim_name: &str,
sargs: Vec<StrategyDef<'d>>,
targs: Vec<ATermRef>,
current: ATermRef,
) -> Result<ATermRef> {
targs: Vec<ATermRef<'f>>,
current: ATermRef<'f>,
) -> Result<ATermRef<'f>> {
let prim_ref = self.primitives
.iter()
.flat_map(|prims| prims.get(prim_name))
......@@ -71,17 +71,17 @@ impl<'d, 'f : 'd> MutContext<'d, 'f> {
.ok_or_else(|| Error::UndefinedStrategy(strat_name.to_owned()))
}
pub fn get_term(&self, term_name: &str) -> Result<ATermRef> {
pub fn get_term(&self, term_name: &str) -> Result<ATermRef<'f>> {
Scopes::get_term_option(&self.scopes.borrow(), term_name).and_then(|(_, o)| {
o.ok_or(Error::StrategyFailed)
})
}
pub fn match_term(&self, term_name: &'d str, current: &ATermRef) -> Result<()> {
pub fn match_term(&self, term_name: &'d str, current: &ATermRef<'f>) -> Result<()> {
Scopes::match_term(&mut self.scopes.borrow_mut(), term_name, current)
}
pub fn push_scope(&self, scope: Scope<'d, ATermRef>) {
pub fn push_scope(&self, scope: Scope<'d, ATermRef<'f>>) {
self.scopes.borrow_mut().push(scope);
}
......@@ -95,7 +95,7 @@ impl<'d, 'f : 'd> MutContext<'d, 'f> {
self.push_scope(overlay);
}
fn _apply_overlay(&self, scope: Scope<'d, ATermRef>) {
fn _apply_overlay(&self, scope: Scope<'d, ATermRef<'f>>) {
debug_assert!(
scope.strategy.is_empty(),
"_apply_overlay: Interpreter bug, overlay is leaking a strategy definition"
......@@ -263,10 +263,10 @@ impl<'a, ATerm> Scope<'a, ATerm> {
pub mod Scopes {
use super::*;
pub fn get_term_option(
scopes: &Vec<Scope<ATermRef>>,
pub fn get_term_option<'f>(
scopes: &Vec<Scope<ATermRef<'f>>>,
term_name: &str,
) -> Result<(usize, Option<ATermRef>)> {
) -> Result<(usize, Option<ATermRef<'f>>)> {
let mut offset = None;
for (n,scope) in scopes.iter().enumerate().rev() {
if offset == None && scope.is_overlay {
......@@ -280,10 +280,10 @@ pub mod Scopes {
Err(Error::UndefinedVariable(term_name.to_owned()))
}
pub fn match_term<'a>(
scopes: &mut Vec<Scope<'a, ATermRef>>,
pub fn match_term<'a, 'f>(
scopes: &mut Vec<Scope<'a, ATermRef<'f>>>,
term_name: &'a str,
current: &ATermRef,
current: &ATermRef<'f>,
) -> Result<()> {
let term = get_term_option(scopes, term_name)?;
match term {
......@@ -307,10 +307,5 @@ pub mod Scopes {
}
}
}
unreachable!(format!(
"match_term: First we could find {} unbound, then we couldn't \
anymore?!",
term_name
))
}
}
use aterm as a;
use aterm::ATerm;
use aterm::BrokenF32;
use try_from::{TryFrom, TryInto};
......@@ -8,6 +7,7 @@ use std::borrow::Borrow;
use std::fmt;
use error::Error;
use factory::ATermRef;
/// ```sdf3
/// ModName = {ModNamePart "/"}+
......@@ -49,15 +49,13 @@ impl fmt::Display for Module {
}
}
impl<'a, A> TryFrom<&'a A> for Module
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Module
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
match c.borrow() {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
match c {
"Module" => {
if r.len() == 2 {
let str = match_string(&r[0])?;
......@@ -114,15 +112,13 @@ impl fmt::Display for Decl {
}
}
impl<'a, A> TryFrom<&'a A> for Decl
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Decl
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
match c.borrow() {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
match c {
"Imports" => {
if r.len() == 1 {
return match_list(&r[0]).map(Decl::Imports);
......@@ -161,15 +157,13 @@ impl fmt::Display for ImportModName {
}
}
impl<'a, A> TryFrom<&'a A> for ImportModName
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for ImportModName
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
match c.borrow() {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
match c {
"Import" => {
if r.len() == 1 {
return match_string(&r[0]).map(ImportModName::Import);
......@@ -216,15 +210,13 @@ impl fmt::Display for SDecl {
}
}
impl<'a, A> TryFrom<&'a A> for SDecl
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for SDecl
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
match c.borrow() {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
match c {
"Sorts" => {
if r.len() == 1 {
return match_list(&r[0]).map(SDecl::Sorts);
......@@ -270,15 +262,13 @@ impl fmt::Display for Sort {
}
}
impl<'a, A> TryFrom<&'a A> for Sort
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Sort
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
match c.borrow() {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
match c {
"SortVar" => {
if r.len() == 1 {
return match_string(&r[0]).map(Sort::SortVar);
......@@ -334,15 +324,13 @@ impl fmt::Display for OpDecl {
}
}
impl<'a, A> TryFrom<&'a A> for OpDecl
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for OpDecl
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
match c.borrow() {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
match c {
"OpDecl" => {
if r.len() == 2 {
let str = match_string(&r[0])?;
......@@ -394,14 +382,12 @@ impl fmt::Display for ConstType {
}
}
impl<'a, A> TryFrom<&'a A> for ConstType
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for ConstType
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
if let "ConstType" = c.borrow() {
if r.len() == 1 {
return match_(&r[0]).map(ConstType);
......@@ -438,15 +424,13 @@ impl fmt::Display for Type {
}
}
impl<'a, A> TryFrom<&'a A> for Type
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Type
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
match c.borrow() {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
match c {
"ConstType" => {
if r.len() == 1 {
return match_(&r[0]).map(Type::ConstType);
......@@ -524,15 +508,13 @@ impl fmt::Display for Def {
}
}
impl<'a, A> TryFrom<&'a A> for Def
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Def
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
match c.borrow() {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
match c {
"SDefT" => {
if r.len() == 4 {
let str = match_string(&r[0])?;
......@@ -587,14 +569,12 @@ impl fmt::Display for SVar {
}
}
impl<'a, A> TryFrom<&'a A> for SVar
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for SVar
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
if let "SVar" = c.borrow() {
let r = r;
if r.len() == 1 {
......@@ -658,15 +638,13 @@ impl fmt::Display for StrategyDef {
}
}
impl<'a, A> TryFrom<&'a A> for StrategyDef
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for StrategyDef
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
match c.borrow() {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
match c {
"SDefT" => {
if r.len() == 4 {
let str = match_string(&r[0])?;
......@@ -738,15 +716,13 @@ impl fmt::Display for Anno {
}
}
impl<'a, A> TryFrom<&'a A> for Anno
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Anno
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
match c.borrow() {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
match c {
"Extend" => {
if r.is_empty() {
return Ok(Anno::Extend);
......@@ -779,14 +755,12 @@ impl fmt::Display for VarDec {
}
}
impl<'a, A> TryFrom<&'a A> for VarDec
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for VarDec
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
if let "VarDec" = c.borrow() {
if r.len() == 2 {
let str = match_string(&r[0])?;
......@@ -897,15 +871,13 @@ impl fmt::Display for Strategy {
}
}
impl<'a, A> TryFrom<&'a A> for Strategy
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Strategy
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
match c.borrow() {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
match c {
"Let" => {
if r.len() == 2 {
let defs = match_list(&r[0])?;
......@@ -1034,14 +1006,12 @@ impl fmt::Display for Var {
}
}
impl<'a, A> TryFrom<&'a A> for Var
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Var
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
if let "Var" = c.borrow() {
let r = r;
if r.len() == 1 {
......@@ -1073,15 +1043,13 @@ impl fmt::Display for Term {
}
}
impl<'a, A> TryFrom<&'a A> for Term
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Term
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
match c.borrow() {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
match c {
"Var" => {
let r = r;
if r.len() == 1 {
......@@ -1148,15 +1116,13 @@ impl fmt::Display for PreTerm {
}
}
impl<'a, A> TryFrom<&'a A> for PreTerm
where
A: ATerm,
impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for PreTerm
{
type Err = Error;
fn try_from(value: &'a A) -> Result<Self, Self::Err> {
if let a::Term::Application(ref c, ref r) = value.as_inner().term {
match c.borrow() {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let a::rc::Term::Application(c, ref r) = value.term {
match c {
"Var" => {
let r = r;
if r.len() == 1 {
......@@ -1220,12 +1186,8 @@ where
}
}
fn match_string<A, R>(r: &R) -> Result<String, Error>
where
A: ATerm<Rec = R>,
R: Borrow<A>,
{
if let a::Term::Application(ref str, ref r) = r.borrow().as_inner().term {
fn match_string<'s>(r: &ATermRef<'s>) -> Result<String, Error> {
if let a::rc::Term::Application(ref str, ref r) = r.term {
if r.is_empty() {
return Ok(string_unescape(str));
}
......@@ -1233,12 +1195,8 @@ where
Err(Error::CTreeParse("string"))
}
fn match_strings<A, R>(r: &R) -> Result<Vec<String>, Error>
where
A: ATerm<Rec = R>,
R: Borrow<A>,
{
if let a::Term::List(ref r) = r.borrow().as_inner().term {
fn match_strings<'s>(r: &ATermRef<'s>) -> Result<Vec<String>, Error> {
if let a::rc::Term::List(ref r) = r.term {
return r.into_iter()
.map(match_string)
.collect::<Result<Vec<String>, Error>>();
......@@ -1246,27 +1204,23 @@ where
Err(Error::CTreeParse("list"))
}
fn match_list<'a, A, R, T>(r: &'a R) -> Result<Vec<T>, Error>
fn match_list<'a, 's, T>(r: &'a ATermRef<'s>) -> Result<Vec<T>, Error>
where
T: TryFrom<&'a A, Err = Error>,
A: 'a + ATerm<Rec = R>,
R: Borrow<A>,
T: TryFrom<&'a ATermRef<'s>, Err = Error>,
{
if let a::Term::List(ref r) = r.borrow().as_inner().term {
if let a::rc::Term::List(ref r) = r.term {
return r.iter()
.map(|a| a.borrow().try_into())
.map(|a| a.try_into())
.collect::<Result<Vec<T>, Error>>();
}
Err(Error::CTreeParse("list"))
}
fn match_<'a, A, R, T>(r: &'a R) -> Result<T, Error>
pub fn match_<'a, 's, T>(r: &'a ATermRef<'s>) -> Result<T, Error>
where
T: TryFrom<&'a A, Err = Error>,
A: 'a + ATerm<Rec = R>,
R: Borrow<A>,