Skip to content
Browse files

Add Int support (closes #76).

  • Loading branch information...
1 parent d85c89b commit cdd47bebd920da4f77bf919be51535f0407942c9 @chrisdone chrisdone committed Sep 2, 2012
Showing with 45 additions and 5 deletions.
  1. +2 −1 fay.cabal
  2. +13 −0 js/runtime.js
  3. +2 −0 src/Language/Fay.hs
  4. +4 −1 src/Language/Fay/FFI.hs
  5. +2 −1 src/Language/Fay/Prelude.hs
  6. +7 −2 src/Language/Fay/Stdlib.hs
  7. +1 −0 src/Language/Fay/Types.hs
  8. +1 −0 tests/ints
  9. +13 −0 tests/ints.hs
View
3 fay.cabal
@@ -1,5 +1,5 @@
name: fay
-version: 0.9.1.0
+version: 0.9.1.1
synopsis: A compiler for Fay, a Haskell subset that compiles to JavaScript.
description: Fay is a proper subset of Haskell which can be compiled (type-checked)
with GHC, and compiled to JavaScript. It is lazy, pure, with a Fay monad,
@@ -55,6 +55,7 @@ extra-source-files: examples/ref.hs examples/alert.hs examples/console.hs examp
examples/tailrecursive.hs examples/data.hs examples/canvaswater.hs
examples/canvaswater.html examples/haskell.png
-- Test cases
+ tests/ints.hs tests/ints
tests/asPatternMatch tests/caseList.hs
tests/Double2.hs tests/fromInteger tests/List.hs
tests/RecCon tests/recordPatternMatch
View
13 js/runtime.js
@@ -161,6 +161,11 @@ function Fay$$fayToJs(type,fayObj){
jsObj = _(fayObj);
break;
}
+ case "int": {
+ // Serialize int, just force the argument. Ints are unboxed.
+ jsObj = _(fayObj);
+ break;
+ }
case "bool": {
// Bools are unboxed.
jsObj = _(fayObj);
@@ -210,6 +215,14 @@ function Fay$$jsToFay(type,jsObj){
fayObj = jsObj;
break;
}
+ case "int": {
+ // Int are unboxed, so there's no forcing to do.
+ // But we can do validation that the int has no decimal places.
+ // E.g. Math.round(x)!=x? throw "NOT AN INTEGER, GET OUT!"
+ fayObj = Math.round(jsObj);
+ if(fayObj!==jsObj) throw "Argument " + jsObj + " is not an integer!"
+ break;
+ }
case "bool": {
// Bools are unboxed.
fayObj = jsObj;
View
2 src/Language/Fay.hs
@@ -381,6 +381,7 @@ typeRep typ =
where nom = case typ of
StringType -> "string"
DoubleType -> "double"
+ IntType -> "int"
BoolType -> "bool"
DateType -> "date"
_ -> "unknown"
@@ -400,6 +401,7 @@ argType t =
case t of
TyCon "String" -> StringType
TyCon "Double" -> DoubleType
+ TyCon "Int" -> IntType
TyCon "Bool" -> BoolType
TyApp (TyCon "Fay") a -> JsType (argType a)
TyFun x xs -> FunctionType (argType x : functionTypeArgs xs)
View
5 src/Language/Fay/FFI.hs
@@ -5,7 +5,7 @@
module Language.Fay.FFI where
import Language.Fay.Types (Fay)
-import Prelude (Bool, Char, Double, String, error)
+import Prelude (Bool, Char, Double, String, Int, error)
-- | In case you want to distinguish values with a JsPtr.
data JsPtr a
@@ -19,6 +19,9 @@ instance Foreign ()
-- | All numbers in JS are double.
instance Foreign Double
+-- | Some numbers in JS are int.
+instance Foreign Int
+
-- | Characters are OK.
instance Foreign Char
View
3 src/Language/Fay/Prelude.hs
@@ -6,6 +6,7 @@ module Language.Fay.Prelude
,String
,Integer
,Double
+ ,Int
,Bool(..)
,Show(show)
,Read
@@ -40,7 +41,7 @@ import Language.Fay.Types (Fay)
import Data.Data
import GHC.Real (Ratio)
-import Prelude (Bool(..), Eq(..), Char, Double, Integer, Maybe(..), Ord, Read(..),
+import Prelude (Bool(..), Eq(..), Char, Double, Int, Integer, Maybe(..), Ord, Read(..),
Show(..), String, error, read, (&&), (*), (+), (-), (/), (/=), (<),
(<=), (==), (>), (>=), (||),Monad)
View
9 src/Language/Fay/Stdlib.hs
@@ -10,6 +10,7 @@ module Language.Fay.Stdlib
,elem
,enumFrom
,enumFromTo
+ ,fromIntegral
,filter
,find
,flip
@@ -41,7 +42,7 @@ module Language.Fay.Stdlib
where
import Language.Fay.FFI
-import Prelude (Double,Bool(..),Maybe(..),Ord((>),(<)),Eq(..),Monad(..),Num(..),(||))
+import Prelude (Double,Int,Bool(..),Maybe(..),Ord((>),(<)),Eq(..),Monad(..),Num(..),(||))
-- START
@@ -199,7 +200,8 @@ mapM_ _ [] = return ()
const :: a -> b -> a
const a _ = a
-length (x:xs) = 1 + length xs
+length :: [a] -> Int
+length (_:xs) = 1 + length xs
length [] = 0
mod :: Double -> Double -> Double
@@ -210,3 +212,6 @@ min = ffi "Math.min(%1,%2)"
max :: Double -> Double -> Double
max = ffi "Math.max(%1,%2)"
+
+fromIntegral :: Int -> Double
+fromIntegral = ffi "%1"
View
1 src/Language/Fay/Types.hs
@@ -195,6 +195,7 @@ data FundamentalType
| DateType
| StringType
| DoubleType
+ | IntType
| BoolType
-- | Unknown.
| UnknownType
View
1 tests/ints
@@ -0,0 +1 @@
+123
View
13 tests/ints.hs
@@ -0,0 +1,13 @@
+{-# LANGUAGE NoImplicitPrelude #-}
+
+module Ints where
+
+import Language.Fay.FFI
+import Language.Fay.Prelude
+
+main = do
+ print 123
+
+-- | Print using console.log.
+print :: Int -> Fay ()
+print = ffi "console.log(%1)"

0 comments on commit cdd47be

Please sign in to comment.
Something went wrong with that request. Please try again.