completed index op on hashtable, added puts builtin
This commit is contained in:
parent
f51ead1641
commit
4e4cec0732
|
@ -1,4 +1,5 @@
|
||||||
# Monkey Interpreter
|
# Monkey Interpreter
|
||||||
|
|
||||||
This project is my attempt at writing an interpreter while working through the book, [Writing an Interpreter in Go](https://interpreterbook.com/).
|
This project is my attempt at writing an interpreter while working through the book, [Writing an Interpreter in Go](https://interpreterbook.com/).
|
||||||
It supports all the features from the book.
|
|
||||||
|
This language supports integers, strings, hashtables, functions, closures, arrays, if/else and more!
|
||||||
|
|
|
@ -19,6 +19,7 @@ pub const BUILTINS: LazyCell<HashMap<&'static str, Object>> = LazyCell::new(|| {
|
||||||
match &args[0] {
|
match &args[0] {
|
||||||
Object::String(s) => Object::Integer(s.len() as i64),
|
Object::String(s) => Object::Integer(s.len() as i64),
|
||||||
Object::Array(s) => Object::Integer(s.elements.len() as i64),
|
Object::Array(s) => Object::Integer(s.elements.len() as i64),
|
||||||
|
Object::Hash(h) => Object::Integer(h.pairs.len() as i64),
|
||||||
v => Object::Error(format!("argument to `len` not supported, got {}", v)),
|
v => Object::Error(format!("argument to `len` not supported, got {}", v)),
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
@ -110,5 +111,17 @@ pub const BUILTINS: LazyCell<HashMap<&'static str, Object>> = LazyCell::new(|| {
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
map.insert(
|
||||||
|
"puts",
|
||||||
|
Object::Builtin(BuiltinFunction {
|
||||||
|
func: Box::new(|args: Vec<Object>| {
|
||||||
|
for arg in args {
|
||||||
|
println!("{}", arg.inspect());
|
||||||
|
}
|
||||||
|
Object::Null
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
);
|
||||||
map
|
map
|
||||||
});
|
});
|
||||||
|
|
|
@ -517,6 +517,7 @@ mod tests {
|
||||||
elements: vec![Object::Integer(1), Object::Integer(5)],
|
elements: vec![Object::Integer(1), Object::Integer(5)],
|
||||||
})),
|
})),
|
||||||
),
|
),
|
||||||
|
("len({\"foo\": \"bar\"})", Some(Object::Integer(1))),
|
||||||
];
|
];
|
||||||
|
|
||||||
run_test_cases(&test_cases);
|
run_test_cases(&test_cases);
|
||||||
|
|
|
@ -318,10 +318,14 @@ impl TreeWalker {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eval_hash_index_expression(&self, left: &HashObject, index: Object) -> Option<Object> {
|
fn eval_hash_index_expression(&self, left: &HashObject, index: Object) -> Option<Object> {
|
||||||
Some(Object::Error(format!(
|
if let Object::Function(_) = index {
|
||||||
"index operator not supported: {:?}",
|
return Some(Object::Error(format!("unusable as hash key: {}", index)));
|
||||||
left
|
}
|
||||||
)))
|
|
||||||
|
match left.pairs.get(&index) {
|
||||||
|
Some(v) => Some(v.clone()),
|
||||||
|
None => Some(Object::Null),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fn eval_array_index_expression(array: &Array, index: i64) -> Object {
|
fn eval_array_index_expression(array: &Array, index: i64) -> Object {
|
||||||
let max = array.elements.len() as i64;
|
let max = array.elements.len() as i64;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user