/* Sarumane Script by kmyacc+JavaScript. */ /* 2012-05-04*/ %{ %} %token NUMBER IDENT STRING COMMENT %left '+' '-' %left '*' '/' '%' %% program : stmt_list { return $1; } ; stmt_list : { $$ = '';} | stmt_list stmt { $$ = $1 + $2+ '\n';} ; stmt : assign { $$ = $1 + ';' } | funcall { $$ = $1 + ';' } | def_func | COMMENT ; funcall : IDENT '(' args ')' { $$ = $1 + '(' + $3 + ')';} | IDENT '(' ')' { $$ = $1 + '()';} ; args : expr | args ',' expr { $$ = $1 + ',' + $3;} ; assign : IDENT '=' expr { $$ = $1 + '=' + $3;} ; def_func : IDENT IDENT '(' args ')' '{' stmt_list '}' { $$ = $1 + ' ' + $2 + '(' + $4 + ')' + '{' + '\n' + $7 + '}';} | IDENT IDENT '(' args ')' '{' '}' { $$ = $1 + ' ' + $2 + '(' + $4 + ')' + '{}';} | IDENT IDENT '(' ')' '{' stmt_list '}' { $$ = $1 + ' ' + $2 + '()' + '{' + '\n' + $6 + '}';} | IDENT IDENT '(' ')' '{' '}' { $$ = $1 + ' ' + $2 + '()' + '{}';} ; expr : expr '+' expr { $$ = $1 + '+' + $3; } | expr '-' expr { $$ = $1 + '-' + $3; } | expr '*' expr { $$ = $1 + '*' + $3; } | expr '/' expr { $$ = $1 + '/' + $3; } | expr '%' expr { $$ = $1 + '%' + $3; } | '(' expr ')' { $$ = '(' + $2 + ')'; } | funcall | primary ; primary : NUMBER | IDENT | STRING ; %% /* Lexical analyzer */ var buffer; var token; var yylval; var toktype; var Pjs; function yylex() { //cut for space and tabs var res = buffer.match(/^\s+/); if (res != null) { buffer = buffer.substr(res[0].length); } //Empty if (buffer.length == 0) { return 0; } //COMMENT res = buffer.match(/^\/\//); if (res != null) { var index = buffer.indexOf("\n"); if (index == "-1") { token = buffer; } else { token = buffer.substr(0, index); } buffer = buffer.substr(token.length); yylval = token + ""; return COMMENT; } //IDENT res = buffer.match(/^[a-zA-Z_]\w*/); if (res != null) { token = res[0]; buffer = buffer.substr(res[0].length); yylval = token + ""; return IDENT; } //NUMBER res = buffer.match(/^\d+(\.\d+)?/); if (res != null) { token = res[0]; buffer = buffer.substr(res[0].length); yylval = token - 0; //return numeric-data return NUMBER; } //Text String res = buffer.match(/^".*"|'.*'/); if (res != null) { token = res[0]; buffer = buffer.substr(res[0].length); yylval = token + ""; return STRING; } //Single symbol token = buffer.substr(0, 1); buffer = buffer.substr(1); return token.charCodeAt(0); } function yyerror(msg) { yyprintln(msg); } var _bro = (window.opera? 4:(window.external?3:(window.controllers?1:(document.layers?2:0)))); var _ie5 = (navigator.appName.indexOf('Microsoft Internet Explorer')>=0 && document.getElementById)?1:0; // C // _dom : kind of DOM. // IE4 = 1, IE5+ = 2, NN4 = 3, NN6+ = 4, others = 0 var _dom = document.all ? (document.getElementById ? 2 : 1) : document.getElementById ? 4 : document.layers ? 3 : 0; function getDivFromName(nm) { if (_dom==4 || _dom==2) return document.getElementById(nm); if (_dom==1) return document.all(nm); if (_dom==3) { var s = ''; for (var i = 1; i < arguments.length; i++) s += 'document.layers.'+arguments[i]+'.'; return eval(s+'document.layers.'+nm); } return null; } function writeDivHTML(div,html) { if (document.layers) { div.document.open(); div.document.write(html); div.document.close(); } else if (typeof div.innerHTML!="undefined") { div.innerHTML = html; } } function setAnswer(ans) { ans= ans.replace(/\n/g,"
"); writeDivHTML(getDivFromName("output"), ans); } function clearAnswer() { calc_vars = {}; outputs = ""; setAnswer(outputs); } function stopScript(){ // not work if (Pjs) Pjs.exit(); } function runScript() { clearAnswer(); buffer = document.compiledtext.one.value; var script = yyparse(); setAnswer(script); stopScript(); var canvas = document.getElementById('canvas0'); try{ Pjs = new Processing(canvas, script); }catch(e){ alert(e); } return false; }