ableos_userland/programs/axel2wat/src/main.rs

93 lines
2.1 KiB
Rust

// 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::*;
if false {
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;
pub enum Tokens {
// set memory [0-9]+ "[a-zA-Z]+"
SetMemory(u64, String),
}