Commit 1b78b676 authored by Michael Hanus 's avatar Michael Hanus
Browse files

Former JavaScript library packaged

parents
*~
.cpm
.curry
Copyright (c) 2017, Michael Hanus
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the names of the copyright holders nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
javascript - Libraries to represent JavaScript programs in Curry
================================================================
This package contains libraries with data types to
represent JavaScript programs in Curry and show them in
JavaScript syntax.
{
"name": "javascript",
"version": "1.0.0",
"author": "Michael Hanus <mh@informatik.uni-kiel.de>",
"synopsis": "Libraries to represent JavaScript programs in Curry",
"category": [ "Metaprogramming", "Web" ],
"dependencies": { },
"compilerCompatibility": {
"pakcs": ">= 1.14.0, < 2.0.0",
"kics2": ">= 0.5.0, < 2.0.0"
},
"exportedModules": [ "JavaScript.Types", "JavaScript.Show" ],
"license": "BSD-3-Clause",
"licenseFile": "LICENSE",
"source": {
"git": "https://git.ps.informatik.uni-kiel.de/curry-packages/javascript.git",
"tag": "$version"
}
}
------------------------------------------------------------------------------
--- A library to represent JavaScript programs.
---
--- @author Michael Hanus
--- @version May 2017
--- @category general
------------------------------------------------------------------------------
module JavaScript.Show
( showJSExp, showJSStat, showJSFDecl )
where
import List ( intercalate )
import JavaScript.Types
------------------------------------------------------------------------------
--- Shows a JavaScript expression as a string in JavaScript syntax.
showJSExp :: JSExp -> String
showJSExp (JSString s) = "\""++s++"\""
showJSExp (JSInt i) = show i
showJSExp (JSBool b) = if b then "true" else "false"
showJSExp (JSIVar i) = "x"++show i
showJSExp (JSIArrayIdx ai i) = "x"++show ai++"["++show i++"]"
showJSExp (JSOp op e1 e2) =
"(" ++ showJSExp e1 ++ " " ++ op ++ " " ++ showJSExp e2 ++ ")"
showJSExp (JSFCall f args) =
f ++ "(" ++ intercalate "," (map showJSExp args) ++ ")"
showJSExp (JSApply f e) = showJSExp f ++ "(" ++ showJSExp e ++ ")"
showJSExp (JSLambda params body) =
"function(" ++ intercalate "," (map (showJSExp . JSIVar) params) ++
") {" ++ concatMap (showJSStat 1) body ++ "} "
--- Shows a JavaScript statement as a string in JavaScript syntax
--- with indenting.
--- @param i - number of spaces to indent this statement
--- @param jstat - the JavaScript statement to print
showJSStat :: Int -> JSStat -> String
showJSStat i (JSAssign e1 e2) =
blanks i ++ showJSExp e1 ++ " = " ++ showJSExp e2 ++";"
showJSStat i (JSIf e s1 s2) =
blanks i ++ "if ("++showJSExp e++") {\n"++
concatMap ((++"\n") . (showJSStat (i+2))) s1 ++
if null s2
then blanks i ++ "}"
else blanks i ++ "} else {\n" ++
concatMap ((++"\n") . (showJSStat (i+2))) s2 ++
blanks i ++ "}"
showJSStat i (JSSwitch e bs) =
blanks i ++ "switch ("++showJSExp e++") {\n"++
concatMap showJSBranch bs ++
blanks i ++ "}"
where
showJSBranch (JSCase cs bstats) =
blanks (i+2) ++ "case \"" ++ cs ++ "\" :\n" ++
concatMap ((++"\n") . (showJSStat (i+4))) bstats ++
blanks (i+4) ++ "break;\n"
showJSBranch (JSDefault bstats) =
blanks (i+2) ++ "default :\n" ++
concatMap ((++"\n") . (showJSStat (i+4))) bstats
showJSStat i (JSPCall p args) =
blanks i ++ p ++ "(" ++ intercalate "," (map showJSExp args) ++ ")"
showJSStat i (JSReturn e) = blanks i ++ "return " ++ showJSExp e ++";"
showJSStat i (JSVarDecl vi) = blanks i ++ "var x" ++ show vi ++";"
blanks n = replicate n ' '
--- Shows a JavaScript function declaration as a string in JavaScript syntax.
showJSFDecl (JSFDecl f args body) =
"function " ++ f ++ "(" ++
intercalate "," (map showJSExp (map JSIVar args)) ++ ") {\n" ++
concatMap ((++"\n") . (showJSStat 2)) body ++"}\n\n"
------------------------------------------------------------------------------
------------------------------------------------------------------------------
--- A library to represent JavaScript programs.
---
--- @author Michael Hanus
--- @version May 2017
--- @category general
------------------------------------------------------------------------------
module JavaScript.Types
( JSExp(..), JSStat(..), JSBranch(..), JSFDecl(..), jsConsTerm )
where
------------------------------------------------------------------------------
--- Type of JavaScript expressions.
--- @cons JSString - string constant
--- @cons JSInt - integer constant
--- @cons JSBool - Boolean constant
--- @cons JSIVar - indexed variable
--- @cons JSIArrayIdx - array access to index array variable
--- @cons JSOp - infix operator expression
--- @cons JSFCall - function call
--- @cons JSApply - function call where the function is an expression
--- @cons JSLambda - (anonymous) function with indexed variables as arguments
data JSExp = JSString String
| JSInt Int
| JSBool Bool
| JSIVar Int
| JSIArrayIdx Int Int
| JSOp String JSExp JSExp
| JSFCall String [JSExp]
| JSApply JSExp JSExp
| JSLambda [Int] [JSStat]
--- Type of JavaScript statements.
--- @cons JSAssign - assignment
--- @cons JSIf - conditional
--- @cons JSSwitch - switch statement
--- @cons JSPCall - procedure call
--- @cons JSReturn - return statement
--- @cons JSVarDecl - local variable declaration
data JSStat = JSAssign JSExp JSExp
| JSIf JSExp [JSStat] [JSStat]
| JSSwitch JSExp [JSBranch]
| JSPCall String [JSExp]
| JSReturn JSExp
| JSVarDecl Int
-- Type of branches in a switch statement.
--- @cons JSCase - case branch
--- @cons JSDefault - default branch
data JSBranch = JSCase String [JSStat]
| JSDefault [JSStat]
-- Type of JavaScript function declarations.
data JSFDecl = JSFDecl String [Int] [JSStat]
------------------------------------------------------------------------------
--- Representation of constructor terms in JavaScript as array structures.
--- @param cons - the name of the data constructor
--- @param args - the arguments of the constructor term
jsConsTerm :: String -> [JSExp] -> JSExp
jsConsTerm cons args = JSFCall "new Array" (JSString cons : args)
------------------------------------------------------------------------------
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment