Minor cleanup.

Functional Programming FTW!
This commit is contained in:
Simon Forman 2022-09-28 21:56:53 -07:00
parent 61bcf3588a
commit abddda47da
1 changed files with 22 additions and 22 deletions

View File

@ -68,34 +68,34 @@ type joy_dict = string -> joy_list
exception StackUnderflow of string exception StackUnderflow of string
exception ValueError of string exception ValueError of string
let pop_int : joy_list -> int * joy_list = let pop_item : joy_list -> joy_type * joy_list =
fun stack -> fun stack ->
match stack with match stack with
| [] -> raise (StackUnderflow "Not enough values on stack.") | [] -> raise (StackUnderflow "Not enough values on stack.")
| head :: tail -> ( | head :: tail -> (head, tail)
match head with
| JoyInt i -> (i, tail)
| _ -> raise (ValueError "Not an integer."))
let pop_list : joy_list -> joy_list * joy_list = let is_int : joy_type -> int =
fun stack -> fun jt ->
match stack with match jt with JoyInt i -> i | _ -> raise (ValueError "Not an integer.")
| [] -> raise (StackUnderflow "Not enough values on stack.")
| head :: tail -> (
match head with
| JoyList el -> (el, tail)
| _ -> raise (ValueError "Not a list."))
let pop_bool : joy_list -> bool * joy_list = let is_list : joy_type -> joy_list =
fun stack -> fun jt ->
match stack with match jt with JoyList el -> el | _ -> raise (ValueError "Not a list.")
| [] -> raise (StackUnderflow "Not enough values on stack.")
| head :: tail -> (
match head with
| JoyTrue -> (true, tail)
| JoyFalse -> (false, tail)
| _ -> raise (ValueError "Not a Boolean value."))
let is_bool : joy_type -> bool =
fun jt ->
match jt with
| JoyTrue -> true
| JoyFalse -> false
| _ -> raise (ValueError "Not a Boolean value.")
let pop_thing func stack =
let jt, stack = pop_item stack in
(func jt, stack)
let pop_int : joy_list -> int * joy_list = pop_thing is_int
let pop_list : joy_list -> joy_list * joy_list = pop_thing is_list
let pop_bool : joy_list -> bool * joy_list = pop_thing is_bool
let push_bool b stack = if b then JoyTrue :: stack else JoyFalse :: stack let push_bool b stack = if b then JoyTrue :: stack else JoyFalse :: stack
(* (*