Skip to content

Commit

Permalink
javascript: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
raichoo committed Jun 24, 2014
1 parent c47bd86 commit d557267
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
6 changes: 2 additions & 4 deletions jsrts/Runtime-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ var i$RESERVE = function(vm,n) {
}

var __IDRRT__charCode = function(str) {
var type = typeof str;
if (type == "string")
if (typeof str == "string")
return str.charCodeAt(0);
else
return str;
}

var __IDRRT__fromCharCode = function(chr) {
var type = typeof chr;
if (type == "string")
if (typeof chr == "string")
return chr;
else
return String.fromCharCode(chr);
Expand Down
34 changes: 18 additions & 16 deletions src/IRTS/CodegenJavaScript.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE PatternGuards #-}
module IRTS.CodegenJavaScript (codegenJavaScript, codegenNode, JSTarget(..)) where

import Idris.AbsSyntax hiding (TypeCase)
Expand Down Expand Up @@ -582,7 +582,9 @@ jsIsNull :: JS -> JS
jsIsNull js = JSBinOp "==" js JSNull

jsBigInt :: JS -> JS
jsBigInt = JSNum . JSInteger . JSBigIntExpr
jsBigInt (JSString "0") = JSNum (JSInteger JSBigZero)
jsBigInt (JSString "1") = JSNum (JSInteger JSBigOne)
jsBigInt js = JSNum $ JSInteger $ JSBigIntExpr js

jsUnPackBits :: JS -> JS
jsUnPackBits js = JSIndex js $ JSNum (JSInt 0)
Expand All @@ -606,32 +608,32 @@ jsPackSBits16 js = JSNew "Int16Array" [JSArray [js]]
jsPackSBits32 :: JS -> JS
jsPackSBits32 js = JSNew "Int32Array" [JSArray [js]]

jsTAG :: JS -> JS
jsTAG js =
JSTernary (jsIsNumber js `jsOr` jsIsNull js) (
JSNum (JSInt $ negate 1)
) (JSTernary (js `jsInstanceOf` "i$CON") (
JSProj js "tag"
) (JSNum (JSInt $ negate 1)))

jsCTAG :: JS -> JS
jsCTAG js = JSProj js "tag"

-- TODO unsafe
jsCASE :: Bool -> Reg -> [(Int, [BC])] -> Maybe [BC] -> JS
jsCASE safe reg cases def =
{-JSSwitch (tag safe (translateReg reg)) (-}
JSSwitch (JSTernary (translateReg reg `jsInstanceOf` "i$CON") (
JSProj (translateReg reg) "tag") (JSNum (JSInt $ negate 1))) (
JSSwitch (tag safe $ translateReg reg) (
map ((JSNum . JSInt) *** prepBranch) cases
) (fmap prepBranch def)
where
tag :: Bool -> JS -> JS
tag True = jsCTAG
tag False = jsTAG

prepBranch :: [BC] -> JS
prepBranch bc = JSSeq $ map translateBC bc

jsTAG :: JS -> JS
jsTAG js =
JSTernary (jsIsNumber js `jsOr` jsIsNull js) (
JSNum (JSInt $ negate 1)
) (JSTernary (js `jsInstanceOf` "i$CON") (
JSProj js "tag"
) (JSNum (JSInt $ negate 1)))

jsCTAG :: JS -> JS
jsCTAG js = JSProj js "tag"


jsCONSTCASE :: Reg -> [(Const, [BC])] -> Maybe [BC] -> JS
jsCONSTCASE reg cases def =
JSCond $ (
Expand Down

0 comments on commit d557267

Please sign in to comment.