# Forth in F#

#### Implement an evaluator for a very simple subset of Forth

1 | ```
exercism fetch fsharp forth
``` |

#### ForthTest.fs

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | // This file was auto-generated based on version 1.5.0 of the canonical data. module ForthTest open FsUnit.Xunit open Xunit open Forth [<Fact>] let ``Parsing and numbers - numbers just get pushed onto the stack`` () = let expected = Some [1; 2; 3; 4; 5] evaluate ["1 2 3 4 5"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Addition - can add two numbers`` () = let expected = Some [3] evaluate ["1 2 +"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Addition - errors if there is nothing on the stack`` () = let expected = None evaluate ["+"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Addition - errors if there is only one value on the stack`` () = let expected = None evaluate ["1 +"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Subtraction - can subtract two numbers`` () = let expected = Some [-1] evaluate ["3 4 -"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Subtraction - errors if there is nothing on the stack`` () = let expected = None evaluate ["-"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Subtraction - errors if there is only one value on the stack`` () = let expected = None evaluate ["1 -"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Multiplication - can multiply two numbers`` () = let expected = Some [8] evaluate ["2 4 *"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Multiplication - errors if there is nothing on the stack`` () = let expected = None evaluate ["*"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Multiplication - errors if there is only one value on the stack`` () = let expected = None evaluate ["1 *"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Division - can divide two numbers`` () = let expected = Some [4] evaluate ["12 3 /"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Division - performs integer division`` () = let expected = Some [2] evaluate ["8 3 /"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Division - errors if dividing by zero`` () = let expected = None evaluate ["4 0 /"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Division - errors if there is nothing on the stack`` () = let expected = None evaluate ["/"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Division - errors if there is only one value on the stack`` () = let expected = None evaluate ["1 /"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Combined arithmetic - addition and subtraction`` () = let expected = Some [-1] evaluate ["1 2 + 4 -"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Combined arithmetic - multiplication and division`` () = let expected = Some [2] evaluate ["2 4 * 3 /"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Dup - copies a value on the stack`` () = let expected = Some [1; 1] evaluate ["1 dup"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Dup - copies the top value on the stack`` () = let expected = Some [1; 2; 2] evaluate ["1 2 dup"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Dup - errors if there is nothing on the stack`` () = let expected = None evaluate ["dup"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Drop - removes the top value on the stack if it is the only one`` () = let expected: int list option = Some [] evaluate ["1 drop"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Drop - removes the top value on the stack if it is not the only one`` () = let expected = Some [1] evaluate ["1 2 drop"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Drop - errors if there is nothing on the stack`` () = let expected = None evaluate ["drop"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Swap - swaps the top two values on the stack if they are the only ones`` () = let expected = Some [2; 1] evaluate ["1 2 swap"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Swap - swaps the top two values on the stack if they are not the only ones`` () = let expected = Some [1; 3; 2] evaluate ["1 2 3 swap"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Swap - errors if there is nothing on the stack`` () = let expected = None evaluate ["swap"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Swap - errors if there is only one value on the stack`` () = let expected = None evaluate ["1 swap"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Over - copies the second element if there are only two`` () = let expected = Some [1; 2; 1] evaluate ["1 2 over"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Over - copies the second element if there are more than two`` () = let expected = Some [1; 2; 3; 2] evaluate ["1 2 3 over"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Over - errors if there is nothing on the stack`` () = let expected = None evaluate ["over"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Over - errors if there is only one value on the stack`` () = let expected = None evaluate ["1 over"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``User-defined words - can consist of built-in words`` () = let expected = Some [1; 1; 1] evaluate [": dup-twice dup dup ;"; "1 dup-twice"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``User-defined words - execute in the right order`` () = let expected = Some [1; 2; 3] evaluate [": countup 1 2 3 ;"; "countup"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``User-defined words - can override other user-defined words`` () = let expected = Some [1; 1; 1] evaluate [": foo dup ;"; ": foo dup dup ;"; "1 foo"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``User-defined words - can override built-in words`` () = let expected = Some [1; 1] evaluate [": swap dup ;"; "1 swap"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``User-defined words - can override built-in operators`` () = let expected = Some [12] evaluate [": + * ;"; "3 4 +"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``User-defined words - cannot redefine numbers`` () = let expected = None evaluate [": 1 2 ;"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``User-defined words - errors if executing a non-existent word`` () = let expected = None evaluate ["foo"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Case-insensitivity - DUP is case-insensitive`` () = let expected = Some [1; 1; 1; 1] evaluate ["1 DUP Dup dup"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Case-insensitivity - DROP is case-insensitive`` () = let expected = Some [1] evaluate ["1 2 3 4 DROP Drop drop"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Case-insensitivity - SWAP is case-insensitive`` () = let expected = Some [2; 3; 4; 1] evaluate ["1 2 SWAP 3 Swap 4 swap"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Case-insensitivity - OVER is case-insensitive`` () = let expected = Some [1; 2; 1; 2; 1] evaluate ["1 2 OVER Over over"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Case-insensitivity - user-defined words are case-insensitive`` () = let expected = Some [1; 1; 1; 1] evaluate [": foo dup ;"; "1 FOO Foo foo"] |> should equal expected [<Fact(Skip = "Remove to run test")>] let ``Case-insensitivity - definitions are case-insensitive`` () = let expected = Some [1; 1; 1; 1] evaluate [": SWAP DUP Dup dup ;"; "1 swap"] |> should equal expected |