forked from AbleOS/ableos_userland
create axel and write a basic transpiler
This commit is contained in:
parent
b07943c72b
commit
18b18a67ad
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -59,6 +59,10 @@ dependencies = [
|
|||
"std",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "axel2wat"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.13.1"
|
||||
|
|
|
@ -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
7
programs/axel2wat/Cargo.lock
generated
Normal 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"
|
8
programs/axel2wat/Cargo.toml
Normal file
8
programs/axel2wat/Cargo.toml
Normal 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]
|
1
programs/axel2wat/assets/basic.axel
Normal file
1
programs/axel2wat/assets/basic.axel
Normal file
|
@ -0,0 +1 @@
|
|||
function "local" "abcd"(i32, i32, i32) -> i64;
|
2
programs/axel2wat/assets/test.wat
Normal file
2
programs/axel2wat/assets/test.wat
Normal file
|
@ -0,0 +1,2 @@
|
|||
(module (func $name (import "host" "name")(param i32 i32))
|
||||
)
|
BIN
programs/axel2wat/main.wasm
Normal file
BIN
programs/axel2wat/main.wasm
Normal file
Binary file not shown.
3
programs/axel2wat/main.wat
Normal file
3
programs/axel2wat/main.wat
Normal file
|
@ -0,0 +1,3 @@
|
|||
(module
|
||||
(func $name (import "host" "name") (param i32))
|
||||
)
|
83
programs/axel2wat/src/main.rs
Normal file
83
programs/axel2wat/src/main.rs
Normal 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;
|
Loading…
Reference in a new issue