1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

Compare commits

...

2 commits

Author SHA1 Message Date
azur 1eb3296ab6 remove let without in 2023-03-02 12:04:12 +07:00
azur f6a5f904c0 args parsing 2023-03-02 11:46:10 +07:00
10 changed files with 225 additions and 100 deletions

182
Cargo.lock generated
View file

@ -13,6 +13,32 @@ dependencies = [
"version_check",
]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cc"
version = "1.0.79"
@ -35,6 +61,21 @@ dependencies = [
"stacker",
]
[[package]]
name = "clap"
version = "2.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "getrandom"
version = "0.2.8"
@ -55,6 +96,30 @@ dependencies = [
"ahash",
]
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.139"
@ -67,6 +132,39 @@ version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6"
dependencies = [
"unicode-ident",
]
[[package]]
name = "psm"
version = "0.1.21"
@ -76,11 +174,21 @@ dependencies = [
"cc",
]
[[package]]
name = "quote"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
dependencies = [
"proc-macro2",
]
[[package]]
name = "renxi"
version = "0.1.0"
dependencies = [
"chumsky",
"structopt",
]
[[package]]
@ -96,6 +204,80 @@ dependencies = [
"winapi",
]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "unicode-ident"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
[[package]]
name = "unicode-segmentation"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
[[package]]
name = "unicode-width"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.4"

View file

@ -5,3 +5,4 @@ edition = "2021"
[dependencies]
chumsky = "0.9.0"
structopt = "0.3.26"

48
a.hlm
View file

@ -1,43 +1,15 @@
let name: str = "john";
println("Hello, " + name + "!");
let a = 17, b = 35 in
let c = a * 2 in
let a: num = 17, b: num = 35 in
let c: num = a * 2 in
println(b + c);
func foo (a: int, b: int) {
let c = a * 2;
// func foo (a: int, b: int) {
// let c = a * 2;
//
// let res = b + c in
// return res + a;
// }
let res = b + c in
return res + a;
}
println((\x: int -> x + 1)(1));
──────────────────────────────────────────────────
(println (+ "Hello, " name "!"))
(let [a 17] [b 35]
(let [c (* a 2)]
(println (+ b c))))
(func foo [a int b int] (block
(let [c (* a 2)])
(let [res (+ b c)]
(return (+ res a)))
))
──────────────────────────────────────────────────
console.log("Hello, " + name + "!");
let a = 17;
let b = 35;
let c = a * 2;
console.log(b + c);
const foo = (a, b) => {
let c = a * 2;
let res = b + c;
return res + a;
}
println((\x: int -> x + 1)(1));

11
src/args.rs Normal file
View file

@ -0,0 +1,11 @@
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
pub struct Options {
#[structopt(name = "FILE", parse(from_os_str))]
pub file: PathBuf,
#[structopt(short, long = "out", parse(from_os_str))]
pub output: Option<PathBuf>,
}

View file

@ -1,15 +1,17 @@
#![feature(trait_alias)]
pub mod read;
pub mod trans;
pub mod args;
use std::io::Write;
use args::Options;
use read::parse::{lex, parse};
use structopt::StructOpt;
use trans::low::{translate_expr, translate_js};
fn main() {
let path = std::env::args().nth(1).expect("No file path provided");
let src = std::fs::read_to_string(path).expect("Failed to read file");
let opt = Options::from_args();
let src = std::fs::read_to_string(opt.file).expect("Failed to read file");
let (tokens, lex_errs) = lex(src.to_owned());
@ -20,7 +22,8 @@ fn main() {
let nexprs = ast.into_iter().map(|(e, _)| translate_expr(e)).collect::<Vec<_>>();
let jsexprs = nexprs.into_iter().map(translate_js).collect::<Vec<_>>();
let mut file = std::fs::File::create("out.js").expect("Failed to create file");
let mut file = std::fs::File::create(opt.output.unwrap_or("out.js".into()))
.expect("Failed to create file");
let s = jsexprs
.into_iter()
.map(|e| {

View file

@ -293,26 +293,17 @@ pub fn expr_parser() -> impl P<Spanned<PExpr>> {
.then(expr.clone())
.map(|(vars, body)| PExpr::Let {
vars,
body: Some(Box::new(body)),
body: Box::new(body),
})
.boxed()
.labelled("let..in");
let let_def = just(Token::Let)
.ignore_then(let_binds)
.map(|vars| PExpr::Let {
vars,
body: None,
})
.labelled("let");
let atom = lit
.or(sym)
.or(vec)
.or(paren_expr)
.or(lam)
.or(let_in)
.or(let_def)
.map_with_span(|e, s| (e, s))
.boxed()
.labelled("atom");

View file

@ -37,6 +37,6 @@ pub enum PExpr {
},
Let {
vars: Vec<(String, Type, Spanned<Self>)>,
body: Option<Box<Spanned<Self>>>,
body: Box<Spanned<Self>>,
},
}

View file

@ -34,7 +34,6 @@ pub enum Expr {
args: Vec<(String, Type)>,
body: Box<Self>,
},
Define(Vec<(String, Box<Self>)>),
}
impl Display for Expr {
@ -72,11 +71,6 @@ impl Display for Expr {
}
write!(f, " {})", body)
},
Expr::Define(vars) => {
vars.iter().try_for_each(|(name, val)| {
write!(f, "(define {} {})", name, val)
})
},
}
}
}

View file

@ -19,7 +19,6 @@ pub enum JSExpr {
args: Vec<(String, Type)>,
body: Box<Self>,
},
Let(Vec<(String, Box<Self>)>),
}
impl Display for JSExpr {
@ -77,11 +76,6 @@ impl Display for JSExpr {
}
write!(f, ") => {})", body)
},
JSExpr::Let(vars) => {
vars.iter().try_for_each(|(name, expr)| {
write!(f, "let {} = {};", name, expr)
})
},
}
}
}

View file

@ -52,34 +52,19 @@ pub fn translate_expr(expr: PExpr) -> Expr {
body: Box::new(translate_expr((*body).0)),
},
PExpr::Let { vars, body } => {
if let Some(body) = body {
let mut expr: Expr = translate_expr(body.0); // The expression we're building up
for (name, ty, val) in vars.into_iter().rev() { // Reverse so we can build up the lambda
// e.g.: let x : t = e1 in e2; => (lambda (x : t) = e2)(e1)
// Build up the lambda
expr = Expr::Lambda {
args: vec![(name, ty)],
body: Box::new(expr),
};
// Call the lambda with the value
let val = translate_expr(val.0);
expr = Expr::Call(Box::new(expr), vec![val]);
}
expr
} else {
// e.g. let a : t = 1; => (define a 1)
// let a : t = 2, b : t = 3; => (define a 2) (define b 3)
let mut xs: Vec<(String, Box<Expr>)> = vec![];
for (name, _, val) in vars {
let val = translate_expr(val.0);
xs.push((name, Box::new(val)));
}
Expr::Define(xs)
let mut expr: Expr = translate_expr(body.0); // The expression we're building up
for (name, ty, val) in vars.into_iter().rev() { // Reverse so we can build up the lambda
// e.g.: let x : t = e1 in e2; => (lambda (x : t) = e2)(e1)
// Build up the lambda
expr = Expr::Lambda {
args: vec![(name, ty)],
body: Box::new(expr),
};
// Call the lambda with the value
let val = translate_expr(val.0);
expr = Expr::Call(Box::new(expr), vec![val]);
}
expr
}
}
}
@ -143,13 +128,5 @@ pub fn translate_js(expr: Expr) -> JSExpr {
args,
body: Box::new(translate_js(*body)),
},
Expr::Define(xs) => {
let ns = xs
.into_iter()
.map(|(name, val)| (name, Box::new(translate_js(*val))))
.collect();
JSExpr::Let(ns)
}
}
}