From f787b4facfa02a82a06ee9a468366900a09f3cf3 Mon Sep 17 00:00:00 2001 From: Kimiyuki Onaka Date: Sat, 31 Jul 2021 07:08:14 +0900 Subject: [PATCH 1/2] feat: Embed original Python code --- src/Jikka/Main.hs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Jikka/Main.hs b/src/Jikka/Main.hs index 24fb1814..9f843e1d 100644 --- a/src/Jikka/Main.hs +++ b/src/Jikka/Main.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE OverloadedStrings #-} + -- | -- Module : Jikka.Main -- Description : is the entry point of the @jikka@ command. / @jikka@ コマンドのエントリポイントです。 @@ -9,6 +11,7 @@ module Jikka.Main where import Data.Maybe (fromMaybe) +import qualified Data.Text as T import qualified Data.Text.IO as T import Data.Version (showVersion) import qualified Jikka.CPlusPlus.Convert.BundleRuntime as BundleRuntime @@ -28,13 +31,15 @@ data Flag | Verbose | Version | Target String - | Bundle Bool + | BundleRuntimeHeaders Bool + | EmbedOriginalCode Bool deriving (Eq, Ord, Show, Read) data Options = Options { verbose :: Bool, target :: Maybe Target, - bundleRuntime :: Bool + bundleRuntimeHeaders :: Bool, + embedOriginalCode :: Bool } deriving (Eq, Ord, Show, Read) @@ -43,7 +48,8 @@ defaultOptions = Options { verbose = False, target = Nothing, - bundleRuntime = True + bundleRuntimeHeaders = True, + embedOriginalCode = True } header :: String -> String @@ -55,8 +61,10 @@ options = Option ['v'] ["verbose"] (NoArg Verbose) "", Option [] ["version"] (NoArg Version) "", Option [] ["target"] (ReqArg Target "TARGET") "\"python\", \"rpython\", \"core\" or \"cxx\"", - Option [] ["bundle"] (NoArg (Bundle True)) "bundles runtime headers", - Option [] ["no-bundle"] (NoArg (Bundle False)) "" + Option [] ["bundle-runtime-headers"] (NoArg (BundleRuntimeHeaders True)) "bundles C++ runtime headers", + Option [] ["no-bundle-runtime-headers"] (NoArg (BundleRuntimeHeaders False)) "", + Option [] ["embed-original-code"] (NoArg (EmbedOriginalCode True)) "embeds the original Python code", + Option [] ["no-embed-original-code"] (NoArg (EmbedOriginalCode False)) "" ] main :: String -> [String] -> IO ExitCode @@ -103,7 +111,8 @@ parseFlags _ = go defaultOptions Target target -> do target <- parseTarget target go (opts {target = Just target}) flags - Bundle p -> go (opts {bundleRuntime = p}) flags + BundleRuntimeHeaders p -> go (opts {bundleRuntimeHeaders = p}) flags + EmbedOriginalCode p -> go (opts {embedOriginalCode = p}) flags runSubcommand :: String -> Options -> FilePath -> ExceptT Error IO () runSubcommand subcmd opts path = case subcmd of @@ -112,9 +121,16 @@ runSubcommand subcmd opts path = case subcmd of let target' = fromMaybe CPlusPlusTarget (target opts) output <- liftEither $ Convert.run target' path input output <- - if target' == CPlusPlusTarget && bundleRuntime opts + if target' == CPlusPlusTarget && bundleRuntimeHeaders opts then BundleRuntime.run output else return output + output <- + return $ + if embedOriginalCode opts + then + let headers = ["// This C++ code is transpiled using Jikka transpiler https://github.com/kmyk/Jikka", "// The original Python code:"] + in T.unlines (headers ++ map ("// " <>) (T.lines input)) <> output + else output liftIO $ T.putStr output "debug" -> Debug.run path "execute" -> Execute.run (fromMaybe CoreTarget (target opts)) path From 6ba1a043ba3e80cb45bc5235fa41d812ad3dfb52 Mon Sep 17 00:00:00 2001 From: Kimiyuki Onaka Date: Sat, 31 Jul 2021 07:22:43 +0900 Subject: [PATCH 2/2] feat: Add version info to the banner of C++ code --- src/Jikka/Main.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Jikka/Main.hs b/src/Jikka/Main.hs index 9f843e1d..6c4ee7c2 100644 --- a/src/Jikka/Main.hs +++ b/src/Jikka/Main.hs @@ -75,7 +75,7 @@ main name args = do putStr usage return ExitSuccess (parsed, _, []) | Version `elem` parsed -> do - putStrLn $ showVersion version + putStrLn $ 'v' : showVersion version return ExitSuccess (parsed, [subcmd, path], []) -> case parseFlags name parsed of Left err -> do @@ -128,7 +128,7 @@ runSubcommand subcmd opts path = case subcmd of return $ if embedOriginalCode opts then - let headers = ["// This C++ code is transpiled using Jikka transpiler https://github.com/kmyk/Jikka", "// The original Python code:"] + let headers = ["// This C++ code is transpiled using Jikka transpiler v" <> T.pack (showVersion version) <> " https://github.com/kmyk/Jikka", "// The original Python code:"] in T.unlines (headers ++ map ("// " <>) (T.lines input)) <> output else output liftIO $ T.putStr output