@throw-out-error/minecraft-mcfunction
Version:
A simple way to create your mcfunction files using Typescript syntax.
95 lines • 3.94 kB
JavaScript
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _functions, _stack, _root, _counter;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Transpiler = void 0;
const events_1 = require("events");
const commands_1 = require("../commands");
const __1 = require("..");
const stack_1 = __importDefault(require("./stack"));
function getSubCommands(cmd) {
var _a, _b;
return ((_b = (_a = cmd[commands_1.Command.ARGUMENTS]) === null || _a === void 0 ? void 0 : _a.flatMap((arg) => {
if (!(arg instanceof commands_1.Command))
return [];
return [arg, ...getSubCommands(arg)];
})) !== null && _b !== void 0 ? _b : []);
}
class Transpiler extends events_1.EventEmitter {
constructor() {
super();
_functions.set(this, new Map());
_stack.set(this, new stack_1.default());
_root.set(this, '.');
_counter.set(this, 0);
this.on('command', (cmd) => {
try {
__classPrivateFieldGet(this, _stack).peek().add(cmd);
}
catch (_a) {
throw Error('Commands are only allowed inside functions');
}
});
this.on('function:start', () => __classPrivateFieldGet(this, _stack).push());
this.on('function:end', this.endFunction.bind(this));
}
static emit(type, ...args) {
var _a;
return (_a = this.running) === null || _a === void 0 ? void 0 : _a.emit(type, ...args);
}
endFunction({ name, call = true, } = {}) {
if (!name) {
name = `${__classPrivateFieldGet(this, _root)}/__${__classPrivateFieldGet(this, _counter)}`;
}
if (__classPrivateFieldGet(this, _functions).has(name)) {
throw Error(`Duplicate function name ${name}`);
}
// Pop stack
const commands = __classPrivateFieldGet(this, _stack).pop();
for (let cmd of commands) {
for (let sub of getSubCommands(cmd)) {
commands.delete(sub);
}
}
const fun = new __1.McFunction(name, commands);
__classPrivateFieldGet(this, _functions).set(name, fun);
if (call) {
__classPrivateFieldGet(this, _stack).peek().add(commands_1.run_function(name));
}
}
transpile(source, name = source.name || 'root') {
if (Transpiler.running)
throw Error('Transpilation in progress');
Transpiler.running = this;
__classPrivateFieldGet(this, _functions).clear();
__classPrivateFieldSet(this, _counter, 0);
__classPrivateFieldSet(this, _root, name);
this.emit('function:start');
source();
this.emit('function:end', { name, call: false });
Transpiler.running = undefined;
const rootFunction = __classPrivateFieldGet(this, _functions).get(name);
return {
rootFunction: rootFunction,
functions: new Map(__classPrivateFieldGet(this, _functions).entries()),
};
}
}
exports.Transpiler = Transpiler;
_functions = new WeakMap(), _stack = new WeakMap(), _root = new WeakMap(), _counter = new WeakMap();
//# sourceMappingURL=index.js.map