Skip to content

Commit

Permalink
handle some more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
x86y committed Oct 2, 2023
1 parent 322eb0d commit 0a14369
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ path = "src/bin/main.rs"
[dependencies]
prettyplease = "0.2.15"
quote = "1.0.33"
syn = { version = "2.0.37", features = ["full"] }
syn = { version = "2.0.37", features = ["full", "extra-traits"] }
wasm-bindgen = "0.2.87"
44 changes: 33 additions & 11 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,53 @@
<body>
<form id="wasmForm">
<textarea id="inputVal" placeholder="Write rust code here">
fn main() {
for i in 0..5 {
println!("Out");
for i in 0..15 {
println!("In");
pub fn eval(&mut self, s: String) {
let st = &mut vec![];
let b = &mut 0;
let a = &mut self.a;
let p = &mut self.p;
let ip = &mut self.ip;
let by = s.as_bytes();
while *ip < by.len() {
let c = by[*ip];
match c as char {
']' => {
if a[*p] != 0 {
*ip = st.pop().unwrap();
}
*b = *ip;
}
'[' => {
if a[*p] == 0 {
*ip = *b;
}
st.push(*ip);
}
'+' => a[*p] += 1,
'-' => a[*p] -= 1,
'>' => *p += 1,
'<' => *p -= 1,
'.' => self.b.push(a[*p]),
',' => {},
_ => unreachable!(),
};
*ip += 1;
}
}
loop {
println!("Forever loop");
}
}
</textarea>
<button type="submit">Enter</button>
<pre><div id="output"></div></pre>
</form>

<script type="module">
import init, { terser_loops } from "./pkg/libfranca.js";
import init, { mkterse } from "./pkg/libfranca.js";

init();

document.getElementById('wasmForm').addEventListener('submit', function(e) {
e.preventDefault();
const inputValue = document.getElementById('inputVal').value;
const result = terser_loops(inputValue);
const result = mkterse(inputValue);
document.getElementById('output').textContent = result;
});
</script>
Expand Down
3 changes: 1 addition & 2 deletions pkg/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
README.md

*
10 changes: 8 additions & 2 deletions pkg/libfranca.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
* @param {string} i
* @returns {string}
*/
export function terser_loops(i: string): string;
export function mkterse(i: string): string;
/**
* @param {string} src
* @returns {string}
*/
export function tersify(src: string): string;

export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;

export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly terser_loops: (a: number, b: number, c: number) => void;
readonly mkterse: (a: number, b: number, c: number) => void;
readonly tersify: (a: number, b: number, c: number) => void;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
Expand Down
27 changes: 25 additions & 2 deletions pkg/libfranca.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,37 @@ function getStringFromWasm0(ptr, len) {
* @param {string} i
* @returns {string}
*/
export function terser_loops(i) {
export function mkterse(i) {
let deferred2_0;
let deferred2_1;
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passStringToWasm0(i, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
wasm.terser_loops(retptr, ptr0, len0);
wasm.mkterse(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
deferred2_0 = r0;
deferred2_1 = r1;
return getStringFromWasm0(r0, r1);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
}
}

/**
* @param {string} src
* @returns {string}
*/
export function tersify(src) {
let deferred2_0;
let deferred2_1;
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passStringToWasm0(src, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
wasm.tersify(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
deferred2_0 = r0;
Expand Down
Binary file modified pkg/libfranca_bg.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion pkg/libfranca_bg.wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function terser_loops(a: number, b: number, c: number): void;
export function mkterse(a: number, b: number, c: number): void;
export function tersify(a: number, b: number, c: number): void;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
Expand Down
5 changes: 2 additions & 3 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#![feature(custom_inner_attributes)]

use libfranca::loops::{terser_loops, unfolder};
use libfranca::loops::mkterse;

fn main() {
unfolder();
let i = terser_loops("fn main() { while true {} }".into());
let i = mkterse("fn a() { let a = 5; let mut b = 10; }".into());
println!("{i}");
}
15 changes: 14 additions & 1 deletion src/lib/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
pub mod loops;

use quote::quote;
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
pub fn tersify(src: String) -> String {
let t1 = loops::mkterse(src);
// let t2 = conds::terser_conds(t1);
let gen = quote! { #t1 };
println!("{}", gen);
let f: syn::File = syn::parse2(gen).unwrap();
let pretty = prettyplease::unparse(&f);
pretty.to_string()
}

/// forks
#[macro_export]
macro_rules! atop {
Expand Down Expand Up @@ -245,7 +259,6 @@ pub const T: bool = true;
pub const F: bool = false;
pub type Sstr = &'static str;


#[cfg(test)]
mod tests {
#[test]
Expand Down
45 changes: 44 additions & 1 deletion src/lib/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,26 @@ macro_rules! parse_syn_int {
}
};
}

fn transform_stmt(stmt: &Stmt) -> Vec<Stmt> {
match stmt {
Stmt::Local(syn::Local {
attrs: _,
let_token: _,
pat,
init,
semi_token: _,
}) => {
let init = init.as_ref().unwrap();
let expr = &init.expr;
if let syn::Pat::Ident(ident) = pat {
if ident.mutability.is_some() {
return vec![parse_quote! { lm!(#expr); }];
} else {
return vec![parse_quote! { l!(#expr); }];
}
}
}
Stmt::Expr(syn::Expr::ForLoop(loop_expr), _) => {
if let syn::Expr::Range(range_expr) = &*loop_expr.expr {
if let (syn::Expr::Lit(start), syn::Expr::Lit(end)) = (
Expand All @@ -38,6 +56,31 @@ fn transform_stmt(stmt: &Stmt) -> Vec<Stmt> {
let new_body = transform_block(&loop_expr.body);
return vec![parse_quote! { W!(#cond, { #new_body }); }];
}
Stmt::Expr(syn::Expr::If(if_expr), _) => {
let inner = &if_expr.cond;
let then_body = transform_block(&if_expr.then_branch);
if if_expr.else_branch.is_some() {
let else_body = &*if_expr.else_branch.as_ref().unwrap().1;
return vec![
parse_quote! { I!(#inner, #then_body ); },
parse_quote! { I!(#inner, #else_body ); },
];
}
return vec![parse_quote! { I!(#inner, { #then_body }); }];
}
Stmt::Expr(syn::Expr::Match(match_expr), _) => {
let expr = &match_expr.expr;
let arms = &match_expr.arms;
let patterns = arms.iter().map(|arm| &arm.pat);
let bodies = arms.iter().map(|arm| &arm.body);
let transformed: Vec<_> = patterns
.zip(bodies)
.map(|(pat, body)| {
quote! { #pat , #body }
})
.collect();
return vec![parse_quote! { M!(#expr; #(#transformed);*); }];
}
_ => return vec![stmt.clone()],
}
vec![]
Expand Down Expand Up @@ -109,7 +152,7 @@ pub fn unfolder() {
}

#[wasm_bindgen]
pub fn terser_loops(i: String) -> String {
pub fn mkterse(i: String) -> String {
let item_fn: ItemFn = syn::parse_str(&i).unwrap();
let transformed_fn = transform_loop_with_macro(item_fn);
let generated_code = quote! { #transformed_fn };
Expand Down

0 comments on commit 0a14369

Please sign in to comment.