Browse Source

updates

pull/5/head
Jason Williams 6 years ago
parent
commit
8f74c5428c
  1. 1
      src/bin/bin.rs
  2. 25
      src/lib/exec.rs
  3. 5
      tests/js/test.js

1
src/bin/bin.rs

@ -12,6 +12,7 @@ pub fn main() {
// Setup executor
let expr = Parser::new(tokens).parse_all().unwrap();
print!("{:#?}", expr);
let mut engine: Interpreter = Executor::new();
let result = engine.run(&expr);

25
src/lib/exec.rs

@ -101,7 +101,7 @@ impl Executor for Interpreter {
ExprDef::BlockExpr(ref es) => {
let mut obj = to_value(None::<()>);
for e in es.iter() {
let val = try!(self.run(e));
let val = self.run(e)?;
if e == es.last().unwrap() {
obj = val;
}
@ -127,39 +127,40 @@ impl Executor for Interpreter {
Ok(val)
}
ExprDef::GetConstFieldExpr(ref obj, ref field) => {
let val_obj = try!(self.run(obj));
let val_obj = self.run(obj)?;
println!("{:?}", val_obj.get_field(String::from("length")));
Ok(val_obj.borrow().get_field(field.clone()))
}
ExprDef::GetFieldExpr(ref obj, ref field) => {
let val_obj = try!(self.run(obj));
let val_field = try!(self.run(field));
let val_obj = self.run(obj)?;
let val_field = self.run(field)?;
Ok(val_obj.borrow().get_field(val_field.borrow().to_string()))
}
ExprDef::CallExpr(ref callee, ref args) => {
let (this, func) = match callee.def {
ExprDef::GetConstFieldExpr(ref obj, ref field) => {
let obj = try!(self.run(obj));
let obj = self.run(obj)?;
(obj.clone(), obj.borrow().get_field(field.clone()))
}
ExprDef::GetFieldExpr(ref obj, ref field) => {
let obj = try!(self.run(obj));
let field = try!(self.run(field));
let obj = self.run(obj)?;
let field = self.run(field)?;
(
obj.clone(),
obj.borrow().get_field(field.borrow().to_string()),
)
}
_ => (self.global.clone(), try!(self.run(&callee.clone()))),
_ => (self.global.clone(), self.run(&callee.clone())?),
};
let mut v_args = Vec::with_capacity(args.len());
for arg in args.iter() {
v_args.push(try!(self.run(arg)));
v_args.push(self.run(arg)?);
}
match *func {
ValueData::Function(ref func) => match *func.borrow() {
Function::NativeFunc(ref ntv) => {
let func = ntv.data;
func(this, try!(self.run(callee)), v_args)
func(this, self.run(callee)?, v_args)
}
Function::RegularFunc(ref data) => {
let scope = self.make_scope(this);
@ -179,8 +180,8 @@ impl Executor for Interpreter {
}
ExprDef::WhileLoopExpr(ref cond, ref expr) => {
let mut result = Gc::new(ValueData::Undefined);
while try!(self.run(cond)).borrow().is_true() {
result = try!(self.run(expr));
while self.run(cond)?.borrow().is_true() {
result = self.run(expr)?;
}
Ok(result)
}

5
tests/js/test.js

@ -1,3 +1,2 @@
var a = "jason";
var b = 1;
a + b;
var a = new String("jason");
a;
Loading…
Cancel
Save