Find file
Fetching contributors…
Cannot retrieve contributors at this time
146 lines (133 sloc) 3.96 KB
<!DOCTYPE html>
<html>
<head>
<title>Pygmy</title>
<style>
pre#input
{
color: Green;
}
.operator
{
color: Black;
}
.identifier
{
color: #277;
}
.string
{
color: Red;
}
.number
{
color: #54F;
}
.error
{
border-bottom: 1px solid red;
border-bottom-style: dashed;
}
</style>
</head>
<body>
<pre id="input" contenteditable="true">
factorial:: (n) {
n = 0 => 1
n * factorial| n - 1
}
x: 12
alert| factorial| x
</pre>
<br />
<button id="tokenizer">Tokenize</button>
<button id="parser">Parse</button>
<button id="compiler">Compile</button><br />
<button id="runner">Run</button><br />
Output:<br />
<pre id="output">
</pre>
<iframe id="runframe" style="visibility:hidden;"></iframe>
</body>
<script src="lib.js"></script>
<script src="error.js"></script>
<script src="tokenizer.js"></script>
<script src="parser.js"></script>
<script src="compiler.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!--<script src="http://jquery-debounce.googlecode.com/files/jquery.debounce-1.0.5.js"></script>-->
<script>
if (!String.prototype.entityify) {
String.prototype.entityify = function () {
return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
};
}
$.fn.getPreText = function () {
var ce = $("<pre />").html(this.html());
if ($.browser.webkit)
ce.find("div").replaceWith(function () { return "\n" + this.innerHTML; });
if ($.browser.msie)
ce.find("p").replaceWith(function () { return this.innerHTML + "<br>"; });
if ($.browser.mozilla || $.browser.opera || $.browser.msie)
ce.find("br").replaceWith("\n");
return ce.text();
};
var getTokens = function () {
var t, text = $("#input").getPreText(),
error;
try {
t = tokenize(text);
}
catch (e) {
t = e;
error = true;
}
text = text.split("");
for (var i = 0; i < text.length; i++) {
text[i] = text[i].entityify();
}
for (var i = 0; i < t.length; i++) {
var token = t[i];
text[token.from] = "<span class=\"" + token.type + "\">" + text[token.from];
text[token.to - 1] += "</span>";
}
$("#input").html(text.join(""));
return t;
};
var getStatements = function () {
var tokens = getTokens(), statements;
try {
statements = parse(tokens);
}
catch (e) {
statements = e;
var text = text = $("#input").getPreText().split("");
for (var i = 0; i < e.length; i++) {
var token = e[i];
text[token.from] = "<span class=\"" + token.type + "\">" + text[token.from];
text[token.to - 1] += "</span>";
}
$("#input").html(text.join(""));
}
return statements;
};
$(function () {
$("#tokenizer").click(function () {
var t = getTokens();
$("#output").html(JSON.stringify(t, ["type", "value", "from", "to", "line", "message"], 4));
});
$("#parser").click(function () {
var statements = getStatements();
$("#output").html(JSON.stringify(statements, ["id", "type", "value", "arguments", "arity", "first", "second", "args", "from", "to", "message"], 4));
});
$("#compiler").click(function () {
$("#output").html(compile(getStatements(), "javascript"));
});
$("#runner").click(function () {
$("#runframe").attr("src", "");
$("#runframe").attr("src", "Runner.html");
});
getTokens();
});
</script>
</html>