diff options
author | Minteck <contact@minteck.org> | 2022-12-06 08:31:49 +0100 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-12-06 08:31:49 +0100 |
commit | 57e58512fc1f5da319c1d149c5e961c43917782f (patch) | |
tree | 677bfbf392fb6e49f0c3c855cf884eb2809a7204 | |
parent | d4d1b63391be8a8ef8f1764e2d1a41810c8b2901 (diff) | |
download | wing-57e58512fc1f5da319c1d149c5e961c43917782f.tar.gz wing-57e58512fc1f5da319c1d149c5e961c43917782f.tar.bz2 wing-57e58512fc1f5da319c1d149c5e961c43917782f.zip |
Add events
-rw-r--r-- | .build | 2 | ||||
-rw-r--r-- | src/functions/list_personalities.js | 2 | ||||
-rw-r--r-- | src/index.ts | 1 | ||||
-rw-r--r-- | src/operators/item.js | 11 | ||||
-rw-r--r-- | src/operators/listcopy.js | 16 | ||||
-rw-r--r-- | src/wing/WingErrors.ts | 27 | ||||
-rw-r--r-- | src/wing/WingEvents.ts | 59 | ||||
-rw-r--r-- | src/wing/WingInterpreter.ts | 63 |
8 files changed, 168 insertions, 13 deletions
@@ -1 +1 @@ -8.33.20221205
\ No newline at end of file +9.5.20221206
\ No newline at end of file diff --git a/src/functions/list_personalities.js b/src/functions/list_personalities.js index 28d1ee2..b9a8e5e 100644 --- a/src/functions/list_personalities.js +++ b/src/functions/list_personalities.js @@ -1,4 +1,4 @@ -WingAPI.createFunction("lspers", true, (parameter, error) => { +WingAPI.createFunction("list_personalities", true, (parameter, error) => { if (!Object.keys(variables).includes(parameter)) { error("Invalid parameter"); return; diff --git a/src/index.ts b/src/index.ts index 9eaae8c..cc0ced4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,6 +20,7 @@ global.operators = {}; global.constants = []; global.modules = {}; global.lists = {}; +global.events = {}; global.foreach = {}; global.strictMode = false; diff --git a/src/operators/item.js b/src/operators/item.js index 17ff3a2..7363f3b 100644 --- a/src/operators/item.js +++ b/src/operators/item.js @@ -7,12 +7,17 @@ WingAPI.createOperator("item", [ } ], (parameters, value, error) => { if (personalities[parameters[0]].includes("list")) { - if (lists[variables[parameters[0]]][parameters[1]]) { - return lists[variables[parameters[0]]][parameters[1]]; + if (lists[variables[parameters[0]]][parseInt(parameters[1])]) { + return lists[variables[parameters[0]]][parseInt(parameters[1])]; } else { error("List index out of range"); } } else { - error("Variable does not have the list personality"); + let list = variables[parameters[0]].split(""); + if (list[parseInt(parameters[1])]) { + return list[parseInt(parameters[1])]; + } else { + error("List index out of range"); + } } })
\ No newline at end of file diff --git a/src/operators/listcopy.js b/src/operators/listcopy.js new file mode 100644 index 0000000..9a1c5d3 --- /dev/null +++ b/src/operators/listcopy.js @@ -0,0 +1,16 @@ +const { randomUUID } = require('crypto'); + +WingAPI.createOperator("listcopy", [ + { + type: "variable" + } +], (parameters, value, error) => { + let name = "[List '" + randomUUID() + "']"; + + if (personalities[parameters[0]].includes("list")) { + lists[name] = lists[variables[parameters[0]]]; + return name; + } else { + error("Variable does not have the list personality"); + } +})
\ No newline at end of file diff --git a/src/wing/WingErrors.ts b/src/wing/WingErrors.ts index 80437c4..1e28a67 100644 --- a/src/wing/WingErrors.ts +++ b/src/wing/WingErrors.ts @@ -1,23 +1,36 @@ import Wing from "./Wing"; import chalk from "chalk"; +import WingEvents from "./WingEvents"; export default class WingErrors extends Wing { public static error(text, line, globalLine, currentFunction, lines, filename, code) { if (global.strictMode) { - WingErrors.message("red", true, text, line, globalLine, currentFunction, lines, filename, code); - process.exit(2); + WingErrors.crash(text, line, globalLine, currentFunction, lines, filename, code); } else { - WingErrors.message("yellow", true, text, line, globalLine, currentFunction, lines, filename, code); + if (Object.keys(events).includes("warning")) { + WingEvents.emit("warning", text, line, globalLine, currentFunction, lines, filename); + } else { + WingErrors.message("yellow", true, text, line, globalLine, currentFunction, lines, filename, code); + } } } public static crash(text, line, globalLine, currentFunction, lines, filename, code) { - WingErrors.message("red", true, text, line, globalLine, currentFunction, lines, filename, code); + if (Object.keys(events).includes("error")) { + WingEvents.emit("error", text, line, globalLine, currentFunction, lines, filename); + } else { + WingErrors.message("red", true, text, line, globalLine, currentFunction, lines, filename, code); + } + if (!global.linter.enabled) process.exit(2); } public static deprecation(text, line, globalLine, currentFunction, lines, filename, code) { - WingErrors.message("cyan", false, text, line, globalLine, currentFunction, lines, filename, code); + if (Object.keys(events).includes("deprecation")) { + WingEvents.emit("deprecation", text, line, globalLine, currentFunction, lines, filename); + } else { + WingErrors.message("cyan", false, text, line, globalLine, currentFunction, lines, filename, code); + } } private static message(color, showTrace, text, line, globalLine, currentFunction, lines, filename, code) { @@ -129,4 +142,6 @@ export default class WingErrors extends Wing { } } } -}
\ No newline at end of file +} + +let events = global.events;
\ No newline at end of file diff --git a/src/wing/WingEvents.ts b/src/wing/WingEvents.ts new file mode 100644 index 0000000..147f21d --- /dev/null +++ b/src/wing/WingEvents.ts @@ -0,0 +1,59 @@ +import Wing from "./Wing"; +import WingErrors from "./WingErrors"; +import WingInterpreter from "./WingInterpreter"; + +export default class WingEvents extends Wing { + public static emit(func: string, parameters: string|null|undefined, index: number, globalLines: number, currentFunction: string|null|undefined, lines: string[], file: string) { + let interpret = WingInterpreter.interpret; + let error = WingErrors.error; + let crash = WingErrors.crash; + let deprecation = WingErrors.deprecation; + let execute = true; + + if (!Object.keys(events).includes(func)) { + execute = false; + } + + if (parameters && execute) { + while (parameters.match(/\w*(?<!\\)\$([a-zA-Z0-9-_]+)/m)) { + let vars = /\w*(?<!\\)\$([a-zA-Z0-9-_]+)/m.exec(parameters); + let name = vars[1]; + + if (!Object.keys(variables).includes(name)) { + error("Unresolved reference ($" + name + ")", index, globalLines, currentFunction, lines, file, "ERR_VARIABLE_NOENT"); + execute = false; + break; + } + + parameters = parameters.replace(/\w*(?<!\\)\$([a-zA-Z0-9-_]+)/m, variables[name]); + } + + parameters = parameters.replaceAll("\\$", "$"); + } + + if (execute) { + if (typeof events[func] === "string") { + let lines = events[func].split("\n"); + let initialIndex = parseInt(lines[1].substring(2)); + + if (parameters && lines[0].startsWith("@@$")) { + let parameter = lines[0].substring(3); + variables[parameter] = parameters; + + lines.shift(); + lines.shift(); + interpret(lines, initialIndex, func, file); + } else if (lines[0].startsWith("@@$")) { + parameters = ""; + } else { + lines.shift(); + lines.shift(); + interpret(lines, initialIndex, func, file); + } + } + } + } +} + +let events = global.events; +let variables = global.variables;
\ No newline at end of file diff --git a/src/wing/WingInterpreter.ts b/src/wing/WingInterpreter.ts index 71cc3ee..c9cb52e 100644 --- a/src/wing/WingInterpreter.ts +++ b/src/wing/WingInterpreter.ts @@ -6,6 +6,7 @@ import WingConditions from "./WingConditions"; import WingOperators from "./WingOperators"; import WingPersonalities from "./WingPersonalities"; import {randomUUID} from "crypto"; +import WingEvents from "./WingEvents"; export default class WingInterpreter extends Wing { public static interpret(lines: string[], globalLines: number|null|undefined, currentFunction: string|null|undefined, file: string) { @@ -34,6 +35,13 @@ export default class WingInterpreter extends Wing { if (currentContext && currentContext.type === "function") { functions[currentContext.target] += line + "\n"; contexts.push(currentContext); + index++; + globalLines++; + if (_debug) console.log("-------------------"); + continue; + } else if (currentContext && currentContext.type === "event") { + events[currentContext.target] += line + "\n"; + contexts.push(currentContext); index++; globalLines++; if (_debug) console.log("-------------------"); continue; @@ -61,6 +69,12 @@ export default class WingInterpreter extends Wing { } else if (line === "else do") { if (currentContext && currentContext.type === "function") { functions[currentContext.target] += line + "\n"; + index++; + globalLines++; + if (_debug) console.log("-------------------"); + continue; + } else if (currentContext && currentContext.type === "event") { + events[currentContext.target] += line + "\n"; index++; globalLines++; if (_debug) console.log("-------------------"); continue; @@ -93,6 +107,10 @@ export default class WingInterpreter extends Wing { let pop = true; if (contexts.length > 0) { + if (currentContext && currentContext.type === "event") { + if (contexts.length > 1) events[currentContext.target] += line + "\n"; + } + if (currentContext && currentContext.type === "function") { if (contexts.length > 1) functions[currentContext.target] += line + "\n"; } @@ -151,6 +169,13 @@ export default class WingInterpreter extends Wing { continue; } + if (currentContext && currentContext.type === "event") { + events[currentContext.target] += line + "\n"; + index++; globalLines++; + if (_debug) console.log("-------------------"); + continue; + } + if (currentContext && currentContext.type === "unmatched") { index++; globalLines++; if (_debug) console.log("-------------------"); @@ -261,10 +286,43 @@ export default class WingInterpreter extends Wing { functions[name] = "@@\n@@" + index + "\n"; } } + } else if (line.match(/^on ([a-zA-Z0-9-_]+)( (\$([a-zA-Z0-9-_]+)))?( +do|)$/m)) { + let match = /^on ([a-zA-Z0-9-_]+)( (\$([a-zA-Z0-9-_]+))*)?( +do|)$/m.exec(line); + let name = match[1]; + let execute = true; + + if (name.startsWith("_")) { + error("Event names starting with an underscore are reserved", index, globalLines, currentFunction, lines, file, "ERR_FUNCTION_RESERVED"); + execute = false; + } + + if (Object.keys(events).includes(name)) { + error("Attempted to override existing event " + name, index, globalLines, currentFunction, lines, file, "ERR_FUNCTION_OVERRIDE"); + execute = false; + } + + if (execute) { + let parameter = match[4]; + contexts.push({ + type: "event", + target: name + }); + + if (parameter) { + events[name] = "@@$" + parameter + "\n@@" + index + "\n"; + } else { + events[name] = "@@\n@@" + index + "\n"; + } + } + } else if (line.match(/^emit ([a-zA-Z0-9-_]+)( (.*)|)$/gm)) { + let func = /^emit ([a-zA-Z0-9-_]+)( (.*)|)$/gm.exec(line)[1]; + let parameters = /^emit ([a-zA-Z0-9-_]+)( (.*)|)$/gm.exec(line)[3]; + + WingEvents.emit(func, parameters, index, globalLines, currentFunction, lines, file); } else if (line.match(/^([a-zA-Z0-9-_]+)( (.*)|)$/gm)) { let execute = true; - let func = /([a-zA-Z0-9-_]+)( (.*)|)/gm.exec(line)[1]; - let parameters = /([a-zA-Z0-9-_]+)( (.*)|)/gm.exec(line)[3]; + let func = /^([a-zA-Z0-9-_]+)( (.*)|)$/gm.exec(line)[1]; + let parameters = /^([a-zA-Z0-9-_]+)( (.*)|)$/gm.exec(line)[3]; if (!Object.keys(functions).includes(func)) { error("Unresolved reference (function " + func + ")", index, globalLines, currentFunction, lines, file, "ERR_FUNCTION_NOENT"); @@ -447,6 +505,7 @@ let constants = global.constants; let functions = global.functions; let linter = global.linter.enabled; let lists = global.lists; +let events = global.events; let foreach = global.foreach; let modules = global.modules; let interpret = WingInterpreter.interpret; |