-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started trn module working on wat and wasm
- Loading branch information
Showing
10 changed files
with
163 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
(module | ||
(func $fib (export "fib") (param $n i32) (result i32) | ||
(block $1 | ||
) | ||
(block $begin | ||
) | ||
(block $end | ||
) | ||
(block $4 | ||
br $begin | ||
) | ||
i32.const 0 | ||
) | ||
(func $main (export "main") (result i32) | ||
br $1 | ||
(block $1 | ||
) | ||
i32.const 0 | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod transpiler; | ||
pub mod wat_transpiler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::{fs::File, io::Write}; | ||
use crate::{fe::ast::*, cli::*}; | ||
use crate::trn::wat_transpiler::WatTranspiler; | ||
|
||
pub trait Transpiler { | ||
fn transpile(&self, nodes: &Vec<AstNode>) -> Vec<String>; | ||
} | ||
|
||
fn remove_extension(filepath: String, ext: &str) -> String { | ||
filepath.as_str().trim_end_matches(ext).to_string() | ||
} | ||
|
||
pub fn replace_extension(filepath: String, old_ext: &str, new_ext: &str) -> String { | ||
format!("{}{}", remove_extension(filepath, old_ext), new_ext) | ||
} | ||
|
||
pub fn transpilation_mode<T: Transpiler + 'static>(transpiler: T, nodes: &Vec<AstNode>, | ||
output_filepath: String) { | ||
let mut file = File::create(output_filepath).expect("could not create file"); | ||
for line in transpiler.transpile(nodes).iter() { | ||
file.write_all(line.as_bytes()).expect("could not write line"); | ||
file.write_all(b"\n").expect("could not write new line"); | ||
} | ||
} | ||
|
||
pub fn transpile(options: &CliOptions, nodes: &Vec<AstNode>, filepath: String) { | ||
if options.wat || options.wasm { | ||
let wat_filepath: String = replace_extension(filepath.clone(), "irl", "wat"); | ||
transpilation_mode(WatTranspiler{}, nodes, wat_filepath.clone()); | ||
if !options.wasm {return;} | ||
let wasm_filepath: String = replace_extension(filepath.clone(), "irl", "wasm"); | ||
options.run_command(&["wat2wasm", wat_filepath.as_str(), "-o", wasm_filepath.as_str()]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::collections::HashSet; | ||
use crate::fe::ast::*; | ||
use crate::trn::transpiler::Transpiler; | ||
|
||
pub struct WatTranspiler; | ||
|
||
impl Transpiler for WatTranspiler { | ||
fn transpile(&self, nodes: &Vec<AstNode>) -> Vec<String> { | ||
let mut lines: Vec<String> = Vec::new(); | ||
lines.push(String::from("(module")); | ||
lines.append(&mut transpile_nodes_to_wat(nodes, 1, &mut HashSet::new())); | ||
lines.push(String::from(")")); | ||
lines | ||
} | ||
} | ||
|
||
fn transpile_nodes_to_wat(nodes: &Vec<AstNode>, indent_sz: usize, | ||
vis_labels: &mut HashSet<String>) -> Vec<String> { | ||
|
||
let mut lines: Vec<String> = Vec::new(); | ||
for node in nodes.iter() { | ||
lines.append(&mut transpile_node_to_wat(node, indent_sz, vis_labels)); | ||
} | ||
lines | ||
} | ||
|
||
fn make_line(indent_sz: usize, text: String) -> String {let mut line: String = String::new(); | ||
for _ in 0..indent_sz {line.push_str(" ");} | ||
line.push_str(&text); | ||
line | ||
} | ||
|
||
fn transpile_node_to_wat(node: &AstNode, indent_sz: usize, vis_labels: &mut HashSet<String>) -> Vec<String> { | ||
let mut lines: Vec<String> = Vec::new(); | ||
match node { | ||
AstNode::Function(function_node) => { | ||
let mut line: String = make_line(indent_sz, format!("(func ${} (export \"{}\")", | ||
function_node.name, function_node.name)); | ||
for arg_name in function_node.args.iter() { | ||
line += &format!(" (param ${} i32)", arg_name); | ||
} | ||
line += " (result i32)"; | ||
lines.push(line); | ||
lines.append(&mut transpile_nodes_to_wat(&function_node.body, indent_sz+1, vis_labels)); | ||
lines.push(make_line(indent_sz+1, String::from("i32.const 0"))); | ||
lines.push(make_line(indent_sz, String::from(" )"))); | ||
} | ||
AstNode::Label(label_node) => { | ||
lines.push(make_line(indent_sz, format!("(block ${}", label_node.name))); | ||
vis_labels.insert(label_node.name.clone()); | ||
lines.append(&mut transpile_nodes_to_wat(&label_node.body, indent_sz+1, vis_labels)); | ||
lines.push(make_line(indent_sz, String::from(")"))); | ||
} | ||
AstNode::Goto(goto_node) => { | ||
if vis_labels.contains(&goto_node.name) { | ||
lines.push(make_line(indent_sz, format!("br ${}", goto_node.name))); | ||
} | ||
} | ||
_ => {} | ||
} | ||
lines | ||
} |