Skip to content

Commit 51ede5b

Browse files
committed
Sketch more of interpreter loop
1 parent e708916 commit 51ede5b

File tree

4 files changed

+55
-71
lines changed

4 files changed

+55
-71
lines changed

src/ast.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,15 @@ pub struct Unit
331331
// Unit-level (top level) function
332332
pub unit_fn: Function,
333333
}
334+
335+
336+
337+
/// Represents an entire program containing one or more units
338+
#[derive(Clone, Debug)]
339+
pub struct Program
340+
{
341+
342+
343+
344+
pub main_fn: FunId,
345+
}

src/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,7 @@ mod tests
13951395
{
13961396
// Make sure that we can parse our test and example files
13971397
parse_file("tests/empty.pls");
1398+
parse_file("tests/fact.pls");
13981399
parse_file("examples/helloworld.pls");
13991400
parse_file("examples/fib.pls");
14001401
}

src/vm.rs

Lines changed: 31 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ pub enum Insn
8383
// Logical negation
8484
not,
8585

86-
// Get value type
87-
type_of,
86+
// Type check operations
87+
is_nil,
88+
is_int64,
89+
is_object,
90+
is_array,
8891

8992
/*
9093
// Objects manipulation
@@ -379,7 +382,7 @@ impl Actor
379382
assert!(self.stack.len() == 0);
380383
assert!(self.frames.len() == 0);
381384

382-
// TODO: figure out how fns should be referred to?
385+
// TODO: do we need a closure value here?
383386
//let mut fun = fun.unwrap_obj();
384387

385388
// Push a new stack frame
@@ -398,10 +401,17 @@ impl Actor
398401
// The base pointer will point at the first local
399402
let mut bp = self.stack.len();
400403

404+
405+
406+
407+
// FIXME
401408
// Get a compiled address for this function
402409
//let mut pc = self.get_version(fun, 0);
410+
let mut pc = 0;
411+
412+
413+
403414

404-
/*
405415
macro_rules! pop {
406416
() => { self.stack.pop().unwrap() }
407417
}
@@ -413,9 +423,7 @@ impl Actor
413423
macro_rules! push_bool {
414424
($b: expr) => { push!(if $b { True } else { False }) }
415425
}
416-
*/
417426

418-
/*
419427
loop
420428
{
421429
if pc >= self.insns.len() {
@@ -617,7 +625,7 @@ impl Actor
617625

618626
let b = match (v0, v1) {
619627
(Int64(v0), Int64(v1)) => v0 == v1,
620-
(Value::String(p0), Value::String(p1)) => p0 == p1,
628+
//(Value::String(p0), Value::String(p1)) => p0 == p1,
621629
_ => panic!()
622630
};
623631

@@ -630,7 +638,7 @@ impl Actor
630638

631639
let b = match (v0, v1) {
632640
(Int64(v0), Int64(v1)) => v0 != v1,
633-
(Value::String(p0), Value::String(p1)) => p0 != p1,
641+
//(Value::String(p0), Value::String(p1)) => p0 != p1,
634642
_ => panic!()
635643
};
636644

@@ -650,31 +658,7 @@ impl Actor
650658
push!(b);
651659
}
652660

653-
// Get value type
654-
Insn::type_of => {
655-
let v0 = pop!();
656-
657-
let s = match v0 {
658-
Value::None => "none",
659-
660-
Value::True => "bool",
661-
Value::False => "bool",
662-
663-
Value::Int64(_) => "int64",
664-
Value::String(_) => "string",
665-
666-
Value::Object(_) => "object",
667-
Value::Array(_) => "array",
668-
669-
_ => panic!()
670-
};
671-
672-
// FIXME: locking here is slow/inefficient
673-
// We ideally want to cache the type strings somewhere
674-
let s = self.alloc.get_string(s);
675-
push!(s);
676-
}
677-
661+
/*
678662
// Create new empty object
679663
Insn::obj_new => {
680664
let new_obj = Object::new(&mut self.alloc);
@@ -714,7 +698,9 @@ impl Actor
714698
let obj = pop!().unwrap_obj();
715699
Object::seal(obj);
716700
}
701+
*/
717702

703+
/*
718704
// Create new empty array
719705
Insn::arr_new { capacity } => {
720706
let new_arr = Array::new(
@@ -773,7 +759,9 @@ impl Actor
773759
let arr = pop!().unwrap_arr();
774760
Array::freeze(arr);
775761
}
762+
*/
776763

764+
/*
777765
// Create new empty bytearray
778766
Insn::ba_new { capacity } => {
779767
let new_arr = ByteArray::new(
@@ -798,22 +786,9 @@ impl Actor
798786
let arr = pop!().unwrap_ba();
799787
ByteArray::write_u32(arr, idx, val);
800788
}
789+
*/
801790

802791
// Jump if true
803-
Insn::if_true_stub { target_idx } => {
804-
let v = pop!();
805-
806-
match v {
807-
Value::True => {
808-
let target_pc = self.get_version(fun, target_idx);
809-
self.insns[pc - 1] = Insn::if_true { target_pc };
810-
pc = target_pc;
811-
}
812-
Value::False => {},
813-
_ => panic!()
814-
}
815-
}
816-
817792
Insn::if_true { target_pc } => {
818793
let v = pop!();
819794

@@ -825,20 +800,6 @@ impl Actor
825800
}
826801

827802
// Jump if false
828-
Insn::if_false_stub { target_idx } => {
829-
let v = pop!();
830-
831-
match v {
832-
Value::False => {
833-
let target_pc = self.get_version(fun, target_idx);
834-
self.insns[pc - 1] = Insn::if_false { target_pc };
835-
pc = target_pc;
836-
}
837-
Value::True => {},
838-
_ => panic!()
839-
}
840-
}
841-
842803
Insn::if_false { target_pc } => {
843804
let v = pop!();
844805

@@ -850,16 +811,11 @@ impl Actor
850811
}
851812

852813
// Unconditional jump
853-
Insn::jump_stub{ target_idx } => {
854-
let target_pc = self.get_version(fun, target_idx);
855-
self.insns[pc - 1] = Insn::jump { target_pc };
856-
pc = target_pc;
857-
}
858-
859814
Insn::jump { target_pc } => {
860815
pc = target_pc;
861816
}
862817

818+
/*
863819
Insn::call_host { host_fn, argc } => {
864820
if host_fn.num_params() != (argc as usize) {
865821
panic!();
@@ -938,7 +894,9 @@ impl Actor
938894
}
939895
}
940896
}
897+
*/
941898

899+
/*
942900
// call (arg0, arg1, ..., argN, fun)
943901
Insn::call { argc } => {
944902
// Function to call
@@ -961,7 +919,9 @@ impl Actor
961919
// Get a compiled address for this function
962920
pc = self.get_version(fun, 0);
963921
}
922+
*/
964923

924+
/*
965925
// call (arg0, arg1, ..., argN, fun)
966926
Insn::call_known { argc, callee } => {
967927
// Get a compiled address for this function
@@ -973,7 +933,9 @@ impl Actor
973933
// Executed the patched instruction next
974934
pc -= 1;
975935
}
936+
*/
976937

938+
/*
977939
// call (arg0, arg1, ..., argN, fun)
978940
Insn::call_pc { argc, callee, target_pc } => {
979941
// Function being currently executed
@@ -995,6 +957,7 @@ impl Actor
995957
// Get a compiled address for this function
996958
pc = target_pc;
997959
}
960+
*/
998961

999962
Insn::ret => {
1000963
if self.stack.len() <= bp {
@@ -1022,17 +985,14 @@ impl Actor
1022985

1023986
pc = top_frame.ret_addr;
1024987
bp = top_frame.prev_bp;
1025-
fun = self.frames[self.frames.len()-1].fun;
988+
//fun = self.frames[self.frames.len()-1].fun;
1026989

1027990
push!(ret_val);
1028991
}
1029992

1030993
_ => panic!("unknown opcode {:?}", insn)
1031994
}
1032995
}
1033-
*/
1034-
1035-
todo!();
1036996
}
1037997
}
1038998

tests/fact.pls

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fun fact(n)
2+
{
3+
if (n <= 2)
4+
return n;
5+
6+
return n * fact(n-1);
7+
}
8+
9+
//assert(fact(1) == 1);
10+
//assert(fact(2) == 2);
11+
assert(fact(3) == 6);

0 commit comments

Comments
 (0)