Skip to content

Commit

Permalink
Replace Panic js constructor with Throw
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Grant Jeffries committed Nov 4, 2019
1 parent 3006e8a commit ef52cc2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 22 deletions.
8 changes: 2 additions & 6 deletions bowtie-js/src/Bowtie/JS/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@ data AST
| IndexArray AST Natural
| IfThen AST AST
| Else AST
| Throw Text
| Throw AST -- Will only be used with JSString
| Equal AST AST
| LambdaUnit AST -- ^ @(() => { " <> ast <> "})()@

| JSInt Integer
| JSString Text
| JSOp Operation
deriving (Eq, Show)

data Operation
= Compare AST AST
| Compare AST AST
| Plus AST AST -- ^ Only works on Ints
| Multiply AST AST -- ^ Only works on Ints
| ShowInt AST -- ^ Only works on Int
| Panic AST -- ^ Only works on Text
deriving (Eq, Show)
8 changes: 4 additions & 4 deletions bowtie-js/src/Bowtie/JS/Imperativize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ coreToImp topExpr =
(Block
( Assignment (Var (Id "$1")) (coreToImp expr)
: fmap altToImp alts
<> [Else (Throw "no match")]
<> [Else (Throw (JSString "no match"))]
))

Core.PrimInt n ->
JSInt n

Core.PrimOp op ->
JSOp (coreOperationToImp op)
coreOperationToImp op

coreOperationToImp :: Core.Operation -> JS.Operation
coreOperationToImp :: Core.Operation -> JS.AST
coreOperationToImp op =
case op of
Core.Compare e1 e2 ->
Expand All @@ -105,7 +105,7 @@ coreOperationToImp op =
ShowInt (coreToImp expr)

Core.Panic expr ->
Panic (coreToImp expr)
LambdaUnit (Throw (coreToImp expr))

-- eg ["Maybe", 5], not [Maybe, 5]
conToString :: Id -> JS.AST
Expand Down
14 changes: 2 additions & 12 deletions bowtie-js/src/Bowtie/JS/Serialize.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Bowtie.JS.Serialize
( serializeTop
, serialize
, serializeOperation
) where

import Bowtie.JS.AST
Expand Down Expand Up @@ -51,8 +50,8 @@ serialize topAst =
Else ast ->
" else { " <> serialize ast <> " }"

Throw t ->
"throw \"" <> t <> "\""
Throw ast ->
"throw " <> serialize ast

Equal a1 a2 ->
serialize a1 <> " === " <> serialize a2
Expand All @@ -66,12 +65,6 @@ serialize topAst =
JSString t ->
"\"" <> t <> "\""

JSOp op ->
serializeOperation op

serializeOperation :: Operation -> Text
serializeOperation op =
case op of
Compare ast1 ast2 ->
"$compareBuiltin(" <> serialize ast1 <> ", " <> serialize ast2 <> ")"

Expand All @@ -84,9 +77,6 @@ serializeOperation op =
ShowInt ast ->
"$unicodeListizeBuiltin(" <> serialize ast <> ".toString())"

Panic expr -> -- Will only be used with Text
"(() => { throw " <> serialize expr <> "})()"

serializeId :: Id -> Text
serializeId (Id t) =
"_" <> t

0 comments on commit ef52cc2

Please sign in to comment.