create axel and write a basic transpiler

This commit is contained in:
Able 2023-04-07 17:53:33 -05:00
parent b07943c72b
commit 18b18a67ad
9 changed files with 109 additions and 0 deletions

4
Cargo.lock generated
View file

@ -59,6 +59,10 @@ dependencies = [
"std", "std",
] ]
[[package]]
name = "axel2wat"
version = "0.1.0"
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.13.1" version = "0.13.1"

View file

@ -33,6 +33,7 @@ members = [
"programs/ari_client", "programs/ari_client",
"programs/ari_server", "programs/ari_server",
"programs/axel2wat",
"programs/delete", "programs/delete",
"programs/list", "programs/list",
"programs/shell", "programs/shell",

7
programs/axel2wat/Cargo.lock generated Normal file
View file

@ -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"

View file

@ -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]

View file

@ -0,0 +1 @@
function "local" "abcd"(i32, i32, i32) -> i64;

View file

@ -0,0 +1,2 @@
(module (func $name (import "host" "name")(param i32 i32))
)

BIN
programs/axel2wat/main.wasm Normal file

Binary file not shown.

View file

@ -0,0 +1,3 @@
(module
(func $name (import "host" "name") (param i32))
)

View file

@ -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<ATypes>,
results: Vec<ATypes>,
}
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<ATypes>, results: Vec<ATypes>) -> 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;