Skip to content

Commit

Permalink
Avoid conflicts with builtin JS keywords.
Browse files Browse the repository at this point in the history
(By prepending generated functions with underscore)
  • Loading branch information
Ian Grant Jeffries committed Nov 1, 2019
1 parent f63d831 commit cb2c97b
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 97 deletions.
2 changes: 1 addition & 1 deletion bowtie-js/display.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

let topLevelResult = result;
let topLevelResult = _result;

function main() {
const canvas = document.getElementById("canvas");
Expand Down
2 changes: 1 addition & 1 deletion bowtie-js/src/Bowtie/JS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ transpileAndExecute src = do

appendConsoleLog :: Text -> Text
appendConsoleLog js =
js <> "\n\nconsole.log(result);"
js <> "\n\nconsole.log(_result);"

-- * Below should be in a lib somewhere

Expand Down
7 changes: 4 additions & 3 deletions bowtie-js/src/Bowtie/JS/AST.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module Bowtie.JS.AST where

import Bowtie.Lib.Id
import Bowtie.Lib.Prelude

-- h/t PureScript
data AST
= Var Text
| Lam Text AST
= Var Id
| Lam Id AST
| App AST AST

| Assignment AST AST
Expand All @@ -21,7 +22,7 @@ data AST
deriving (Eq, Show)

data Alt
= Alt Text [Text] AST
= Alt Id [Id] AST
deriving (Eq, Show)

data Operation
Expand Down
31 changes: 19 additions & 12 deletions bowtie-js/src/Bowtie/JS/Imperativize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ makeImp (Environment env) expr =
bindings = fmap assign (OrderedMap.toList coreBindings)

assign (id, e) =
Assignment (Var (unId id)) (coreToImp e)
Assignment (Var id) (coreToImp e)

result = assign (Id "result", coreExpr)

coreToImp :: Core.Expr -> JS.AST
coreToImp topExpr =
case topExpr of
Core.Var id ->
Var (unId id)
Var id

Core.Lam id _typ expr ->
Lam (unId id) ((coreToImp expr))
Lam id ((coreToImp expr))

Core.App e1 e2 ->
App (coreToImp e1) (coreToImp e2)
Expand All @@ -47,15 +47,15 @@ coreToImp topExpr =
let
f :: (Id, (Core.Expr, typ)) -> AST
f (id, (expr, _)) =
Assignment (Var (unId id)) (coreToImp expr)
Assignment (Var id) (coreToImp expr)
in
Block
( fmap f (List.sortOn fst (HashMap.toList bindings))
<> [coreToImp body] -- PERFORMANCE
)

Core.Construct id ->
Var (unId id)
Var id

Core.Case expr alts ->
Case (coreToImp expr) (fmap altToImp alts)
Expand All @@ -68,7 +68,7 @@ coreToImp topExpr =

altToImp :: Core.Alt -> Alt
altToImp (Core.Alt id bindings body) =
Alt (unId id) (fmap unId bindings) (coreToImp body)
Alt id bindings (coreToImp body)

coreOperationToImp :: Core.Operation -> JS.Operation
coreOperationToImp op =
Expand All @@ -90,17 +90,24 @@ coreOperationToImp op =

conTypeToFunction :: (Id, TypeScheme) -> JS.AST
conTypeToFunction (id, TypeScheme _ tsType) =
Assignment (Var (unId id)) (addLambdas xs (Array (JSString (unId id) : fmap Var xs)))
Assignment
(Var id)
(addLambdas args (Array (conAsString : fmap Var args)))
where
addLambdas :: [Text] -> JS.AST -> JS.AST
-- eg ["Maybe", 5], not [Maybe, 5]
conAsString :: JS.AST
conAsString =
JSString (unId id)

addLambdas :: [Id] -> JS.AST -> JS.AST
addLambdas [] ast = ast
addLambdas (y:ys) ast = Lam y (addLambdas ys ast)

xs :: [Text]
xs =
args :: [Id]
args =
f 1 tsType

f :: Natural -> Type -> [Text]
f :: Natural -> Type -> [Id]
f n typ =
case typ of
TVariable _ ->
Expand All @@ -110,7 +117,7 @@ conTypeToFunction (id, TypeScheme _ tsType) =
[]

TArrow _ t2 ->
"arg" <> show n : f (n + 1) t2
Id ("arg" <> show n) : f (n + 1) t2

TypeApp _ _ -> -- eg List a
[]
Expand Down
18 changes: 12 additions & 6 deletions bowtie-js/src/Bowtie/JS/Serialize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Bowtie.JS.Serialize
) where

import Bowtie.JS.AST
import Bowtie.Lib.Id
import Bowtie.Lib.Prelude

import qualified Data.Text as Text
Expand All @@ -23,10 +24,10 @@ serialize :: AST -> Text -- TODO: text builder
serialize topAst =
case topAst of
Var id ->
id
serializeId id

Lam id ast ->
id <> " => " <> serialize ast
serializeId id <> " => " <> serialize ast

App a1 a2 ->
serialize a1 <> "(" <> serialize a2 <> ")"
Expand Down Expand Up @@ -54,13 +55,14 @@ serialize topAst =

Case ast alts ->
let
mkAssign :: (Natural, Text) -> Text
mkAssign (n, t) =
"const " <> t <> " = $1[" <> show n <> "];\n"
mkAssign :: (Natural, Id) -> Text
mkAssign (n, id) =
"const " <> serializeId id <> " = $1[" <> show n <> "];\n"

f :: Alt -> Text
f (Alt id bindings expr) =
"if ($1[0] === \"" <> id <> "\") {"
-- unId not serializeId because it's ["Unit"] not [Unit].
"if ($1[0] === \"" <> unId id <> "\") {"
<> foldMap mkAssign (zip [1..] bindings)
<> "return " <> serialize expr <> "} else "
in
Expand Down Expand Up @@ -98,3 +100,7 @@ serializeOperation op =

Panic expr -> -- Only works on Text
experize ("throw " <> serialize expr)

serializeId :: Id -> Text
serializeId (Id t) =
"_" <> t
8 changes: 4 additions & 4 deletions bowtie-js/test/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ testApps =
exerciseLunarLander :: Text
exerciseLunarLander =
[s|
const resYYY1 = result[2](["Tick"])
const resYYY2 = resYYY1[2](["KeyDown", 119]) // TODO: this and the one below can be changed without failing the test
const resYYY3 = resYYY2[2](["KeyUp", 119])
console.log(resYYY3);
const $res1 = _result[2](["Tick"])
const $res2 = $res1[2](["KeyDown", 119]) // TODO: this and the one below can be changed without failing the test
const $res3 = $res2[2](["KeyUp", 119])
console.log($res3);
|]
4 changes: 2 additions & 2 deletions bowtie-js/test/well-typed-golden-files/001-simplest.bowtie
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function $compareBuiltin(a, b) {
}
}

const result = 0;
const _result = 0;

console.log(result);
console.log(_result);
*/
6 changes: 3 additions & 3 deletions bowtie-js/test/well-typed-golden-files/002-variable.bowtie
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ function $compareBuiltin(a, b) {
}
}

const a = 0;
const result = a;
const _a = 0;
const _result = _a;

console.log(result);
console.log(_result);
*/
6 changes: 3 additions & 3 deletions bowtie-js/test/well-typed-golden-files/003-lambda.bowtie
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Function: result]
[Function: _result]


/*
Expand All @@ -24,7 +24,7 @@ function $compareBuiltin(a, b) {
}
}

const result = n => n(0);
const _result = _n => _n(0);

console.log(result);
console.log(_result);
*/
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ function $compareBuiltin(a, b) {
}
}

const Unit = ["Unit"];
const result = Unit;
const _Unit = ["Unit"];
const _result = _Unit;

console.log(result);
console.log(_result);
*/
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ function $compareBuiltin(a, b) {
}
}

const BoolInt = arg1 => arg2 => ["BoolInt", arg1, arg2];
const False = ["False"];
const True = ["True"];
const result = BoolInt(True)(0);
const _BoolInt = _arg1 => _arg2 => ["BoolInt", _arg1, _arg2];
const _False = ["False"];
const _True = ["True"];
const _result = _BoolInt(_True)(0);

console.log(result);
console.log(_result);
*/
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ function $compareBuiltin(a, b) {
}
}

const False = ["False"];
const True = ["True"];
const result = (() => { const $1 = True;
const _False = ["False"];
const _True = ["True"];
const _result = (() => { const $1 = _True;
if ($1[0] === "False") {return 0} else if ($1[0] === "True") {return 1} else {throw "no match";} })();

console.log(result);
console.log(_result);
*/
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ function $compareBuiltin(a, b) {
}
}

const IdentityInt = arg1 => ["IdentityInt", arg1];
const result = (() => { const $1 = IdentityInt(1);
if ($1[0] === "IdentityInt") {const n = $1[1];
return n} else {throw "no match";} })();
const _IdentityInt = _arg1 => ["IdentityInt", _arg1];
const _result = (() => { const $1 = _IdentityInt(1);
if ($1[0] === "IdentityInt") {const _n = $1[1];
return _n} else {throw "no match";} })();

console.log(result);
console.log(_result);
*/
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ function $compareBuiltin(a, b) {
}
}

const BoolInt = arg1 => arg2 => ["BoolInt", arg1, arg2];
const Na = ["Na"];
const Unit = ["Unit"];
const Ya = ["Ya"];
const foo = b => n => Unit;
const result = (() => { const $1 = BoolInt(Ya)(1);
if ($1[0] === "BoolInt") {const b = $1[1];
const n = $1[2];
return foo(b)(n)} else {throw "no match";} })();
const _BoolInt = _arg1 => _arg2 => ["BoolInt", _arg1, _arg2];
const _Na = ["Na"];
const _Unit = ["Unit"];
const _Ya = ["Ya"];
const _foo = _b => _n => _Unit;
const _result = (() => { const $1 = _BoolInt(_Ya)(1);
if ($1[0] === "BoolInt") {const _b = $1[1];
const _n = $1[2];
return _foo(_b)(_n)} else {throw "no match";} })();

console.log(result);
console.log(_result);
*/
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ function $compareBuiltin(a, b) {
}
}

const False = ["False"];
const True = ["True"];
const foo = b => (() => { const $1 = b;
if ($1[0] === "True") {return 1} else if ($1[0] === "False") {return foo(True)} else {throw "no match";} })();
const result = foo(False);
const _False = ["False"];
const _True = ["True"];
const _foo = _b => (() => { const $1 = _b;
if ($1[0] === "True") {return 1} else if ($1[0] === "False") {return _foo(_True)} else {throw "no match";} })();
const _result = _foo(_False);

console.log(result);
console.log(_result);
*/
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ function $compareBuiltin(a, b) {
}
}

const Just = arg1 => ["Just", arg1];
const Nothing = ["Nothing"];
const Unit = ["Unit"];
const result = (() => { const $1 = Just(1);
if ($1[0] === "Just") {const n = $1[1];
return n} else if ($1[0] === "Nothing") {return (() => { const $1 = Just(Unit);
const _Just = _arg1 => ["Just", _arg1];
const _Nothing = ["Nothing"];
const _Unit = ["Unit"];
const _result = (() => { const $1 = _Just(1);
if ($1[0] === "Just") {const _n = $1[1];
return _n} else if ($1[0] === "Nothing") {return (() => { const $1 = _Just(_Unit);
if ($1[0] === "Unit") {return 0} else {throw "no match";} })()} else {throw "no match";} })();

console.log(result);
console.log(_result);
*/
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ function $compareBuiltin(a, b) {
}
}

const Unit = ["Unit"];
const identity = a => a;
const result = (() => { const $1 = identity(Unit);
if ($1[0] === "Unit") {return identity(0)} else {throw "no match";} })();
const _Unit = ["Unit"];
const _identity = _a => _a;
const _result = (() => { const $1 = _identity(_Unit);
if ($1[0] === "Unit") {return _identity(0)} else {throw "no match";} })();

console.log(result);
console.log(_result);
*/
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ function $compareBuiltin(a, b) {
}
}

const Cons = arg1 => arg2 => ["Cons", arg1, arg2];
const Nil = ["Nil"];
const Unit = ["Unit"];
const result = (() => { const $1 = Cons(1)(Nil);
if ($1[0] === "Cons") {const n = $1[1];
const rest = $1[2];
return n} else if ($1[0] === "Nil") {return (() => { const $1 = Cons(Unit)(Nil);
if ($1[0] === "Cons") {const u = $1[1];
const rest = $1[2];
const _Cons = _arg1 => _arg2 => ["Cons", _arg1, _arg2];
const _Nil = ["Nil"];
const _Unit = ["Unit"];
const _result = (() => { const $1 = _Cons(1)(_Nil);
if ($1[0] === "Cons") {const _n = $1[1];
const _rest = $1[2];
return _n} else if ($1[0] === "Nil") {return (() => { const $1 = _Cons(_Unit)(_Nil);
if ($1[0] === "Cons") {const _u = $1[1];
const _rest = $1[2];
return 2} else if ($1[0] === "Nil") {return 3} else {throw "no match";} })()} else {throw "no match";} })();

console.log(result);
console.log(_result);
*/
Loading

0 comments on commit cb2c97b

Please sign in to comment.