git.iko.soy/rowtype-yoga/purs-eval
on main help sign in
feat: add capabilities and launch the compiler
commitc5609ef15b5c55a994886d5b609a617aabb6aa58 authorWalker <walker@praiseweb.com.br> dateSat 29 Oct 2022, 22:11 (4 years ago) parentad3ab45
3 files changed+50−11
flake.nix+2 src/Main.purs+40−7 test/Main.purs+8−4
flake.nix+2
@@ -33,10 +33,12 @@
3333 debug
3434 aff
3535 effect
36+ node-buffer
3637 node-process
3738 test-unit
3839 httpure
3940 affjax-node
41+ node-streams-aff
4042 ];
4143
4244 foreign."Affjax.Node".node_modules = npm.node_modules { src = ./.; } + /node_modules;
src/Main.purs+40−7
@@ -1,23 +1,56 @@
11module Main where
22
3−import Prelude (Unit, ($), (<<<), void, pure, bind)
4−
3+import Prelude (Unit, ($), (<<<), void, pure, bind, show, discard)
4+import Control.Bind ((=<<))
55import Control.Monad.Error.Class (liftEither)
6+import Control.Monad.Reader.Trans (ReaderT, runReaderT, ask)
7+import Control.Monad.Trans.Class (lift)
68import Data.Bifunctor (lmap)
79import Data.Maybe (Maybe(Just))
10+import Data.Monoid ((<>))
11+import Data.Tuple (Tuple (Tuple))
12+import Data.Array (concat)
813import Effect (Effect)
9−import Effect.Aff (Aff, error)
14+import Effect.Aff (Aff, error, launchAff_)
15+import Effect.Class (liftEffect)
16+import Node.Buffer (Buffer)
1017import Node.Process (stdin, stdout)
1118import Node.Stream (pipe)
19+import Node.Stream.Aff (readSome, write, end, toStringUTF8, fromStringUTF8)
1220import Affjax.ResponseFormat (string)
1321import Affjax.Node (post, printError)
1422import Affjax.RequestBody (RequestBody (String))
1523
16−compile :: String -> Aff String
17−compile s = do
18− mRes <- post string "http://localhost:3000/compile" $ Just $ String s
24+type Settings = { protocol :: String, hostname :: String, port :: Int }
25+type Code = String
26+type Compiler = ReaderT Code (ReaderT Settings Aff)
27+
28+askCode :: Compiler Code
29+askCode = ask
30+
31+askSettings :: Compiler Settings
32+askSettings = lift ask
33+
34+liftAff :: forall a. Aff a -> Compiler a
35+liftAff h = lift $ lift h
36+
37+compile :: Compiler Code
38+compile = do
39+ code <- askCode
40+ s <- askSettings
41+ let url = s.protocol <> "://" <> s.hostname <> ":" <> show s.port <> "/compile"
42+ mRes <- liftAff $ post string url $ Just $ String code
1943 res <- liftEither $ (error <<< printError) `lmap` mRes
2044 pure res.body
2145
46+runCompiler :: Settings -> Code -> Aff Code
47+runCompiler s code = runReaderT (runReaderT compile code) s
48+
2249main :: Effect Unit
23−main = void $ stdin `pipe` stdout
50+main = launchAff_ do
51+ Tuple input _ <- readSome stdin
52+ code <- toStringUTF8 input
53+ code' <- runCompiler { protocol: "https", hostname: "try.purescript.org", port: 80 } code
54+ output <- fromStringUTF8 code'
55+ write stdout output
56+ end stdout
test/Main.purs+8−4
@@ -2,12 +2,12 @@ module Test.Main where
22
33import Prelude (Unit, ($), (==), (>>=), unit, pure, bind, const)
44import Control.Apply ((*>))
5−import Control.Monad.Error.Class (class MonadThrow, throwError, liftMaybe)
5+import Control.Monad.Error.Class (throwError, liftMaybe)
66import Control.Monad.Reader.Trans (ReaderT, ask, runReaderT)
77import Effect
88import Effect.Aff (Aff, bracket)
99import Effect.Class (liftEffect)
10−import Effect.Exception (Error, error)
10+import Effect.Exception (error)
1111import HTTPure.Request (Request)
1212import HTTPure.Response (ok)
1313import HTTPure.Server (serve)
@@ -17,10 +17,13 @@ import HTTPure.Method (Method(Post))
1717import Test.Unit (suite, test)
1818import Test.Unit.Main (runTest)
1919import Test.Unit.Assert (equal)
20−import Main (compile)
20+import Main (Settings, runCompiler)
2121
2222type RequestValidator = ReaderT Request Aff Unit
2323
24+settings :: Settings
25+settings = { protocol: "http", hostname: "localhost", port: 3000 }
26+
2427validatePath :: RequestValidator
2528validatePath = do
2629 let invalidPath = error "invalid path"
@@ -41,7 +44,7 @@ runReqValidator req = runReaderT (validatePath *> validateMethod) req
4144
4245mockSrv :: forall a. Body a => a -> Aff (Effect Unit)
4346mockSrv res = do
44− close <- liftEffect $ serve 3000 (\req -> runReqValidator req *> ok res) $ pure unit
47+ close <- liftEffect $ serve settings.port (\req -> runReqValidator req *> ok res) $ pure unit
4548 pure $ close $ pure unit
4649
4750setupSrv :: forall a b. Body a => a -> Aff b -> Aff b
@@ -52,5 +55,6 @@ main = runTest do
5255 suite "compile" do
5356 test "produces expected output" do
5457 let expected = "2"
58+ compile = runCompiler settings
5559 actual <- setupSrv expected (compile "1+1")
5660 expected `equal` actual