Skip to content

Commit

Permalink
Update builtin function names.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Grant Jeffries committed Nov 1, 2019
1 parent 2be1fbe commit 5626738
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
23 changes: 12 additions & 11 deletions bowtie-js/src/Bowtie/JS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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!
Expand Down
4 changes: 2 additions & 2 deletions bowtie-js/src/Bowtie/JS/Serialize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <> ")"
Expand All @@ -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)

0 comments on commit 5626738

Please sign in to comment.