From 900c225a1295e52dcb5e8059eaf4ca25a2a859f0 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sat, 12 Feb 2022 21:25:20 +0700 Subject: [PATCH] import file --- example/hello_world.hyc | 4 +++- src/front/lex.rs | 1 + src/front/model.rs | 3 +++ src/front/parser.rs | 14 ++++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/example/hello_world.hyc b/example/hello_world.hyc index 1671678..a86a494 100644 --- a/example/hello_world.hyc +++ b/example/hello_world.hyc @@ -1,3 +1,5 @@ +import "path/to/library.hyc"; + // user defined function func foo :: (a, b) -> Bool = { return a == b; @@ -6,7 +8,7 @@ func foo :: (a, b) -> Bool = { // entry point func main :: () -> Int = { // if else in variable definition - let cond_str :: String = if true { return "t" } else { return "f" }; + let cond_str :: String = if foo(1, 1) { return "t" } else { return "f" }; // Infix operator let n :: Bool = 2 == 2; diff --git a/src/front/lex.rs b/src/front/lex.rs index 86369dd..4344069 100644 --- a/src/front/lex.rs +++ b/src/front/lex.rs @@ -92,6 +92,7 @@ fn lex_reserved_identifier(input: &Bytes) -> IResult<&Bytes, Token> { |s| { let c = str_from_bytes(s); c.map(|syntax| match syntax { + "import" => Token::Import, "if" => Token::If, "else" => Token::Else, "let" => Token::Let, diff --git a/src/front/model.rs b/src/front/model.rs index 2c87064..84b3b6c 100644 --- a/src/front/model.rs +++ b/src/front/model.rs @@ -19,6 +19,7 @@ pub enum Token { Semicolon, Colon, Comma, If, Else, Let, Func, Return, + Import, } /// Token struct with position information. @@ -100,6 +101,8 @@ pub type Program = Vec; #[derive(Clone, Debug, PartialEq)] pub enum Stmt { + Import(Literal), + Let(Ident, Ident, Expr), Func(Ident, Vec, Ident, Vec), Call(Ident, Vec), diff --git a/src/front/parser.rs b/src/front/parser.rs index a2df2eb..e8de9f4 100644 --- a/src/front/parser.rs +++ b/src/front/parser.rs @@ -19,6 +19,8 @@ macro_rules! tag_token ( ) ); +tag_token!(tag_import, Token::Import); + tag_token!(tag_let, Token::Let); tag_token!(tag_func, Token::Func); tag_token!(tag_return, Token::Return); @@ -271,8 +273,20 @@ fn parse_let_stmt(input: Tokens) -> IResult { )(input) } +fn parse_import(input: Tokens) -> IResult { + map( + tuple(( + tag_import, + parse_literal, + opt(tag_semicolon), + )), + |(_, path, _)| Stmt::Import(path), + )(input) +} + fn parse_stmt(input: Tokens) -> IResult { alt(( + parse_import, parse_let_stmt, parse_func_stmt, parse_call_stmt,