Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example with a naive Clojure->Rust transpiler #91

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/examples/codegen_*
/target
**/*.rs.bk

Expand Down Expand Up @@ -63,4 +64,4 @@ Session.vim
tags

.idea
Cargo.lock
Cargo.lock
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ itertools= "0.9"
url = "2.1.1"
regex = "1.3.7"
if_chain = "1.0"
reqwest = { version = "0.10.4", features = ["blocking"] }
reqwest = { version = "0.10.4", features = ["blocking"] }
2 changes: 1 addition & 1 deletion examples/guess_number.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

(if (= answer (str number))
(println "correct!")
(println "wrong, the correct was " number)))
(println "wrong, the correct was " number)))
17 changes: 17 additions & 0 deletions examples/transpiler/codegen.rs.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[macro_use]
extern crate rust_clojure;

use rust_clojure::environment::Environment;
use rust_clojure::value::{Evaluable, ToValue, Value};
use std::rc::Rc;

use rust_clojure::persistent_vector::{PersistentVector};

fn main() {
let v = %%;

let environment = Rc::new(Environment::clojure_core_environment());
for val in v.into_iter() {
val.eval(Rc::clone(&environment));
}
}
169 changes: 169 additions & 0 deletions examples/transpiler/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#[macro_use]
extern crate rust_clojure;

use std::fs::File;
use std::path::PathBuf;
use std::fs;
use std::io::{self, Write};
use std::io::BufRead;
use std::io::BufReader;

use rust_clojure::environment;
use rust_clojure::environment::Environment;
use rust_clojure::reader;
use rust_clojure::value::{Evaluable, ToValue, Value};
use std::rc::Rc;
use std::process::{Command, Stdio};

use rust_clojure::symbol::*;
use rust_clojure::persistent_list::ToPersistentListIter;
// use rust_clojure::persistent_list_map::*;
use rust_clojure::persistent_vector::{PersistentVector, ToPersistentVectorIter};


fn codegen_value (value: Rc<Value>) -> String {
match value.as_ref() {
Value::Symbol(s) => format!("sym!(\"{}\").to_rc_value()", s.name),
Value::PersistentList(l) => {
let vals: String = Rc::new(l.clone()).iter()
.map(codegen_value)
.collect::<Vec<_>>()
.join(" \n");

format!("list_val!({})", vals)
},
Value::PersistentVector(pv) => {
let vals: String = Rc::new(pv.clone()).iter()
.map(codegen_value)
.collect::<Vec<_>>()
.join(",");
format!("PersistentVector{{ vals: vec![{}] }}", vals)
},
Value::String(s) => format!("String::from(\"{}\").to_rc_value()", s),
Value::Boolean(b) => format!("{}.to_rc_value()", b),
Value::I32(i) => format!("{}i32.to_rc_value()", i),
Value::F64(f)=> format!("{}f64.to_rc_value()", f),
Value::Nil => {
"Value::Nil".into()
}
_ => "".into()
}
}

fn apply_rustfmt(source: String) -> io::Result<String>
{
let rustfmt = rustfmt_path();
let mut cmd = Command::new(&rustfmt);
cmd.stdin(Stdio::piped()).stdout(Stdio::piped());
let mut child = cmd.spawn().expect("Error running rustfmt");
let mut child_stdin = child.stdin.take().unwrap();
let mut child_stdout = child.stdout.take().unwrap();

// Write to stdin in a new thread, so that we can read from stdout on this
// thread. This keeps the child from blocking on writing to its stdout which
// might block us from writing to its stdin.
let stdin_handle = ::std::thread::spawn(move || {
let _ = child_stdin.write_all(source.as_bytes());
source
});

let mut output = vec![];
io::copy(&mut child_stdout, &mut output)?;

let status = child.wait()?;
let source = stdin_handle.join().expect(
"The thread writing to rustfmt's stdin doesn't do \
anything that could panic",
);
match String::from_utf8(output) {
Ok(bindings) => match status.code() {
Some(0) => Ok(bindings),
Some(2) => Err(io::Error::new(
io::ErrorKind::Other,
"Rustfmt parsing errors.".to_string(),
)),
Some(3) => {
println!("Rustfmt could not format some lines.");
Ok(bindings)
}
_ => Err(io::Error::new(
io::ErrorKind::Other,
"Internal rustfmt error".to_string(),
)),
},
_ => Ok(source)
}
}
fn read_source<B: BufRead>(mut reader: B) -> Vec<Value>
{
let mut last_val = reader::read(&mut reader);

let mut values = Vec::new();
values.push(last_val.clone());
let value = loop {
// @TODO this is hardcoded until we refactor Conditions to have keys, so that
// we can properly identify them
// @FIXME
if let Value::Condition(cond) = &last_val {
if cond != "Tried to read empty stream; unexpected EOF" {
println!("Error reading file: {}", cond);
}

break last_val;
}

last_val = reader::read(&mut reader);
if let Value::Condition(cond) = last_val.clone() {
}else{
values.push(last_val.clone());
}
};
values
}

/// Gets the rustfmt path to rustfmt the generated bindings.
fn rustfmt_path() -> std::path::PathBuf {
if let Ok(rustfmt) = std::env::var("RUSTFMT") {
rustfmt.into()
}else{
"rustfmt".into()
}
}

fn main() -> io::Result<()>
{
let args = std::env::args().collect::<Vec<_>>();
if args.len() < 3 {
eprintln!("A Clojure-to-Rust transpiler\n\n\
Usage: {} <clojure-file> <output-path> [output-filename]\n\n\
[output-filename] defaults to main.rs\n", args[0]);
std::process::exit(-1);
}
let output_folder: PathBuf = (&args[2]).into();
let source_file: String = (&args[1]).into();

let output_filename = args.get(3).cloned().unwrap_or("main.rs".into());

let output_file: PathBuf = output_folder.join(output_filename);

let codegen_template = include_str!("codegen.rs.tmpl");

let core = File::open(source_file).unwrap();
let reader = BufReader::new(core);
let values = read_source(reader);
let v_str = values.clone().into_iter()
.map(|v| codegen_value(Rc::new(v)))
.collect::<Vec<_>>()
.join(",\n");


let value_vec_code= format!("vec![{}];", v_str);
let codegen_output = codegen_template.replace("%%", &value_vec_code);
let codegen_output = apply_rustfmt(codegen_output)?;

fs::create_dir_all(&output_folder)?;
let mut fp = File::create(&output_file)?;
write!(fp, "{}", codegen_output)?;
println!("Code written to {:?}", output_file);
Ok(())
}
2 changes: 1 addition & 1 deletion examples/what_is_your_name.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(print "What is your name? " )
(flush)
(let [answer (read-line)]
(println "Hello, " answer "!"))
(println "Hello, " answer "!"))
2 changes: 1 addition & 1 deletion src/clojure/string.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

(def clojure.string/split-lines
(fn [s]
(clojure.string/split s #"\r?\n")))
(clojure.string/split s #"\r?\n")))
34 changes: 34 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#[macro_use]
extern crate nom;
extern crate itertools;

#[macro_use]
pub mod persistent_list_map;
#[macro_use]
pub mod persistent_list;
#[macro_use]
pub mod protocol;
#[macro_use]
pub mod symbol;
#[macro_use]
pub mod var;
pub mod clojure_std;
pub mod clojure_string;
pub mod environment;
pub mod error_message;
pub mod ifn;
pub mod iterable;
pub mod keyword;
pub mod lambda;
pub mod maps;
pub mod namespace;
pub mod persistent_vector;
pub mod reader;
pub mod repl;
pub mod rust_core;
pub mod type_tag;
pub mod user_action;
pub mod util;
pub mod value;
pub mod protocols;
pub mod traits;
4 changes: 2 additions & 2 deletions src/persistent_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ macro_rules! list {
$(
temp_list_as_vec.push($val.to_rc_value());
)*
temp_list_as_vec.into_iter().collect::<crate::persistent_list::PersistentList>()
temp_list_as_vec.into_iter().collect::<$crate::persistent_list::PersistentList>()
}
};
}
Expand All @@ -40,7 +40,7 @@ macro_rules! list_val {
$(
temp_list_as_vec.push($val.to_rc_value());
)*
temp_list_as_vec.into_iter().collect::<crate::persistent_list::PersistentList>().to_value()
temp_list_as_vec.into_iter().collect::<$crate::persistent_list::PersistentList>().to_value()
}
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ pub struct Symbol {
pub ns: String,
pub meta: PersistentListMap,
}
#[macro_export]
macro_rules! sym {
($x:expr) => {
Symbol::intern($x)
$crate::symbol::Symbol::intern($x)
}
}
impl Hash for Symbol {
Expand Down