-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export duplicate JavaScript functions with parametric polymorphism
- Loading branch information
1 parent
3c0f092
commit a4dd0c4
Showing
10 changed files
with
1,881 additions
and
1,497 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
module IDL.Cleaner | ||
( declsToIdl | ||
, getEnums | ||
, getFuncs | ||
, getTypes | ||
) where | ||
|
||
import Data.List (foldl') | ||
|
||
import qualified Data.Map as Map | ||
|
||
import IDL.AST | ||
|
||
-- constants | ||
|
||
excludedTypes :: [String] | ||
excludedTypes = | ||
[ "ArrayBuffer" | ||
, "DOMString" | ||
, "Float32Array" | ||
, "FloatArray" | ||
, "GLbitfield" | ||
, "GLboolean" | ||
, "GLbyte" | ||
, "GLclampf" | ||
, "GLenum" | ||
, "GLfloat" | ||
, "GLint" | ||
, "GLintptr" | ||
, "GLshort" | ||
, "GLsizei" | ||
, "GLsizeiptr" | ||
, "GLubyte" | ||
, "GLuint" | ||
, "GLushort" | ||
, "HTMLCanvasElement" | ||
, "Int32Array" | ||
, "WebGLContextAttributes" | ||
, "any" | ||
, "boolean" | ||
, "long" | ||
, "object" | ||
, "sequence" | ||
, "void" | ||
] | ||
|
||
-- public functions | ||
|
||
declsToIdl :: [Decl] -> IDL | ||
declsToIdl = cleanup . foldr partition emptyIdl | ||
|
||
getTypes :: IDL -> [Type] | ||
getTypes = map snd . Map.toList . types | ||
|
||
getEnums :: IDL -> [Decl] | ||
getEnums = map snd . Map.toList . enums | ||
|
||
getFuncs :: IDL -> [Decl] | ||
getFuncs = map snd . Map.toList . functions | ||
|
||
-- private functions | ||
|
||
partition :: Decl -> IDL -> IDL | ||
partition e@Enum{} idl = idl | ||
{ enums = Map.insert (enumName e) e (enums idl) | ||
} | ||
partition f@Function{} idl = idl | ||
{ functions = underscore f $ functions idl | ||
, types = insertFuncTypes f $ types idl | ||
} | ||
partition _ idl = idl | ||
|
||
underscore :: Decl -> Map.Map String Decl -> Map.Map String Decl | ||
underscore f fs | ||
| Map.member name fs = underscore (f { methodName = name ++ "_" }) fs | ||
| otherwise = Map.insert name f fs | ||
where | ||
name = methodName f | ||
|
||
insertFuncTypes :: Decl -> Map.Map String Type -> Map.Map String Type | ||
insertFuncTypes f types = foldl' insert types ftypes | ||
where | ||
ftypes = methodRetType f : map argType (funcArgs f) | ||
insert ts Generic = ts | ||
insert ts t = Map.insert (typeName t) t ts | ||
|
||
cleanup :: IDL -> IDL | ||
cleanup idl = idl { types = removeExcluded $ types idl } | ||
where | ||
removeExcluded types = foldl' (flip Map.delete) types excludedTypes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.