I see you have a TODO referencing the need for simple I/O, but since I was curious I figured I'd have a stab at something basic myself.
I google'd "pl/0 examples" and found this wikipedia page:
- https://en.wikipedia.org/wiki/PL/0
That contains a simple "dump primes" sample, which contains a call to write
to output a number. Adding support for that was trivial, albeit simple because I assumed only integer output. With that in place:
$ cat primes.pl0
const max = 100;
var arg, ret;
procedure isprime;
var i;
begin
ret := 1;
i := 2;
while i < arg do
begin
if ( arg / i * i ) = arg then
begin
ret := 0;
i := arg
end;
i := i + 1
end
end;
procedure primes;
begin
arg := 2;
while arg < max do
begin
call isprime;
if ret = 1 then write arg;
arg := arg + 1
end
end;
call primes
.
Then compiling and running:
frodo ~/pl0c $ ./pl0c primes.pl0 > p.c
frodo ~/pl0c $ gcc p.c
frodo ~/pl0c $ ./a.out
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
The change is trivial:
frodo ~/pl0c $ git diff *.c
diff --git a/pl0c.c b/pl0c.c
index f416730..7044009 100644
--- a/pl0c.c
+++ b/pl0c.c
@@ -33,6 +33,7 @@
#define TOK_VAR 'V'
#define TOK_PROCEDURE 'P'
#define TOK_CALL 'c'
+#define TOK_WRITE 'w'
#define TOK_BEGIN 'B'
#define TOK_END 'E'
#define TOK_IF 'i'
@@ -184,6 +185,8 @@ ident(void)
return TOK_PROCEDURE;
else if (!strcmp(token, "call"))
return TOK_CALL;
+ else if (!strcmp(token, "write"))
+ return TOK_WRITE;
else if (!strcmp(token, "begin"))
return TOK_BEGIN;
else if (!strcmp(token, "end"))
@@ -679,6 +682,12 @@ statement(void)
cg_call();
expect(TOK_IDENT);
break;
+ case TOK_WRITE:
+ expect(TOK_WRITE);
+ if ( type == TOK_IDENT)
+ aout("printf(\"%%d\\n\",%s);",token);
+ expect(TOK_IDENT);
+ break;
case TOK_BEGIN:
cg_symbol();
expect(TOK_BEGIN);
@@ -827,6 +836,8 @@ main(int argc, char *argv[])
readin(argv[1]);
startp = raw;
+ aout("#include <stdio.h>\n");
+
initsymtab();
I explicitly submitted this as a diff rather than a pull-request because no doubt if you were to do this you'd do it properly, and support input too. Right now I don't need that to satisfy my curiosity :)