update docs, function returns, close #4

sdl
Marcus 2021-05-09 19:42:03 -04:00
parent 50b89ce8a5
commit 16c4133bf6
8 changed files with 34 additions and 32 deletions

View File

@ -19,12 +19,13 @@ export default class Instance extends Serializable {
ctx[name] = this.module.imports[name]; ctx[name] = this.module.imports[name];
this.locals.push(name); this.locals.push(name);
} }
// ctx.Instance = Instance;
ctx.create = this.system.newInstance.bind(this.system); ctx.create = this.system.newInstance.bind(this.system);
this.locals.push('create'); this.locals.push('create');
this.context = ctx; this.context = ctx;
}; };
ready = false;
constructor(module, location, parameters, system) { constructor(module, location, parameters, system) {
super(); super();
this.module = module; this.module = module;
@ -46,8 +47,10 @@ export default class Instance extends Serializable {
} }
invokeInternal(name, ...args) { invokeInternal(name, ...args) {
// console.log('invoking', name);
const content = this.module.functions[name]; const content = this.module.functions[name];
evalInContext(content, this.context, this.locals); if(!content) throw new TypeError(content + ' is not a function!');
return evalInContext(content, this.context, this.locals);
} }
get link () { get link () {
@ -55,16 +58,24 @@ export default class Instance extends Serializable {
} }
} }
minify()
function evalInContext(js, context, locals) { function evalInContext(js, context, locals) {
//# Return the results of the in-line anonymous function we .call with the passed context //# Return the results of the in-line anonymous function we .call with the passed context
const that = this; const that = this;
return function() { return function() {
const preminJs = ` const preminJs = `
'use strict';
(() => {
${locals.map((k) => ` ${locals.map((k) => `
const ${k} = this.${k}; const ${k} = this.${k};
`).join('\n')} `).join('\n')}
${js}`; ${js};
})();`;
const newJs = minify(preminJs); const newJs = minify(preminJs);
return eval(newJs); // newJs should inject into result...
let result;
eval(newJs);
return result;
}.call(context); }.call(context);
} }

View File

@ -14,7 +14,7 @@ export default class System extends Serializable {
try { try {
mkdirSync(location); mkdirSync(location);
} catch {} } catch {}
this.newInstance('xyz.places.main'); this.newInstance('main');
} }
getModule(name) { getModule(name) {

View File

@ -3,13 +3,12 @@
PROGRAM -> _ STATEMENT:+ _ {% ([,stuff,]) => { return stuff } %} PROGRAM -> _ STATEMENT:+ _ {% ([,stuff,]) => { return stuff } %}
STATEMENT -> _ LINK_DECLARATION EOL {% ([,stuff]) => { return stuff } %} STATEMENT -> _ LINK_DECLARATION EOL {% ([,stuff]) => { return stuff } %}
| _ %RESTORE _ JS_BLOCK EOL {% ([,,,block]) => { return { type: 'restore', block: block } } %} | _ IDENTIFIER _ JS_BLOCK EOL {% ([,name,,block]) => { return { type: 'function', name: name, block } } %}
| _ IDENTIFIER _ JS_BLOCK EOL {% ([,name,,block]) => { return { type: 'function', name: name, block: block } } %} | _ %NAMESPACE __ NAMESPACE EOL {% ([,,,namespace]) => { return { type: 'namespace', namespace: namespace[0] } } %}
| _ %NAMESPACE __ NAMESPACE EOL {% ([,,,namespace]) => { return { type: 'namespace', namespace: namespace[0] } } %} | _ DIRECTIVE EOL {% ([,directive]) => { return { type: 'directive', value: directive }} %}
| _ DIRECTIVE EOL {% ([,directive]) => { return { type: 'directive', value: directive }} %} | _ %IMPORT __ STRING __ %AS __ IDENTIFIER EOL {% ([,,,moduleName,,,,identifier]) => { return { type: 'import', name: identifier, importName: moduleName }} %}
| _ %IMPORT __ STRING __ %AS __ IDENTIFIER EOL {% ([,,,moduleName,,,,identifier]) => { return { type: 'import', name: identifier, importName: moduleName }} %} | _ %RUNTIME __ %MEMBER __ IDENTIFIER EOL {% ([,,,,,identifier]) => {return{ type: 'variable', persist: false, name: identifier }} %}
| _ %RUNTIME __ %MEMBER __ IDENTIFIER EOL {% ([,,,,,identifier]) => {return{ type: 'variable', persist: false, name: identifier }} %} | _ %MEMBER __ IDENTIFIER EOL {% ([,,,identifier]) => {return{ type: 'variable', persist: true, name: identifier }} %}
| _ %MEMBER __ IDENTIFIER EOL {% ([,,,identifier]) => {return{ type: 'variable', persist: true, name: identifier }} %}
DIRECTIVE -> _ %SINGLETON {% () => 'singleton' %} DIRECTIVE -> _ %SINGLETON {% () => 'singleton' %}
| _ %KEEPALIVE {% () => 'keepalive' %} | _ %KEEPALIVE {% () => 'keepalive' %}
@ -26,7 +25,7 @@ EOL -> _ %SEMICOLON:?
STRING -> %STRING {% ([d]) => d.value.substring(1, d.value.length - 1) %} STRING -> %STRING {% ([d]) => d.value.substring(1, d.value.length - 1) %}
SEMICOLON -> %SEMICOLON SEMICOLON -> %SEMICOLON
IDENTIFIER -> %IDENTIFIER {% ([id]) => id.value %} IDENTIFIER -> %IDENTIFIER {% ([id]) => id.value %}
JS_BLOCK -> %JS_BLOCK {% ([block]) => minify(block.value.substring(2, block.value.length - 2)) %} JS_BLOCK -> %JS_BLOCK {% ([block]) => (`result = (() => {${block.value.substring(2, block.value.length - 2)}})();`) %}
| %JS_BLOCK2 {% ([block]) => minify(block.value.substring(2, block.value.length - 2)) %} | %JS_BLOCK2 {% ([block]) => (`result = (() => {${block.value.substring(1, block.value.length - 1)}})();`) %}
_ -> null | %SPACE {% () => undefined %} _ -> null | %SPACE {% () => undefined %}
__ -> %SPACE {% () => undefined %} __ -> %SPACE {% () => undefined %}

8
run.js
View File

@ -36,7 +36,6 @@ function createParser() {
const lexer = moo.compile({ const lexer = moo.compile({
LINK: 'link', LINK: 'link',
RESTORE: 'restore',
NAMESPACE: 'namespace', NAMESPACE: 'namespace',
REQUIRED: 'required', REQUIRED: 'required',
SINGLETON: 'singleton', SINGLETON: 'singleton',
@ -101,8 +100,6 @@ const entry = process.argv[3];
return acc; return acc;
}, {}); }, {});
console.log(modules)
const sys = new System(modules); const sys = new System(modules);
})() })()
@ -158,14 +155,11 @@ async function parseModule(location) {
else module.imports[item.name] = imported; else module.imports[item.name] = imported;
} else if (item.type === 'variable') { } else if (item.type === 'variable') {
module.variables[item.persist ? 'cold' : 'warm'].push(item.name);
} }
} }
} catch (e) { } catch (e) {
console.error(e); console.error(e);
if (debug) {
console.log(JSON.stringify(parsed, null, 2));
}
} }
return module; return module;

View File

@ -42,13 +42,13 @@ singleton;
functions can be referred to in other functions with or without the `this.` prefix. Use the prefix to disambiguate if local variables exist with the same identifier. functions can be referred to in other functions with or without the `this.` prefix. Use the prefix to disambiguate if local variables exist with the same identifier.
```vogue ```vogue
functionName (param1, param2) [[ functionName (param1, param2) {
// javacsript code // javacsript code
]] }
functionName [[ anotherFunction {
// javascript code // javascript code
]] }
``` ```
## Restore ## Restore

View File

@ -10,5 +10,5 @@ restore {
} }
ping { ping {
console.log('Ping!', new Date().getTime()); return new Date().getTime();
} }

View File

@ -1,11 +1,9 @@
singleton;
import 'terminal-kit' as terminalkit; import 'terminal-kit' as terminalkit;
singleton;
link currentSave; link currentSave;
restore { restore {
terminalkit.terminal.cyan('~Welcome to Vogue~\n'); terminalkit.terminal.cyan('~Welcome to Vogue~\n');
this.currentSave ??= create('xyz.places.world', {}); this.currentSave ??= create('world', {});
} }

View File

@ -5,7 +5,7 @@ link character;
restore { restore {
if(this.places.empty) { if(this.places.empty) {
for(let i = 0; i < 3; i ++) { for(let i = 0; i < 3; i ++) {
const place = create('xyz.places.places.forest', { const place = create('places.forest', {
world: this world: this
}); });
this.places.push(place); this.places.push(place);
@ -13,6 +13,6 @@ restore {
} }
for(const place of this.places) { for(const place of this.places) {
place.ping(); console.log(place.ping());
} }
} }