2022-02-11 04:01:05 +00:00
|
|
|
|
|
|
|
import { spawnSync } from "child_process";
|
2022-02-15 17:25:00 +00:00
|
|
|
import { existsSync, readdirSync, readFileSync, writeFileSync } from "fs";
|
2022-01-28 17:22:59 +00:00
|
|
|
import { describe } from "mocha";
|
2022-02-09 04:21:23 +00:00
|
|
|
import { Bin, BoxConstraints, Packer } from "../common/ecs/binpack";
|
2022-02-11 04:01:05 +00:00
|
|
|
import { ECSCompiler } from "../common/ecs/compiler";
|
2022-01-28 17:22:59 +00:00
|
|
|
import { Dialect_CA65, EntityManager, SourceFileExport } from "../common/ecs/ecs";
|
|
|
|
|
|
|
|
function testCompiler() {
|
2022-02-03 02:06:44 +00:00
|
|
|
let em = new EntityManager(new Dialect_CA65()); // TODO
|
|
|
|
let c = new ECSCompiler(em);
|
2022-01-28 17:22:59 +00:00
|
|
|
try {
|
|
|
|
c.parseFile(`
|
2022-02-01 15:13:37 +00:00
|
|
|
// comment
|
|
|
|
/*
|
|
|
|
mju,fjeqowfjqewiofjqe
|
|
|
|
*/
|
2022-01-28 17:22:59 +00:00
|
|
|
component Kernel
|
|
|
|
lines: 0..255
|
|
|
|
bgcolor: 0..255
|
|
|
|
end
|
|
|
|
component Bitmap
|
|
|
|
data: array of 0..255
|
|
|
|
end
|
|
|
|
component HasBitmap
|
|
|
|
bitmap: [Bitmap]
|
|
|
|
end
|
|
|
|
|
|
|
|
system SimpleKernel
|
|
|
|
locals 8
|
2022-02-05 05:56:43 +00:00
|
|
|
on preframe do with [Kernel] --- JUNK_AT_END
|
2022-01-28 17:22:59 +00:00
|
|
|
lda #5
|
|
|
|
sta #6
|
|
|
|
Label:
|
|
|
|
---
|
|
|
|
end
|
|
|
|
|
2022-01-29 03:13:33 +00:00
|
|
|
comment ---
|
|
|
|
|
|
|
|
---
|
|
|
|
|
2022-01-28 17:22:59 +00:00
|
|
|
scope Root
|
|
|
|
entity kernel [Kernel]
|
2022-01-29 03:13:33 +00:00
|
|
|
const lines = 0xc0
|
2022-02-15 17:25:00 +00:00
|
|
|
//const lines = $c0
|
|
|
|
end
|
|
|
|
entity nobmp [Bitmap]
|
|
|
|
const data = [4]
|
|
|
|
end
|
|
|
|
entity bmp [Bitmap]
|
|
|
|
const data = [1,2,3]
|
2022-01-28 17:22:59 +00:00
|
|
|
end
|
|
|
|
entity player1 [HasBitmap]
|
2022-02-15 17:25:00 +00:00
|
|
|
init bitmap = #bmp
|
2022-01-28 17:22:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
`, 'foo.txt');
|
2022-02-07 20:58:03 +00:00
|
|
|
//console.log('json', c.em.toJSON());
|
2022-01-28 17:22:59 +00:00
|
|
|
let src = new SourceFileExport();
|
|
|
|
c.exportToFile(src);
|
|
|
|
// TODO: test?
|
2022-02-15 17:25:00 +00:00
|
|
|
console.log(src.toString());
|
2022-02-10 15:21:24 +00:00
|
|
|
return em;
|
2022-01-28 17:22:59 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
for (let err of c.errors) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
console.log(c.tokens);
|
2022-01-29 03:13:33 +00:00
|
|
|
throw e;
|
2022-01-28 17:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: files in markdown?
|
|
|
|
// TODO: jsr OperModeExecutionTree?
|
|
|
|
|
|
|
|
describe('Tokenizer', function() {
|
|
|
|
it('Should use Compiler', function() {
|
|
|
|
testCompiler();
|
|
|
|
});
|
|
|
|
});
|
2022-02-06 02:43:45 +00:00
|
|
|
|
|
|
|
describe('Compiler', function() {
|
|
|
|
let testdir = './test/ecs/';
|
|
|
|
let files = readdirSync(testdir).filter(f => f.endsWith('.ecs'));
|
2022-02-07 20:58:03 +00:00
|
|
|
files.forEach((ecsfn) => {
|
2022-02-16 20:50:48 +00:00
|
|
|
it('Should compile ' + ecsfn, function() {
|
|
|
|
let asmfn = ecsfn.replace('.ecs','.asm');
|
|
|
|
let goodfn = ecsfn.replace('.ecs','.txt');
|
|
|
|
let ecspath = testdir + ecsfn;
|
|
|
|
let goodpath = testdir + goodfn;
|
|
|
|
let dialect = new Dialect_CA65();
|
|
|
|
let em = new EntityManager(dialect);
|
|
|
|
em.mainPath = ecspath;
|
|
|
|
let compiler = new ECSCompiler(em);
|
|
|
|
compiler.getImportFile = (path: string) => {
|
|
|
|
return readFileSync(testdir + path, 'utf-8');
|
|
|
|
}
|
|
|
|
let code = readFileSync(ecspath, 'utf-8');
|
2022-02-22 16:59:40 +00:00
|
|
|
var outtxt = '';
|
|
|
|
try {
|
|
|
|
compiler.parseFile(code, ecspath);
|
|
|
|
// TODO: errors
|
|
|
|
let out = new SourceFileExport();
|
|
|
|
em.exportToFile(out);
|
|
|
|
outtxt = out.toString();
|
|
|
|
} catch (e) {
|
|
|
|
outtxt = e.toString();
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
if (compiler.errors.length)
|
|
|
|
outtxt = compiler.errors.map(e => `${e.line}:${e.msg}`).join('\n');
|
2022-02-16 20:50:48 +00:00
|
|
|
let goodtxt = existsSync(goodpath) ? readFileSync(goodpath, 'utf-8') : '';
|
|
|
|
if (outtxt.trim() != goodtxt.trim()) {
|
|
|
|
let asmpath = '/tmp/' + asmfn;
|
|
|
|
writeFileSync(asmpath, outtxt, 'utf-8');
|
|
|
|
console.log(spawnSync('/usr/bin/diff', [goodpath, asmpath], {encoding:'utf-8'}).stdout);
|
|
|
|
throw new Error(`files different; to fix: cp ${asmpath} ${goodpath}`);
|
|
|
|
}
|
|
|
|
});
|
2022-02-06 02:43:45 +00:00
|
|
|
});
|
|
|
|
});
|
2022-02-08 22:30:11 +00:00
|
|
|
|
2022-02-09 04:21:23 +00:00
|
|
|
function testPack(bins: Bin[], boxes: BoxConstraints[]) {
|
|
|
|
let packer = new Packer();
|
|
|
|
for (let bin of bins) packer.bins.push(bin);
|
|
|
|
for (let bc of boxes) packer.boxes.push(bc);
|
|
|
|
if (!packer.pack()) throw new Error('cannot pack')
|
2022-02-15 17:25:00 +00:00
|
|
|
//console.log(packer.boxes);
|
|
|
|
//console.log(packer.bins[0].free)
|
2022-02-09 04:21:23 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 22:30:11 +00:00
|
|
|
describe('Box Packer', function() {
|
|
|
|
it('Should pack boxes', function() {
|
2022-02-09 04:21:23 +00:00
|
|
|
testPack(
|
|
|
|
[
|
|
|
|
new Bin({ left:0, top:0, right:10, bottom:10 })
|
|
|
|
], [
|
|
|
|
{ width: 5, height: 5 },
|
|
|
|
{ width: 5, height: 5 },
|
|
|
|
{ width: 5, height: 5 },
|
|
|
|
{ width: 5, height: 5 },
|
|
|
|
]
|
|
|
|
);
|
|
|
|
});
|
2022-02-09 13:39:41 +00:00
|
|
|
it('Should pack top-aligned boxes', function() {
|
|
|
|
testPack(
|
|
|
|
[
|
|
|
|
new Bin({ left:0, top:0, right:10, bottom:10 })
|
|
|
|
], [
|
|
|
|
{ width: 5, height: 7, top: 0 },
|
|
|
|
{ width: 5, height: 7, top: 1 },
|
|
|
|
{ width: 5, height: 1 },
|
|
|
|
{ width: 5, height: 1 },
|
|
|
|
{ width: 5, height: 3 },
|
|
|
|
{ width: 5, height: 1 },
|
|
|
|
]
|
|
|
|
);
|
|
|
|
});
|
2022-02-17 18:52:13 +00:00
|
|
|
it('Should pack unaligned boxes', function() {
|
2022-02-09 04:21:23 +00:00
|
|
|
testPack(
|
|
|
|
[
|
|
|
|
new Bin({ left:0, top:0, right:10, bottom:10 })
|
|
|
|
], [
|
|
|
|
{ width: 3, height: 7, top: 0 },
|
|
|
|
{ width: 3, height: 7, top: 1 },
|
2022-02-09 13:39:41 +00:00
|
|
|
{ width: 3, height: 7, top: 2 },
|
|
|
|
{ width: 5, height: 1 },
|
|
|
|
{ width: 3, height: 1 },
|
2022-02-09 04:21:23 +00:00
|
|
|
]
|
|
|
|
);
|
2022-02-08 22:30:11 +00:00
|
|
|
});
|
2022-02-17 18:52:13 +00:00
|
|
|
it('Should pack multiple bins', function() {
|
|
|
|
testPack(
|
|
|
|
[
|
|
|
|
new Bin({ left:0, top:0, right:10, bottom:10 }),
|
|
|
|
new Bin({ left:0, top:0, right:10, bottom:10 })
|
|
|
|
], [
|
|
|
|
|
|
|
|
{ width: 5, height: 10 },
|
|
|
|
{ width: 5, height: 10 },
|
|
|
|
{ width: 5, height: 5 },
|
|
|
|
{ width: 5, height: 10 },
|
|
|
|
{ width: 5, height: 5 },
|
|
|
|
]
|
|
|
|
);
|
|
|
|
});
|
2022-02-08 22:30:11 +00:00
|
|
|
});
|
|
|
|
|