Skip to content

Commit

Permalink
made 'require' monad (for both cmdline and browser, but not tested in…
Browse files Browse the repository at this point in the history
… cmdline)
  • Loading branch information
zot committed Apr 25, 2012
1 parent f32582e commit ffe42d4
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 161 deletions.
1 change: 1 addition & 0 deletions browserRepl.cs
Expand Up @@ -9,6 +9,7 @@
write = null
envFrame = null
writeOutput = (line)->
if output?
output.innerHTML += "<span>#{line}</span>"
output.lastChild.scrollIntoView()

Expand Down
6 changes: 4 additions & 2 deletions browserRepl.js
Expand Up @@ -18,8 +18,10 @@
envFrame = null;

writeOutput = function writeOutput(line) {
output.innerHTML += "<span>" + line + "</span>";
return output.lastChild.scrollIntoView();
if (typeof output !== "undefined" && output !== null) {
output.innerHTML += "<span>" + line + "</span>";
return output.lastChild.scrollIntoView();
}
};

getHtml = function getHtml(x) {
Expand Down
13 changes: 7 additions & 6 deletions leisure.cs
Expand Up @@ -594,12 +594,13 @@ continueApply tag(apply(laz(func))(laz(arg)), func.leisureStart, arg.leisureEnd)
req = (name, gl)-> processDefs(require(name), gl)

processDefs = (res, gl)->
gl = gl ? global
if res.defs? then for i,v of res.defs
gl[i] = v
processTokenDefs res.tokenDefs
res.leisureFuncNames = ctx.leisureFuncNames
res.ctx = ctx
if res
gl = gl ? global
if res.defs? then for i,v of res.defs
gl[i] = v
processTokenDefs res.tokenDefs
res.leisureFuncNames = ctx.leisureFuncNames
res.ctx = ctx
res

processTokenDefs = (defs)->
Expand Down
106 changes: 11 additions & 95 deletions leisure.html
Expand Up @@ -2,7 +2,11 @@
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<script src="leisure.js"></script>
<link type="text/css" rel="stylesheet" href="leisure.css">
<!-- CSS themes: add the theme name to the body's class to use the theme -->
<link type="text/css" rel="stylesheet" href="gaudy.css">
<link type="text/css" rel="stylesheet" href="thin.css">
<script src="leisure.js" onload="LEI = event"></script>
<script src="prim.js"></script>
<script src="pretty.js"></script>
<script src="replCore.js"></script>
Expand All @@ -18,11 +22,6 @@
-->
<script>//<[!CDATA[
function init() {
/*
var input = document.getElementById('input')
var output = document.getElementById('output')
var defs = document.getElementById('defs')
*/
var doc = document.getElementById('doc')

try {
Expand Down Expand Up @@ -64,83 +63,12 @@
Repl.evalLine(window.getSelection().toString())
}
function evalDoc() {
var doc = document.getElementById('editor')

Repl.clearEnv()
var pgm = Notebook.initNotebook(doc)
try {
gen = ReplCore.generateCode('doc', pgm, false)
Leisure.eval(gen)
} catch (err) {
alert(err.stack)
}
Notebook.evalDoc(document.getElementById('editor'))
}
//]]>
</script>
<style type="text/css" media="screen">
body {
overflow: hidden;
}

</style>
<style>
.cell {
vertical-align: top;
outline: solid black 2px;
}
.codeExpr {
font-style: italic;
font-weight: bold;
margin: 3px;
}
.codeMain {
vertical-align: top;
margin: 3px;
display: inline-block;
border-width: 29px 24px 30px 34px;
-moz-border-image: url(Stock_Photo___Blackboard_by_shigeoARAI.jpg) 29 24 30 34 stretch;
-webkit-border-image: url(Stock_Photo___Blackboard_by_shigeoARAI.jpg) 29 24 30 34 stretch;
-o-border-image: url(Stock_Photo___Blackboard_by_shigeoARAI.jpg) 29 24 30 34 stretch;
border-image: url(Stock_Photo___Blackboard_by_shigeoARAI.jpg) 29 24 30 34 stretch;
}
.output {
border: red solid 2px;
margin: 3px;
left-margin: 10px;
display: inline-block;
padding: 5px;
}
.output span {
vertical-align: top;
}
.codeMainExpr {
vertical-align: top;
border-width: 29px 30px 37px 34px;
-moz-border-image: url(whiteboardTestWithShadow.png) 29 30 37 34 stretch;
-webkit-border-image: url(whiteboardTestWithShadow.png) 29 30 37 34 stretch;
-o-border-image: url(whiteboardTestWithShadow.png) 29 30 37 34 stretch;
border-image: url(whiteboardTestWithShadow.png) 29 30 37 34 stretch;
margin: 3px;
display: inline-block;
}
.codeName {
color: blue;
font-weight: bold;
margin: 3px;
}
.codeBody {
font-style: italic;
font-weight: bold;
margin: 3px;
}
.noselect, output, output * {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
</style>
</head>
<body onload="init()" style="border: 0; padding: 0; margin: 0">
<body class="thin" onload="init()">
<table style="position: absolute; vertical-align: top; width: 100%; height: 100%">
<tr style="height: 0">
<td>
Expand All @@ -152,15 +80,15 @@
</td>
</tr>
<tr style="height: 100%">
<td class="cell" style="width: 50%">
<div style="position: relative; width: 100%; height: 100%; overflow: auto">
<pre style="position: absolute; width: 100%; min-height: 100%; margin: 0; padding: 0" id="editor" class="cell" contentEditable="true">maluba a b=a
<td style="width: 50%">
<div class="leisure-notebook-holder">
<pre class="leisure-notebook" id="editor" contentEditable="true">maluba a b=a
true true
b

fred a=a

print hello
print 'hello'
do
n <- prompt 'What is your name?'
print (concat ['hello, ', n])
Expand All @@ -172,19 +100,7 @@
circle</pre>
</div>
</td>
<!--
<td class="cell" style="width: 50%">
<div style="width: 100%; height: 100%; position: relative; overflow: auto">
<pre id="output" style="position: absolute; width: 100%; margin: 0"></pre>
</div>
</td>
-->
</tr>
<!--
<tr style="height: 0"><td colspan="2" class="cell">
<table style="width: 100%; white-space: nowrap"><tr><td style="width: 100%"><input id="input" type="text" style="width: 100%; margin: 0"></td><td style="width: 0"><div>Load file: <input type="file" onchange="Repl.handleFiles(this)"></input><button onclick="Repl.clearEnv()">Clear</button></td></tr></table>
</td></tr>
-->
</table>
</body>
</html>
20 changes: 11 additions & 9 deletions leisure.js
Expand Up @@ -1083,17 +1083,19 @@ misrepresented as being the original software.

processDefs = function processDefs(res, gl) {
var i, v, _ref;
gl = gl != null ? gl : global;
if (res.defs != null) {
_ref = res.defs;
for (i in _ref) {
v = _ref[i];
gl[i] = v;
if (res) {
gl = gl != null ? gl : global;
if (res.defs != null) {
_ref = res.defs;
for (i in _ref) {
v = _ref[i];
gl[i] = v;
}
}
processTokenDefs(res.tokenDefs);
res.leisureFuncNames = ctx.leisureFuncNames;
res.ctx = ctx;
}
processTokenDefs(res.tokenDefs);
res.leisureFuncNames = ctx.leisureFuncNames;
res.ctx = ctx;
return res;
};

Expand Down
2 changes: 1 addition & 1 deletion maps.js
@@ -1,4 +1,4 @@
(function(){
var maps = (function(){
var root;

if ((typeof window !== 'undefined' && window !== null) && (!(typeof global !== 'undefined' && global !== null) || global === window)) {
Expand Down
46 changes: 44 additions & 2 deletions notebook.cs
Expand Up @@ -7,6 +7,7 @@
Leisure = window.Leisure
ReplCore = window.ReplCore
window.Notebook = root = {}
Prim = window.Prim
else root = exports ? this

delay = (func)->
Expand Down Expand Up @@ -116,6 +117,7 @@ parent.removeChild node

markupDefs = (defs)->
pgm = ''
auto = ''
for i in defs
[main, name, def, body] = i
if name?
Expand All @@ -128,13 +130,14 @@ bod.appendChild textNode('\n')
bx.appendChild bod
pgm += "#{name} #{def} #{body}\n"
else
if main.leisureAuto then auto += "#{body}\n"
s = codeSpan body, 'codeExpr'
s.appendChild textNode('\n')
s.setAttribute('generatedNL', '')
bx = box main, 'codeMainExpr', true
bx.appendChild s
makeOutputBox(bx)
pgm
[pgm, auto]

textNode = (text)-> document.createTextNode(text)

Expand All @@ -145,7 +148,6 @@ cleanOutput exBox
b.innerHTML = "X"
exBox.appendChild b
ReplCore.processLine(prepExpr(exBox.source.textContent), envFor(exBox))
#alert("Eval: #{exBox.source.textContent}")

cleanOutput = (exBox)->
while exBox.firstChild != exBox.lastChild
Expand All @@ -155,6 +157,7 @@ exBox.removeChild exBox.lastChild

envFor = (box)->
exBox = getBox box
require: req
write: (msg)->
div = document.createElement('div')
div.innerHTML = "#{msg}\n"
Expand Down Expand Up @@ -239,6 +242,7 @@ node.appendChild textNode(text)
ex = txt.substring(mainStart, mainEnd).match /^(.*[^ \n])[ \n]*$/
exEnd = if ex then mainStart + ex[1].length else mainEnd
outerRange = makeRange el, mainStart, exEnd
if leading.match /@auto/ then outerRange.leisureAuto = true
[outerRange, null, null, txt.substring(mainStart, exEnd), next]

makeRange = (el, off1, off2)->
Expand Down Expand Up @@ -283,11 +287,49 @@ nodeEnd child

addsLine = (node)-> node.nodeName == 'BR' or (node.nodeType == 1 and getComputedStyle(node, null).display == 'block' and node.childNodes.length > 0)

req = (file, cont)->
if !(file.match /\.js$/) then file = "#{file}.js"
name = file.substring(0, file.length - 3)
s = document.createElement 'script'
s.setAttribute 'src', file
s.addEventListener 'load', ->
Leisure.processDefs global[name], global
cont(_false())
document.head.appendChild s

loadQueue = []

queueAfterLoad = (func)-> loadQueue.push(func)

evalDoc = (el)->
Repl.clearEnv()
[pgm, auto] = initNotebook(el)
try
if auto
Notebook.queueAfterLoad -> Leisure.processDefs(Leisure.eval(ReplCore.generateCode('_doc', pgm, false)), global)
auto += "\nfinishLoading 'fred'\n";
Leisure.eval(ReplCore.generateCode('_auto', auto, false))
else Leisure.processDefs(Leisure.eval(ReplCore.generateCode('_doc', pgm, false)), global)
catch err
alert(err.stack)


Leisure.define 'finishLoading', (bubba)->
Prim.makeMonad 'end', (env, cont)->
for i in loadQueue
i()
loadQueue = []
cont(_false())

Prim.defaultEnv.require = req

root.initNotebook = initNotebook
root.bindNotebook = bindNotebook
root.evalOutput = evalOutput
root.cleanOutput = cleanOutput
root.envFor = envFor
root.queueAfterLoad = queueAfterLoad
root.evalDoc = evalDoc

#root.selection = -> window.getSelection().getRangeAt(0)
#root.test = -> flatten(root.selection().cloneContents().childNodes[0])

0 comments on commit ffe42d4

Please sign in to comment.