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

Compare commits

..

No commits in common. "1eb3296ab6006b05be58ade1bca9f3e3507d237f" and "4413226affbb91a64730afd9bb4589428097ba81" have entirely different histories.

10 changed files with 100 additions and 225 deletions

182
Cargo.lock generated
View file

@ -13,32 +13,6 @@ dependencies = [
"version_check", "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]] [[package]]
name = "cc" name = "cc"
version = "1.0.79" version = "1.0.79"
@ -61,21 +35,6 @@ dependencies = [
"stacker", "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]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.8" version = "0.2.8"
@ -96,30 +55,6 @@ dependencies = [
"ahash", "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]] [[package]]
name = "libc" name = "libc"
version = "0.2.139" version = "0.2.139"
@ -132,39 +67,6 @@ version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" 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]] [[package]]
name = "psm" name = "psm"
version = "0.1.21" version = "0.1.21"
@ -174,21 +76,11 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "quote"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
dependencies = [
"proc-macro2",
]
[[package]] [[package]]
name = "renxi" name = "renxi"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chumsky", "chumsky",
"structopt",
] ]
[[package]] [[package]]
@ -204,80 +96,6 @@ dependencies = [
"winapi", "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]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"

View file

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

46
a.hlm
View file

@ -1,15 +1,43 @@
let name: str = "john";
println("Hello, " + name + "!"); println("Hello, " + name + "!");
let a: num = 17, b: num = 35 in let a = 17, b = 35 in
let c: num = a * 2 in let c = a * 2 in
println(b + c); println(b + c);
// func foo (a: int, b: int) { func foo (a: int, b: int) {
// let c = a * 2; let c = a * 2;
//
// let res = b + c in let res = b + c in
// return res + a; return res + a;
// } }
println((\x: int -> x + 1)(1)); 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;
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -52,19 +52,34 @@ pub fn translate_expr(expr: PExpr) -> Expr {
body: Box::new(translate_expr((*body).0)), body: Box::new(translate_expr((*body).0)),
}, },
PExpr::Let { vars, body } => { PExpr::Let { vars, body } => {
let mut expr: Expr = translate_expr(body.0); // The expression we're building up if let Some(body) = body {
for (name, ty, val) in vars.into_iter().rev() { // Reverse so we can build up the lambda let mut expr: Expr = translate_expr(body.0); // The expression we're building up
// e.g.: let x : t = e1 in e2; => (lambda (x : t) = e2)(e1) for (name, ty, val) in vars.into_iter().rev() { // Reverse so we can build up the lambda
// Build up the lambda // e.g.: let x : t = e1 in e2; => (lambda (x : t) = e2)(e1)
expr = Expr::Lambda {
args: vec![(name, ty)], // Build up the lambda
body: Box::new(expr), expr = Expr::Lambda {
}; args: vec![(name, ty)],
// Call the lambda with the value body: Box::new(expr),
let val = translate_expr(val.0); };
expr = Expr::Call(Box::new(expr), vec![val]); // 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)
} }
expr
} }
} }
} }
@ -128,5 +143,13 @@ pub fn translate_js(expr: Expr) -> JSExpr {
args, args,
body: Box::new(translate_js(*body)), 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)
}
} }
} }