-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from KPMGE/implement-builtin-functions
Implement builtin functions
- Loading branch information
Showing
7 changed files
with
70 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ edition = "2021" | |
|
||
[dependencies] | ||
gflags = "0.3.12" | ||
lazy_static = "1.4.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::evaluator::Object; | ||
use lazy_static::lazy_static; | ||
use std::collections::HashMap; | ||
|
||
pub type BuiltinFn = fn(Vec<Object>) -> Object; | ||
|
||
lazy_static! { | ||
pub static ref BUILTIN_FUNCTIONS: HashMap<&'static str, BuiltinFn> = | ||
HashMap::from([("len", len as BuiltinFn)]); | ||
} | ||
|
||
fn len(args: Vec<Object>) -> Object { | ||
let first_obj = args | ||
.first() | ||
.expect("len function must be provided a string argument!"); | ||
|
||
match first_obj { | ||
Object::String(str) => Object::Integer(str.len() as i32), | ||
_ => panic!("len function must be provided a string argument!"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,8 +115,6 @@ impl Lexer { | |
self.read_char(); | ||
} | ||
|
||
self.read_char(); | ||
|
||
str | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod ast; | ||
mod builtin; | ||
pub mod evaluator; | ||
pub mod lexer; | ||
pub mod parser; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters