ybakos wrote
It seems that Jack classes must have at least one field defined. Your SnakeGame class only has static members. I haven't come across this before and never noticed it in the language spec (if that requirement even exists).
Jack classes that are
instantiated must have at least one field. As long as it's not constructed it's OK.
Here's a short test that will show the error.
class NoField
{
constructor NoField new() {
return this;
}
function void hello() {
do Output.printString("Hello ");
return;
}
method void world() {
do Output.printString("world!");
return;
}
}
class Main
{
function void main() {
var NoField nof;
do NoField.hello();
let nof = NoField.new();
do nof.world();
return;
}
}
Using JackCompiler this prints "Hello ERR5". With my compiler it prints "Hello world!". I would call this a JackCompiler error.
That Memory.alloc() can't handle zero length is documented, in the error code list if nowhere else.
The requirement to allocate at least 1 word when constructing zero length string constants is mentioned somewhere but I can't find it in a quick scan of chapters 11 and 12 where it would likely be.
I don't find any mention of the need to allocate at least 1 word when constructing objects. I would expect it it be somewhere in chapter 11 if it's anywhere.
My compiler code reads:
# allocate and initialize this -- note that the OS chokes on
# alloc(0) so we must alloc 1 word minimum.
words = self.symbolTable.VarCount(SYMK_FIELD)
if words == 0:
words = 1
self.vmWriter.WritePush(SEG_CONST, words)
self.vmWriter.WriteCall('Memory.alloc', 1)
Note that I may have just done this out of habit. I
never call malloc(0). I've been bit by too many subtle (and not so subtle) bugs in systems that claim to allow it!
--Mark