Skip to content

Commit

Permalink
Use raw ints in stack repr
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazhafiz committed Feb 19, 2023
1 parent 56eb3aa commit 330a855
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
25 changes: 9 additions & 16 deletions co_lc/vm_fiber.ml
Expand Up @@ -7,15 +7,11 @@
stack representation is typed, when a production implementation likely would
not implement it as such. *)

exception Bad_stack of string

let invalid_state s = raise (Bad_stack s)

type value = [ `Int of int ] [@@deriving show]
type value = int [@@deriving show]
type word = value [@@deriving show]
type block = word Array.t

let debug_word = `Int 0xAAAAAAAA
let debug_word = 0xAAAAAAAA
let empty_block = Array.make 0 debug_word
let vals_of_block words = Array.to_list words

Expand Down Expand Up @@ -93,28 +89,25 @@ let make ~ret ~arg =
Stack.extend stack (Array.make ret debug_word);
let top = Stack.len stack in
Stack.extend stack arg;
Stack.push stack (`Int 0);
Stack.push stack (`Int 0);
Stack.push stack (`Int top);
Stack.push stack 0;
Stack.push stack 0;
Stack.push stack top;
let fp = Stack.len stack in
{ stack; fp = ref fp; top = ref top }

let pop { stack; _ } = Stack.pop stack
let pop_int fiber = match pop fiber with `Int n -> n
let pop_int fiber = match pop fiber with n -> n
let pop_block { stack; _ } block_size = Stack.splice_off stack block_size

let in_place_int { stack; _ } f =
let modifier = function
| `Int n -> `Int (f n)
| _ -> invalid_state "not an int"
in
let modifier = function n -> f n in
Stack.modify_top stack modifier

let push_int { stack; _ } n = Stack.push stack (`Int n)
let push_int { stack; _ } n = Stack.push stack n
let push_block { stack; _ } block = Stack.extend stack block

let push_zeroed fiber n =
let block = Array.make n (`Int 0) in
let block = Array.make n 0 in
push_block fiber block

let push fiber = function
Expand Down
2 changes: 1 addition & 1 deletion co_lc/vm_fiber.mli
Expand Up @@ -9,7 +9,7 @@ type block
val empty_block : block
(** Zero-sized [block]. *)

type value = [ `Int of int ]
type value = int

val show_value : value -> string
val vals_of_block : block -> value list
Expand Down
6 changes: 3 additions & 3 deletions co_lc/vm_readback.ml
Expand Up @@ -6,7 +6,7 @@ let show_value = Vm_fiber.show_value

exception Bad_value of string

let expect_int l = match List.hd l with `Int n -> (n, List.tl l)
let expect_int l = match List.hd l with n -> (n, List.tl l)
let noloc = Ast.noloc
let noty = ref (Ast.Unbd (-1))

Expand Down Expand Up @@ -46,7 +46,7 @@ let rec build_ast symbols vals ty =
let lambda, captures =
if List.length lambda_set = 1 then List.hd lambda_set
else
let (`Int bit) = List.nth vals (stksize - 1) in
let bit = List.nth vals (stksize - 1) in
List.nth lambda_set bit
in

Expand All @@ -66,7 +66,7 @@ let rec build_ast symbols vals ty =
let t_s = string_of_ty symbols t in

let items =
if List.nth vals (size - 1) = `Int 1 then
if List.nth vals (size - 1) = 1 then
let completed, _ = build_ast symbols (drop_n vals 2) t in
App ((noloc, noty, Var (`Sym "`Done")), completed)
else Var (`Sym "`Pending")
Expand Down

0 comments on commit 330a855

Please sign in to comment.