create axel and write a basic transpiler

master
Able 2023-04-07 17:53:33 -05:00
parent 1d6d91964f
commit 77164b5728
9 changed files with 109 additions and 0 deletions

4
Cargo.lock generated
View File

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

View File

@ -33,6 +33,7 @@ members = [
"programs/ari_client",
"programs/ari_server",
"programs/axel2wat",
"programs/delete",
"programs/list",
"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;