Errata

to Write Your Own Compiler

There are some minor (and harmless!) inconsistencies in the operator table of the compiler (page 104,105):

        Ops := [[ 7, 1, "mod",  BINOP,  CG_MOD          ],
		...
                [ 7, 2, "*",    BINOP,  CG_MUL          ],
		...
                [ 7, 2, "/",    BINOP,  CG_DIV          ],

Column two of the table should reflect the length of the symbols in column three (i.e.: 3, 1, 1). The length field is used in the scanning algorithm, but the above errors do not cause it to malfunction. Thanks, Pat, for pointing this out!

In copies of the book purchased before March 2018, the compiler mistakenly interprets the statement

ie (x) if (y) v; else w;

as an IF with an unexpected ELSE. The following patch fixes this bug. The patch affects pages 91 and 98 of the book. Later versions of the book and the source code archive already contain the patch.

--- t.t.old	2017-05-18 10:13:35.000000000 +0200
+++ t.t	2017-05-18 10:14:16.000000000 +0200
@@ -1217,7 +1217,7 @@
 	gen(CG_JMPFALSE, 0);
 	xrparen();
 	stmt();
-	ie (alt) do
+	if (alt) do
 		gen(CG_JUMPFWD, 0);
 		swap();
 		gen(CG_RESOLV, 0);
@@ -1225,9 +1225,6 @@
 		T := scan();
 		stmt();
 	end
-	else if (T = KELSE) do
-		aw("ELSE without IE", 0);
-	end
 	gen(CG_RESOLV, 0);
 end
 
@@ -1370,6 +1367,8 @@
 		if_stmt(1);
 	else ie (T = KIF)
 		if_stmt(0);
+	else ie (T = KELSE)
+		aw("ELSE without IE", 0);
 	else ie (T = KLEAVE)
 		leave_stmt();
 	else ie (T = KLOOP)
 

contact  |  privacy