This repository has been archived on 2023-11-14. You can view files and clone it, but cannot push or open issues/pull-requests.
vogue/Module.js

92 lines
1.7 KiB
JavaScript
Raw Normal View History

import createAst from './createAst.js'
import path from 'path';
import debug from 'debug';
const log = debug('vogue:module');
2021-05-02 17:42:04 -04:00
export default class Module {
links = {
required: {
single: [],
arrays: []
},
optional: {
single: [],
arrays: []
}
};
globals = [];
functions = [];
identifiers = {};
name = {
space: '',
last: '',
full: ''
2021-05-06 23:05:36 -04:00
};
imports = {};
variables = {
cold: [],
warm: []
}
singleton = false;
keepalive = false;
async directive({value}) {
this[value] = true;
}
async link({required, array, name}) {
this.links
[required ? 'required' : 'optional']
[array ? 'arrays' : 'single']
.push(name);
}
async namespace({namespace}) {
this.name.space = namespace;
this.name.full = this.name.space + '.' + this.name.last;
}
async function({name, block}) {
this.functions[name] = block;
}
async import({importName, name}) {
const imported = await import(importName);
if('default' in imported) this.imports[name] = imported.default;
else this.imports[name] = imported;
}
2021-05-10 23:13:17 -04:00
async variable({persist, name}) {
this.variables[persist ? 'cold' : 'warm'].push(name);
2021-05-10 23:13:17 -04:00
}
static async create(location) {
const module = new Module();
const ast = createAst(location);
const name = path.parse(location).name;
2021-05-10 23:13:17 -04:00
module.name.last = name;
module.name.full = name;
// move module whole loop ass bitch into module.
for (const item of ast) {
if ('name' in item) {
if(item.name in module.identifiers)
2021-05-10 23:13:17 -04:00
throw new Error('Identifier ' + item.name + ' already declared!');
else module.identifiers[item.name] = item.type;
}
2021-05-10 23:13:17 -04:00
if(item.type in module) {
await module[item.type](item);
2021-05-10 23:13:17 -04:00
}
}
log('='.repeat(80));
log(location);
log(module);
return module;
2021-05-10 23:13:17 -04:00
}
}