Implement an evaluator for a very simple subset of Forth.
Forth is a stack-based programming language. Implement a very basic evaluator for a small subset of Forth.
Your evaluator has to support the following words:
+
, -
, *
, /
(integer arithmetic)DUP
, DROP
, SWAP
, OVER
(stack manipulation)Your evaluator also has to support defining new words using the
customary syntax: : word-name definition ;
.
To keep things simple the only data type you need to support is signed integers of at least 16 bits size.
You should use the following rules for the syntax: a number is a sequence of one or more (ASCII) digits, a word is a sequence of one or more letters, digits, symbols or punctuation that is not a number. (Forth probably uses slightly different rules, but this is close enough.)
Words are case-insensitive.
For installation and learning resources, refer to the exercism help page.
To work on the exercises, you will need Opam
and Base
. Consult opam website for instructions on how to install opam
for your OS. Once opam
is installed open a terminal window and run the following command to install base:
opam install base
To run the tests you will need OUnit
. Install it using opam
:
opam install ounit
A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:
make
utop
is a command line program which allows you to run Ocaml code interactively. The easiest way to install it is via opam:
opam install utop
Consult utop for more detail.
The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.
If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
open Base
open OUnit2
open Forth
let print_int_list_option (xs: int list option) = match xs with
| None -> "None"
| Some xs -> "Some [" ^ String.concat ~sep:";" (List.map ~f:Int.to_string xs) ^ "]"
let ae exp got _test_ctxt = assert_equal ~printer:print_int_list_option exp got
let parsing_and_numbers_tests = [
"numbers just get pushed onto the stack" >::
ae (Some [1; 2; 3; 4; 5]) (evaluate ["1 2 3 4 5"]);
]
let addition_tests = [
"can add two numbers" >::
ae (Some [3]) (evaluate ["1 2 +"]);
"errors if there is nothing on the stack" >::
ae None (evaluate ["+"]);
"errors if there is only one value on the stack" >::
ae None (evaluate ["1 +"]);
]
let subtraction_tests = [
"can subtract two numbers" >::
ae (Some [-1]) (evaluate ["3 4 -"]);
"errors if there is nothing on the stack" >::
ae None (evaluate ["-"]);
"errors if there is only one value on the stack" >::
ae None (evaluate ["1 -"]);
]
let multiplication_tests = [
"can multiply two numbers" >::
ae (Some [8]) (evaluate ["2 4 *"]);
"errors if there is nothing on the stack" >::
ae None (evaluate ["*"]);
"errors if there is only one value on the stack" >::
ae None (evaluate ["1 *"]);
]
let division_tests = [
"can divide two numbers" >::
ae (Some [4]) (evaluate ["12 3 /"]);
"performs integer division" >::
ae (Some [2]) (evaluate ["8 3 /"]);
"errors if dividing by zero" >::
ae None (evaluate ["4 0 /"]);
"errors if there is nothing on the stack" >::
ae None (evaluate ["/"]);
"errors if there is only one value on the stack" >::
ae None (evaluate ["1 /"]);
]
let combined_arithmetic_tests = [
"addition and subtraction" >::
ae (Some [-1]) (evaluate ["1 2 + 4 -"]);
"multiplication and division" >::
ae (Some [2]) (evaluate ["2 4 * 3 /"]);
]
let dup_tests = [
"copies the top value on the stack" >::
ae (Some [1; 1]) (evaluate ["1 DUP"]);
"is case-insensitive" >::
ae (Some [1; 2; 2]) (evaluate ["1 2 Dup"]);
"errors if there is nothing on the stack" >::
ae None (evaluate ["dup"]);
]
let drop_tests = [
"removes the top value on the stack if it is the only one" >::
ae (Some []) (evaluate ["1 drop"]);
"removes the top value on the stack if it is not the only one" >::
ae (Some [1]) (evaluate ["1 2 drop"]);
"errors if there is nothing on the stack" >::
ae None (evaluate ["drop"]);
]
let swap_tests = [
"swaps the top two values on the stack if they are the only ones" >::
ae (Some [2; 1]) (evaluate ["1 2 swap"]);
"swaps the top two values on the stack if they are not the only ones" >::
ae (Some [1; 3; 2]) (evaluate ["1 2 3 swap"]);
"errors if there is nothing on the stack" >::
ae None (evaluate ["swap"]);
"errors if there is only one value on the stack" >::
ae None (evaluate ["1 swap"]);
]
let over_tests = [
"copies the second element if there are only two" >::
ae (Some [1; 2; 1]) (evaluate ["1 2 over"]);
"copies the second element if there are more than two" >::
ae (Some [1; 2; 3; 2]) (evaluate ["1 2 3 over"]);
"errors if there is nothing on the stack" >::
ae None (evaluate ["over"]);
"errors if there is only one value on the stack" >::
ae None (evaluate ["1 over"]);
]
let user_defined_words_tests = [
"can consist of built-in words" >::
ae (Some [1; 1; 1]) (evaluate [": dup-twice dup dup ;"; "1 dup-twice"]);
"execute in the right order" >::
ae (Some [1; 2; 3]) (evaluate [": countup 1 2 3 ;"; "countup"]);
"can override other user-defined words" >::
ae (Some [1; 1; 1]) (evaluate [": foo dup ;"; ": foo dup dup ;"; "1 foo"]);
"can override built-in words" >::
ae (Some [1; 1]) (evaluate [": swap dup ;"; "1 swap"]);
"can override built-in operators" >::
ae (Some [12]) (evaluate [": + * ;"; "3 4 +"]);
"cannot redefine numbers" >::
ae None (evaluate [": 1 2 ;"]);
"errors if executing a non-existent word" >::
ae None (evaluate ["foo"]);
]
let () =
run_test_tt_main (
"forth tests" >:::
List.concat [
parsing_and_numbers_tests;
addition_tests;
subtraction_tests;
multiplication_tests;
division_tests;
combined_arithmetic_tests;
dup_tests;
drop_tests;
swap_tests;
over_tests;
user_defined_words_tests
]
)
open Base
type operation =
| Push of int
| Add
| Sub
| Mult
| Div
| Dup
| Drop
| Swap
| Over
| Custom of string * operation list
| Custom_invoke of string
| Noop
| Bad_op
module type Stack = sig
type t
val empty : t
val apply_op : t option -> operation -> t option
val add_custom : t -> t option
val invoke_custom : t option -> string -> t option
val get_list : t option -> int list option
end
let (>>=) x f =
match x with
| None -> None
| Some x -> f x
module Stack = struct
type t = {
st: int list;
c: operation list
}
let empty = Some {st=[]; c=[]}
let push ({st=s;_} as stack) i = Some {stack with st=(i::s)}
let pop s =
match s with
| [] -> None
| hd::tl -> Some (hd, tl)
let apply ({st=s;_} as stack) f =
pop s >>= (fun (x, s) ->
pop s >>= (fun (y, s) ->
Some {stack with st=((f y x)::s)}))
let apply_list ({st=s;_} as stack) f =
pop s >>= (fun (x, s) ->
pop s >>= (fun (y, s) ->
Some {stack with st=((f y x)@s)}))
let add s =
apply s (+)
let sub s =
apply s (-)
let mult s =
apply s ( * )
let div s =
try apply s (/)
with Division_by_zero -> None
let dup ({st=s;_} as stack) =
pop s >>= (fun (x, s) -> Some {stack with st=(x::x::s)})
let drop ({st=s;_} as stack) =
if List.length s = 0 then None else Some {stack with st=List.tl_exn s}
let swap s =
apply_list s (fun a b -> [a; b])
let over s =
apply_list s (fun a b -> [a; b; a])
let add_custom ({c=c_l;_} as arg) c =
Some {arg with c=c::c_l}
let find_override name lst =
match (lst |> List.findi ~f:(fun _ x -> match x with
| Custom (n, _) -> String.(=) n name
| _ -> false)) with
| Some (_, Custom (_, ops)) -> Some ops
| _ -> None
let rec invoke_custom stack name =
stack >>= (fun {st=_; c=c_l} ->
match find_override name c_l with
| Some ops -> List.fold ~f:(fun acc x -> acc >>= (fun s -> apply_op (Some s) x)) ~init:stack ops
| _ -> None)
and apply_op stack op =
stack >>=
(fun ({st=_; c=c_l} as s) ->
let f c f =
match find_override c c_l with
| Some _ -> invoke_custom (Some s) c
| _ -> f s
in
match op with
| Add -> f "+" add
| Sub -> f "-" sub
| Mult -> f "*" mult
| Div -> f "/" div
| Dup -> f "dup" dup
| Drop -> f "drop" drop
| Swap -> f "swap" swap
| Over -> f "over" over
| Push x -> push s x
| Noop -> Some s
| Custom (_, _) as c -> add_custom s c
| Custom_invoke name -> invoke_custom (Some s) name
| Bad_op -> None)
let get_list s =
match s with
| None -> None
| Some x -> Some x.st
end
let evaluate ops =
let get_operations tokens =
let is_number n =
n |> String.to_list_rev |> List.fold ~f:(fun acc x -> acc && Char.is_digit x) ~init:true
in
let rec get_ops acc tokens =
match tokens with
| [] -> acc
| hd::tl ->
let (op, rest) =
match hd with
| "+" -> Add, tl
| "-" -> Sub, tl
| "*" -> Mult, tl
| "/" -> Div, tl
| "dup" -> Dup, tl
| "drop" -> Drop, tl
| "swap" -> Swap, tl
| "over" -> Over, tl
| ":" -> (get_custom_ops tl), []
| ";" -> (Noop, [])
| n -> ((if is_number n then Push (Int.of_string n) else Custom_invoke n), tl)
in get_ops (op::acc) rest
and get_custom_ops lst =
match lst with
| [] -> raise (Failure "Syntax error")
| hd::tl -> if is_number hd then Bad_op else Custom (hd, List.rev (get_ops [] tl))
in
tokens |>
List.map ~f:(fun x -> String.lowercase x) |>
get_ops [] |>
List.rev
in
ops |>
List.fold ~f:(fun acc x -> List.append acc (get_operations (String.split ~on:' ' x))) ~init:[] |>
List.fold ~f:(fun acc op -> Stack.apply_op acc op) ~init:Stack.empty |>
Stack.get_list >>= (fun x -> Some (List.rev x))
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,092 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More