puppy-transpiler
Version:
Puppy Transpiler
245 lines (244 loc) • 7.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var Lib = /** @class */ (function () {
function Lib(puppy, Matter) {
this.puppy = puppy;
this.Body = Matter['Body'];
this.Composite = Matter['Composite'];
this.Constraint = Matter['Constraint'];
}
/* python */
Lib.prototype.int = function (x, radix) {
if (typeof x === 'number') {
return x | 0;
}
if (typeof x === 'string') {
var v = Number.parseInt(x, radix);
return Number.isNaN(v) ? 0 : v;
}
if (typeof x === 'boolean') {
return x ? 1 : 0;
}
return x | 0;
};
Lib.prototype.float = function (x) {
if (typeof x === 'number') {
return x;
}
if (typeof x === 'string') {
var v = Number.parseFloat(x);
return isNaN(v) ? 0.0 : v;
}
if (typeof x === 'boolean') {
return x ? 1.0 : 0.0;
}
return x;
};
Lib.prototype.str = function (obj) {
var _this = this;
if (typeof obj === 'number' || typeof obj === 'string') {
return "" + obj;
}
if (typeof obj === 'boolean') {
return obj ? 'True' : 'False';
}
if (Array.isArray(obj)) {
return '[' + obj.map(function (x) { return _this.repr(x); }).join(', ') + ']';
}
if (obj === undefined) {
return 'undefined';
}
if (obj.x && obj.y) {
return "(" + obj.x + ", " + obj.y + ")";
}
if (obj.text) {
return obj.text;
}
return ('{' +
Object.keys(obj)
.map(function (key) { return key + ": " + _this.repr(obj[key]); })
.join(', ') +
'}');
};
Lib.prototype.repr = function (obj) {
if (typeof obj === 'string') {
if (obj.indexOf('"') == -1) {
return "\"" + obj + "\"";
}
return "'" + obj + "'";
}
return this.str(obj);
};
/* operator */
Lib.prototype.anyAdd = function (x, y) {
if (Array.isArray(x) && Array.isArray(y)) {
return x.concat(y);
}
return x + y;
};
Lib.prototype.anyMul = function (x, y) {
if (typeof x === 'string') {
var s = '';
for (var i = 0; i < y; i += 1) {
s += x;
}
return s;
}
if (Array.isArray(x)) {
var a = [];
for (var i = 0; i < y; i += 1) {
a = a.concat(x);
}
return a;
}
return x * y;
};
Lib.prototype.anyIn = function (x, a) {
return a.indexOf(x) >= 0;
};
Lib.prototype.range = function (x, y, z) {
var start = 0;
var end = 0;
var step = 1;
if (y === undefined) {
end = x;
}
else if (z !== undefined) {
start = x;
end = y;
step = z === 0 ? 1 : z;
}
else {
start = x;
end = y;
}
var xs = [];
if (start <= end) {
if (step < 0) {
step = -step;
}
for (var i = start; i < end; i += step) {
xs.push(i);
if (xs.length > 100000) {
// safety break
break;
}
}
}
else {
if (step > 0) {
step = -step;
}
for (var i = start; i > end; i += step) {
xs.push(i);
if (xs.length > 100000) {
// safety break
break;
}
}
}
return xs;
};
/* string/array (method) */
Lib.prototype.get = function (a, name, value, ref) {
var v = a[name];
if (v === undefined) {
return value;
}
return v;
};
Lib.prototype.index = function (a, index, ref) {
if (typeof a === 'string') {
return a.charAt((index + a.length) % a.length);
}
if (Array.isArray(a)) {
return a[(index + a.length) % a.length];
}
return undefined;
};
Lib.prototype.slice = function (a, x, y, ref) {
if (typeof a === 'string') {
if (y == undefined) {
y = a.length;
}
return a.substring(x, y);
}
if (Array.isArray(a)) {
if (y == undefined) {
y = a.length;
}
return a.slice(x, y);
}
return undefined;
};
Lib.prototype.find = function (s, sub) {
return s.indexOf(sub);
};
Lib.prototype.join = function (s, list) {
return list.join(s);
};
/* list */
Lib.prototype.append = function (xs, x) {
xs.push(x);
};
Lib.prototype.len = function (x) {
if (typeof x === 'string' || Array.isArray(x.length)) {
return x.length;
}
return 0;
};
// public map(func: any, xs: number[]) {
// return Array.from(xs, func); // funcがダメ
// }
/* vec */
Lib.prototype.vec = function (x, y) {
if (x === void 0) { x = 0; }
if (y === void 0) { y = 0; }
return { x: x, y: y };
};
/* Matter.Body */
Lib.prototype.setPosition = function (body, x, y) {
this.Body.setPosition(body, { x: x, y: y });
};
Lib.prototype.translate = function (body, x, y) {
this.Body.translate(body, { x: x, y: y });
};
Lib.prototype.applyForce = function (body, x, y, fx, fy) {
this.Body.applyForce(body, { x: x, y: y }, { x: fx, y: fy });
};
Lib.prototype.rotate = function (body, angle, _x, _y) {
this.Body.rotate(body, angle);
};
Lib.prototype.scale = function (body, sx, sy, _x, _y) {
this.Body.scale(body, sx, sy);
};
Lib.prototype.setAngle = function (body, angle) {
this.Body.setAngle(body, angle);
};
Lib.prototype.setVelocity = function (body, x, y) {
this.Body.setVelocity(body, { x: x, y: y });
};
Lib.prototype.setAngularVelocity = function (body, velocity) {
this.Body.setAngularVelocity(body, velocity);
};
Lib.prototype.setDensity = function (body, density) {
this.Body.setDensity(body, density);
};
Lib.prototype.setMass = function (body, mass) {
this.Body.setMass(body, mass);
};
Lib.prototype.setStatic = function (body, flag) {
this.Body.setStatic(body, flag);
};
Lib.prototype.newComosite = function (options) {
return this.Composite.create({ label: 'comosite' });
};
Lib.prototype.addBody = function (parent, body) {
this.Composite.addBody(parent, body);
};
Lib.prototype.addConstraint = function (parent, options) {
this.Composite.addConstraint(this.Constraint.create(options));
};
return Lib;
}());
exports.Lib = Lib;