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];
this.locals.push(name);
}
// ctx.Instance = Instance;
ctx.create = this.system.newInstance.bind(this.system);
this.locals.push('create');
this.context = ctx;
};
ready = false;
constructor(module, location, parameters, system) {
super();
this.module = module;
@ -46,8 +47,10 @@ export default class Instance extends Serializable {
}
invokeInternal(name, ...args) {
// console.log('invoking', 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 () {
@ -55,16 +58,24 @@ export default class Instance extends Serializable {
}
}
minify()
function evalInContext(js, context, locals) {
//# Return the results of the in-line anonymous function we .call with the passed context
const that = this;
return function() {
const preminJs = `
'use strict';
(() => {
${locals.map((k) => `
const ${k} = this.${k};
`).join('\n')}
${js}`;
${js};
})();`;
const newJs = minify(preminJs);
return eval(newJs);
// newJs should inject into result...
let result;
eval(newJs);
return result;
}.call(context);
}

View File

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

View File

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

8
run.js
View File

@ -36,7 +36,6 @@ function createParser() {
const lexer = moo.compile({
LINK: 'link',
RESTORE: 'restore',
NAMESPACE: 'namespace',
REQUIRED: 'required',
SINGLETON: 'singleton',
@ -100,8 +99,6 @@ const entry = process.argv[3];
set(acc, val.name.full, val);
return acc;
}, {});
console.log(modules)
const sys = new System(modules);
})()
@ -158,14 +155,11 @@ async function parseModule(location) {
else module.imports[item.name] = imported;
} else if (item.type === 'variable') {
module.variables[item.persist ? 'cold' : 'warm'].push(item.name);
}
}
} catch (e) {
console.error(e);
if (debug) {
console.log(JSON.stringify(parsed, null, 2));
}
}
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.
```vogue
functionName (param1, param2) [[
functionName (param1, param2) {
// javacsript code
]]
}
functionName [[
anotherFunction {
// javascript code
]]
}
```
## Restore

View File

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

View File

@ -1,11 +1,9 @@
singleton;
import 'terminal-kit' as terminalkit;
singleton;
link currentSave;
restore {
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 {
if(this.places.empty) {
for(let i = 0; i < 3; i ++) {
const place = create('xyz.places.places.forest', {
const place = create('places.forest', {
world: this
});
this.places.push(place);
@ -13,6 +13,6 @@ restore {
}
for(const place of this.places) {
place.ping();
console.log(place.ping());
}
}