-
Notifications
You must be signed in to change notification settings - Fork 27
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
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
18701b0
Add stub of JavaScript target
Kroisse 795b56d
Add package.json output
Kroisse 8ab0f6b
Add test for compilePackage'
Kroisse d7bb4b0
Add more test case
Kroisse 88653f6
Produce corresponding (empty) js files for nrm files
heejongahn eac87d7
Add file extension only to filename, not path
heejongahn 4c146e1
Remove unnecessary hierarchy
heejongahn d139a3d
Add more test
Kroisse f823863
fix
Kroisse 83c4677
Implement base of CodeBuilder
Kroisse bfa5907
Introduce PrettyPrint to build the code
heejongahn 5abd277
Implement base of compileTypeDeclaration for records
Kroisse c9041f8
Add "version" into package.json output
Kroisse 423cadb
Lint
Kroisse 2c11bb7
Enclose BoundModule context into CodeBuilder
Kroisse 7b4eb87
Lots of improvements
Kroisse 5dcf927
Lint
Kroisse 953aa4c
Add "use strict"; directive in the JS output
Kroisse ae89537
Add a set of JS keywords
Kroisse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
{-# LANGUAGE FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses, TypeOperators #-} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As
aeson
,aeson-pretty
andpretty
are common dependencies tolibrary
andtest-suite spec
, it'd better omit version specifies here.