UNPKG

redc

Version:

Compiles RED lang into Minecraft schematics

37 lines (36 loc) 1.66 kB
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; import { parse } from './parser.js'; import { compile } from './compiler.js'; import { toSchematic } from './builder.js'; import { WORD_SIZE, CAPACITY, OFFSET, REDCError } from './meta.js'; export function build(file, options) { return new Promise(function (resolve, reject) { var _a; var lines = file.split('\n'); var data = parse(lines); var bin = compile(data, options); var len = bin.length; if (bin.length > CAPACITY) { throw new REDCError("Program is too large! Your program is ".concat(bin.length, " words, while programs can only be at most ").concat(CAPACITY, " words")); } else { var empty = '-'.repeat(WORD_SIZE); for (var i = bin.length; i < CAPACITY; i++) { bin.push(empty); } var next = __spreadArray(__spreadArray([ empty ], bin.slice(-OFFSET), true), bin.slice(0, -OFFSET), true); (_a = options === null || options === void 0 ? void 0 : options.out) === null || _a === void 0 ? void 0 : _a.call(options, "".concat(len, " bytes used out of ").concat(CAPACITY, " (").concat((100 * len / CAPACITY).toFixed(2), "%)")); resolve(toSchematic(next)); } }); }