@@ -17,8 +17,10 @@ import qualified Generate.JavaScript.Variable as Var
17import qualified Optimize.DecisionTree as DT
17import qualified Optimize.DecisionTree as DT
20-- CODE CHUNKS
21-- CODE CHUNKS
23 = JsExpr (Expression ())
25 = JsExpr (Expression ())
24 | JsBlock [Statement ()]
26 | JsBlock [Statement ()]
@@ -74,11 +76,13 @@ toExpr code =
74 function [] stmts `call` []
76 function [] stmts `call` []
77-- DEFINITIONS
80-- DEFINITIONS
79generateDef :: Opt.Def -> State Int (Statement ())
83generateDef :: Opt.Def -> State Int (Statement ())
80generateDef def =
84generateDef def =
81− do (home, name, jsBody) <-
85+ do (_home, name, jsBody) <-
82 case def of
86 case def of
83 Opt.TailDef (Opt.Facts home _) name argNames body ->
87 Opt.TailDef (Opt.Facts home _) name argNames body ->
84 (,,) home name <$> generateTailFunction name argNames body
88 (,,) home name <$> generateTailFunction name argNames body
@@ -89,8 +93,10 @@ generateDef def =
89 return (Var.define name jsBody)
93 return (Var.define name jsBody)
92-- EXPRESSIONS
97-- EXPRESSIONS
94generateJsExpr :: Opt.Expr -> State Int (Expression ())
100generateJsExpr :: Opt.Expr -> State Int (Expression ())
95generateJsExpr optExpr =
101generateJsExpr optExpr =
96 toExpr <$> generateCode optExpr
102 toExpr <$> generateCode optExpr
@@ -199,8 +205,10 @@ generateCode expr =
199 jsExpr $ BuiltIn.crash home region maybeOptBranchProblem
205 jsExpr $ BuiltIn.crash home region maybeOptBranchProblem
202-- FUNCTIONS
209-- FUNCTIONS
204generateFunction :: [String] -> Opt.Expr -> State Int Code
212generateFunction :: [String] -> Opt.Expr -> State Int Code
205generateFunction args body =
213generateFunction args body =
206 do code <- generateCode body
214 do code <- generateCode body
@@ -235,8 +243,10 @@ generateFunctionWithArity rawArgs code =
235 foldl (\body arg -> function [arg] [ReturnStmt () (Just body)]) innerBody otherArgs
243 foldl (\body arg -> function [arg] [ReturnStmt () (Just body)]) innerBody otherArgs
238-- GENERATE IFS
247-- GENERATE IFS
240generateIf :: [(Opt.Expr, Opt.Expr)] -> Opt.Expr -> State Int Code
250generateIf :: [(Opt.Expr, Opt.Expr)] -> Opt.Expr -> State Int Code
241generateIf givenBranches givenFinally =
251generateIf givenBranches givenFinally =
@@ -292,8 +302,10 @@ crushIfsHelp visitedBranches unvisitedBranches finally =
292 crushIfsHelp (visiting : visitedBranches) unvisited finally
302 crushIfsHelp (visiting : visitedBranches) unvisited finally
295-- CASE EXPRESSIONS
306-- CASE EXPRESSIONS
297generateCase
309generateCase
298 :: String
310 :: String
299 -> Opt.Decider Opt.Choice
311 -> Opt.Decider Opt.Choice
@@ -305,8 +317,10 @@ generateCase exprName decider jumps =
305 foldM (goto labelRoot) decider jumps
317 foldM (goto labelRoot) decider jumps
308-- handle any jumps
321-- handle any jumps
310goto :: String -> [Statement ()] -> (Int, Opt.Expr) -> State Int [Statement ()]
324goto :: String -> [Statement ()] -> (Int, Opt.Expr) -> State Int [Statement ()]
311goto labelRoot deciderStmts (target, branch) =
325goto labelRoot deciderStmts (target, branch) =
@@ -324,8 +338,10 @@ toLabel root target =
324 Id () (root ++ "_" ++ show target)
338 Id () (root ++ "_" ++ show target)
327-- turn deciders into ifs and switches
342-- turn deciders into ifs and switches
329generateDecider
345generateDecider
330 :: String
346 :: String
331 -> String
347 -> String
@@ -380,8 +396,10 @@ testToExpr test =
380 Literal.literal lit
396 Literal.literal lit
383-- work with paths
400-- work with paths
385pathToTestableExpr :: String -> DT.Path -> DT.Test -> State Int (Expression ())
403pathToTestableExpr :: String -> DT.Path -> DT.Test -> State Int (Expression ())
386pathToTestableExpr root path exampleTest =
404pathToTestableExpr root path exampleTest =
387 do accessExpr <- generateJsExpr (pathToExpr root path)
405 do accessExpr <- generateJsExpr (pathToExpr root path)
@@ -415,8 +433,10 @@ pathToExpr root fullPath =
418-- BINARY OPERATORS
437-- BINARY OPERATORS
421 :: Var.Canonical
441 :: Var.Canonical
422 -> Opt.Expr
442 -> Opt.Expr
@@ -428,8 +448,10 @@ binop func left right =
428 jsExpr (makeExpr func jsLeft jsRight)
448 jsExpr (makeExpr func jsLeft jsRight)
431-- BINARY OPERATOR HELPERS
452-- BINARY OPERATOR HELPERS
433makeExpr :: Var.Canonical -> (Expression () -> Expression () -> Expression ())
455makeExpr :: Var.Canonical -> (Expression () -> Expression () -> Expression ())
434makeExpr qualifiedOp@(Var.Canonical home op) =
456makeExpr qualifiedOp@(Var.Canonical home op) =