hnock

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

commit e55f3ed31fc2ec9ea628b0129835ccf2f098059e
parent 1ec86b420ba40f49bc45946647ba00a50828142a
Author: Jared Tobin <jared@jtobin.ca>
Date:   Fri, 13 Jul 2018 14:03:03 +1200

Misc cleanup.

Diffstat:
MREADME.md | 29+++++++++++++++++++++++++++++
Mlib/Nock.hs | 28++++++++++++++++++++++++----
Mlib/Nock/Eval.hs | 24++++++++++++------------
Mlib/Nock/Parse.hs | 9++++++---
Msrc/Main.hs | 2+-
5 files changed, 72 insertions(+), 20 deletions(-)

diff --git a/README.md b/README.md @@ -1,3 +1,32 @@ # hnock A Nock interpreter. + +## Usage + +From bash, simply pipe Nock expressions into the executable `hnock`: + +``` +$ echo '*[[[4 5] [6 14 15]] [0 7]]' | hnock +[14 15] +``` + +For playing around in GHCi, import the `Nock` library and use `hnock` to parse +and evaluate Nock expressions: + +``` +*Nock> hnock "*[[[4 5] [6 14 15]] [0 7]]" +[14 15] +``` + +To evaluate raw nock Nouns, i.e. to compute `nock(a)` for some noun `a`, use +`enock`: + +``` +*Nock> let expression = hnock "[[[4 5] [6 14 15]] [0 7]]" +*Nock> expression +[[[4 5] [6 [14 15]]] [0 7]] +*Nock> enock expression +[14 15] +``` + diff --git a/lib/Nock.hs b/lib/Nock.hs @@ -1,10 +1,30 @@ {-# OPTIONS_GHC -Wall #-} module Nock ( - module X + module L + , module P + + , enock + , hnock + + , eval + , nock ) where -import Nock.Eval as X -import Nock.Language as X -import Nock.Parse as X +import Data.Text as T +import Nock.Eval as E +import Nock.Language as L +import Nock.Parse as P + +hnock :: T.Text -> Noun +hnock input = case runParser expr [] "ghci" input of + Left perr -> error (show perr) + Right ex -> case eval ex of + Left err -> error (show err) + Right e -> e + +enock :: Noun -> Noun +enock noun = case nock noun of + Left err -> error (show err) + Right e -> e diff --git a/lib/Nock/Eval.hs b/lib/Nock/Eval.hs @@ -88,27 +88,27 @@ tar noun = case noun of in tistar (Cell a b) Cell a (Cell (Atom 6) (Cell b (Cell c d))) -> - tar (tar6 a b c d) + tar6 a b c d Cell a (Cell (Atom 7) (Cell b c)) -> tar (Cell a (Cell (Atom 2) (Cell b (Cell (Atom 1) c)))) Cell a (Cell (Atom 8) (Cell b c)) -> - tar (tar8 a b c) + tar8 a b c Cell a (Cell (Atom 9) (Cell b c)) -> - tar (tar9 a b c) + tar9 a b c Cell a (Cell (Atom 10) (Cell (Cell b c) d)) -> - tar (tar10 a b c d) + tar10 a b c d Cell a (Cell (Atom 10) (Cell _ c)) -> tar (Cell a c) _ -> Left (Error noun) -tar6 :: Noun -> Noun -> Noun -> Noun -> Noun -tar6 a b c d = +tar6 :: Noun -> Noun -> Noun -> Noun -> Possibly Noun +tar6 a b c d = tar $ Cell a (Cell (Atom 2) (Cell (Cell (Atom 0) (Atom 1)) @@ -121,8 +121,8 @@ tar6 a b c d = (Cell (Atom 4) (Cell (Atom 4) b)))))))))) -tar8 :: Noun -> Noun -> Noun -> Noun -tar8 a b c = +tar8 :: Noun -> Noun -> Noun -> Possibly Noun +tar8 a b c = tar $ Cell a (Cell (Atom 7) (Cell @@ -130,8 +130,8 @@ tar8 a b c = (Cell (Cell (Atom 0) (Atom 1)) b)) (Cell (Cell (Atom 0) (Atom 1)) c))) -tar9 :: Noun -> Noun -> Noun -> Noun -tar9 a b c = +tar9 :: Noun -> Noun -> Noun -> Possibly Noun +tar9 a b c = tar $ Cell a (Cell (Atom 7) (Cell c @@ -139,8 +139,8 @@ tar9 a b c = (Cell (Cell (Atom 0) (Atom 1)) (Cell (Atom 0) b))))) -tar10 :: Noun -> Noun -> Noun -> Noun -> Noun -tar10 a _ c d = +tar10 :: Noun -> Noun -> Noun -> Noun -> Possibly Noun +tar10 a _ c d = tar $ Cell a (Cell (Atom 8) (Cell c diff --git a/lib/Nock/Parse.hs b/lib/Nock/Parse.hs @@ -2,13 +2,16 @@ {-# LANGUAGE OverloadedStrings #-} module Nock.Parse ( - parse + P.runParser + + , parse + , expr ) where import Nock.Language -import qualified Text.Parsec as P -import qualified Data.Text as T import Control.Applicative ((<|>)) +import qualified Data.Text as T +import qualified Text.Parsec as P parse :: T.Text -> Either P.ParseError Expr parse = P.runParser expr [] "input" diff --git a/src/Main.hs b/src/Main.hs @@ -18,7 +18,7 @@ main = do case parse input of Left parseErr -> T.putStrLn (T.pack (show parseErr)) - Right expr -> case eval expr of + Right e -> case eval e of Left err -> T.putStrLn (T.pack (show err)) Right noun -> T.putStrLn (T.pack (show noun))