Documented and cleaned up parser a bit so I have an easier time circling back to this, refactored splash.js to be properly namespaced and handle window/stage resizing

This commit is contained in:
Ryan McGrath 2010-08-19 03:03:05 -07:00
parent 7ba03595eb
commit 91bf367d80
2 changed files with 56 additions and 22 deletions

View file

@ -8,28 +8,51 @@
* @Requires: jQuery 1.3.2 or greater (preferably 1.4.2, I make no guarantee for anything else, but it should work)
*/
if(typeof splash === "undefined")
var splash = {};
var splash = {
/* Stored reference to a jQuery'd window object, so we don't do repeated useless lookups. */
win: null,
var game_on = function(json) {
/* Stage for programs to run. Gets set at document ready, below. */
stage: null,
/* Handles resizing of the overall window and stage.
*
* @param win - jQuery'd window object.
*/
resizeStage: function(callbackfn) {
if(typeof callbackfn !== "undefined")
splash.stage.animate({"height": splash.win.height() - 50}, 800, callbackfn);
else
splash.stage.animate({"height": splash.win.height() - 50}, 800);
},
/* Self explanatory - masked call to console.log, "handles" browsers that don't support it. */
debug: function(msg) { if(typeof console !== "undefined" && typeof console.log === "function") console.log(msg); },
/* splash.game_on gets called by the compiled/written out .splash file. Every
* app gets a "main" function injected (see: parser.js - exports.writeApp), which
* is merely reference to calling splash.game_on.
*
* This, of course, has to walk the JSON chain and actually construct the events for
* the stage.
*/
game_on: function(json) {
if(/App Code/.test(json)) return;
debug(json);
// Parse JSON object here - need a lookup table for name/noun/verb/etc?
splash.debug(json);
}
};
if(typeof debug === "undefined")
var debug = function(msg) {
/* TODO: Support debugging in other annoying consoles (Opera, I'm looking at you) */
if(typeof console !== "undefined" && typeof console.log === "function") console.log(msg);
};
$(document).ready(function() {
splash.wrapper = $("#splash_app_wrapper");
splash.stage = $("#splash_app_wrapper");
splash.win = $(window);
splash.win.resize(splash.resizeStage);
/* Rough guesstimation for now on desired height, finagle later. Wait until the frame is our desired height before
* "running" our program.
* "running" our program (in this case, "main" is our program to run, injected by the compiler).
*/
splash.wrapper.animate({"height": $(window).height() - 50}, 800, function() {
main();
});
splash.resizeStage(main);
});

View file

@ -4,7 +4,7 @@
* @Requires:
* Node.js (Run latest for safety ;P)
* line_reader.js (custom, supplied in libraries)
* wrench.js (custom, supplied in libraries
* wrench.js (custom, supplied in libraries)
*
* @Note: This is just a compiler/parser. Core Splash takes place inside the browser - no need
* to muck around in DOM logic here when it's readily available over there. ;P
@ -28,6 +28,10 @@ var sys = require('sys'),
var determineAttrName = function(str) { return str.replace(whitespace, '').split(':')[0]; };
var determineVal = function(str) { return str.replace(whitespace, '').split(':')[1]; };
/* Aptly, returns a variable type from parsing a string.
*
* e.g, given "verb b:", this will return "verb"
*/
var getType = function(str) {
if(/verb/.test(str)) return "verb";
@ -45,7 +49,7 @@ exports.writeApp = function(text) {
* one directory up from this file for execution.
*/
wrench.copyDirSyncRecursive(currPath + '/lib/app_base', currPath + '/' + appName);
fs.writeFileSync(currPath + "/" + appName + "/scripts/compiled.js", "function main() { game_on(" + finalJSON + "); };", encoding="utf8");
fs.writeFileSync(currPath + "/" + appName + "/scripts/compiled.js", "function main() { splash.game_on(" + finalJSON + "); };", encoding="utf8");
};
/* In Splash, explanations are essentially variable declarations,
@ -58,23 +62,30 @@ exports.writeApp = function(text) {
* who: daniel
* action: move
* direction: left
* done
* end
*
* @Note: This parse is currently very "top down", and not as forgiving as it should be.
*/
while(expr.hasNextLine()) {
var currLine = expr.getNextLine(),
isBeginning = stripper.test(currLine);
isBeginning = stripper.test(currLine); /* Tests to see if we make a declaration. */
if(creatingBlock || isBeginning) {
creatingBlock = /end/.test(currLine) ? false : true;
/* type = verb/noun/etc
* name = name of the variable (e.g, verb run, name = run)
*/
if(isBeginning) block["type"] = getType(currLine);
if(isBeginning) block["name"] = currLine.replace(stripper, '').replace(whitespace, '');
else block[determineAttrName(currLine)] = determineVal(currLine);
/* If this is false, we're done building up a new block/JSON structure, so we can
* push it onto the stack for the browser to get down the road. We also reset the
* block here, so subsequent operations have a blank slate to work with.
*/
if(!creatingBlock) {
finalRep.push(JSON.stringify(block)); //this.parseExplain(block);
finalRep.push(JSON.stringify(block));
block = {};
}
}