apple-ii-universal-machine / envelope /bytecode-envelope.js
SNAPKITTYWEST's picture
push from SNAPKITTYWEST/apple-ii-universal-machine
e870690 verified
Raw History Blame Contribute Delete
4.55 kB
var BytecodeEnvelope = {
compileLispToBytecode: function(lispExpr) {
if (!lispExpr) return ['PUSH_UNKNOWN', 'HALT'];
var tokens = lispExpr.replace(/[()]/g, ' ').trim().split(/\s+/).filter(function(t) { return t.length > 0; });
var bytecode = [];
bytecode.push('PUSH_EVENT');
tokens.forEach(function(token) {
var lower = token.toLowerCase();
if (lower === 'event') bytecode.push('DEFINE_EVENT');
else if (lower === 'schema') bytecode.push('DEFINE_SCHEMA');
else if (lower === 'entities') bytecode.push('LOAD_ENTITIES');
else if (lower === 'pipeline') bytecode.push('START_PIPELINE');
else if (lower === 'agent') bytecode.push('LOAD_AGENT');
else if (lower === 'trust') bytecode.push('CHECK_TRUST');
else if (lower === 'seal') bytecode.push('SEAL');
else if (lower === 'audit') bytecode.push('AUDIT_WRITE');
else if (lower === 'deploy') bytecode.push('DEPLOY');
else if (lower === 'create') bytecode.push('CREATE');
else if (lower === 'delete') bytecode.push('DELETE');
else if (lower === 'run') bytecode.push('EXECUTE');
else bytecode.push('PUSH "' + token + '"');
});
bytecode.push('VERIFY');
bytecode.push('SEAL');
bytecode.push('AUDIT_WRITE');
bytecode.push('HALT');
return bytecode;
},
run: function(bytecode) {
if (!bytecode || bytecode.length === 0) return { output: [], steps: 0, error: 'Empty bytecode' };
var stack = [];
var output = [];
var steps = 0;
var maxSteps = 100;
for (var i = 0; i < bytecode.length && steps < maxSteps; i++) {
var op = bytecode[i];
steps++;
if (op.startsWith('PUSH ')) {
var val = op.substring(5).replace(/"/g, '');
stack.push(val);
output.push('PUSH ' + val);
} else if (op === 'DEFINE_EVENT') {
output.push('DEFINE_EVENT βœ“');
} else if (op === 'DEFINE_SCHEMA') {
output.push('DEFINE_SCHEMA βœ“');
} else if (op === 'LOAD_ENTITIES') {
output.push('LOAD_ENTITIES βœ“');
} else if (op === 'START_PIPELINE') {
output.push('START_PIPELINE βœ“');
} else if (op === 'LOAD_AGENT') {
output.push('LOAD_AGENT βœ“');
} else if (op === 'CHECK_TRUST') {
output.push('CHECK_TRUST βœ“');
} else if (op === 'SEAL') {
output.push('SEAL βœ“');
} else if (op === 'AUDIT_WRITE') {
output.push('AUDIT_WRITE βœ“');
} else if (op === 'DEPLOY') {
output.push('DEPLOY βœ“');
} else if (op === 'CREATE') {
output.push('CREATE βœ“');
} else if (op === 'DELETE') {
output.push('DELETE βœ“');
} else if (op === 'EXECUTE') {
output.push('EXECUTE βœ“');
} else if (op === 'VERIFY') {
output.push('VERIFY βœ“');
} else if (op === 'HALT') {
output.push('HALT');
break;
} else {
output.push('OP: ' + op);
}
}
return {
output: output,
steps: steps,
halted: steps < maxSteps
};
},
render: function(bytecode, result) {
return [
'╔══════════════════════════════════════════════════════════════╗',
'β•‘ BYTECODE EXECUTION β•‘',
'╠══════════════════════════════════════════════════════════════╣',
'β•‘ PROGRAM: ' + bytecode.join(' β†’ ').substring(0, 47).padEnd(47) + 'β•‘',
'β•‘ STEPS: ' + String(result.steps).padEnd(47) + 'β•‘',
'β•‘ STATUS: ' + (result.halted ? 'HALTED βœ“' : 'MAX STEPS').padEnd(47) + 'β•‘',
'╠══════════════════════════════════════════════════════════════╣',
'β•‘ TRACE: β•‘'
].concat(result.output.map(function(o) { return 'β•‘ ' + o.substring(0, 53).padEnd(53) + 'β•‘'; }))
.concat(['β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•'])
.join('\n');
}
};