on master help sign in
cosmetic: add additional whitespace and suppress warning about unused declaration _home
8d31d35Evan Czaplicki
src/Generate/JavaScript/Expression.hs+23−1
@@ -17,8 +17,10 @@ import qualified Generate.JavaScript.Variable as Var
17import qualified Optimize.DecisionTree as DT
17import qualified Optimize.DecisionTree as DT
18
18
19
19
20+
20-- CODE CHUNKS
21-- CODE CHUNKS
21
22
23+
22data Code
24data Code
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` []
75
77
76
78
79+
77-- DEFINITIONS
80-- DEFINITIONS
78
81
82+
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)
90
94
91
95
96+
92-- EXPRESSIONS
97-- EXPRESSIONS
93
98
99+
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
200
206
201
207
208+
202-- FUNCTIONS
209-- FUNCTIONS
203
210
211+
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
236
244
237
245
246+
238-- GENERATE IFS
247-- GENERATE IFS
239
248
249+
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 =
242 let
252 let
@@ -292,8 +302,10 @@ crushIfsHelp visitedBranches unvisitedBranches finally =
292 crushIfsHelp (visiting : visitedBranches) unvisited finally
302 crushIfsHelp (visiting : visitedBranches) unvisited finally
293
303
294
304
305+
295-- CASE EXPRESSIONS
306-- CASE EXPRESSIONS
296
307
308+
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
306
318
307
319
320+
308-- handle any jumps
321-- handle any jumps
309
322
323+
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) =
312 let
326 let
@@ -324,8 +338,10 @@ toLabel root target =
324 Id () (root ++ "_" ++ show target)
338 Id () (root ++ "_" ++ show target)
325
339
326
340
341+
327-- turn deciders into ifs and switches
342-- turn deciders into ifs and switches
328
343
344+
329generateDecider
345generateDecider
330 :: String
346 :: String
331 -> String
347 -> String
@@ -380,8 +396,10 @@ testToExpr test =
380 Literal.literal lit
396 Literal.literal lit
381
397
382
398
399+
383-- work with paths
400-- work with paths
384
401
402+
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 =
415 expr
433 expr
416
434
417
435
436+
418-- BINARY OPERATORS
437-- BINARY OPERATORS
419
438
439+
420binop
440binop
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)
429
449
430
450
451+
431-- BINARY OPERATOR HELPERS
452-- BINARY OPERATOR HELPERS
432
453
454+
433makeExpr :: Var.Canonical -> (Expression () -> Expression () -> Expression ())
455makeExpr :: Var.Canonical -> (Expression () -> Expression () -> Expression ())
434makeExpr qualifiedOp@(Var.Canonical home op) =
456makeExpr qualifiedOp@(Var.Canonical home op) =
435 let
457 let