hnock

A Nock interpreter.
Log | Files | Refs | README | LICENSE

commit 2e24df5c9e3c4f15e3814220a5a586d5c49d3d0f
parent 8e8fbb4662a078811a0ea78a84ba3bf8af04310b
Author: Jared Tobin <jared@jtobin.ca>
Date:   Fri, 13 Jul 2018 11:39:47 +1200

Add small command line tool, tests.

Diffstat:
Mhnock.cabal | 20++++++++++++++++++++
Mlib/Nock.hs | 81++-----------------------------------------------------------------------------
Asrc/Main.hs | 24++++++++++++++++++++++++
Atest/Main.hs | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 149 insertions(+), 79 deletions(-)

diff --git a/hnock.cabal b/hnock.cabal @@ -26,3 +26,23 @@ library , parsec >= 3.1.13.0 && < 3.2 , text >= 1.2.3.0 && < 1.3 +executable hnock + default-language: Haskell2010 + ghc-options: -Wall -O2 + hs-source-dirs: src + Main-is: Main.hs + build-depends: + base >= 4.8 && < 4.12 + , hnock + , text >= 1.2.3.0 && < 1.3 + +Test-suite hnock-test + type: exitcode-stdio-1.0 + default-language: Haskell2010 + ghc-options: -Wall + hs-source-dirs: test + Main-is: Main.hs + build-depends: + base >= 4.8 && < 4.12 + , hnock + diff --git a/lib/Nock.hs b/lib/Nock.hs @@ -4,84 +4,7 @@ module Nock ( module X ) where -import Nock.Language as X import Nock.Eval as X +import Nock.Language as X +import Nock.Parse as X --- test expressions ----------------------------------------------------------- - --- /[3 [[4 5] [6 14 15]]] --- should be [6 [14 15]] -test0 :: Expr -test0 = - Fas - (Noun (Cell (Atom 3) - (Cell (Cell (Atom 4) (Atom 5)) - (Cell (Atom 6) (Cell (Atom 14) (Atom 15)))))) - --- *[[[4 5] [6 14 15]] [0 7]] --- should be [14 15] -test1 :: Expr -test1 = - Tar - (Noun (Cell - (Cell (Cell (Atom 4) (Atom 5)) - (Cell (Atom 6) (Cell (Atom 14) (Atom 15)))) - (Cell (Atom 0) (Atom 7)))) - --- *[42 [1 153 218]] --- should be [153 218] -test2 :: Expr -test2 = - Tar - (Noun (Cell (Atom 42) - (Cell (Atom 1) (Cell (Atom 153) (Atom 218))))) - --- *[57 [4 0 1]] --- should be 58 -test3 :: Expr -test3 = - Tar - (Noun (Cell (Atom 57) - (Cell (Atom 4) - (Cell (Atom 0) (Atom 1))))) - --- *[[132 19] [4 0 3]] --- should be 20 -test4 :: Expr -test4 = - Tar - (Noun (Cell (Cell (Atom 132) (Atom 19)) - (Cell (Atom 4) - (Cell (Atom 0) (Atom 3))))) - --- /[7 [[4 5] [6 14 15]]] --- should be [14 15] -test5 :: Expr -test5 = - Fas - (Noun (Cell (Atom 7) - (Cell (Cell (Atom 4) (Atom 5)) - (Cell (Atom 6) (Cell (Atom 14) (Atom 15)))))) - --- *[77 [2 [1 42] [1 1 153 218]]] --- should be [153 218] -test6 :: Expr -test6 = - Tar - (Noun - (Cell (Atom 77) - (Cell (Atom 2) - (Cell (Cell (Atom 1) (Atom 42)) - (Cell (Atom 1) - (Cell (Atom 1) - (Cell (Atom 153) (Atom 218)))))))) - -main :: IO () -main = do - print (nock test0) - print (nock test1) - print (nock test2) - print (nock test3) - print (nock test4) - print (nock test5) - print (nock test6) diff --git a/src/Main.hs b/src/Main.hs @@ -0,0 +1,24 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Main where + +import Control.Monad (when) +import qualified Data.Text as T +import qualified Data.Text.IO as T +import Nock +import System.Exit (exitSuccess) + +main :: IO () +main = do + input <- T.getContents + + when (T.length input == 0) $ do + T.putStrLn "USAGE: echo EXPR | ./hnock" + exitSuccess + + let parsed = parse input + + case parsed of + Left err -> T.putStrLn (T.pack (show err)) + Right expr -> T.putStrLn (T.pack (show (nock expr))) + diff --git a/test/Main.hs b/test/Main.hs @@ -0,0 +1,103 @@ + +module Main where + +import Nock + +-- /[3 [[4 5] [6 14 15]]] +-- should be [6 [14 15]] +expr0 :: Expr +expr0 = + Fas + (Noun (Cell (Atom 3) + (Cell (Cell (Atom 4) (Atom 5)) + (Cell (Atom 6) (Cell (Atom 14) (Atom 15)))))) + +noun0 :: Noun +noun0 = Cell (Atom 6) (Cell (Atom 14) (Atom 15)) + +-- *[[[4 5] [6 14 15]] [0 7]] +-- should be [14 15] +expr1 :: Expr +expr1 = + Tar + (Noun (Cell + (Cell (Cell (Atom 4) (Atom 5)) + (Cell (Atom 6) (Cell (Atom 14) (Atom 15)))) + (Cell (Atom 0) (Atom 7)))) + +noun1 :: Noun +noun1 = Cell (Atom 14) (Atom 15) + +-- *[42 [1 153 218]] +-- should be [153 218] +expr2 :: Expr +expr2 = + Tar + (Noun (Cell (Atom 42) + (Cell (Atom 1) (Cell (Atom 153) (Atom 218))))) + +noun2 :: Noun +noun2 = Cell (Atom 153) (Atom 218) + +-- *[57 [4 0 1]] +-- should be 58 +expr3 :: Expr +expr3 = + Tar + (Noun (Cell (Atom 57) + (Cell (Atom 4) + (Cell (Atom 0) (Atom 1))))) + +noun3 :: Noun +noun3 = Atom 58 + +-- *[[132 19] [4 0 3]] +-- should be 20 +expr4 :: Expr +expr4 = + Tar + (Noun (Cell (Cell (Atom 132) (Atom 19)) + (Cell (Atom 4) + (Cell (Atom 0) (Atom 3))))) + +noun4 :: Noun +noun4 = Atom 20 + +-- /[7 [[4 5] [6 14 15]]] +-- should be [14 15] +expr5 :: Expr +expr5 = + Fas + (Noun (Cell (Atom 7) + (Cell (Cell (Atom 4) (Atom 5)) + (Cell (Atom 6) (Cell (Atom 14) (Atom 15)))))) + +noun5 :: Noun +noun5 = Cell (Atom 14) (Atom 15) + +-- *[77 [2 [1 42] [1 1 153 218]]] +-- should be [153 218] +expr6 :: Expr +expr6 = + Tar + (Noun + (Cell (Atom 77) + (Cell (Atom 2) + (Cell (Cell (Atom 1) (Atom 42)) + (Cell (Atom 1) + (Cell (Atom 1) + (Cell (Atom 153) (Atom 218)))))))) + +noun6 :: Noun +noun6 = Cell (Atom 153) (Atom 218) + +main :: IO () +main = do + print (nock expr0 == noun0) + print (nock expr1 == noun1) + print (nock expr2 == noun2) + print (nock expr3 == noun3) + print (nock expr4 == noun4) + print (nock expr5 == noun5) + print (nock expr6 == noun6) +