1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-06-01 20:41:36 +00:00

ecs: decoders

This commit is contained in:
Steven Hugg 2022-02-04 23:56:43 -06:00
parent 3a77e31e47
commit 0904e83bf6
5 changed files with 171 additions and 51 deletions

View File

@ -17,8 +17,8 @@
var keywords1, keywords2; var keywords1, keywords2;
var directives_list = [ var directives_list = [
'end', 'component', 'system', 'entity', 'scope', 'using', 'demo', 'end', 'component', 'system', 'entity', 'scope', 'using', 'demo', 'decode',
'const', 'init', 'locals', 'const', 'locals', 'var',
'on', 'do', 'emit', 'limit', 'on', 'do', 'emit', 'limit',
'once', 'foreach', 'with', 'join', 'if', 'once', 'foreach', 'with', 'join', 'if',
]; ];
@ -34,7 +34,7 @@
directives_list.forEach(function (s) { directives.set(s, 'def'); }); directives_list.forEach(function (s) { directives.set(s, 'def'); });
keywords_list.forEach(function (s) { directives.set(s, 'keyword'); }); keywords_list.forEach(function (s) { directives.set(s, 'keyword'); });
var opcodes = /^[a-z][a-z][a-z]\b/i; var opcodes = /^\s[a-z][a-z][a-z]\s/i;
var numbers = /^(0x[\da-f]+|[\da-f]+h|[0-7]+o|[01]+b|\d+d?)\b/i; var numbers = /^(0x[\da-f]+|[\da-f]+h|[0-7]+o|[01]+b|\d+d?)\b/i;
var tags = /^\{\{.*\}\}/; var tags = /^\{\{.*\}\}/;
var comment = /\/\/.*/; var comment = /\/\/.*/;

View File

@ -1,6 +1,7 @@
import { mergeLocs, Tokenizer, TokenType } from "../tokenizer"; import { mergeLocs, Tokenizer, TokenType } from "../tokenizer";
import { SourceLocated } from "../workertypes"; import { SourceLocated } from "../workertypes";
import { newDecoder } from "./decoder";
import { Action, ArrayType, ComponentType, DataField, DataType, DataValue, ECSError, Entity, EntityArchetype, EntityManager, EntityScope, IntType, Query, RefType, SelectType, SourceFileExport, System } from "./ecs"; import { Action, ArrayType, ComponentType, DataField, DataType, DataValue, ECSError, Entity, EntityArchetype, EntityManager, EntityScope, IntType, Query, RefType, SelectType, SourceFileExport, System } from "./ecs";
export enum ECSTokenType { export enum ECSTokenType {
@ -142,7 +143,7 @@ export class ECSCompiler extends Tokenizer {
} }
return { dtype: 'array', index, elem, baseoffset } as ArrayType; return { dtype: 'array', index, elem, baseoffset } as ArrayType;
} }
this.internalError(); throw new Error(); this.compileError(`I expected a data type here.`); throw new Error();
} }
parseDataValue(field: DataField) : DataValue { parseDataValue(field: DataField) : DataValue {
@ -170,7 +171,7 @@ export class ECSCompiler extends Tokenizer {
} }
return id; return id;
} }
this.internalError(); throw new Error(); this.compileError(`I expected a ${field.dtype} here.`); throw new Error();
} }
parseDataArray() { parseDataArray() {
@ -234,17 +235,12 @@ export class ECSCompiler extends Tokenizer {
this.expectToken('with'); this.expectToken('with');
join = this.parseQuery(); join = this.parseQuery();
} }
let emits; if (this.ifToken('limit')) {
if (this.peekToken().str == 'limit') {
this.consumeToken();
if (!query) { this.compileError(`A "${select}" query can't include a limit.`); } if (!query) { this.compileError(`A "${select}" query can't include a limit.`); }
else query.limit = this.expectInteger(); else query.limit = this.expectInteger();
} }
if (this.peekToken().str == 'emit') { if (this.ifToken('cyclecritical')) {
this.consumeToken(); // TODO
this.expectToken('(');
emits = this.parseEventList();
this.expectToken(')');
} }
let text = this.parseCode(); let text = this.parseCode();
let action = { text, event, query, join, select }; let action = { text, event, query, join, select };
@ -336,33 +332,54 @@ export class ECSCompiler extends Tokenizer {
parseEntity() : Entity { parseEntity() : Entity {
if (!this.currentScope) { this.internalError(); throw new Error(); } if (!this.currentScope) { this.internalError(); throw new Error(); }
let name = ''; let entname = '';
if (this.peekToken().type == TokenType.Ident) { if (this.peekToken().type == TokenType.Ident) {
name = this.expectIdent().str; entname = this.expectIdent().str;
} }
let etype = this.parseEntityArchetype(); let etype = this.parseEntityArchetype();
let e = this.currentScope.newEntity(etype); let entity = this.currentScope.newEntity(etype);
e.name = name; entity.name = entname;
let cmd; let cmd;
// TODO: remove init? // TODO: remove init?
while ((cmd = this.expectTokens(['const', 'init', 'var', 'end']).str) != 'end') { while ((cmd = this.expectTokens(['const', 'init', 'var', 'decode', 'end']).str) != 'end') {
if (cmd == 'var') cmd = 'init'; if (cmd == 'var') cmd = 'init';
// TODO: check data types if (cmd == 'init' || cmd == 'const') {
let name = this.expectIdent().str; // TODO: check data types
let comps = this.em.componentsWithFieldName([{etype: e.etype, cmatch:e.etype.components}], name); let name = this.expectIdent().str;
if (comps.length == 0) this.compileError(`I couldn't find a field named "${name}" for this entity.`) this.setEntityProperty(entity, name, cmd, (field) : DataValue => {
if (comps.length > 1) this.compileError(`I found more than one field named "${name}" for this entity.`) this.expectToken('=');
let field = comps[0].fields.find(f => f.name == name); return this.parseDataValue(field);
if (!field) { this.internalError(); throw new Error(); } });
this.expectToken('='); } else if (cmd == 'decode') {
let value = this.parseDataValue(field); let decoderid = this.expectIdent().str;
let symtype = this.currentScope.isConstOrInit(comps[0], name); let code = this.expectTokenTypes([ECSTokenType.CodeFragment]).str;
if (symtype && symtype != cmd) code = code.substring(3, code.length-3);
this.compileError(`I can't mix const and init values for a given field in a scope.`); let decoder = newDecoder(decoderid, code);
if (cmd == 'const') this.currentScope.setConstValue(e, comps[0], name, value); if (!decoder) { this.compileError(`I can't find a "${decoderid}" decoder.`); throw new Error() }
if (cmd == 'init') this.currentScope.setInitValue(e, comps[0], name, value); let result = decoder.parse();
for (let entry of Object.entries(result.properties)) {
this.setEntityProperty(entity, entry[0], 'const', (field) : DataValue => {
return entry[1];
});
}
}
} }
return e; return entity;
}
setEntityProperty(e: Entity, name: string, cmd: 'init' | 'const', valuefn: (field: DataField) => DataValue) {
if (!this.currentScope) { this.internalError(); throw new Error(); }
let comps = this.em.componentsWithFieldName([{etype: e.etype, cmatch:e.etype.components}], name);
if (comps.length == 0) this.compileError(`I couldn't find a field named "${name}" for this entity.`)
if (comps.length > 1) this.compileError(`I found more than one field named "${name}" for this entity.`)
let field = comps[0].fields.find(f => f.name == name);
if (!field) { this.internalError(); throw new Error(); }
let value = valuefn(field);
let symtype = this.currentScope.isConstOrInit(comps[0], name);
if (symtype && symtype != cmd)
this.compileError(`I can't mix const and init values for a given field in a scope.`);
if (cmd == 'const') this.currentScope.setConstValue(e, comps[0], name, value);
if (cmd == 'init') this.currentScope.setInitValue(e, comps[0], name, value);
} }
parseEntityArchetype() : EntityArchetype { parseEntityArchetype() : EntityArchetype {

114
src/common/ecs/decoder.ts Normal file
View File

@ -0,0 +1,114 @@
import { threadId } from "worker_threads";
import { DataValue, ECSError } from "./ecs";
export interface DecoderResult {
properties: {[name: string] : DataValue}
}
abstract class LineDecoder {
lines : string[][];
constructor(
text: string
) {
// split the text into lines and into tokens
this.lines = text.split('\n').map(s => s.trim()).filter(s => !!s).map(s => s.split(/\s+/));
}
decodeBits(s: string, n: number, msbfirst: boolean) {
if (s.length != n) throw new ECSError(`Expected ${n} characters`);
let b = 0;
for (let i=0; i<n; i++) {
let bit;
if (s.charAt(i) == 'x') bit = 1;
else if (s.charAt(i) == '.') bit = 0;
else throw new ECSError('need x or .');
if (bit) {
if (msbfirst) b |= 1 << (n-1-i);
else b |= 1 << i;
}
}
return b;
}
assertTokens(toks: string[], count: number) {
if (toks.length != count) throw new ECSError(`Expected ${count} tokens on line.`);
}
hex(s: string) {
let v = parseInt(s, 16);
if (isNaN(v)) throw new ECSError(`Invalid hex value: ${s}`)
return v;
}
abstract parse() : DecoderResult;
}
export class VCSSpriteDecoder extends LineDecoder {
parse() {
let height = this.lines.length;
let bitmapdata = new Uint8Array(height);
let colormapdata = new Uint8Array(height);
for (let i=0; i<height; i++) {
let toks = this.lines[height - 1 - i];
this.assertTokens(toks, 2);
bitmapdata[i] = this.decodeBits(toks[0], 8, true);
colormapdata[i] = this.hex(toks[1]);
}
return {
properties: {
bitmapdata, colormapdata, height: height-1
}
}
}
}
export class VCSVersatilePlayfieldDecoder extends LineDecoder {
parse() {
let height = this.lines.length;
let data = new Uint8Array(192) //height * 2); TODO
data.fill(0x3f);
// pf0 pf1 pf2 colupf colubk ctrlpf trash
const regs = [0x0d, 0x0e, 0x0f, 0x08, 0x09, 0x0a, 0x3f];
let prev = [0,0,0,0,0,0,0];
let cur = [0,0,0,0,0,0,0];
for (let i=0; i<height; i++) {
let toks = this.lines[i];
this.assertTokens(toks, 4);
cur[0] = this.decodeBits(toks[0].substring(0,4), 4, false) << 4;
cur[1] = this.decodeBits(toks[0].substring(4,12), 8, true);
cur[2] = this.decodeBits(toks[0].substring(12,20), 8, false);
if (toks[1] != '..') cur[3] = this.hex(toks[1]);
if (toks[2] != '..') cur[4] = this.hex(toks[2]);
if (toks[3] != '..') cur[5] = this.hex(toks[3]);
let changed = [];
for (let j=0; j<cur.length; j++) {
if (cur[j] != prev[j])
changed.push(j);
}
if (changed.length > 1) {
console.log(changed, cur, prev);
throw new ECSError(`More than one register change in line ${i+1}: [${changed}]`);
}
let chgidx = changed.length ? changed[0] : regs.length-1;
data[height*2 - i*2 - 1] = regs[chgidx];
data[height*2 - i*2 - 2] = cur[chgidx];
prev[chgidx] = cur[chgidx];
}
return {
properties: {
data
}
}
}
}
export function newDecoder(name: string, text: string) : LineDecoder | undefined {
let cons = (DECODERS as any)[name];
if (cons) return new cons(text);
}
const DECODERS = {
'vcs_sprite': VCSSpriteDecoder,
'vcs_versatile': VCSVersatilePlayfieldDecoder,
}

View File

@ -51,6 +51,8 @@ can you query specific entities? merge with existing queries?
bigints? bigints?
source/if query? source/if query?
only worry about intersection when non-contiguous ranges?
crazy idea -- full expansion, then relooper crazy idea -- full expansion, then relooper
how to avoid cycle crossing for critical code and data? how to avoid cycle crossing for critical code and data?
@ -119,7 +121,6 @@ export interface ActionBase extends SourceLocated {
select: SelectType; select: SelectType;
event: string; event: string;
text: string; text: string;
emits?: string[];
} }
export interface ActionOnce extends ActionBase { export interface ActionOnce extends ActionBase {
@ -675,7 +676,8 @@ class ActionEval {
return this.scope.includeResource(args[0]); return this.scope.includeResource(args[0]);
} }
__emit(args: string[]) { __emit(args: string[]) {
return this.scope.generateCodeForEvent(args[0]); let event = args[0];
return this.scope.generateCodeForEvent(event);
} }
__local(args: string[]) { __local(args: string[]) {
let tempinc = parseInt(args[0]); let tempinc = parseInt(args[0]);
@ -1058,11 +1060,9 @@ export class EntityScope implements SourceLocated {
} }
let s = this.dialect.code(); let s = this.dialect.code();
//s += `\n; event ${event}\n`; //s += `\n; event ${event}\n`;
let emitcode: { [event: string]: string } = {};
systems = systems.filter(s => this.systems.includes(s)); systems = systems.filter(s => this.systems.includes(s));
for (let sys of systems) { for (let sys of systems) {
// TODO: does this work if multiple actions? // TODO: does this work if multiple actions?
// TODO: should 'emits' be on action?
// TODO: share storage // TODO: share storage
//if (sys.tempbytes) this.allocateTempBytes(sys.tempbytes); //if (sys.tempbytes) this.allocateTempBytes(sys.tempbytes);
let tmplabel = `${sys.name}_tmp`; let tmplabel = `${sys.name}_tmp`;
@ -1071,18 +1071,8 @@ export class EntityScope implements SourceLocated {
let numActions = 0; let numActions = 0;
for (let action of sys.actions) { for (let action of sys.actions) {
if (action.event == event) { if (action.event == event) {
if (action.emits) {
for (let emit of action.emits) {
if (emitcode[emit]) {
console.log(`already emitted for ${sys.name}:${event}`);
}
//console.log('>', emit);
// TODO: cycles
emitcode[emit] = this.generateCodeForEvent(emit);
//console.log('<', emit, emitcode[emit].length);
}
}
// TODO: use Tokenizer so error msgs are better // TODO: use Tokenizer so error msgs are better
// TODO: keep event tree
let codeeval = new ActionEval(this, sys, action); let codeeval = new ActionEval(this, sys, action);
codeeval.tmplabel = tmplabel; codeeval.tmplabel = tmplabel;
codeeval.begin(); codeeval.begin();

View File

@ -242,8 +242,7 @@ function testECS() {
em.defineSystem({ em.defineSystem({
name: 'frameloop', name: 'frameloop',
actions: [ actions: [
{ text: TEMPLATE1, event: 'start', select: 'with', query: { include: [c_kernel] }, { text: TEMPLATE1, event: 'start', select: 'with', query: { include: [c_kernel] } }
emits: ['preframe', 'kernel', 'postframe'] }
] ]
}) })
em.defineSystem({ em.defineSystem({
@ -313,7 +312,7 @@ end
system SimpleKernel system SimpleKernel
locals 8 locals 8
on preframe do once [Kernel] --- JUNK_AT_END on preframe do with [Kernel] --- JUNK_AT_END
lda #5 lda #5
sta #6 sta #6
Label: Label: