shithub: riscv

Download patch

ref: 3b24eb4c1f4f7bccd6335002e0e37b98a54bf7fd
parent: 7f124310099b0ab12463b28f9d39104a8f17bc82
author: spew <devnull@localhost>
date: Fri Feb 3 13:55:02 EST 2017

games/mix: implement Knuth's specification for comments

--- a/sys/man/1/mix
+++ b/sys/man/1/mix
@@ -92,7 +92,8 @@
 The
 .I addr
 field of the above instructions must be an integer between 0 and 3999
-inclusive. A number-sign (#) starts a comment which extends to the end
+inclusive. A number-sign (#) or an asterisk (*) at the beginning of
+a line starts a comment which extends to the end
 of the line.
 .SH SOURCE
 .B /sys/src/games/mix
@@ -118,4 +119,4 @@
 implemented.
 .PP
 Comments are handled as described above and not
-as Knuth specifies.
+exactly as Knuth specifies.
--- a/sys/src/games/mix/examples/primes.m
+++ b/sys/src/games/mix/examples/primes.m
@@ -1,3 +1,5 @@
+* EXAMPLE PROGRAM ... TABLE OF PRIMES
+*
 L	EQU	500
 PRINTER	EQU	18
 PRIME	EQU	-1
@@ -34,6 +36,7 @@
 	LD4	24,4
 	J5N	2B
 	HLT
+* INITIAL CONTENTS OF TABLES AND BUFFERS
 ORIG	PRIME+1
 	CON	2
 	ORIG	BUF0-5
--- a/sys/src/games/mix/mix.c
+++ b/sys/src/games/mix/mix.c
@@ -298,10 +298,33 @@
 	return fmtprint(f, "%d\t%d,%d(%d | %d:%d)", opc, apart, ipart, fpart, a, b);
 }
 
+Rune
+getr(void)
+{
+	static int bol = 1;
+	Rune r;
+
+	r = Bgetrune(&bin);
+	switch(r) {
+	case '*':
+		if(!bol)
+			break;
+	case '#':
+		skipto('\n');
+	case '\n':
+		line++;
+		bol = 1;
+		return '\n';
+	}
+	bol = 0;
+	return r;
+}
+
 long
 yylex(void)
 {
 	static Rune buf[11];
+	static int bol;
 	Rune r, *bp, *ep;
 	static char cbuf[100];
 	int isnum;
@@ -310,7 +333,7 @@
 		return -1;
 
 Loop:
-	r = Bgetrune(&bin);
+	r = getr();
 	switch(r) {
 	case Beof:
 		return -1;
@@ -318,35 +341,32 @@
 	case ' ':
 		goto Loop;
 	case '\n':
-		line++;
+	case '*':
 	case '+':
 	case '-':
-	case '*':
 	case ':':
 	case ',':
 	case '(':
 	case ')':
 	case '=':
+		bol = 0;
 		return r;
 	case '/':
-		r = Bgetrune(&bin);
-		if(r == '/')
+		r = getr();
+		if(r == '/') {
+			bol = 0;
 			return LSS;
-		else
+		} else
 			Bungetrune(&bin);
 		return '/';
 	case '"':
 		for(bp = buf; bp < buf+5; bp++) {
-			*bp = Bgetrune(&bin);
+			*bp = getr();
 		}
-		if(Bgetrune(&bin) != '"')
+		if(getr() != '"')
 			yyerror("Bad string literal\n");
 		yylval.rbuf = buf;
 		return LSTR;
-	case '#':
-		skipto('\n');
-		line++;
-		return '\n';
 	}
 	bp = buf;
 	ep = buf+nelem(buf)-1;
@@ -359,7 +379,7 @@
 		*bp++ = r;
 		if(isnum && (r >= Runeself || !isdigit(r)))
 			isnum = 0;
-		r = Bgetrune(&bin);
+		r = getr();
 		switch(r) {
 		case Beof:
 		case '\t':