Skip to content

Commit 27b9f71

Browse files
authored
Merge pull request #412 from petertseng/forth
forth: Expect a list rather than a string or Text
2 parents 82ae062 + 9927fc6 commit 27b9f71

File tree

6 files changed

+46
-28
lines changed

6 files changed

+46
-28
lines changed

exercises/forth/HINTS.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ and implement the following functions:
55

66
- `empty` returns an empty `ForthState`.
77
- `evalText` evaluates an input Text, returning the new state.
8-
- `formatStack` returns the current stack as Text, with the element on top
9-
of the stack being the rightmost element in the output
8+
- `toList` returns the current stack as a list, with the element on top
9+
of the stack being the rightmost (last) element.
1010

1111
You will find the type signatures already in place, but it is up to you
1212
to define the functions.

exercises/forth/src/Example.hs exercises/forth/examples/success-standard/Forth.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Forth
33
( ForthError(..)
44
, ForthState
55
, evalText
6-
, formatStack
6+
, toList
77
, empty
88
) where
99

@@ -147,5 +147,5 @@ stepWord (User xs) = mapCode (xs ++)
147147
evalText :: Text -> ForthState -> Either ForthError ForthState
148148
evalText = eval . parseText
149149

150-
formatStack :: ForthState -> Text
151-
formatStack = T.pack . unwords . map show . reverse . forthStack
150+
toList :: ForthState -> [Int]
151+
toList = reverse . forthStack
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: forth
2+
3+
dependencies:
4+
- base
5+
- text
6+
7+
library:
8+
exposed-modules: Forth
9+
source-dirs: src
10+
dependencies:
11+
- containers
12+
13+
tests:
14+
test:
15+
main: Tests.hs
16+
source-dirs: test
17+
dependencies:
18+
- forth
19+
- hspec

exercises/forth/package.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ library:
1010
dependencies:
1111
# - foo # List here the packages you
1212
# - bar # want to use in your solution.
13-
- containers
1413

1514
tests:
1615
test:

exercises/forth/src/Forth.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Forth
44
( ForthError(..)
55
, ForthState
66
, evalText
7-
, formatStack
7+
, toList
88
, empty
99
) where
1010

@@ -23,5 +23,5 @@ empty = undefined
2323
evalText :: Text -> ForthState -> Either ForthError ForthState
2424
evalText = undefined
2525

26-
formatStack :: ForthState -> Text
27-
formatStack = undefined
26+
toList :: ForthState -> [Int]
27+
toList = undefined

exercises/forth/test/Tests.hs

+19-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Control.Monad (foldM)
44
import Test.Hspec (Spec, describe, it, shouldBe)
55
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
66

7-
import Forth (ForthError(..), empty, evalText, formatStack)
7+
import Forth (ForthError(..), empty, evalText, toList)
88

99
main :: IO ()
1010
main = hspecWith defaultConfig {configFastFail = True} specs
@@ -15,61 +15,61 @@ specs = describe "forth" $ do
1515
-- As of 2016-10-02, there was no reference file
1616
-- for the test cases in `exercism/x-common`.
1717

18-
let runTexts = fmap formatStack . foldM (flip evalText) empty
18+
let runTexts = fmap toList . foldM (flip evalText) empty
1919

2020
it "no input, no stack" $
21-
formatStack empty `shouldBe` ""
21+
toList empty `shouldBe` []
2222

2323
it "numbers just get pushed onto the stack" $
24-
runTexts ["1 2 3 4 5"] `shouldBe` Right "1 2 3 4 5"
24+
runTexts ["1 2 3 4 5"] `shouldBe` Right [1, 2, 3, 4, 5]
2525

2626
it "non-word characters are separators" $
27-
runTexts ["1\NUL2\SOH3\n4\r5 6\t7"] `shouldBe` Right "1 2 3 4 5 6 7"
27+
runTexts ["1\NUL2\SOH3\n4\r5 6\t7"] `shouldBe` Right [1, 2, 3, 4, 5, 6, 7]
2828

2929
it "basic arithmetic" $ do
30-
runTexts ["1 2 + 4 -"] `shouldBe` Right "-1"
31-
runTexts ["2 4 * 3 /"] `shouldBe` Right "2"
30+
runTexts ["1 2 + 4 -"] `shouldBe` Right [-1]
31+
runTexts ["2 4 * 3 /"] `shouldBe` Right [2]
3232

3333
it "division by zero" $
3434
runTexts ["4 2 2 - /"] `shouldBe` Left DivisionByZero
3535

3636
it "dup" $ do
37-
runTexts ["1 DUP" ] `shouldBe` Right "1 1"
38-
runTexts ["1 2 Dup"] `shouldBe` Right "1 2 2"
37+
runTexts ["1 DUP" ] `shouldBe` Right [1, 1]
38+
runTexts ["1 2 Dup"] `shouldBe` Right [1, 2, 2]
3939
runTexts ["dup" ] `shouldBe` Left StackUnderflow
4040

4141
it "drop" $ do
42-
runTexts ["1 drop" ] `shouldBe` Right ""
43-
runTexts ["1 2 drop"] `shouldBe` Right "1"
42+
runTexts ["1 drop" ] `shouldBe` Right []
43+
runTexts ["1 2 drop"] `shouldBe` Right [1]
4444
runTexts ["drop" ] `shouldBe` Left StackUnderflow
4545

4646
it "swap" $ do
47-
runTexts ["1 2 swap" ] `shouldBe` Right "2 1"
48-
runTexts ["1 2 3 swap"] `shouldBe` Right "1 3 2"
47+
runTexts ["1 2 swap" ] `shouldBe` Right [2, 1]
48+
runTexts ["1 2 3 swap"] `shouldBe` Right [1, 3, 2]
4949
runTexts ["1 swap" ] `shouldBe` Left StackUnderflow
5050
runTexts ["swap" ] `shouldBe` Left StackUnderflow
5151

5252
it "over" $ do
53-
runTexts ["1 2 over" ] `shouldBe` Right "1 2 1"
54-
runTexts ["1 2 3 over"] `shouldBe` Right "1 2 3 2"
53+
runTexts ["1 2 over" ] `shouldBe` Right [1, 2, 1]
54+
runTexts ["1 2 3 over"] `shouldBe` Right [1, 2, 3, 2]
5555
runTexts ["1 over" ] `shouldBe` Left StackUnderflow
5656
runTexts ["over" ] `shouldBe` Left StackUnderflow
5757

5858
it "defining a new word" $
5959
runTexts [ ": dup-twice dup dup ;"
60-
, "1 dup-twice" ] `shouldBe` Right "1 1 1"
60+
, "1 dup-twice" ] `shouldBe` Right [1, 1, 1]
6161

6262
it "redefining an existing word" $
6363
runTexts [ ": foo dup ;"
6464
, ": foo dup dup ;"
65-
, "1 foo" ] `shouldBe` Right "1 1 1"
65+
, "1 foo" ] `shouldBe` Right [1, 1, 1]
6666

6767
it "redefining an existing built-in word" $
6868
runTexts [ ": swap dup ;"
69-
, "1 swap" ] `shouldBe` Right "1 1"
69+
, "1 swap" ] `shouldBe` Right [1, 1]
7070

7171
it "defining words with odd characters" $
72-
runTexts [": € 220371 ; €"] `shouldBe` Right "220371"
72+
runTexts [": € 220371 ; €"] `shouldBe` Right [220371]
7373

7474
it "defining a number" $
7575
runTexts [": 1 2 ;"] `shouldBe` Left InvalidWord

0 commit comments

Comments
 (0)