From 18b18a67ad77680454079f6d0432c4799bb22dce Mon Sep 17 00:00:00 2001 From: Able Date: Fri, 7 Apr 2023 17:53:33 -0500 Subject: [PATCH] create axel and write a basic transpiler --- Cargo.lock | 4 ++ Cargo.toml | 1 + programs/axel2wat/Cargo.lock | 7 +++ programs/axel2wat/Cargo.toml | 8 +++ programs/axel2wat/assets/basic.axel | 1 + programs/axel2wat/assets/test.wat | 2 + programs/axel2wat/main.wasm | Bin 0 -> 30 bytes programs/axel2wat/main.wat | 3 + programs/axel2wat/src/main.rs | 83 ++++++++++++++++++++++++++++ 9 files changed, 109 insertions(+) create mode 100644 programs/axel2wat/Cargo.lock create mode 100644 programs/axel2wat/Cargo.toml create mode 100644 programs/axel2wat/assets/basic.axel create mode 100644 programs/axel2wat/assets/test.wat create mode 100644 programs/axel2wat/main.wasm create mode 100644 programs/axel2wat/main.wat create mode 100644 programs/axel2wat/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 09ef62d..b5037de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,10 @@ dependencies = [ "std", ] +[[package]] +name = "axel2wat" +version = "0.1.0" + [[package]] name = "base64" version = "0.13.1" diff --git a/Cargo.toml b/Cargo.toml index 621912c..332cccd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ members = [ "programs/ari_client", "programs/ari_server", + "programs/axel2wat", "programs/delete", "programs/list", "programs/shell", diff --git a/programs/axel2wat/Cargo.lock b/programs/axel2wat/Cargo.lock new file mode 100644 index 0000000..c8a3e2f --- /dev/null +++ b/programs/axel2wat/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "axel" +version = "0.1.0" diff --git a/programs/axel2wat/Cargo.toml b/programs/axel2wat/Cargo.toml new file mode 100644 index 0000000..cccabab --- /dev/null +++ b/programs/axel2wat/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "axel2wat" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/programs/axel2wat/assets/basic.axel b/programs/axel2wat/assets/basic.axel new file mode 100644 index 0000000..78a2964 --- /dev/null +++ b/programs/axel2wat/assets/basic.axel @@ -0,0 +1 @@ +function "local" "abcd"(i32, i32, i32) -> i64; diff --git a/programs/axel2wat/assets/test.wat b/programs/axel2wat/assets/test.wat new file mode 100644 index 0000000..c6db36b --- /dev/null +++ b/programs/axel2wat/assets/test.wat @@ -0,0 +1,2 @@ +(module (func $name (import "host" "name")(param i32 i32)) +) \ No newline at end of file diff --git a/programs/axel2wat/main.wasm b/programs/axel2wat/main.wasm new file mode 100644 index 0000000000000000000000000000000000000000..d22a39b31fb284b6995f2b6c91591ecd9147195c GIT binary patch literal 30 lcmZQbEY4+QU|?WmWlUhKXJF!GWXZ@cE@8<_%uQur002yW1)%@{ literal 0 HcmV?d00001 diff --git a/programs/axel2wat/main.wat b/programs/axel2wat/main.wat new file mode 100644 index 0000000..5b78568 --- /dev/null +++ b/programs/axel2wat/main.wat @@ -0,0 +1,3 @@ +(module + (func $name (import "host" "name") (param i32)) +) \ No newline at end of file diff --git a/programs/axel2wat/src/main.rs b/programs/axel2wat/src/main.rs new file mode 100644 index 0000000..9260677 --- /dev/null +++ b/programs/axel2wat/src/main.rs @@ -0,0 +1,83 @@ +// function "local" "abcd"(i32, i32, i32) -> i64; + +use core::fmt; +use std::process::Command; +use std::{fmt::write, fs::File}; +#[derive(Debug)] +pub enum ATypes { + Num32, + Num64, +} +impl fmt::Display for ATypes { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ATypes::Num32 => write!(f, "i32"), + ATypes::Num64 => write!(f, "i64"), + } + } +} +pub struct Function { + location: String, + name: String, + params: Vec, + results: Vec, +} +impl fmt::Display for Function { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "\t(func ${} (", self.name)?; + if self.location == "local" { + write!(f, "export \"{}\") ", self.name)?; + } else { + write!(f, "import \"{}\" \"{}\") ", self.location, self.name)?; + } + if self.params.len() > 0 { + write!(f, "(param")?; + for param in &self.params { + write!(f, " {}", param)?; + } + write!(f, ")")?; + } + + write!(f, ")")?; + Ok(()) + } +} + +impl Function { + pub fn new(location: String, name: String, params: Vec, results: Vec) -> Self { + Self { + location, + name, + params, + results, + } + } +} + +fn main() -> std::io::Result<()> { + use crate::ATypes::*; + + let fil = include_str!("../assets/basic.axel"); + // println!("{}", fil); + + let fun = Function::new( + "host".to_string(), + "name".to_string(), + vec![Num32], + vec![Num32], + ); + + let axel_out = format!("(module\n{}\n)", fun); + + let path = "main.wat"; + let mut output = File::create(path).unwrap(); + write!(output, "{}", axel_out)?; + + let output = Command::new("wat2wasm") + .arg("main.wat") + .output() + .expect("Failed to execute command"); + + Ok(()) +} +use std::io::Write;