Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/libs/Editor_files/Parser.js
4627 views
1
var pgli = pgli || {};
2
pgli.lang = pgli.lang || {};
3
4
pgli.lang.Parser = gamecore.Base.extend('Parser',
5
{
6
7
xStruct: {
8
"scale": 0,
9
"x": 1,
10
"y": 2,
11
"width": 3,
12
"height": 4
13
},
14
15
16
patternVar:/\@(\w+)/g,
17
patternMethod: /\#(\w+)(\(([^\)]+)\))/g,
18
19
debug: 1,
20
21
22
parseExpression: function(string, scope, xform)
23
{
24
static = pgli.lang.Parser;
25
26
var self = this;
27
var orig = string;
28
var s = (scope !== null && typeof(scope) !== "undefined")
29
var x = (xform !== null && typeof(xform) !== "undefined" && xform.length > 0)
30
31
if(typeof(string) != "string")
32
return string;
33
34
if(scope != undefined)
35
{
36
string = string.replace(this.patternVar,function(match,varName)
37
{
38
if(x && varName in static.xStruct)
39
return xform[static.xStruct[varName]];
40
else if(s && varName in scope)
41
return scope[varName]
42
else
43
return 0
44
});
45
}
46
47
string = string.replace(this.patternMethod,function(match,methodName,a,params)
48
{
49
if(self.debug < 2) console.log(arguments);
50
return self.execFunction(methodName, params);
51
});
52
53
if(self.debug < 2) console.log("#Parsed expr: '"+string+"' from '"+orig+"'");
54
55
try {
56
return eval(string);
57
} catch (e) {
58
return string;
59
}
60
61
62
},
63
64
parseRepeat: function(string, scope)
65
{
66
var items = string.split(" ");
67
68
if(items.length != 4)
69
throw "Syntax error in repeat expression";
70
71
if(self.debug < 2) console.warn(items[0].substr(1))
72
if(self.debug < 2) console.warn(Number(this.parseExpression(items[1], scope)))
73
if(self.debug < 2) console.warn(items[2])
74
if(self.debug < 2) console.warn(Number(this.parseExpression(items[3], scope)))
75
76
return new pgli.lang.Iterator(
77
items[0].substr(1),
78
Number(this.parseExpression(items[1], scope)),
79
items[2],
80
Number(this.parseExpression(items[3], scope))
81
);
82
},
83
84
parseModule: function(string)
85
{
86
try
87
{
88
return JSON.parse(string);
89
}
90
catch(e)
91
{
92
trace("(!) Syntax error in module.");
93
return {error:"unable to parse module"};
94
}
95
},
96
97
execFunction: function(methodName, params)
98
{
99
var hasP = params != undefined;
100
var p = hasP ? params.replace(" ", "").split(',') : [];
101
102
if(methodName == "random")
103
{
104
var r = Math.random();
105
if(hasP && p.length == 2) try
106
{
107
var min = eval(p[0]);
108
var max = eval(p[1]);
109
r = Math.round(r * (max-min) + min);
110
} catch(e) {
111
console.warn('Bad method format: '+methodName+' / '+params);
112
return 0;
113
}
114
return r;
115
}
116
else if(methodName == "mod")
117
{
118
if(hasP && p.length == 2) try
119
{
120
var base = eval(p[0]);
121
var div = eval(p[1]);
122
if(div == 0) throw "Divide by 0";
123
return Math.floor(base/div);
124
} catch(e) {
125
console.warn('Bad method format: '+methodName+' / '+params);
126
}
127
return 0;
128
}
129
else
130
{
131
console.warn("Unsupported method : "+methodName);
132
return 0;
133
}
134
}
135
},
136
{
137
138
});
139
140
141
142