Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
curry-packages
curry2js
Commits
21521aff
Commit
21521aff
authored
Mar 30, 2017
by
Michael Hanus
Browse files
js includes added
parent
dff21d94
Changes
5
Hide whitespace changes
Inline
Side-by-side
.gitignore
View file @
21521aff
*~
.curry
.cpm
src/C2JSPackageConfig.curry
include/curry2js_prims.js
0 → 100644
View file @
21521aff
//----------------------
// Primitives for JavaScript compilation:
// higher-order application (in case of explicitApply):
function
apply
(
f
,
e
)
{
var
missing
=
f
[
1
];
if
(
missing
>
1
)
{
r
=
new
Array
();
r
[
0
]
=
f
[
0
];
r
[
1
]
=
missing
-
1
;
for
(
var
i
=
2
;
i
<
f
.
length
;
i
++
)
r
[
i
]
=
f
[
i
];
r
[
i
]
=
e
;
return
r
;
}
else
{
return
fullapply
(
f
,
e
);
}
}
// applying a partial constructor application to its last argument
// (in case of explicitApply):
function
fullconsapply
(
f
,
e
)
{
r
=
new
Array
();
r
[
0
]
=
f
[
0
];
for
(
var
i
=
2
;
i
<
f
.
length
;
i
++
)
r
[
i
-
1
]
=
f
[
i
];
r
[
i
-
1
]
=
e
;
return
r
;
}
// boolean equality:
function
boolEq
(
x1
,
x2
)
{
switch
(
typeof
(
x1
))
{
case
"
number
"
:
return
(
x1
==
x2
);
case
"
boolean
"
:
return
(
x1
==
x2
);
case
"
string
"
:
if
(
typeof
(
x2
)
==
"
string
"
)
{
return
(
x1
==
x2
);
}
else
{
return
boolEq
(
string2characterlist
(
x1
),
x2
);
}
case
"
object
"
:
if
(
typeof
(
x2
)
==
"
string
"
)
{
x2
=
string2characterlist
(
x2
);
}
var
eq
=
true
;
for
(
var
i
=
0
;
eq
&&
i
<
x1
.
length
;
i
++
)
eq
=
boolEq
(
x1
[
i
],
x2
[
i
])
;
return
eq
;
default
:
alert
(
"
Internal error: Unhandled type
"
+
typeof
(
x1
)
+
"
in boolEq!
"
);
}
}
// Implementation of Prelude.div
function
preludeDiv
(
x
,
y
)
{
return
(
x
-
(
x
%
y
))
/
y
;
}
// Implementation of Prelude.ord
function
preludeOrd
(
s
)
{
return
s
.
charCodeAt
(
0
);
}
// Implementation of Prelude.char
function
preludeChr
(
n
)
{
return
String
.
fromCharCode
(
n
);
}
// failed (should not occur in correctly translated programs!)
function
alertFailed
()
{
alert
(
"
Internal error: Failure occurred!
"
);
}
// Transform a standard string into a (lazy) list-of-character representation,
// i.e., only the first cons of the list is built
function
string2charlist
(
s
)
{
if
(
s
==
""
)
{
return
new
Array
(
"
[]
"
);
}
else
{
return
new
Array
(
"
:
"
,
s
.
charAt
(
0
),
s
.
substring
(
1
));
}
}
// Transform a standard string into a list-of-character representation
function
string2characterlist
(
s
)
{
var
len
=
s
.
length
;
var
i
=
s
.
length
;
var
tail
=
new
Array
(
"
[]
"
);
while
(
i
>
0
)
{
i
--
;
tail
=
new
Array
(
"
:
"
,
s
.
charAt
(
i
),
tail
);
}
return
tail
;
}
//----------------------
include/wui_prims.js
0 → 100644
View file @
21521aff
//----------------------
// Prelude for inclusion into WUI scripts:
var
AllowSubmission
=
true
;
// check whether submission is possible:
function
submissionAllowed
()
{
var
allowed
=
AllowSubmission
;
AllowSubmission
=
true
;
if
(
!
allowed
)
{
alert
(
"
Submission not possible due to errors in input fields!
"
);
}
return
allowed
;
}
// Boolean conjunction (similar to && but evaluate both arguments
// due to strictness)
function
And
(
b1
,
b2
)
{
return
b1
&&
b2
}
// Get integer value of a string element with a given name:
function
intValueOf
(
ElemName
)
{
return
parseInt
(
document
.
getElementsByName
(
ElemName
)[
0
].
value
)
;
}
// Get string value of an element with a given name:
function
stringValueOf
(
ElemName
)
{
if
(
LazyStringConversion
)
{
// defined by Curry2JS translator
return
document
.
getElementsByName
(
ElemName
)[
0
].
value
;
}
else
{
return
string2characterlist
(
document
.
getElementsByName
(
ElemName
)[
0
].
value
)
;
}
}
// Get integer value of a selection element with a given name:
function
selectValueOf
(
ElemName
,
ValueField
)
{
return
ValueField
[
parseInt
(
document
.
getElementsByName
(
ElemName
)[
0
].
value
)]
;
}
// Get Boolean value of a check box element with a given name:
function
checkBoxValueOf
(
ElemName
)
{
return
document
.
getElementsByName
(
ElemName
)[
0
].
checked
;
}
// Show or hide the error message in a WUI element by modifying the CSS
// style class depending on the Boolean value
// of the second argument, and return the Boolean value:
function
setErrorClassName
(
ElemName
,
b
)
{
var
field
=
document
.
getElementById
(
ElemName
);
var
errmsgfield
=
document
.
getElementById
(
"
MSG_
"
+
ElemName
);
if
(
b
)
{
if
(
errmsgfield
)
{
errmsgfield
.
className
=
"
wuihide
"
;
}
if
(
field
)
{
field
.
className
=
"
wuipassiveerrmsg
"
;
}
}
else
{
if
(
errmsgfield
)
{
errmsgfield
.
className
=
"
wuinohide
"
;
}
if
(
field
)
{
field
.
className
=
"
wuiactiveerrmsg
"
;
}
}
return
b
;
}
// Hide the error message in a WUI element and return true:
function
unsetErrorClassName
(
ElemName
)
{
var
field
=
document
.
getElementById
(
ElemName
);
var
errmsgfield
=
document
.
getElementById
(
"
MSG_
"
+
ElemName
);
if
(
errmsgfield
)
{
errmsgfield
.
className
=
"
wuihide
"
;
}
if
(
field
)
{
field
.
className
=
"
wuipassiveerrmsg
"
;
}
return
true
;
}
// Return input string without prefix and suffix white spaces
function
stripWhiteSpaces
(
s
)
{
var
len
=
s
.
length
;
var
start
=
0
;
var
stop
=
len
;
while
(
start
<
len
&&
s
.
charAt
(
start
)
==
'
'
)
{
start
++
;
}
while
(
stop
>
start
&&
s
.
charAt
(
stop
-
1
)
==
'
'
)
{
stop
--
;
}
return
s
.
substring
(
start
,
stop
);
}
// Contains an element an integer string?
function
parseIntCheck
(
ElemName
)
{
var
elem
=
document
.
getElementsByName
(
ElemName
)[
0
];
var
elemstr
=
stripWhiteSpaces
(
elem
.
value
);
var
i
=
0
;
var
c
;
if
(
elemstr
==
""
)
{
return
false
;
}
if
(
elemstr
.
charAt
(
0
)
==
"
-
"
)
{
i
++
;
}
while
(
i
<
elemstr
.
length
)
{
c
=
elemstr
.
charCodeAt
(
i
);
if
(
c
<
48
||
c
>
57
)
{
return
false
;
}
i
++
;
}
return
true
;
}
// Is a string not empty?
function
notEmpty
(
s
)
{
return
(
s
!=
""
)
;
}
// Transform an array into a list representation:
function
array2list
(
a
)
{
return
array2listi
(
a
,
0
);
}
function
array2listi
(
a
,
i
)
{
if
(
i
>
a
.
length
)
{
alert
(
"
Internal error in array2list
"
);
}
if
(
i
==
a
.
length
)
{
return
new
Array
(
"
[]
"
);
}
else
{
return
new
Array
(
"
:
"
,
a
[
i
],
array2listi
(
a
,
i
+
1
));
}
}
//----------------------
package.json
View file @
21521aff
...
...
@@ -5,6 +5,7 @@
"synopsis"
:
"A compiler for Curry into JavaScript programs used in WUIs."
,
"category"
:
[
"Web"
],
"dependencies"
:
{
},
"configModule"
:
"C2JSPackageConfig"
,
"executable"
:
{
"name"
:
"curry2js"
,
"main"
:
"Curry2JS"
...
...
src/Curry2JS.curry
View file @
21521aff
...
...
@@ -5,24 +5,30 @@
--- @version January 16, 2007
------------------------------------------------------------------------------
import Distribution (installDir)
import JavaScript
import List
module Curry2JS (main) where
import Char (isAlphaNum)
import Directory (doesFileExist, getModificationTime)
import FlatCurry.Types
import FlatCurry.Files
import FlatCurry.Compact
import F
latCurry.Show
import F
ilePath ((</>))
import Integer
import System (system, getArgs)
import Directory
import Char (isAlphaNum)
import Unsafe
import JavaScript
import List
import Maybe
import ReadNumeric (readNat)
import System (system, getArgs)
import C2JSPackageConfig (packagePath)
------------------------------------------------------------------------------
-- General definitions:
-- Directory for js includes:
jsIncludeDir :: String
jsIncludeDir = packagePath </> "include"
-- Should higher-order calls (i.e., "apply") be implemented as an explicit
-- apply function? Otherwise, they are implemented as anonymous functions.
explicitApply :: Bool
...
...
@@ -812,8 +818,8 @@ generateJavaScript mainmodname imports mainfuns target = do
(concatMap showJSFDecl (flatprog2JS prog) ++
"var LazyStringConversion = " ++
(if lazyStringConversion then "true" else "false") ++ ";\n\n")
readFile (
installDir++"/include/
curry2js_prims.js") >>= appendFile target
readFile (
installDir++"/include/
wui_prims.js") >>= appendFile target
readFile (
jsIncludeDir </> "
curry2js_prims.js") >>= appendFile target
readFile (
jsIncludeDir </> "
wui_prims.js"
) >>= appendFile target
putStrLn $ "JavaScript program written into \""++target++"\""
system $ "chmod 644 "++target
done
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment