Skip to content

Commit

Permalink
Started adding parameters to the CSM scripting language for #24
Browse files Browse the repository at this point in the history
  • Loading branch information
rehno-lindeque committed Dec 8, 2011
1 parent 511117a commit 48dda01
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 16 deletions.
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
<span><input id='source-autocompile' name='source-autocompile' type='checkbox' disabled='disabled'><label for='source-autocompile'>Auto-compile</label></span>
<input id='source-compile' name='source-compile' type='button' value='Compile'>
<textarea id='source-code' name='source-code'>
var a = range(0,10);

translate({ offset: [0.0, 0.4, 0.0] },
union(
box({ dimensions: [0.2, 0.5, 0.2] })
Expand Down
37 changes: 28 additions & 9 deletions src/compile.csm.coffee
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
# Compile the source code into a concrete solid model
compileCSM = (source, callback) ->
compileCSM = (csmSourceCode, callback) ->
#TODO: Do we need to supply our own try-catch block? For now we're just relying on JSandbox's error catching code...
prefix =

# Extract all parameters from the source
# (Note: if desired this could be optimized quite a bit)

variablesSource = csmSourceCode.match /var[^;]*;/g
csmSourceCode = (csmSourceCode.replace /var[^;]*;/g, '').trim()

variables = (v.split 'var' for v in variablesSource).flatten()
variables = (v.split ',' for v in variables).flatten()
variables = ((v.split '=')[0] for v in variables when (v.search '=') != -1)
variables = (v.trim() for v in variables when (v.search /\(\)\=\,/) == -1)

# Concatenate the sandbox source code
sandboxSourceCode =
'''
(function(){
/* BEGIN API */
''' + state.api.sourceCode +
''' + "\n#{state.api.sourceCode}\n" +
'''
try {
''' + "\n#{variablesSource.join '\n'}\n" +
'''
/* BEGIN SOURCE */
return scene(
'''
postfix =
''' + csmSourceCode +
'''
);
Expand All @@ -22,10 +39,12 @@ compileCSM = (source, callback) ->
}
})();
'''
#mecha.log prefix + source + postfix

console.log sandboxSourceCode

# Run the script inside a webworker sandbox
requestId = JSandbox.eval
data: prefix + source + postfix
data: sandboxSourceCode
callback: (result) ->
# TEMPORARY
console.log result
Expand Down
50 changes: 44 additions & 6 deletions static/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
_results = [];
for (_i = 0, _len = this.length; _i < _len; _i++) {
x = this[_i];
_results.push(Array.isArray(x) ? flatten(x) : [x]);
_results.push(Array.isArray(x) ? x.flatten() : [x]);
}
return _results;
}).call(this));
Expand Down Expand Up @@ -176,12 +176,50 @@

})();

compileCSM = function(source, callback) {
var postfix, prefix, requestId;
prefix = '(function(){\n /* BEGIN API */\n ' + state.api.sourceCode + ' try {\n /* BEGIN SOURCE */\n return scene(\n';
postfix = ' \n );\n } catch(err) {\n return String(err);\n }\n})();';
compileCSM = function(csmSourceCode, callback) {
var requestId, sandboxSourceCode, v, variables, variablesSource;
variablesSource = csmSourceCode.match(/var[^;]*;/g);
csmSourceCode = (csmSourceCode.replace(/var[^;]*;/g, '')).trim();
variables = ((function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = variablesSource.length; _i < _len; _i++) {
v = variablesSource[_i];
_results.push(v.split('var'));
}
return _results;
})()).flatten();
variables = ((function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = variables.length; _i < _len; _i++) {
v = variables[_i];
_results.push(v.split(','));
}
return _results;
})()).flatten();
variables = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = variables.length; _i < _len; _i++) {
v = variables[_i];
if ((v.search('=')) !== -1) _results.push((v.split('='))[0]);
}
return _results;
})();
variables = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = variables.length; _i < _len; _i++) {
v = variables[_i];
if ((v.search(/\(\)\=\,/)) === -1) _results.push(v.trim());
}
return _results;
})();
sandboxSourceCode = '(function(){\n /* BEGIN API */\n' + ("\n" + state.api.sourceCode + "\n") + '\ntry {\n' + ("\n" + (variablesSource.join('\n')) + "\n") + '\n/* BEGIN SOURCE */\nreturn scene(\n' + csmSourceCode + ' \n );\n } catch(err) {\n return String(err);\n }\n})();';
console.log(sandboxSourceCode);
return requestId = JSandbox.eval({
data: prefix + source + postfix,
data: sandboxSourceCode,
callback: function(result) {
console.log(result);
return callback(result);
Expand Down
Loading

0 comments on commit 48dda01

Please sign in to comment.