Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forth: Expect a list rather than a string or Text #412

Merged
merged 2 commits into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions exercises/forth/HINTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ and implement the following functions:

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

You will find the type signatures already in place, but it is up to you
to define the functions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Forth
( ForthError(..)
, ForthState
, evalText
, formatStack
, toList
, empty
) where

Expand Down Expand Up @@ -147,5 +147,5 @@ stepWord (User xs) = mapCode (xs ++)
evalText :: Text -> ForthState -> Either ForthError ForthState
evalText = eval . parseText

formatStack :: ForthState -> Text
formatStack = T.pack . unwords . map show . reverse . forthStack
toList :: ForthState -> [Int]
toList = reverse . forthStack
19 changes: 19 additions & 0 deletions exercises/forth/examples/success-standard/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: forth

dependencies:
- base
- text

library:
exposed-modules: Forth
source-dirs: src
dependencies:
- containers

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great...you even remembered to remove the comments. :)

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- forth
- hspec
1 change: 0 additions & 1 deletion exercises/forth/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ library:
dependencies:
# - foo # List here the packages you
# - bar # want to use in your solution.
- containers

tests:
test:
Expand Down
6 changes: 3 additions & 3 deletions exercises/forth/src/Forth.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Forth
( ForthError(..)
, ForthState
, evalText
, formatStack
, toList
, empty
) where

Expand All @@ -23,5 +23,5 @@ empty = undefined
evalText :: Text -> ForthState -> Either ForthError ForthState
evalText = undefined

formatStack :: ForthState -> Text
formatStack = undefined
toList :: ForthState -> [Int]
toList = undefined
38 changes: 19 additions & 19 deletions exercises/forth/test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Control.Monad (foldM)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import Forth (ForthError(..), empty, evalText, formatStack)
import Forth (ForthError(..), empty, evalText, toList)

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

let runTexts = fmap formatStack . foldM (flip evalText) empty
let runTexts = fmap toList . foldM (flip evalText) empty

it "no input, no stack" $
formatStack empty `shouldBe` ""
toList empty `shouldBe` []

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

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

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

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

it "dup" $ do
runTexts ["1 DUP" ] `shouldBe` Right "1 1"
runTexts ["1 2 Dup"] `shouldBe` Right "1 2 2"
runTexts ["1 DUP" ] `shouldBe` Right [1, 1]
runTexts ["1 2 Dup"] `shouldBe` Right [1, 2, 2]
runTexts ["dup" ] `shouldBe` Left StackUnderflow

it "drop" $ do
runTexts ["1 drop" ] `shouldBe` Right ""
runTexts ["1 2 drop"] `shouldBe` Right "1"
runTexts ["1 drop" ] `shouldBe` Right []
runTexts ["1 2 drop"] `shouldBe` Right [1]
runTexts ["drop" ] `shouldBe` Left StackUnderflow

it "swap" $ do
runTexts ["1 2 swap" ] `shouldBe` Right "2 1"
runTexts ["1 2 3 swap"] `shouldBe` Right "1 3 2"
runTexts ["1 2 swap" ] `shouldBe` Right [2, 1]
runTexts ["1 2 3 swap"] `shouldBe` Right [1, 3, 2]
runTexts ["1 swap" ] `shouldBe` Left StackUnderflow
runTexts ["swap" ] `shouldBe` Left StackUnderflow

it "over" $ do
runTexts ["1 2 over" ] `shouldBe` Right "1 2 1"
runTexts ["1 2 3 over"] `shouldBe` Right "1 2 3 2"
runTexts ["1 2 over" ] `shouldBe` Right [1, 2, 1]
runTexts ["1 2 3 over"] `shouldBe` Right [1, 2, 3, 2]
runTexts ["1 over" ] `shouldBe` Left StackUnderflow
runTexts ["over" ] `shouldBe` Left StackUnderflow

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

it "redefining an existing word" $
runTexts [ ": foo dup ;"
, ": foo dup dup ;"
, "1 foo" ] `shouldBe` Right "1 1 1"
, "1 foo" ] `shouldBe` Right [1, 1, 1]

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

it "defining words with odd characters" $
runTexts [": € 220371 ; €"] `shouldBe` Right "220371"
runTexts [": € 220371 ; €"] `shouldBe` Right [220371]

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