Giter VIP home page Giter VIP logo

pl0c's Introduction

pl0c

pl0c is a compiler for the PL/0 language. It reads in PL/0 source code and outputs equivalent C source code.

It was written to be the subject of a series of blog posts on writing a compiler from a hands-on practical perspective for the beginner.

Building

Just run make. Then (optionally) make install.

If you are on a platform that does not include strtonum(3) in its libc, remove -DHAVE_STRTONUM from CFLAGS. In this case, you'll have to put strtonum.c in the same directory as pl0c. This will be improved.

Version

The current version is 1.0.2.

You can find a source tarball in the Releases tab.

Issues and Pull Requests

Issues and Pull Requests are accepted at any time.

If your Pull Request changes the compiler in any way, I ask that you remember to update the bootstrap compiler using make genbootstrap and including the updated bootstrap compiler as part of the Pull Request.

pl0c's People

Contributors

ibara avatar smlckz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pl0c's Issues

I/O - Simple output of integers - proof of concept.

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:

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 :)

Compile for Windows

Can we create a compiler for Windows or just Linux? And after generating the program, can it generate a helloWorld.exe?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.