From 3768562c52571a8c5f64ecb63a8951921e9b18fc Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sat, 14 May 2022 05:52:20 +0700 Subject: [PATCH] fix tuple codegen, generics example, change syntax --- Cargo.lock | 8 ++++++++ Cargo.toml | 1 + checker/Cargo.toml | 7 +++++++ checker/src/lib.rs | 1 + checker/src/syntax.rs | 0 codegen/src/lib.rs | 3 +-- core/Cargo.toml | 1 + core/src/main.rs | 1 + examples/factorial.hz | 2 +- examples/generics.hz | 6 ++++++ syntax/src/lex.rs | 3 ++- syntax/src/parse.rs | 14 ++++++++------ 12 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 checker/Cargo.toml create mode 100644 checker/src/lib.rs create mode 100644 checker/src/syntax.rs create mode 100644 examples/generics.hz diff --git a/Cargo.lock b/Cargo.lock index b0435db..582c937 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,6 +28,13 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "checker" +version = "0.1.0" +dependencies = [ + "syntax", +] + [[package]] name = "chumsky" version = "0.8.0" @@ -107,6 +114,7 @@ dependencies = [ name = "hzc" version = "0.1.0" dependencies = [ + "checker", "codegen", "colored", "syntax", diff --git a/Cargo.toml b/Cargo.toml index 737be42..df87439 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "core", "syntax", + "checker", "transformer", "codegen", ] \ No newline at end of file diff --git a/checker/Cargo.toml b/checker/Cargo.toml new file mode 100644 index 0000000..cab0fcd --- /dev/null +++ b/checker/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "checker" +version = "0.1.0" +edition = "2021" + +[dependencies] +syntax = { path = "../syntax" } diff --git a/checker/src/lib.rs b/checker/src/lib.rs new file mode 100644 index 0000000..a80cdbc --- /dev/null +++ b/checker/src/lib.rs @@ -0,0 +1 @@ +pub mod syntax; \ No newline at end of file diff --git a/checker/src/syntax.rs b/checker/src/syntax.rs new file mode 100644 index 0000000..e69de29 diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index ca6251e..4d32b60 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -34,8 +34,7 @@ impl Codegen { match expr { Expr::Literal(lit) => self.gen_literal(&lit.0), Expr::Identifier(name) => { format!("_{}{}", name.0, semicolon!()) }, - Expr::Tuple(elems) => { format!("({}{})", elems.iter().map(|e| self.gen_expr(&e.0, false)).collect::>().join(", "), semicolon!()) }, - Expr::Vector(elems) => { format!("[{}{}]", elems.iter().map(|e| self.gen_expr(&e.0, false)).collect::>().join(", "), semicolon!()) }, + Expr::Tuple(elems) | Expr::Vector(elems) => { format!("[{}{}]", elems.iter().map(|e| self.gen_expr(&e.0, false)).collect::>().join(", "), semicolon!()) }, Expr::Object { fields } => { format!("{{{}}}", fields.iter().map(|(name, expr)| format!("{}: {}", name.0, self.gen_expr(&expr.0, false))).collect::>().join(",\n ")) diff --git a/core/Cargo.toml b/core/Cargo.toml index 62fe8f9..69d4899 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] syntax = { path = "../syntax" } +checker = { path = "../checker" } codegen = { path = "../codegen" } colored = "2.0.0" \ No newline at end of file diff --git a/core/src/main.rs b/core/src/main.rs index 69a2c28..bbbb5c4 100644 --- a/core/src/main.rs +++ b/core/src/main.rs @@ -31,6 +31,7 @@ fn main() { return; } + println!("{:#?}", ast.as_ref().unwrap()); info!("Parsed in {}ms", time.elapsed().as_millis()); // diff --git a/examples/factorial.hz b/examples/factorial.hz index 432ddc5..961a323 100644 --- a/examples/factorial.hz +++ b/examples/factorial.hz @@ -1,4 +1,4 @@ -fun factorial (n : int) : int = +factorial (n : int) : int = if n == 0 | return 1 | return n * factorial(n - 1) diff --git a/examples/generics.hz b/examples/generics.hz new file mode 100644 index 0000000..5f20345 --- /dev/null +++ b/examples/generics.hz @@ -0,0 +1,6 @@ +print T (x : T) : void = @write(x) + +a : int = 123 +print(a) +b : string = "Hello, World!" +print(b) \ No newline at end of file diff --git a/syntax/src/lex.rs b/syntax/src/lex.rs index 1ef9cd7..18c5b9f 100644 --- a/syntax/src/lex.rs +++ b/syntax/src/lex.rs @@ -22,7 +22,7 @@ pub enum Token { Arrow, And, Or, // Symbols & Delimiters - Assign, Dot, Comma, Colon, Semicolon, At, + Assign, Dot, Comma, Colon, Semicolon, At, Hash, Open(Delimiter), Close(Delimiter), } @@ -62,6 +62,7 @@ pub fn lexer() -> impl Parser, Error = Simple> { just(':').to(Token::Colon), just(';').to(Token::Semicolon), just('@').to(Token::At), + just('#').to(Token::Hash), )); let delim = choice(( diff --git a/syntax/src/parse.rs b/syntax/src/parse.rs index 0ec3eaa..ffc2411 100644 --- a/syntax/src/parse.rs +++ b/syntax/src/parse.rs @@ -139,7 +139,7 @@ fn expr_parser() -> impl P> { .repeated()) .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()) .then_ignore(just(Token::Dot)) .then(atom.clone()) @@ -154,7 +154,7 @@ fn expr_parser() -> impl P> { args: args.into_iter().flatten().collect() }, span )}).labelled("method"); - let access = just(Token::Semicolon) + let access = just(Token::Colon) .ignore_then(identexpr) .then_ignore(just(Token::Dot)) .then(atom.clone()) @@ -234,9 +234,8 @@ fn expr_parser() -> impl P> { (Expr::Redefine { name: *Box::new(ident), value: Box::new(expr) }, span) }); - let function = just(Token::KwFun) - .ignore_then(identifier()) - // Generics + let function = identifier() + // Generic .then(identifier().repeated()) // Arguments .then( @@ -300,7 +299,10 @@ fn expr_parser() -> impl P> { #[allow(clippy::type_complexity)] pub fn parse(tokens: Vec<(Token, std::ops::Range)>, len: usize) -> (Option)>>, Vec>) { - 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, tokens.into_iter(), ));