Skip to content

Commit 0862556

Browse files
committed
feat: split object system
1 parent 110f945 commit 0862556

File tree

10 files changed

+72
-18
lines changed

10 files changed

+72
-18
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
members = [
33
"lexer",
44
"parser",
5+
"object",
56
"interpreter",
67
"wasm"
78
]

interpreter/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ path = "main.rs"
2121
[dependencies]
2222
phf = { version = "0.9", features = ["macros"] }
2323
monkey-parser = { path = "../parser", version = "0.6.0" }
24+
monkey-object = { path = "../object", version = "0.6.0" }

interpreter/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::object::{Object, BuiltinFunc};
21
use std::rc::Rc;
32
use phf::phf_map;
3+
use object::{BuiltinFunc, Object};
44

55
pub static BUILTINS: phf::Map<&'static str, BuiltinFunc> = phf_map! {
66
"len" => len,

interpreter/lib.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
mod builtins;
2-
mod object;
3-
pub mod environment;
1+
use std::cell::RefCell;
2+
use std::collections::HashMap;
3+
use std::rc::Rc;
44

5+
use object::environment::*;
56
use parser::ast::*;
6-
use crate::environment::*;
7-
use crate::object::{EvalError, Object};
8-
use parser::lexer::token::{TokenKind, Token};
9-
use std::rc::Rc;
10-
use std::cell::RefCell;
7+
use parser::lexer::token::{Token, TokenKind};
8+
use object::{Object, EvalError};
119
use crate::builtins::BUILTINS;
12-
use std::collections::HashMap;
10+
11+
mod builtins;
1312

1413
pub fn eval(node: Node, env: &Env) -> Result<Rc<Object>, EvalError> {
1514
match node {
@@ -272,11 +271,13 @@ fn eval_literal(literal: &Literal, env: &Env) -> Result<Rc<Object>, EvalError> {
272271

273272
#[cfg(test)]
274273
mod tests {
274+
use std::cell::RefCell;
275+
use std::rc::Rc;
276+
277+
use object::environment::*;
275278
use parser::*;
279+
276280
use crate::eval;
277-
use std::rc::Rc;
278-
use std::cell::RefCell;
279-
use crate::environment::*;
280281

281282
fn apply_test(test_cases: &[(&str, &str)]) {
282283
let env: Env = Rc::new(RefCell::new(Default::default()));

interpreter/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::stdin;
33
use interpreter::eval;
44
use std::rc::Rc;
55
use std::cell::RefCell;
6-
use interpreter::environment::Env;
6+
use object::environment::Env;
77

88
fn main() {
99
println!("Welcome to monkey interpreter by gengjiawen");

object/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "monkey-object"
3+
version = "0.6.0"
4+
description = "a object system for monkey lang"
5+
homepage = "https://github.com/gengjiawen/monkey-rust"
6+
repository = "https://github.com/gengjiawen/monkey-rust"
7+
authors = ["gengjiawen <[email protected]>"]
8+
edition = "2018"
9+
license = "MIT"
10+
11+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
12+
13+
[lib]
14+
name = "object"
15+
path= "object.rs"
16+
17+
[dependencies]
18+
monkey-parser = { path = "../parser", version = "0.6.0" }
19+
20+
[dev-dependencies]
21+
insta = "1.7.1"

object/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# monkey-rust
2+
![Rust](https://github.com/gengjiawen/monkey-rust/workflows/Rust/badge.svg)
3+
[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/gengjiawen/monkey_rust)
4+
5+
This is a object for the Monkey programming language written in Rust
6+
7+
![The Monkey Programming Language](https://cloud.githubusercontent.com/assets/1013641/22617482/9c60c27c-eb09-11e6-9dfa-b04c7fe498ea.png)
8+
9+
## What’s Monkey?
10+
11+
Monkey has a C-like syntax, supports **variable bindings**, **prefix** and **infix operators**, has **first-class** and **higher-order functions**, can handle **closures** with ease and has **integers**, **booleans**, **arrays** and **hashes** built-in.
12+
13+
Official site is: https://monkeylang.org/. It's has various implementation languages :).
14+
15+
There is a book about learning how to make an interpreter: [Writing An Interpreter In Go](https://interpreterbook.com/#the-monkey-programming-language). This is where the Monkey programming language come from.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22
use std::rc::Rc;
33
use std::cell::RefCell;
4-
use crate::object::Object;
4+
use crate::Object;
55

66
pub type Env = Rc<RefCell<Environment>>;
77

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
use std::collections::HashMap;
12
use std::fmt;
23
use std::fmt::Formatter;
4+
use std::hash::{Hash, Hasher};
35
use std::rc::Rc;
4-
use crate::environment::Env;
6+
57
use parser::ast::{BlockStatement, IDENTIFIER};
6-
use std::collections::HashMap;
7-
use std::hash::{Hash, Hasher};
8+
9+
use crate::environment::Env;
10+
11+
pub mod environment;
812

913
pub type EvalError = String;
1014
pub type BuiltinFunc = fn(Vec<Rc<Object>>) -> Rc<Object>;

0 commit comments

Comments
 (0)