From d841f5d2c14812952aea4c26950190fb65251ed9 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sat, 12 Feb 2022 20:17:13 +0700 Subject: [PATCH] if else and commenting --- example/hello_world.hyc | 14 +++++++------- src/front/lex.rs | 18 +++++++++++++++--- src/front/parser.rs | 19 +++++++++++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/example/hello_world.hyc b/example/hello_world.hyc index 062bc84..68f9bad 100644 --- a/example/hello_world.hyc +++ b/example/hello_world.hyc @@ -1,10 +1,10 @@ -func join :: (a, b) = { - let result :: String = from(a, b); - return result +// user defined function +func foo :: (a, b) = { + return a; }; +// entry point func main :: () = { - let foo :: String = join("f", "oo"); - print(foo); - return 0 -}; \ No newline at end of file + // if else in variable definition + let cond_str :: String = if true { return "t" } else { return "f" }; +}; diff --git a/src/front/lex.rs b/src/front/lex.rs index 97aa658..8dbb9d8 100644 --- a/src/front/lex.rs +++ b/src/front/lex.rs @@ -1,6 +1,6 @@ use nom::{ branch::alt, - bytes::complete::{tag, take}, + bytes::complete::{tag, take, take_until}, character::complete::{multispace0, alphanumeric1, alpha1, digit1}, combinator::{map, map_res, recognize}, IResult, @@ -119,15 +119,27 @@ fn lex_illegal(input: &Bytes) -> IResult<&Bytes, Token> { map(take(1usize), |_| Token::Illegal)(input) } +fn lex_comment(input: &Bytes) -> IResult<&Bytes, ()> { + let (i1, c1) = take(2usize)(input)?; + if c1.as_bytes() == b"//" { + let (i2, _) = take_until("\n")(i1)?; + let (i3, _) = take(1usize)(i2)?; + let (i4, _) = multispace0(i3)?; + let (i5, _) = lex_comment(i4)?; + Ok((i5, ())) + } else { Ok((input, ())) } +} + // Tokens fn lex_token(input: &Bytes) -> IResult<&Bytes, Token> { + let (i1, _) = lex_comment(input)?; alt(( lex_operator_punctuation, - lex_string, lex_reserved_identifier, + lex_string, lex_integer, lex_illegal, - ))(input) + ))(i1) } fn lex_tokens(input: &Bytes) -> IResult<&Bytes, Vec> { diff --git a/src/front/parser.rs b/src/front/parser.rs index 8b7bf4a..2695cde 100644 --- a/src/front/parser.rs +++ b/src/front/parser.rs @@ -22,6 +22,8 @@ macro_rules! tag_token ( tag_token!(tag_let, Token::Let); tag_token!(tag_func, Token::Func); tag_token!(tag_return, Token::Return); +tag_token!(tag_if, Token::If); +tag_token!(tag_else, Token::Else); tag_token!(tag_assign, Token::Assign); tag_token!(tag_typehint, Token::Typehint); @@ -71,6 +73,7 @@ fn parse_atom_expr(input: Tokens) -> IResult { alt(( parse_literal_expr, parse_ident_expr, + parse_if_expr, ))(input) } @@ -130,6 +133,22 @@ fn parse_expr(input: Tokens, precedence: Precedence, left: Expr) -> IResult IResult { + map( + tuple(( + tag_if, + parse_expr_lowest, + parse_block_stmt, + parse_else_expr, + )), + |(_, cond, then, else_)| Expr::If { cond: Box::new(cond), then, else_ }, + )(input) +} + +fn parse_else_expr(input: Tokens) -> IResult> { + opt(preceded(tag_else, parse_block_stmt))(input) +} + fn parse_comma_exprs(input: Tokens) -> IResult { preceded(tag_comma, parse_expr_lowest)(input) }