apple2js/test/js
Ian Flanigan d67f3d8086
Applesoft compiler fixes (#98)
* Add tests for Applesoft compiler in preparation for refactoring

While refactoring the compiler, I found several small bugs:

*   Lower-case letters in strings and REM statements were converted
    to upper-case.
*   Lines are stored in the order received, not sorted by line number.
*   Does not prefer `ATN` to `AT`.
*   Does not prefer `TO` to `AT`.
*   `DATA` statements don't preserve spaces.
*   `DATA` statements don't preserve lowercase.

These will be fixed in the upcoming refactoring.

* Refactor the Applesoft Compiler

Before, the compiler had a few bugs that were not trivial to solve
because the implementation was in one heavily-nested function.

In this refactoring of the compiler, things like tokenization have
been split into separate methods which makes them a bit easier to
understand.

This refactoring also passes all of the tests.

* Set `PRGEND` when compiling to memory

Before, `PRGEND` was not adjusted which made round-tripping from
the Applesoft compiler to the decompiler not work.  This change
now updates `PRGEND` with the end-of-program + 2 bytes which seems
to be the most frequent value that I have observed.

* Fix two compiler bugs

In debugging the decompiler, I noticed two bugs in the compiler:
*   The first character after a line number was skipped.
*   `?` was not accepted as a shortcut for `PRINT`.

This change fixes these two problems and adds tests.

* Ignore spaces more aggressively

It turns out that Applesoft happily accepts 'T H E N' for `THEN`
but the parser did not. This change fixes that and adds tests for
some odd cases.

Interestingly, this means that there are some valid statements
that Applesoft can never parse correctly because it is greedy
and ignores (most) spaces. For example, `NOT RACE` will always
parse as `NOTRACE` even though `NOT RACE` is a valid expression.

* Move tokens into a separate file

Because the token lists are just maps in opposite directions, put
them in the same file. In the future, maybe we can build one
automatically.

* Fix `apple2.ts`

I had neglected to actually update `apple2.ts` to use the new
compiler and decompiler. They now do.

Also, the decompiler can be created from `Memory`. It assumes,
though, that the zero page pointers to the start and end of the
program are correct.

* Address comments

*   No more `as const` for tokens.
*   Extracted zero page constants to their own file.

Co-authored-by: Will Scullin <scullin@scullin.com>
2022-06-23 20:41:45 -07:00
..
__image_snapshots__ Videomode refactor 2 (#80) 2021-05-25 12:08:10 -07:00
__mocks__ Add the recommended eslint plugins for TypeScript (#121) 2022-05-31 08:38:40 -07:00
applesoft Applesoft compiler fixes (#98) 2022-06-23 20:41:45 -07:00
cards Add a basic write test for WOZ images (#138) 2022-06-23 06:38:36 -07:00
formats 2IMG Download support. (#137) 2022-06-21 20:34:19 -07:00
ui Preact UI (#106) 2022-05-10 06:52:06 -07:00
base64.test.ts Flesh out some state stuff (#59) 2021-02-27 19:17:36 -08:00
canvas.test.ts jest 27 2021-10-02 07:21:37 -07:00
debugger.spec.ts Cheap and cheerful debugger (#135) 2022-06-19 19:42:34 -07:00
gl.test.ts jest 27 2021-10-02 07:21:37 -07:00
rom.test.ts Jest lint (#118) 2022-05-30 09:29:48 -07:00
util.test.ts 2IMG Download support. (#137) 2022-06-21 20:34:19 -07:00