UNPKG

ocat-lang

Version:

A programming language for the web design and development

211 lines (210 loc) 6.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Memory = void 0; const error_1 = require("../error"); const classes_1 = require("./classes"); class Memory { constructor(strict) { this.variables = new Map(); this.components = new Map(); this.functions = new Map(); this.objects = new Map(); this.orders = new Map(); this.templates = new Map(); this.collections = new Map(); this.themes = new Map(); this.properties = []; this.actuallyTheme = ""; this.strict = false; this.strict = strict !== null && strict !== void 0 ? strict : false; } // Config addProperty(name) { if (!this.properties.includes(name)) { this.properties.push(name); } } get getProperties() { return this.properties; } setStrict() { this.strict = true; } setOrder(name, content) { var _a; if (name && content) { if (name === "Set") { this.orders.set((_a = this.orders.get("Def")) !== null && _a !== void 0 ? _a : "", content); this.orders.delete("Def"); this.addProperty(name); } this.orders.set(name, content); } else { if (name) { this.orders.set(name, "true"); } } } getOrder(name) { if (name) { return this.orders.get(name); } } get getOrders() { return this.orders; } copyFrom(memory) { memory.getVars.forEach((element) => this.declareVar(element.name, element.value, element.type)); memory.getComponents.forEach((element) => this.declareComponent(element.name, element.value)); memory.getFunctions.forEach((element) => this.declareFunction(element.name, element.content)); } // Variables declareVar(name, value, type) { if (name && value && type) { if (this.variables.has(name)) { if (this.strict) { throw new error_1.CustomError(`Cannot declare variable ${name} because it already exists`, error_1.ErrorType.GetError); } else { this.variables.delete(name); throw new error_1.Warning(`Cannot declare variable ${name} because it already exists`); } } this.variables.set(name, { name: name, type: type, value: value, }); } } changeVar(name, value) { var _a, _b; if (name && value) { if (this.variables.has(name)) { this.variables.set(name, { name: name, type: (_b = (_a = this.variables.get(name)) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : "string", value: value, }); } else { if (this.strict) { throw new error_1.CustomError(`Cannot change variable ${name} because it does not exist`, error_1.ErrorType.GetError); } else { this.variables.set(name, { name: name, type: "string", value: value, }); throw new error_1.Warning(`Cannot change variable ${name} because it does not exist`); } } } } getVar(name) { const variable = this.variables.get(name); if (typeof (variable === null || variable === void 0 ? void 0 : variable.value) === typeof {}) { return variable; } else { return undefined; } } get getVars() { return this.variables; } // Components getComponent(name) { var _a; if (name) { return (_a = this.components.get(name)) === null || _a === void 0 ? void 0 : _a.value; } } declareComponent(name, value) { if (name && value) { this.components.set(name, { name: name, value: value, }); } } get getComponents() { return this.components; } // Functions declareFunction(name, value) { if (name && value) { this.functions.set(name, { name: name, content: value, }); } } getFunction(name) { if (name) { return this.functions.get(name); } } get getFunctions() { return this.functions; } get isStrict() { return this.strict; } // Objects declareObject(name) { this.objects.set(name, new classes_1._Object(this.objects.size, name)); } getObject(name) { return this.objects.get(name); } get getObjects() { return this.objects; } // Templates declareTemplate(name, value) { this.templates.set(name, value); } getTemplate(name) { return this.templates.get(name); } get getTemplates() { return this.templates; } // Collections declareCollection(name, value) { this.collections.set(name, value); } getCollection(name) { return this.collections.get(name); } get getCollections() { return this.collections; } // Themes declareTheme(name, properties) { this.themes.set(name, { name: name, properties: properties, }); } getTheme(name) { return this.themes.get(name); } get getThemes() { return this.themes; } getActuallyTheme() { const theme = this.getThemes.get(this.actuallyTheme); if (!theme) { throw new Error("No theme defined"); } return theme; } setActuallyTheme(name) { this.actuallyTheme = name; } } exports.Memory = Memory;