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

fix tuple codegen, generics example, change syntax

This commit is contained in:
Natapat Samutpong 2022-05-14 05:52:20 +07:00
parent 9c3a390ae8
commit 3768562c52
12 changed files with 37 additions and 10 deletions

8
Cargo.lock generated
View file

@ -28,6 +28,13 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "checker"
version = "0.1.0"
dependencies = [
"syntax",
]
[[package]] [[package]]
name = "chumsky" name = "chumsky"
version = "0.8.0" version = "0.8.0"
@ -107,6 +114,7 @@ dependencies = [
name = "hzc" name = "hzc"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"checker",
"codegen", "codegen",
"colored", "colored",
"syntax", "syntax",

View file

@ -2,6 +2,7 @@
members = [ members = [
"core", "core",
"syntax", "syntax",
"checker",
"transformer", "transformer",
"codegen", "codegen",
] ]

7
checker/Cargo.toml Normal file
View file

@ -0,0 +1,7 @@
[package]
name = "checker"
version = "0.1.0"
edition = "2021"
[dependencies]
syntax = { path = "../syntax" }

1
checker/src/lib.rs Normal file
View file

@ -0,0 +1 @@
pub mod syntax;

0
checker/src/syntax.rs Normal file
View file

View file

@ -34,8 +34,7 @@ impl Codegen {
match expr { match expr {
Expr::Literal(lit) => self.gen_literal(&lit.0), Expr::Literal(lit) => self.gen_literal(&lit.0),
Expr::Identifier(name) => { format!("_{}{}", name.0, semicolon!()) }, Expr::Identifier(name) => { format!("_{}{}", name.0, semicolon!()) },
Expr::Tuple(elems) => { format!("({}{})", elems.iter().map(|e| self.gen_expr(&e.0, false)).collect::<Vec<_>>().join(", "), semicolon!()) }, Expr::Tuple(elems) | Expr::Vector(elems) => { format!("[{}{}]", elems.iter().map(|e| self.gen_expr(&e.0, false)).collect::<Vec<_>>().join(", "), semicolon!()) },
Expr::Vector(elems) => { format!("[{}{}]", elems.iter().map(|e| self.gen_expr(&e.0, false)).collect::<Vec<_>>().join(", "), semicolon!()) },
Expr::Object { fields } => { Expr::Object { fields } => {
format!("{{{}}}", format!("{{{}}}",
fields.iter().map(|(name, expr)| format!("{}: {}", name.0, self.gen_expr(&expr.0, false))).collect::<Vec<_>>().join(",\n ")) fields.iter().map(|(name, expr)| format!("{}: {}", name.0, self.gen_expr(&expr.0, false))).collect::<Vec<_>>().join(",\n "))

View file

@ -5,6 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
syntax = { path = "../syntax" } syntax = { path = "../syntax" }
checker = { path = "../checker" }
codegen = { path = "../codegen" } codegen = { path = "../codegen" }
colored = "2.0.0" colored = "2.0.0"

View file

@ -31,6 +31,7 @@ fn main() {
return; return;
} }
println!("{:#?}", ast.as_ref().unwrap());
info!("Parsed in {}ms", time.elapsed().as_millis()); info!("Parsed in {}ms", time.elapsed().as_millis());
// //

View file

@ -1,4 +1,4 @@
fun factorial (n : int) : int = factorial (n : int) : int =
if n == 0 if n == 0
| return 1 | return 1
| return n * factorial(n - 1) | return n * factorial(n - 1)

6
examples/generics.hz Normal file
View file

@ -0,0 +1,6 @@
print T (x : T) : void = @write(x)
a : int = 123
print(a)
b : string = "Hello, World!"
print(b)

View file

@ -22,7 +22,7 @@ pub enum Token {
Arrow, And, Or, Arrow, And, Or,
// Symbols & Delimiters // Symbols & Delimiters
Assign, Dot, Comma, Colon, Semicolon, At, Assign, Dot, Comma, Colon, Semicolon, At, Hash,
Open(Delimiter), Close(Delimiter), Open(Delimiter), Close(Delimiter),
} }
@ -62,6 +62,7 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = Simple<char>> {
just(':').to(Token::Colon), just(':').to(Token::Colon),
just(';').to(Token::Semicolon), just(';').to(Token::Semicolon),
just('@').to(Token::At), just('@').to(Token::At),
just('#').to(Token::Hash),
)); ));
let delim = choice(( let delim = choice((

View file

@ -139,7 +139,7 @@ fn expr_parser() -> impl P<Spanned<Expr>> {
.repeated()) .repeated())
.foldl(|name, args| {( Expr::Intrinsic { name: Box::new(name.clone()), args }, name.1 )}).labelled("intrinsic"); .foldl(|name, args| {( Expr::Intrinsic { name: Box::new(name.clone()), args }, name.1 )}).labelled("intrinsic");
let method = just(Token::Colon) let method = just(Token::Hash)
.ignore_then(identexpr.clone()) .ignore_then(identexpr.clone())
.then_ignore(just(Token::Dot)) .then_ignore(just(Token::Dot))
.then(atom.clone()) .then(atom.clone())
@ -154,7 +154,7 @@ fn expr_parser() -> impl P<Spanned<Expr>> {
args: args.into_iter().flatten().collect() args: args.into_iter().flatten().collect()
}, span )}).labelled("method"); }, span )}).labelled("method");
let access = just(Token::Semicolon) let access = just(Token::Colon)
.ignore_then(identexpr) .ignore_then(identexpr)
.then_ignore(just(Token::Dot)) .then_ignore(just(Token::Dot))
.then(atom.clone()) .then(atom.clone())
@ -234,9 +234,8 @@ fn expr_parser() -> impl P<Spanned<Expr>> {
(Expr::Redefine { name: *Box::new(ident), value: Box::new(expr) }, span) (Expr::Redefine { name: *Box::new(ident), value: Box::new(expr) }, span)
}); });
let function = just(Token::KwFun) let function = identifier()
.ignore_then(identifier()) // Generic
// Generics
.then(identifier().repeated()) .then(identifier().repeated())
// Arguments // Arguments
.then( .then(
@ -300,7 +299,10 @@ fn expr_parser() -> impl P<Spanned<Expr>> {
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
pub fn parse(tokens: Vec<(Token, std::ops::Range<usize>)>, len: usize) -> (Option<Vec<(Expr, std::ops::Range<usize>)>>, Vec<Simple<Token>>) { pub fn parse(tokens: Vec<(Token, std::ops::Range<usize>)>, len: usize) -> (Option<Vec<(Expr, std::ops::Range<usize>)>>, Vec<Simple<Token>>) {
let (ast, parse_error) = expr_parser().repeated().then_ignore(end()).parse_recovery(Stream::from_iter( let (ast, parse_error) = expr_parser()
.repeated()
.then_ignore(end())
.parse_recovery(Stream::from_iter(
len..len + 1, len..len + 1,
tokens.into_iter(), tokens.into_iter(),
)); ));