aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/types.js82
1 files changed, 80 insertions, 2 deletions
diff --git a/lib/types.js b/lib/types.js
index ed83f12..3e37424 100644
--- a/lib/types.js
+++ b/lib/types.js
@@ -1,4 +1,82 @@
//Gods-awful type-checking logic.
-//TypeName: string, Params: Array<string>|Object<string>
-function workOutType() \ No newline at end of file
+//TypeName: string, Params: Array<string>
+function Type(TypeName,Params=[]) {
+ this.typename = "";
+ this.mode = "";
+ //Union/Tuple
+ if (/^\(.+\)$/.test(TypeName)) {
+ const types = [];
+ var depth = 0;
+ var workspace = "";
+ for (const c of /^\((.+)\)$/.exec(TypeName)[1]) {
+ switch (c) {
+ case '(':
+ case '<':
+ depth++;
+ break;
+ case ')':
+ case '>':
+ depth--;
+ break;
+ case ',':
+ if (depth == 0) {
+ this.mode = "tuple";
+ types.push(workspace);
+ workspace = "";
+ continue;
+ }
+ break;
+ case '|':
+ if (depth == 0) {
+ this.mode = "union";
+ types.push(workspace);
+ workspace = "";
+ continue;
+ }
+ break;
+ }
+ workspace += c;
+ }
+ if (workspace) types.push(workspace);
+ this.template = types.map(t => t.trim()).map(t => ["any", ...Params].includes(t) ? t : new Type(t, Params));
+
+ }
+ //Template
+ else if (/^[^<]+<.+>$/.test(TypeName)) {
+ this.typename = /^([^<]+)</.exec(TypeName)[1];
+ this.mode = "templated";
+ const types = [];
+ var depth = 0;
+ var workspace = "";
+ for (const c of /^[^<]+<(.+)>$/.exec(TypeName)[1]) {
+ switch (c) {
+ case '(':
+ case '<':
+ depth++;
+ break;
+ case ')':
+ case '>':
+ depth--;
+ break;
+ case ',':
+ if (depth == 0) {
+ types.push(workspace);
+ workspace = "";
+ continue;
+ }
+ break;
+ }
+ workspace += c;
+ }
+ if (workspace) types.push(workspace);
+ this.template = types.map(t => t.trim()).map(t => ["any", ...Params].includes(t) ? t : new Type(t, Params));
+ }
+ //Standard Type
+ else if (/^[^()|<>,]+$/.test(TypeName)) {
+ this.typename = TypeName;
+ this.mode = "standard"
+ }
+
+ this.params = Object.fromEntries(Params.map(p => [p, 'any']));
+} \ No newline at end of file