diff --git a/bowtie-js/src/Bowtie/JS.hs b/bowtie-js/src/Bowtie/JS.hs index 00cd46e..e314dd7 100644 --- a/bowtie-js/src/Bowtie/JS.hs +++ b/bowtie-js/src/Bowtie/JS.hs @@ -21,24 +21,21 @@ import qualified Bowtie.Interpret as Interpret import qualified Data.ByteString.Lazy as LBS import qualified Data.Text as Text -appendConsoleLog :: Text -> Text -appendConsoleLog js = - js <> "\n\nconsole.log(result);" - -app1 :: Text -app1 = +-- | Internal. +builtinJsSource :: Text +builtinJsSource = [s| -function arrayToListYYY(xs) { +function $arrayToListBuiltin(xs) { const reducer = (accumulator, currentValue) => ["Cons", currentValue, accumulator]; return xs.reduceRight(reducer, ["Nil"]); } -function unicodeListize(t) { +function $unicodeListizeBuiltin(t) { let xs = t.split('').map(c => c.codePointAt()) - return ["Unicode", arrayToListYYY(xs)]; + return ["Unicode", $arrayToListBuiltin(xs)]; } -function compareYYY(a, b) { +function $compareBuiltin(a, b) { if (a > b) { return ["GreaterThan"]; } else if (b > a) { @@ -59,13 +56,17 @@ transpileCore env expr = let jsAST = makeImp env expr in - "'use strict';\n\n" <> app1 <> "\n" <> serializeTop jsAST + "'use strict';\n\n" <> builtinJsSource <> "\n" <> serializeTop jsAST transpileAndExecute :: Text -> IO Text transpileAndExecute src = do let Right js = transpile src runTextCommand "node" js +appendConsoleLog :: Text -> Text +appendConsoleLog js = + js <> "\n\nconsole.log(result);" + -- * Below should be in a lib somewhere -- | NOTE: Only used with trused input! diff --git a/bowtie-js/src/Bowtie/JS/Serialize.hs b/bowtie-js/src/Bowtie/JS/Serialize.hs index d33510a..94a3d07 100644 --- a/bowtie-js/src/Bowtie/JS/Serialize.hs +++ b/bowtie-js/src/Bowtie/JS/Serialize.hs @@ -85,7 +85,7 @@ serializeOperation :: Operation -> Text serializeOperation op = case op of Compare ast1 ast2 -> - "compareYYY(" <> serialize ast1 <> ", " <> serialize ast2 <> ")" + "$compareBuiltin(" <> serialize ast1 <> ", " <> serialize ast2 <> ")" Plus ast1 ast2 -> "(" <> serialize ast1 <> " + " <> serialize ast2 <> ")" @@ -94,7 +94,7 @@ serializeOperation op = "(" <> serialize ast1 <> " * " <> serialize ast2 <> ")" ShowInt ast -> - "unicodeListize(" <> serialize ast <> ".toString())" + "$unicodeListizeBuiltin(" <> serialize ast <> ".toString())" Panic expr -> -- Only works on Text experize ("throw " <> serialize expr)