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

WIP: Add stub of JavaScript target #109

Merged
merged 19 commits into from
Apr 21, 2017
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
3 changes: 3 additions & 0 deletions examples/package.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
version = "0.3.0"

[targets.javascript]
name = "nirum-examples"

[targets.python]
name = "nirum-examples"

Expand Down
14 changes: 12 additions & 2 deletions nirum.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ cabal-version: >=1.10

library
exposed-modules: Nirum.Cli
, Nirum.CodeBuilder
, Nirum.CodeGen
, Nirum.Constructs
, Nirum.Constructs.Annotation
Expand All @@ -44,10 +45,13 @@ library
, Nirum.Package.ModuleSet
, Nirum.Parser
, Nirum.Targets
, Nirum.Targets.JavaScript
, Nirum.Targets.List
, Nirum.Targets.Python
, Nirum.Version
build-depends: base >=4.7 && <5
build-depends: aeson >=1.0.2 && <2
, aeson-pretty >=0.8 && <0.9
, base >=4.7 && <5
, bytestring
, containers >=0.5.6.2 && <0.6
, cmdargs >=0.10.14 && <0.11
Expand All @@ -60,6 +64,7 @@ library
, mtl >=2.2.1 && <3
, parsec
-- only for dealing with htoml's ParserError
, pretty >=1.1.3 && <2
, semver >=0.3.0 && <1.0
, template-haskell >=2.11 && <3
, text >=0.9.1.0 && <1.3
Expand Down Expand Up @@ -97,6 +102,7 @@ test-suite spec
hs-source-dirs: test
main-is: Spec.hs
other-modules: Nirum.CliSpec
, Nirum.CodeBuilderSpec
, Nirum.CodeGenSpec
, Nirum.Constructs.AnnotationSpec
, Nirum.Constructs.DocsSpec
Expand All @@ -112,14 +118,17 @@ test-suite spec
, Nirum.Package.ModuleSetSpec
, Nirum.PackageSpec
, Nirum.ParserSpec
, Nirum.Targets.JavaScriptSpec
, Nirum.Targets.PythonSpec
, Nirum.TargetsSpec
, Nirum.VersionSpec
, Util
default-language: Haskell2010
default-extensions: OverloadedStrings
-- omit version specifiers for shared dependencies to library
build-depends: base
build-depends: aeson >=1.0.2 && <2
, aeson-pretty >=0.8 && <0.9
, base
, bytestring
, containers
, directory
Expand All @@ -135,6 +144,7 @@ test-suite spec
, nirum
, parsec
-- only for dealing with htoml's ParserError
, pretty >=1.1.3 && <2
Copy link
Member

Choose a reason for hiding this comment

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

As aeson,aeson-pretty and pretty are common dependencies to library and test-suite spec, it'd better omit version specifies here.

, process >=1.1 && <2
, semigroups
, semver
Expand Down
84 changes: 84 additions & 0 deletions src/Nirum/CodeBuilder.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{-# LANGUAGE FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses, TypeOperators #-}
Copy link
Member

Choose a reason for hiding this comment

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

Please keep shorter than 80 columns for each line.

module Nirum.CodeBuilder ( CodeBuilder
, lookupType
, nest
, runBuilder
, writeLine
) where

import Control.Applicative (Applicative)
import Control.Monad (Monad)
import qualified Control.Monad.State as ST
import Control.Monad.State (MonadState, State, state, runState)
import Data.Functor (Functor)
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import qualified Data.Text.Lazy.Builder as B
import qualified Text.PrettyPrint as P
import Text.PrettyPrint (($+$))

import Nirum.Constructs.Identifier (Identifier)
import Nirum.Constructs.ModulePath (ModulePath)
import qualified Nirum.Package as PK
import Nirum.Package (BoundModule (..), resolveBoundModule)
import Nirum.Package.Metadata (Package (..), Target (..))


newtype Target t => CodeBuilder t s a = CodeBuilder (State (BuildState t s) a)
deriving ( Applicative
, Functor
, Monad
)

data Target t => BuildState t s =
BuildState { output :: P.Doc
, boundModule :: BoundModule t
, innerState :: s
}

instance Target t => MonadState s (CodeBuilder t s) where
state f = do
st <- get'
let (a, s) = f (innerState st)
put' $ st { innerState = s }
return a

get' :: Target t => CodeBuilder t s (BuildState t s)
get' = CodeBuilder ST.get

put' :: Target t => BuildState t s -> CodeBuilder t s ()
put' = CodeBuilder . ST.put

modify' :: Target t => (BuildState t s -> BuildState t s) -> CodeBuilder t s ()
modify' = CodeBuilder . ST.modify

writeLine :: Target t => P.Doc -> CodeBuilder t s ()
writeLine code = modify' $ \s -> s { output = output s $+$ code }

nest :: Target t => Integer -> CodeBuilder t s a -> CodeBuilder t s a
nest n code = do
st <- get'
let st' = st { output = P.empty }
put' st'
ret <- code
after <- get'
modify' $ \s -> s { output = output st $+$ P.nest (fromIntegral n) (output after) }
return ret

lookupType :: Target t => Identifier -> CodeBuilder t s PK.TypeLookup
lookupType identifier = do
m <- fmap boundModule get'
return $ PK.lookupType identifier m

runBuilder :: Target t => Package t -> ModulePath -> s -> CodeBuilder t s a -> (a, B.Builder)
runBuilder package modPath st (CodeBuilder a) = (ret, rendered)
where
initialState = BuildState { output = P.empty
, boundModule = fromMaybe (error "asdf") (resolveBoundModule modPath package)
, innerState = st
}
(ret, finalState) = runState a initialState
rendered = P.fullRender P.PageMode 80 1.5 concat' (B.singleton '\n') (output finalState)
concat' (P.Chr c) rest = B.singleton c <> rest
concat' (P.Str s) rest = B.fromString s <> rest
concat' (P.PStr s) rest = concat' (P.Str s) rest
3 changes: 3 additions & 0 deletions src/Nirum/Targets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import Nirum.Package.Metadata ( Metadata (Metadata, target)
, TargetName
)
import Nirum.Targets.List (targetProxyMapQ)

-- these targets adds automatically
import Nirum.Targets.JavaScript ()
import Nirum.Targets.Python ()

data BuildError = TargetNameError TargetName
Expand Down
Loading