Sunday, April 23, 2017

Jack Tokenizer : Block Comments - Tougher Than You Thought

// my first program in C++
/* checking if this 
// */
// terminates a comment
// answer : yes, it does - so block comments are more powerful :)
// if I had something above after the */, it would mess things up

#include

int main()
{
  std::cout << "Hello World!";
}


Moral of the story - don't just throw away everything after the //

Saturday, April 22, 2017

Nand2Tetris : Terminal and Non-Terminal Rules

My opinion is that a separate function for each statement type is not necessary. Having worked in perl for so long, I'm sure there's an easy way to whack this out. But, being committed to py just for the sake of learning it (why?)



Sunday, April 16, 2017

Nand2Tetris : Problem with the Jack Programming Language

Programs are *very* hard to debug. There is no debugger.

Now, if they had made it easy to do debugging - tracing variables and adding breakpoints, then it would be a different story.

Saturday, April 15, 2017

Nand2Tetris Ponderables

Have you noticed that the painting of the screen is slower with the CPUemulator running assembly language code than with the VMemulator running compiled jack code?

How on earth did they do a drawCircle function with only integer support. Awesome work..

Nand2Tetris Common Issues

../../tools/JackCompiler.bat Fraction/

Compiling "C:\Users....\09\Fraction\"
Could not find file or directory: C:\Users..

Problem - the '/' provided along with directory name. Yes, lame, I know :) Shimon's response : our purpose is didactic, not production :)

while( 1 ) {

}

use while( true ) ...

Aha - API claims drawCircle can handle radius of 181... Nice try there. I just found out you have to stay below 128 (127 is ok)

In Circle.jack (line 23): In subroutine PlotCircle: Expected (

Most likely reason - you used a "do" when you meant to use "let" :) Don't you miss Perl's errors/warnings :) ?

Gotcha - you HAVE to declare ALL variables before any "action".

var int xyz;
let xyz = 0;
var int pqr;

won't fly :)

Thursday, April 13, 2017

Sunday, April 9, 2017

The Real Lesson in Nand2Tetris Part 2 Deux

Since you know how to generate machine code, what you need to be thinking about is how to disassemble machine code - that is, can you spit out vm (virtual machine) code from the machine code. Then, once you have the machine code, can you decompile it into high-level source code?

That's what the top-notch guys will be thinking about. Not me of course. I'm just a back-bencher.

Jack Programming Helpers


Jack Peculiarities

° The keyword let:
must be used in assignments: let x = 0;
° The keyword do:
must be used for calling a method or a function outside an expression:
do reduce();
° The body of a statement must be within curly brackets, even if it
contains a single statement: if (a > a) {return 3} else {return -a};
° All subroutine must end with a return
° N0 operator priority:
-- The following value if unpredictable: 2 + 3 * 4
-- To enforce priority of operations, use parentheses: 2 + (3 * 4)
° The language is weakly typed

Jack Language Cheat Sheet

class, constructor, method, function Program components
int, boolean, char, void Primitive types

var, static, field Variable declarations
let, do, if, else, while, return Statements

true, false, null Constant values


this Object reference

The Architecture is Crap

Where would I be without the forum - thanks to some geniuses out there, I see how naive I was in implementing call XYZ as jump to XYZ.

Then I realized that they've got it wrong - the implementation of the translator - when you see how hard it is for your own program to insert vm commands.

It would have been nice to have a function that does translate_command and then you can just call it with "call abc" and dump the resulting assembly language code.

Saturday, April 8, 2017

Handy Perl One Liner for Debugging Hack Assembly Language Output

What should you do, if you'd rather see this

0 : // function Class1.set 0
0 : (Class1.set)
0 : // push argument 0
0 : @0
1 : D = A
2 : @ARG
3 : A = M + D
4 : D = M
5 :
5 : @SP
6 : AM = M + 1
7 : A = A - 1

instead of this :

// function Class1.set 0
(Class1.set)
// push argument 0
@0
D = A
@ARG
A = M + D
D = M

@SP
AM = M + 1
A = A - 1

????

perl -n -e 'BEGIN{$ln=0} print "$ln : " and print; $ln++ unless ( m#^\h*\r?$|^\(|^\h*//# );'

That's what! Thank you Larry Perl - please teach Guido van Rossum something..

Shame on Yaron Ukrainitz

Sick

AM = M - 1

works. But

MA = M - 1

gives

In line XYZ, destination expected.

Nand2Tetris : if-goto (influences stack?)

Yes!!

You want to push something onto the stack to test it.

So, if you don't restore the stack-pointer with the if-goto, then you're leaky..

Sunday, April 2, 2017

Thanks Yaron Ukrainotz : Unexpected End of Line

In line 1 : Unexpected end of line

Okay, not hard to debug, but would be easier if you point out that you expect a constant after "@" in line so and so :)