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

import file

This commit is contained in:
Natapat Samutpong 2022-02-12 21:25:20 +07:00
parent 0b82cf058f
commit 900c225a12
4 changed files with 21 additions and 1 deletions

View file

@ -1,3 +1,5 @@
import "path/to/library.hyc";
// user defined function // user defined function
func foo :: (a, b) -> Bool = { func foo :: (a, b) -> Bool = {
return a == b; return a == b;
@ -6,7 +8,7 @@ func foo :: (a, b) -> Bool = {
// entry point // entry point
func main :: () -> Int = { func main :: () -> Int = {
// if else in variable definition // 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 // Infix operator
let n :: Bool = 2 == 2; let n :: Bool = 2 == 2;

View file

@ -92,6 +92,7 @@ fn lex_reserved_identifier(input: &Bytes) -> IResult<&Bytes, Token> {
|s| { |s| {
let c = str_from_bytes(s); let c = str_from_bytes(s);
c.map(|syntax| match syntax { c.map(|syntax| match syntax {
"import" => Token::Import,
"if" => Token::If, "if" => Token::If,
"else" => Token::Else, "else" => Token::Else,
"let" => Token::Let, "let" => Token::Let,

View file

@ -19,6 +19,7 @@ pub enum Token {
Semicolon, Colon, Comma, Semicolon, Colon, Comma,
If, Else, Let, Func, Return, If, Else, Let, Func, Return,
Import,
} }
/// Token struct with position information. /// Token struct with position information.
@ -100,6 +101,8 @@ pub type Program = Vec<Stmt>;
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum Stmt { pub enum Stmt {
Import(Literal),
Let(Ident, Ident, Expr), Let(Ident, Ident, Expr),
Func(Ident, Vec<Ident>, Ident, Vec<Stmt>), Func(Ident, Vec<Ident>, Ident, Vec<Stmt>),
Call(Ident, Vec<Expr>), Call(Ident, Vec<Expr>),

View file

@ -19,6 +19,8 @@ macro_rules! tag_token (
) )
); );
tag_token!(tag_import, Token::Import);
tag_token!(tag_let, Token::Let); tag_token!(tag_let, Token::Let);
tag_token!(tag_func, Token::Func); tag_token!(tag_func, Token::Func);
tag_token!(tag_return, Token::Return); tag_token!(tag_return, Token::Return);
@ -271,8 +273,20 @@ fn parse_let_stmt(input: Tokens) -> IResult<Tokens, Stmt> {
)(input) )(input)
} }
fn parse_import(input: Tokens) -> IResult<Tokens, Stmt> {
map(
tuple((
tag_import,
parse_literal,
opt(tag_semicolon),
)),
|(_, path, _)| Stmt::Import(path),
)(input)
}
fn parse_stmt(input: Tokens) -> IResult<Tokens, Stmt> { fn parse_stmt(input: Tokens) -> IResult<Tokens, Stmt> {
alt(( alt((
parse_import,
parse_let_stmt, parse_let_stmt,
parse_func_stmt, parse_func_stmt,
parse_call_stmt, parse_call_stmt,