1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-09-27 08:54:48 +00:00

ecs: scope events

This commit is contained in:
Steven Hugg 2022-02-03 16:04:48 -06:00
parent 167dff50a5
commit a66d060c08
2 changed files with 31 additions and 4 deletions

View File

@ -294,7 +294,7 @@ export class ECSCompiler extends Tokenizer {
scope.filePath = this.path; scope.filePath = this.path;
this.currentScope = scope; this.currentScope = scope;
let cmd; let cmd;
while ((cmd = this.expectTokens(['using', 'entity', 'scope', 'comment', 'end']).str) != 'end') { while ((cmd = this.expectTokens(['end', 'using', 'entity', 'scope', 'comment', 'on']).str) != 'end') {
if (cmd == 'using') { if (cmd == 'using') {
this.parseScopeUsing(); this.parseScopeUsing();
} }
@ -307,6 +307,9 @@ export class ECSCompiler extends Tokenizer {
if (cmd == 'comment') { if (cmd == 'comment') {
this.expectTokenTypes([ECSTokenType.CodeFragment]); this.expectTokenTypes([ECSTokenType.CodeFragment]);
} }
if (cmd == 'on') {
this.currentScope.addAction(this.annotate(() => this.parseAction()));
}
} }
this.currentScope = scope.parent || null; this.currentScope = scope.parent || null;
return scope; return scope;
@ -315,7 +318,7 @@ export class ECSCompiler extends Tokenizer {
parseScopeUsing() { parseScopeUsing() {
let syslist = this.parseList(this.parseSystemRef, ','); let syslist = this.parseList(this.parseSystemRef, ',');
for (let sys of syslist) { for (let sys of syslist) {
this.currentScope?.systems.push(sys); this.currentScope?.addUsingSystem(sys);
} }
} }

View File

@ -647,6 +647,14 @@ class ActionEval {
let bitofs = parseInt(args[1] || '0'); let bitofs = parseInt(args[1] || '0');
return this.generateCodeForField(fieldName, bitofs, canwrite); return this.generateCodeForField(fieldName, bitofs, canwrite);
} }
__index(args: string[]) {
let ident = args[0]; // TODO?
if (this.entities.length == 1) {
return this.dialect.absolute(ident);
} else {
return this.dialect.indexed_x(ident, 0); //TODO?
}
}
__use(args: string[]) { __use(args: string[]) {
return this.scope.includeResource(args[0]); return this.scope.includeResource(args[0]);
} }
@ -685,7 +693,7 @@ class ActionEval {
} }
return code; return code;
} }
generateCodeForField(fieldName: string, bitofs: number, canwrite: boolean): string { generateCodeForField(fieldName: string, bitofs: number, canWrite: boolean): string {
const action = this.action; const action = this.action;
const qr = this.jr || this.qr; // TODO: why not both! const qr = this.jr || this.qr; // TODO: why not both!
@ -707,12 +715,14 @@ class ActionEval {
// see if all entities have the same constant value // see if all entities have the same constant value
// TODO: should be done somewhere else? // TODO: should be done somewhere else?
let constValues = new Set<DataValue>(); let constValues = new Set<DataValue>();
let isConst = false;
for (let e of this.entities) { for (let e of this.entities) {
let constVal = e.consts[mksymbol(component, fieldName)]; let constVal = e.consts[mksymbol(component, fieldName)];
if (constVal !== undefined) isConst = true;
constValues.add(constVal); // constVal === undefined is allowed constValues.add(constVal); // constVal === undefined is allowed
} }
// can't write to constant // can't write to constant
if (constValues.size > 0 && canwrite) if (isConst && canWrite)
throw new ECSError(`can't write to constant field ${fieldName}`, action); throw new ECSError(`can't write to constant field ${fieldName}`, action);
// is it a constant? // is it a constant?
if (constValues.size == 1) { if (constValues.size == 1) {
@ -781,6 +791,7 @@ export class EntityScope implements SourceLocated {
childScopes: EntityScope[] = []; childScopes: EntityScope[] = [];
systems: System[] = []; systems: System[] = [];
entities: Entity[] = []; entities: Entity[] = [];
events: Action[] = [];
bss = new DataSegment(); bss = new DataSegment();
rodata = new DataSegment(); rodata = new DataSegment();
code = new CodeSegment(); code = new CodeSegment();
@ -813,6 +824,12 @@ export class EntityScope implements SourceLocated {
this.entities.push(entity); this.entities.push(entity);
return entity; return entity;
} }
addUsingSystem(system: System) {
this.systems.push(system);
}
addAction(action: Action) {
this.events.push(action);
}
*iterateEntityFields(entities: Entity[]) { *iterateEntityFields(entities: Entity[]) {
for (let i = 0; i < entities.length; i++) { for (let i = 0; i < entities.length; i++) {
let e = entities[i]; let e = entities[i];
@ -1057,11 +1074,18 @@ export class EntityScope implements SourceLocated {
return symbol; return symbol;
} }
analyzeEntities() { analyzeEntities() {
this.buildLocalSystem();
this.buildSegments(); this.buildSegments();
this.allocateSegment(this.bss, false); this.allocateSegment(this.bss, false);
this.allocateSegment(this.rodata, true); this.allocateSegment(this.rodata, true);
this.allocateROData(this.rodata); this.allocateROData(this.rodata);
} }
buildLocalSystem() {
if (this.events.length) {
let sys : System = { name: this.name, actions: this.events };
this.addUsingSystem(this.em.defineSystem(sys));
}
}
generateCode() { generateCode() {
this.tempOffset = this.maxTempBytes = 0; this.tempOffset = this.maxTempBytes = 0;
// TODO: main scope? // TODO: main scope?