From 40f48cbbf1bb88dda8cf7312d38d5c36e80758df Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sun, 6 Mar 2022 00:13:48 +0700 Subject: [PATCH] use const for function declaration, comma separated args --- example/ex.hyc | 5 ++++- src/back/js.rs | 2 +- src/front/parse.rs | 13 +++++++------ src/main.rs | 1 + src/middle/ir.rs | 16 +--------------- 5 files changed, 14 insertions(+), 23 deletions(-) diff --git a/example/ex.hyc b/example/ex.hyc index dd42636..92078c1 100644 --- a/example/ex.hyc +++ b/example/ex.hyc @@ -1,4 +1,4 @@ -let foo: int = 1; +let foo: string = 1; fun bar (baz: int) -> int = baz + 1; fun qux (quux: int) -> int = do @@ -6,4 +6,7 @@ fun qux (quux: int) -> int = do bar(corge); end; +fun add (lhs: int, rhs: int) -> int = lhs + rhs; +print(add(34, 35)); + print(qux(5)); \ No newline at end of file diff --git a/src/back/js.rs b/src/back/js.rs index 237212c..7e6e9cb 100644 --- a/src/back/js.rs +++ b/src/back/js.rs @@ -46,7 +46,7 @@ fn gen_ir(ir: &IR) -> String { }; format!( - "function {}({}) {{ {} }}", + "const {} = ({}) => {{ {} }};", name, args, body diff --git a/src/front/parse.rs b/src/front/parse.rs index 9e601d5..c3a2f15 100644 --- a/src/front/parse.rs +++ b/src/front/parse.rs @@ -12,7 +12,7 @@ pub enum Token { Delimiter(char), Semicolon, Assign, Colon, - Comma, + Comma, Dot, ReturnHint, // Keywords @@ -46,6 +46,7 @@ pub fn lexer() -> impl Parser, Error = Simple> { just(':').to(Token::Colon), just(',').to(Token::Comma), just("->").to(Token::ReturnHint), + just(".").to(Token::Dot), )); let operator = choice(( @@ -152,9 +153,8 @@ fn expr_parser() -> impl Parser> + Clone { let expr = recursive(|expr| { let args = expr.clone() - .repeated() - .or_not() - .map(|item| item.unwrap_or_else(Vec::new)); + .separated_by(just(Token::Comma)) + .allow_trailing(); let atom = literal .or(ident.map(Expr::Ident)) @@ -177,7 +177,7 @@ fn expr_parser() -> impl Parser> + Clone { args, } }); - + let unary = choice(( just(Token::Operator("-".to_string())).to("-"), just(Token::Operator("!".to_string())).to("!"))) @@ -263,7 +263,8 @@ fn expr_parser() -> impl Parser> + Clone { (ident .then_ignore(just(Token::Colon)) .then(ident)) - .repeated() + .separated_by(just(Token::Comma)) + .allow_trailing() ) .then_ignore(just(Token::Delimiter(')'))) .then_ignore(just(Token::ReturnHint)) diff --git a/src/main.rs b/src/main.rs index 78e8c6a..2760a9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ use middle::ir; /// Contains code generator. pub mod back; +/// Utility functions. pub mod util; use crate::util::log; diff --git a/src/middle/ir.rs b/src/middle/ir.rs index 7fc6631..e50ebe8 100644 --- a/src/middle/ir.rs +++ b/src/middle/ir.rs @@ -1,27 +1,13 @@ -use core::fmt; - use crate::front::parse::Expr; #[derive(Debug, Clone)] pub enum TypeHint { Int, - Float, Double, + Float, Bool, String, } -impl fmt::Display for TypeHint { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - TypeHint::Int => write!(f, "int"), - TypeHint::Float => write!(f, "float"), - TypeHint::Double => write!(f, "double"), - TypeHint::Bool => write!(f, "bool"), - TypeHint::String => write!(f, "char*"), - } - } -} - #[derive(Debug, Clone)] pub enum Value { Int(i64),