Commit 052ea781 authored by Jeff Smits's avatar Jeff Smits

Added ctree_opt, which currently just output the same CTree

I check some small and large examples and the output seems to be the same. Not entirely sure if I got the reverse of the ConsMatch optimisation correct.
parent 04a9d9f8
......@@ -2,7 +2,7 @@
name = "strs"
version = "0.1.0"
dependencies = [
"aterm 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)",
"aterm 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)",
"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)",
......@@ -29,7 +29,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "aterm"
version = "0.17.0"
version = "0.17.1"
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)",
......@@ -307,7 +307,7 @@ 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.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6bd03cb1034b5bd0b08761cc12c21cd993e89a99a0b669ce9ab472bc59217cc3"
"checksum aterm 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2861a3e1dd1e752eb5f0563f4576dfb79cd046b8e121304b02e48c67375d3039"
"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"
......
......@@ -13,7 +13,7 @@ version = "0.1.0"
phf_codegen = "0.7.21"
[dependencies]
aterm = "0.17.0"
aterm = "0.17.1"
fnv = "1.0.5"
linked-hash-map = "0.4.2"
phf = "0.7.21"
......
extern crate structopt;
#[macro_use]
extern crate structopt_derive;
extern crate try_from;
extern crate strs;
extern crate aterm;
use structopt::StructOpt;
use std::path::Path;
use std::process;
use std::io as stdio;
use try_from::TryInto;
use strs::io;
use strs::factory::ATermFactory;
use strs::error::{Error, Result};
use strs::ctree;
use strs::preprocess::full_ctree as preprocess;
#[derive(Debug, StructOpt)]
#[structopt(name = "ctree-opt",
about = "A program that optimises CTree, the Stratego/XT intermediate representation. ")]
struct Opt {
#[structopt(help = "Program file (CTree)")]
program: String,
#[structopt(help = "Output file (CTree), stdout if not present", default_value = "-")]
output: String,
}
fn main() {
use std::error::Error;
let opt = Opt::from_args();
match exec(&opt.program, &opt.output) {
Err(e) => {
eprintln!("{}", e.description());
process::exit(1)
},
Ok(()) => {},
}
}
fn exec(program: &str, output: &str) -> Result<()> {
use aterm::print::ATermWrite;
use std::io::Write;
// Error out if the output file already exists
if output != "-" {
let output: &Path = output.as_ref();
if output.exists() {
return Err(Error::Io(stdio::Error::new(stdio::ErrorKind::AlreadyExists, "file already exists")));
}
}
let factory = ATermFactory::new();
let program = io::read_aterm(&factory, &program)?;
let program: ctree::Module = (&program).try_into()?;
let program: preprocess::Module = program.try_into()?;
let program = optimize(program);
if output == "-" {
println!("{}", program.to_ascii_string()?);
} else {
use std::fs::File;
let mut output_file = File::create(output)?;
write!(output_file, "{}", program.to_ascii_string()?)?;
}
Ok(())
}
fn optimize(program: preprocess::Module) -> preprocess::Module {
// TODO
program
}
use aterm::BrokenF32;
use aterm::print::ATermWrite;
use try_from::{TryFrom, TryInto};
......@@ -178,6 +179,15 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for ImportModName<'s> {
}
}
impl<'s> ATermWrite for ImportModName<'s> {
fn to_ascii<W: fmt::Write>(&self, writer: &mut W) -> fmt::Result {
match *self {
ImportModName::Import(name) => write!(writer, "Import({:?})", name),
ImportModName::ImportWildcard(name) => write!(writer, "ImportWildcard({:?})", name),
}
}
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum SDecl<'s> {
Sorts(Vec<Sort<'s>>),
......@@ -230,6 +240,23 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for SDecl<'s> {
}
}
impl<'a> ATermWrite for SDecl<'a> {
fn to_ascii<W: fmt::Write>(&self, writer: &mut W) -> fmt::Result {
match *self {
SDecl::Sorts(ref imports) => {
write!(writer, "Sorts([")?;
imports.to_ascii(writer)?;
write!(writer, "])")
}
SDecl::Constructors(ref defs) => {
write!(writer, "Constructors([")?;
defs.to_ascii(writer)?;
write!(writer, "])")
}
}
}
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum Sort<'s> {
SortVar(LCID<'s>),
......@@ -283,6 +310,20 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Sort<'s> {
}
}
impl<'s> ATermWrite for Sort<'s> {
fn to_ascii<W: fmt::Write>(&self, writer: &mut W) -> fmt::Result {
match *self {
Sort::SortVar(id) => write!(writer, "SortVar({:?})", id),
Sort::SortNoArgs(id) => write!(writer, "SortNoArgs({:?})", id),
Sort::Sort(id, ref args) => {
write!(writer, "Sort({:?},[", id)?;
args.to_ascii(writer)?;
write!(writer, "])")
}
}
}
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum OpDecl<'s> {
OpDecl(Id<'s>, Type<'s>),
......@@ -354,6 +395,43 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for OpDecl<'s> {
}
}
impl<'s> ATermWrite for OpDecl<'s> {
fn to_ascii<W: fmt::Write>(&self, writer: &mut W) -> fmt::Result {
match *self {
OpDecl::OpDecl(s, ref ty) => {
write!(writer, "OpDecl({:?},", s)?;
ty.to_ascii(writer)?;
write!(writer, ")")
}
OpDecl::OpDeclQ(s, ref ty) => {
write!(writer, "OpDeclQ({:?},", s)?;
ty.to_ascii(writer)?;
write!(writer, ")")
}
OpDecl::OpDeclInj(ref ty) => {
write!(writer, "OpDeclInj(")?;
ty.to_ascii(writer)?;
write!(writer, ")")
}
OpDecl::ExtOpDecl(s, ref ty) => {
write!(writer, "ExtOpDecl({:?},", s)?;
ty.to_ascii(writer)?;
write!(writer, ")")
}
OpDecl::ExtOpDeclQ(s, ref ty) => {
write!(writer, "ExtOpDeclQ({:?},", s)?;
ty.to_ascii(writer)?;
write!(writer, ")")
}
OpDecl::ExtOpDeclInj(ref ty) => {
write!(writer, "ExtOpDeclInj(")?;
ty.to_ascii(writer)?;
write!(writer, ")")
}
}
}
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct ConstType<'s>(pub Sort<'s>);
......@@ -379,6 +457,14 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for ConstType<'s> {
}
}
impl<'s> ATermWrite for ConstType<'s> {
fn to_ascii<W: fmt::Write>(&self, writer: &mut W) -> fmt::Result {
write!(writer, "ConstType(")?;
self.0.to_ascii(writer)?;
write!(writer, ")")
}
}
pub type ArgType<'s> = Type<'s>;
pub type RetType<'s> = ConstType<'s>;
......@@ -427,6 +513,25 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Type<'s> {
}
}
impl<'s> ATermWrite for Type<'s> {
fn to_ascii<W: fmt::Write>(&self, writer: &mut W) -> fmt::Result {
match *self {
Type::FunType(ref argtypes, ref rettype) => {
write!(writer, "FunType([")?;
argtypes.to_ascii(writer)?;
write!(writer, "],")?;
rettype.to_ascii(writer)?;
write!(writer, ")")
}
Type::ConstType(ref sort) => {
write!(writer, "ConstType(")?;
sort.to_ascii(writer)?;
write!(writer, ")")
}
}
}
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum Def<'s> {
SDefT(Id<'s>, Vec<VarDec<'s>>, Vec<VarDec<'s>>, Strategy<'s>),
......@@ -719,6 +824,16 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Anno {
}
}
impl ATermWrite for Anno {
fn to_ascii<W: fmt::Write>(&self, writer: &mut W) -> fmt::Result {
match *self {
Anno::Extend => write!(writer, "Extend"),
Anno::Override => write!(writer, "Override"),
Anno::Internal => write!(writer, "Internal"),
}
}
}
/// non-terminal `TypedId`
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct VarDec<'s>(pub Id<'s>, pub Type<'s>);
......@@ -741,7 +856,15 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for VarDec<'s> {
}
}
}
Err(Error::CTreeParse("TypedId"))
Err(Error::CTreeParse("VarDec"))
}
}
impl<'s> ATermWrite for VarDec<'s> {
fn to_ascii<W: fmt::Write>(&self, writer: &mut W) -> fmt::Result {
write!(writer, "VarDec({:?},", self.0)?;
self.1.to_ascii(writer)?;
write!(writer, ")")
}
}
......
......@@ -10,7 +10,7 @@ pub mod io;
pub mod error;
pub mod factory;
pub mod interpreter;
mod ctree;
pub mod ctree;
mod context;
mod preprocess;
pub mod preprocess;
mod primitives;
This diff is collapsed.
......@@ -72,4 +72,4 @@ macro_rules! primitive {
$body
}
};
}
\ No newline at end of file
}
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