Path: blob/main/projects/HexGL/libs/Editor_files/Parser.js
4627 views
var pgli = pgli || {};1pgli.lang = pgli.lang || {};23pgli.lang.Parser = gamecore.Base.extend('Parser',4{56xStruct: {7"scale": 0,8"x": 1,9"y": 2,10"width": 3,11"height": 412},131415patternVar:/\@(\w+)/g,16patternMethod: /\#(\w+)(\(([^\)]+)\))/g,1718debug: 1,192021parseExpression: function(string, scope, xform)22{23static = pgli.lang.Parser;2425var self = this;26var orig = string;27var s = (scope !== null && typeof(scope) !== "undefined")28var x = (xform !== null && typeof(xform) !== "undefined" && xform.length > 0)2930if(typeof(string) != "string")31return string;3233if(scope != undefined)34{35string = string.replace(this.patternVar,function(match,varName)36{37if(x && varName in static.xStruct)38return xform[static.xStruct[varName]];39else if(s && varName in scope)40return scope[varName]41else42return 043});44}4546string = string.replace(this.patternMethod,function(match,methodName,a,params)47{48if(self.debug < 2) console.log(arguments);49return self.execFunction(methodName, params);50});5152if(self.debug < 2) console.log("#Parsed expr: '"+string+"' from '"+orig+"'");5354try {55return eval(string);56} catch (e) {57return string;58}596061},6263parseRepeat: function(string, scope)64{65var items = string.split(" ");6667if(items.length != 4)68throw "Syntax error in repeat expression";6970if(self.debug < 2) console.warn(items[0].substr(1))71if(self.debug < 2) console.warn(Number(this.parseExpression(items[1], scope)))72if(self.debug < 2) console.warn(items[2])73if(self.debug < 2) console.warn(Number(this.parseExpression(items[3], scope)))7475return new pgli.lang.Iterator(76items[0].substr(1),77Number(this.parseExpression(items[1], scope)),78items[2],79Number(this.parseExpression(items[3], scope))80);81},8283parseModule: function(string)84{85try86{87return JSON.parse(string);88}89catch(e)90{91trace("(!) Syntax error in module.");92return {error:"unable to parse module"};93}94},9596execFunction: function(methodName, params)97{98var hasP = params != undefined;99var p = hasP ? params.replace(" ", "").split(',') : [];100101if(methodName == "random")102{103var r = Math.random();104if(hasP && p.length == 2) try105{106var min = eval(p[0]);107var max = eval(p[1]);108r = Math.round(r * (max-min) + min);109} catch(e) {110console.warn('Bad method format: '+methodName+' / '+params);111return 0;112}113return r;114}115else if(methodName == "mod")116{117if(hasP && p.length == 2) try118{119var base = eval(p[0]);120var div = eval(p[1]);121if(div == 0) throw "Divide by 0";122return Math.floor(base/div);123} catch(e) {124console.warn('Bad method format: '+methodName+' / '+params);125}126return 0;127}128else129{130console.warn("Unsupported method : "+methodName);131return 0;132}133}134},135{136137});138139140141142