1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-05-29 15:41:29 +00:00

ecs: field, ref type checking, start cmd

This commit is contained in:
Steven Hugg 2022-02-20 07:37:51 -06:00
parent 7767608595
commit a444de693b
6 changed files with 143 additions and 91 deletions

14
.gitignore vendored
View File

@ -1,13 +1,13 @@
*~
node_modules
./local/
./tests_output/
./test/output/
/local/
/tests_output/
/test/output/
.DS_Store
./tmp/
./web/
./release/
./gen/
/tmp/
/web/
/release/
/gen/
config.js
chromedriver.log
nightwatch.conf.js

View File

@ -143,3 +143,11 @@ banks need to duplicate code and/or rodata
- don't split critical code across banks
need bank trampoline macro
nested scopes for game modes? (title / demo / play)
critical data fields
if accessed in critical section, make critical
ignore arrays that aren't referenced
use DASM for multipass?
default values for component fields

View File

@ -25,8 +25,7 @@ export class ECSCompiler extends Tokenizer {
debuginfo = false;
constructor(
public readonly em: EntityManager)
{
public readonly em: EntityManager) {
super();
//this.includeEOL = true;
this.setTokenRules([
@ -52,7 +51,7 @@ export class ECSCompiler extends Tokenizer {
if (obj) (obj as SourceLocated).$loc = tok.$loc;
return obj;
}
parseFile(text: string, path: string) {
this.tokenizeFile(text, path);
while (!this.isEOF()) {
@ -78,6 +77,7 @@ export class ECSCompiler extends Tokenizer {
comp.parseFile(text, path);
} catch (e) {
for (var err of comp.errors) this.errors.push(err);
throw e;
}
}
}
@ -99,12 +99,14 @@ export class ECSCompiler extends Tokenizer {
}
if (tok.str == 'import') {
let tok = this.expectTokenTypes([ECSTokenType.QuotedString]);
let path = tok.str.substring(1, tok.str.length-1);
let path = tok.str.substring(1, tok.str.length - 1);
return this.importFile(path);
}
if (tok.str == 'demo') {
let scope = this.parseScope();
scope.isDemo = true;
// TODO: make required
if (this.peekToken().str == 'demo') this.expectToken('demo');
return scope;
}
if (tok.str == 'comment') {
@ -143,7 +145,7 @@ export class ECSCompiler extends Tokenizer {
return { dtype: 'ref', query: this.parseQuery() } as RefType;
}
if (this.ifToken('array')) {
let index : IntType | undefined = undefined;
let index: IntType | undefined = undefined;
if (this.peekToken().type == ECSTokenType.Integer) {
index = this.parseDataType() as IntType;
}
@ -158,7 +160,7 @@ export class ECSCompiler extends Tokenizer {
this.compileError(`I expected a data type here.`); throw new Error();
}
parseDataValue(field: DataField) : DataValue | ForwardRef {
parseDataValue(field: DataField): DataValue | ForwardRef {
let tok = this.peekToken();
if (tok.type == 'integer') {
return this.expectInteger();
@ -175,7 +177,7 @@ export class ECSCompiler extends Tokenizer {
this.compileError(`I expected a ${field.dtype} here.`); throw new Error();
}
parseEntityForwardRef(reftype?: RefType) : ForwardRef {
parseEntityForwardRef(reftype?: RefType): ForwardRef {
let token = this.expectIdent();
return { reftype, token };
}
@ -189,7 +191,7 @@ export class ECSCompiler extends Tokenizer {
expectInteger(): number {
let s = this.consumeToken().str;
let i : number;
let i: number;
if (s.startsWith('$'))
i = parseInt(s.substring(1), 16); // hex $...
else if (s.startsWith('%'))
@ -205,7 +207,7 @@ export class ECSCompiler extends Tokenizer {
let actions: Action[] = [];
let system: System = { name, actions };
let cmd;
while ((cmd = this.expectTokens(['on','locals','end']).str) != 'end') {
while ((cmd = this.expectTokens(['on', 'locals', 'end']).str) != 'end') {
if (cmd == 'on') {
let action = this.annotate(() => this.parseAction(system));
actions.push(action);
@ -225,11 +227,11 @@ export class ECSCompiler extends Tokenizer {
this.consumeToken();
tempbytes = this.expectInteger();
}
let system : System = { name, tempbytes, actions: [] };
let context : ActionContext = { scope: null, system };
let system: System = { name, tempbytes, actions: [] };
let context: ActionContext = { scope: null, system };
let text = this.parseCode(context);
let select : SelectType = 'once';
let action : Action = { text, event: name, select };
let select: SelectType = 'once';
let action: Action = { text, event: name, select };
system.actions.push(action);
return system;
}
@ -240,7 +242,7 @@ export class ECSCompiler extends Tokenizer {
this.expectToken('do');
// TODO: include modifiers in error msg
const select = this.expectTokens(SELECT_TYPE).str as SelectType; // TODO: type check?
const all_modifiers = ['critical','asc','desc']; // TODO
const all_modifiers = ['critical', 'asc', 'desc']; // TODO
let query = undefined;
let join = undefined;
if (select == 'once') {
@ -261,7 +263,7 @@ export class ECSCompiler extends Tokenizer {
if (this.ifToken('fit')) {
fitbytes = this.expectInteger();
}
let context : ActionContext = { scope: null, system };
let context: ActionContext = { scope: null, system };
// parse --- code ---
let text = this.parseCode(context);
let direction = undefined;
@ -271,7 +273,7 @@ export class ECSCompiler extends Tokenizer {
if (modifiers['critical']) (action as ActionWithJoin).critical = true;
return action as ActionWithJoin;
}
parseQuery() {
let q: Query = { include: [] };
let start = this.expectToken('[');
@ -319,12 +321,12 @@ export class ECSCompiler extends Tokenizer {
parseCode(context: ActionContext): string { // TODOActionNode[] {
// TODO: add $loc
let tok = this.expectTokenTypes([ECSTokenType.CodeFragment]);
let code = tok.str.substring(3, tok.str.length-3);
let code = tok.str.substring(3, tok.str.length - 3);
// TODO: add after parsing maybe?
let lines = code.split('\n');
if (this.debuginfo) this.addDebugInfo(lines, tok.$loc.line);
code = lines.join('\n');
let acomp = new ECSActionCompiler(context);
let nodes = acomp.parseFile(code, this.path);
// TODO: return nodes
@ -333,13 +335,13 @@ export class ECSCompiler extends Tokenizer {
addDebugInfo(lines: string[], startline: number) {
const re = /^\s*(;|\/\/|$)/; // ignore comments and blank lines
for (let i=0; i<lines.length; i++) {
for (let i = 0; i < lines.length; i++) {
if (!lines[i].match(re))
lines[i] = this.em.dialect.debug_line(this.path, startline+i) + '\n' + lines[i];
lines[i] = this.em.dialect.debug_line(this.path, startline + i) + '\n' + lines[i];
}
}
parseScope() : EntityScope {
parseScope(): EntityScope {
let name = this.expectIdent().str;
let scope = this.em.newScope(name, this.currentScope || undefined);
scope.filePath = this.path;
@ -382,7 +384,7 @@ export class ECSCompiler extends Tokenizer {
}
}
parseEntity() : Entity {
parseEntity(): Entity {
if (!this.currentScope) { this.internalError(); throw new Error(); }
const scope = this.currentScope;
let entname = '';
@ -392,53 +394,62 @@ export class ECSCompiler extends Tokenizer {
let etype = this.parseEntityArchetype();
let entity = this.currentScope.newEntity(etype);
entity.name = entname;
let cmd2 : string;
let cmd2: string;
// TODO: remove init?
while ((cmd2 = this.expectTokens(['const', 'init', 'var', 'decode', 'end']).str) != 'end') {
let cmd = cmd2; // put in scope
if (cmd == 'var') cmd = 'init'; // TODO: remove?
if (cmd == 'init' || cmd == 'const') {
// TODO: check data types
let name = this.expectIdent().str;
let { c, f } = this.getEntityField(entity, name);
let symtype = this.currentScope.isConstOrInit(c, name);
if (symtype && symtype != cmd)
this.compileError(`I can't mix const and init values for a given field in a scope.`);
this.expectToken('=');
let valueOrRef = this.parseDataValue(f);
if ((valueOrRef as ForwardRef).token != null) {
this.deferred.push(() => {
let refvalue = this.resolveEntityRef(scope, valueOrRef as ForwardRef);
if (cmd == 'const') scope.setConstValue(entity, c, name, refvalue);
if (cmd == 'init') scope.setInitValue(entity, c, name, refvalue);
});
} else {
if (cmd == 'const') scope.setConstValue(entity, c, name, valueOrRef as DataValue);
if (cmd == 'init') scope.setInitValue(entity, c, name, valueOrRef as DataValue);
}
this.parseInitConst(cmd, scope, entity);
} else if (cmd == 'decode') {
let decoderid = this.expectIdent().str;
let codetok = this.expectTokenTypes([ECSTokenType.CodeFragment]);
let code = codetok.str;
code = code.substring(3, code.length-3);
let decoder = newDecoder(decoderid, code);
if (!decoder) { this.compileError(`I can't find a "${decoderid}" decoder.`); throw new Error() }
let result;
try {
result = decoder.parse();
} catch (e) {
throw new ECSError(e.message, decoder.getErrorLocation(codetok.$loc));
}
for (let entry of Object.entries(result.properties)) {
let { c, f } = this.getEntityField(entity, entry[0]);
scope.setConstValue(entity, c, f.name, entry[1] as DataValue);
}
this.parseDecode(scope, entity);
}
}
return entity;
}
getEntityField(e: Entity, name: string) : ComponentFieldPair {
parseInitConst(cmd: string, scope: EntityScope, entity: Entity) {
// TODO: check data types
let name = this.expectIdent().str;
let { c, f } = this.getEntityField(entity, name);
let symtype = scope.isConstOrInit(c, name);
if (symtype && symtype != cmd)
this.compileError(`I can't mix const and init values for a given field in a scope.`);
this.expectToken('=');
let valueOrRef = this.parseDataValue(f);
if ((valueOrRef as ForwardRef).token != null) {
this.deferred.push(() => {
this.lasttoken = (valueOrRef as ForwardRef).token; // for errors
let refvalue = this.resolveEntityRef(scope, valueOrRef as ForwardRef);
if (cmd == 'const') scope.setConstValue(entity, c, f, refvalue);
if (cmd == 'init') scope.setInitValue(entity, c, f, refvalue);
});
} else {
if (cmd == 'const') scope.setConstValue(entity, c, f, valueOrRef as DataValue);
if (cmd == 'init') scope.setInitValue(entity, c, f, valueOrRef as DataValue);
}
}
parseDecode(scope: EntityScope, entity: Entity) {
let decoderid = this.expectIdent().str;
let codetok = this.expectTokenTypes([ECSTokenType.CodeFragment]);
let code = codetok.str;
code = code.substring(3, code.length - 3);
let decoder = newDecoder(decoderid, code);
if (!decoder) { this.compileError(`I can't find a "${decoderid}" decoder.`); throw new Error() }
let result;
try {
result = decoder.parse();
} catch (e) {
throw new ECSError(e.message, decoder.getErrorLocation(codetok.$loc));
}
for (let entry of Object.entries(result.properties)) {
let { c, f } = this.getEntityField(entity, entry[0]);
scope.setConstValue(entity, c, f, entry[1] as DataValue);
}
}
getEntityField(e: Entity, name: string): ComponentFieldPair {
if (!this.currentScope) { this.internalError(); throw new Error(); }
let comps = this.em.componentsWithFieldName([e.etype], name);
if (comps.length == 0) this.compileError(`I couldn't find a field named "${name}" for this entity.`)
@ -449,14 +460,14 @@ export class ECSCompiler extends Tokenizer {
return { c: component, f: field };
}
parseEntityArchetype() : EntityArchetype {
parseEntityArchetype(): EntityArchetype {
this.expectToken('[');
let components = this.parseList(this.parseComponentRef, ',');
this.expectToken(']');
return {components};
return { components };
}
parseComponentRef() : ComponentType {
parseComponentRef(): ComponentType {
let name = this.expectIdent().str;
let cref = this.em.getComponentByName(name);
if (!cref) this.compileError(`I couldn't find a component named "${name}".`)
@ -473,7 +484,7 @@ export class ECSCompiler extends Tokenizer {
return eref;
}
resolveEntityRef(scope: EntityScope, ref: ForwardRef) : number {
resolveEntityRef(scope: EntityScope, ref: ForwardRef): number {
let id = this.findEntityByName(scope, ref.token).id;
if (ref.reftype) {
// TODO: make this a function? elo ehi etc?
@ -485,8 +496,8 @@ export class ECSCompiler extends Tokenizer {
}
return id;
}
parseSystemInstanceRef() : SystemInstance {
parseSystemInstanceRef(): SystemInstance {
let name = this.expectIdent().str;
let system = this.em.getSystemByName(name);
if (!system) this.compileError(`I couldn't find a system named "${name}".`, this.lasttoken.$loc);
@ -495,7 +506,7 @@ export class ECSCompiler extends Tokenizer {
return inst;
}
parseSystemInstanceParameters() : SystemInstanceParameters {
parseSystemInstanceParameters(): SystemInstanceParameters {
let scope = this.currentScope;
if (scope == null) throw new Error();
if (this.peekToken().str == '[') {
@ -526,8 +537,7 @@ export class ECSCompiler extends Tokenizer {
export class ECSActionCompiler extends Tokenizer {
constructor(
public readonly context: ActionContext)
{
public readonly context: ActionContext) {
super();
this.setTokenRules([
{ type: ECSTokenType.Placeholder, regex: /\{\{.*?\}\}/ },
@ -542,7 +552,7 @@ export class ECSActionCompiler extends Tokenizer {
while (!this.isEOF()) {
let tok = this.consumeToken();
if (tok.type == ECSTokenType.Placeholder) {
let args = tok.str.substring(2, tok.str.length-2).split(/\s+/);
let args = tok.str.substring(2, tok.str.length - 2).split(/\s+/);
nodes.push(new CodePlaceholderNode(this.context, tok.$loc, args));
} else if (tok.type == TokenType.CatchAll) {
nodes.push(new CodeLiteralNode(this.context, tok.$loc, tok.str));

View File

@ -309,9 +309,12 @@ export class Dialect_CA65 {
return `.scope ${name}`
}
endScope(name: string) {
return `.endscope\n${name}__Start = ${name}::__Start`
return `.endscope\n${this.scopeSymbol(name)} = ${name}::__Start`
// TODO: scope__start = scope::start
}
scopeSymbol(name: string) {
return `${name}__Start`;
}
align(value: number) {
return `.align ${value}`;
}
@ -365,6 +368,9 @@ export class Dialect_CA65 {
call(symbol: string) {
return ` jsr ${symbol}`;
}
jump(symbol: string) {
return ` jmp ${symbol}`;
}
return() {
return ' rts';
}
@ -662,6 +668,8 @@ class ActionEval {
else state.xreg = new IndexRegister(this.scope, this.qr);
break;
case 'join':
// TODO: Joins don't work in superman (arrays offset?)
// ignore the join query, use the ref
if (state.xreg || state.yreg) throw new ECSError('no free index registers for join', this.action);
this.jr = new EntitySet(this.scope, (this.action as ActionWithJoin).join);
state.xreg = new IndexRegister(this.scope, this.jr);
@ -882,6 +890,10 @@ class ActionEval {
//this.used.add(`arg_${argindex}_${argvalue}`);
return argvalue;
}
__start(args: string[]) {
let startSymbol = this.dialect.scopeSymbol(args[0]);
return this.dialect.jump(startSymbol);
}
wrapCodeInLoop(code: string, action: ActionWithQuery, ents: Entity[], joinfield?: ComponentFieldPair): string {
// TODO: check ents
// TODO: check segment bounds
@ -976,9 +988,8 @@ class ActionEval {
if (baseLookup) {
return this.dialect.absolute(ident);
} else if (entities.length == 1) {
let eidofs = qr.entities.length && qr.entities[0].id - range.elo; // TODO: negative?
if (entityLookup)
eidofs = entities[0].id - range.elo;
// TODO: qr or this.entites?
let eidofs = entities[0].id - range.elo; // TODO: negative?
return this.dialect.absolute(ident, eidofs);
} else {
let ir;
@ -1298,15 +1309,17 @@ export class EntityScope implements SourceLocated {
return this.bss.getFieldRange(c, fn) || this.rodata.getFieldRange(c, fn);
}
// TODO: check type/range of value
setConstValue(e: Entity, component: ComponentType, fieldName: string, value: DataValue) {
this.setConstInitValue(e, component, fieldName, value, 'const');
setConstValue(e: Entity, component: ComponentType, field: DataField, value: DataValue) {
this.setConstInitValue(e, component, field, value, 'const');
}
setInitValue(e: Entity, component: ComponentType, fieldName: string, value: DataValue) {
this.setConstInitValue(e, component, fieldName, value, 'init');
setInitValue(e: Entity, component: ComponentType, field: DataField, value: DataValue) {
this.setConstInitValue(e, component, field, value, 'init');
}
setConstInitValue(e: Entity, component: ComponentType, fieldName: string, value: DataValue,
type: 'const'|'init') {
this.em.singleComponentWithFieldName([e.etype], fieldName, e);
setConstInitValue(e: Entity, component: ComponentType, field: DataField, value: DataValue,
type: 'const'|'init')
{
this.checkValueType(field, value);
let fieldName = field.name;
let cfname = mksymbol(component, fieldName);
let ecfname = mkscopesymbol(this, component, fieldName);
if (e.consts[cfname] !== undefined) throw new ECSError(`"${fieldName}" is already defined as a constant`, e);
@ -1318,6 +1331,24 @@ export class EntityScope implements SourceLocated {
isConstOrInit(component: ComponentType, fieldName: string) : 'const' | 'init' {
return this.fieldtypes[mksymbol(component, fieldName)];
}
checkValueType(field: DataField, value: DataValue) {
if (field.dtype == 'array') {
if (!(value instanceof Uint8Array))
throw new ECSError(`This "${field.name}" value should be an array.`);
} else if (typeof value !== 'number') {
throw new ECSError(`This "${field.name}" ${field.dtype} value should be an number.`);
} else {
if (field.dtype == 'int') {
if (value < field.lo || value > field.hi)
throw new ECSError(`This "${field.name}" value is out of range, should be between ${field.lo} and ${field.hi}.`);
} else if (field.dtype == 'ref') {
// TODO: allow override if number
let eset = new EntitySet(this, field.query);
if (value < 0 || value >= eset.entities.length)
throw new ECSError(`This "${field.name}" value is out of range for this ref type.`);
}
}
}
generateCodeForEvent(event: string, args?: string[], codelabel?: string): string {
// find systems that respond to event
// and have entities in this scope

View File

@ -159,6 +159,7 @@ end
entity NullShape [Bitmap,Colormap]
decode vcs_sprite
---
........ 00
---
end
@ -504,7 +505,7 @@ x.x.x.x.x.x.x.x.x.x. .. 06 ..
var sprite = #Superdude
end
entity Slot1 [SpriteSlot]
var sprite = $ff
var sprite = 0 // $ff
end
using VersatilePlayfield with #Superdude.room

View File

@ -60,8 +60,9 @@ Bitmap_bitmapdata_b8:
.byte >(Bitmap_bitmapdata_e2_b0+31)
.byte >(Bitmap_bitmapdata_e3_b0+31)
Bitmap_bitmapdata_e1_b0:
.byte 0
Bitmap_height_b0:
.byte 255
.byte 0
.byte 17
.byte 27
Colormap_colormapdata_b0:
@ -73,6 +74,7 @@ Colormap_colormapdata_b8:
.byte >(Colormap_colormapdata_e2_b0+31)
.byte >(Colormap_colormapdata_e3_b0+31)
Colormap_colormapdata_e1_b0:
.byte 0
Bitmap_bitmapdata_e2_b0:
.byte 128
.byte 192
@ -622,7 +624,7 @@ Main__INITDATA:
.byte 60
.byte 90
.byte 0
.byte 255
.byte 0
.byte 0
.byte 0
.byte 0