Commit 6151fbfa authored by Jeff Smits's avatar Jeff Smits

Added lib.rs and test template

parent f35cb901
......@@ -7,7 +7,12 @@ use std::path::Path;
use std::ascii::AsciiExt;
fn main() {
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs");
gen_primitives();
gen_strc_tests();
}
fn gen_primitives() {
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("primitives.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
let libs =
......@@ -266,3 +271,10 @@ fn {}<'a>(offset: usize,
"#,
lib_name)
}
fn gen_strc_tests() {
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("strc_tests.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
}
......@@ -28,6 +28,7 @@ macro_rules! eprintln {
}}
}
#[derive(Debug)]
pub struct TracedError(pub Error, pub StackTracer);
pub struct Libraries<A> {
......
use std::path::{Path, PathBuf};
use std::fs::File;
use std::io;
use std::io::Read;
use aterm::parse::ATermRead;
use error::Result;
use factory::{ATermFactory, ATermRef};
use primitives;
use primitives::Primitives;
pub fn read_input<P: AsRef<Path>>(path: &P) -> io::Result<String> {
let mut ctree = String::new();
let path = path.as_ref();
if path == Path::new("-") {
io::stdin().read_to_string(&mut ctree)?;
} else {
File::open(path)?.read_to_string(&mut ctree)?;
}
Ok(ctree)
}
pub fn read_aterm<'a, P: AsRef<Path>>(f: &ATermFactory, path: &P) -> Result<ATermRef> {
let file_contents = read_input(path)?;
let (aterm, _) = f.read_ascii(&file_contents)?;
Ok(aterm)
}
pub fn read_lib<'a>(f: &ATermFactory,
str_path: &Path,
path: &str)
-> Result<(ATermRef, Option<&'static Primitives>)> {
if let Some(&(path, prim)) = primitives::LIBS.get(path) {
Ok((read_aterm(f, &Path::new(&str_path).join(path))?, prim))
} else {
Ok((read_aterm(f, &Path::new(path))?, None))
}
}
#[cfg_attr(rustfmt, rustfmt_skip)]
pub fn find_str_path(str_path: Option<String>) -> PathBuf {
use std::env;
use std::process::Command;
str_path
.or_else(||
env::var_os("STRDIR")
.map(|s|
s.into_string().expect("STRDIR found in environment, but was not valid \
unicode. Bailing out. ")))
.and_then(|s| if s.is_empty() { None } else { Some(s) })
.or_else(||
Command::new("strategoxt-path")
.output()
.ok()
.map(|o|
String::from_utf8(o.stdout)
.ok()
.expect("strategoxt-path successfully executed but the output was not valid \
unicode. Bailing out. ")))
.and_then(|s| {
let mut p = PathBuf::from(s);
// pop once to drop the file name, pop again to go to the parent dir
if !p.pop() || !p.pop() {
None
} else {
p.to_str().map(String::from)
}
})
.map(PathBuf::from)
.unwrap_or(PathBuf::from("."))
}
\ No newline at end of file
extern crate structopt;
#[macro_use]
extern crate structopt_derive;
extern crate aterm;
extern crate try_from;
extern crate phf;
extern crate ref_eq;
extern crate rand;
extern crate linked_hash_map;
pub mod io;
pub mod error;
pub mod factory;
pub mod interpreter;
mod ctree;
mod context;
mod preprocess;
mod primitives;
\ No newline at end of file
......@@ -10,14 +10,10 @@ extern crate linked_hash_map;
use structopt::StructOpt;
use std::path::{Path, PathBuf};
use std::fs::File;
use std::io;
use std::io::Read;
use std::path::PathBuf;
use std::process;
use std::result;
use aterm::parse::ATermRead;
use aterm::print::ATermWrite;
use error::{Result, Error};
......@@ -25,6 +21,7 @@ use factory::{ATermFactory, ATermRef};
use interpreter::{interpret_main, TracedError};
use primitives::Primitives;
mod io;
mod error;
mod factory;
mod interpreter;
......@@ -72,7 +69,7 @@ struct Opt {
#[cfg_attr(rustfmt, rustfmt_skip)]
fn main() {
let opt = Opt::from_args();
let str_path = find_str_path(opt.str_path);
let str_path = io::find_str_path(opt.str_path);
// TODO: support command line arguments in opt.args
match exec(opt.program, str_path, opt.libraries, opt.stack_space) {
Err(TracedError(Error::InterpreterExit(i),_)) => { process::exit(i) },
......@@ -95,9 +92,9 @@ fn exec(program_path: String, str_path: PathBuf, library_paths: Vec<String>, sta
let f = ATermFactory::new();
let libraries = library_paths
.into_iter()
.map(|lib_path| read_lib(&f, str_path.as_ref(), &lib_path))
.map(|lib_path| io::read_lib(&f, str_path.as_ref(), &lib_path))
.collect::<Result<_>>()?;
let program = read_aterm(&f, &program_path)?;
let program = io::read_aterm(&f, &program_path)?;
let term = interpret_main(&f, program, libraries)?;
let mut result = String::new();
term.to_ascii(&mut result)?;
......@@ -106,66 +103,3 @@ fn exec(program_path: String, str_path: PathBuf, library_paths: Vec<String>, sta
.join()
.expect("Something went wrong with the execution thread. ")
}
fn read_input<P: AsRef<Path>>(path: &P) -> io::Result<String> {
let mut ctree = String::new();
let path = path.as_ref();
if path == Path::new("-") {
io::stdin().read_to_string(&mut ctree)?;
} else {
File::open(path)?.read_to_string(&mut ctree)?;
}
Ok(ctree)
}
fn read_aterm<'a, P: AsRef<Path>>(f: &ATermFactory, path: &P) -> Result<ATermRef> {
let file_contents = read_input(path)?;
let (aterm, _) = f.read_ascii(&file_contents)?;
Ok(aterm)
}
fn read_lib<'a>(f: &ATermFactory,
str_path: &Path,
path: &str)
-> Result<(ATermRef, Option<&'static Primitives>)> {
if let Some(&(path, prim)) = primitives::LIBS.get(path) {
Ok((read_aterm(f, &Path::new(&str_path).join(path))?, prim))
} else {
Ok((read_aterm(f, &Path::new(path))?, None))
}
}
#[cfg_attr(rustfmt, rustfmt_skip)]
fn find_str_path(str_path: Option<String>) -> PathBuf {
use std::env;
use std::process::Command;
str_path
.or_else(||
env::var_os("STRDIR")
.map(|s|
s.into_string().expect("STRDIR found in environment, but was not valid \
unicode. Bailing out. ")))
.and_then(|s| if s.is_empty() { None } else { Some(s) })
.or_else(||
Command::new("strategoxt-path")
.output()
.ok()
.map(|o|
String::from_utf8(o.stdout)
.ok()
.expect("strategoxt-path successfully executed but the output was not valid \
unicode. Bailing out. ")))
.and_then(|s| {
let mut p = PathBuf::from(s);
// pop once to drop the file name, pop again to go to the parent dir
if !p.pop() || !p.pop() {
None
} else {
p.to_str().map(String::from)
}
})
.map(PathBuf::from)
.unwrap_or(PathBuf::from("."))
}
extern crate strs;
extern crate aterm;
use strs::io;
use strs::error::Result;
use strs::factory::ATermFactory;
use strs::interpreter::interpret_main;
use aterm::print::ATermWrite;
use std::iter;
// include!(concat!(env!("OUT_DIR"), "/strc_tests.rs"));
#[test]
fn test01() {
let str_path = io::find_str_path(None);
let f = ATermFactory::new();
let libraries = iter::once(String::from("libstratego-lib"))
.map(|lib_path| io::read_lib(&f, str_path.as_ref(), &lib_path))
.collect::<Result<_>>();
assert!(libraries.is_ok());
let libraries = libraries.unwrap();
let program = io::read_aterm(&f, &"tests/strc_tests/test01.ctree");
assert!(program.is_ok());
let program = program.unwrap();
let term = interpret_main(&f, program, libraries);
assert!(term.is_ok());
let term = term.unwrap();
let mut result = String::new();
assert!(term.to_ascii(&mut result).is_ok());
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment