Page 1 of 3

Compiler tutorial: suggest more stuff

Posted: Fri Sep 04, 2009 5:06 pm
by Trond
Some people wanted to know how to create a programming language in PureBasic: http://www.purebasic.fr/english/viewtopic.php?t=34316

So I started a tutorial: http://pbtut.blogspot.com/

Please post here if there is some particular topic you are interested in a tutorial on, or if something wasn't made clear.

Currently wanted:
(of course everything, but the following will be prioritized first)
functions (Joakim)
multiple types

Request it if you want any of this:
whole assembly file generation (so the compiler becomes usable)
anything else you can think of

Done
Expressions with variables, parentheses and 12 operators (4 precedence levels).
multiline input
if statements
loop statements
better code by using registers instead of stack
symbol table
function calls (long type)

Posted: Fri Sep 04, 2009 6:51 pm
by Flype
Looks very interesting, i will have a look.

PS. Can anyone suggest a better publishing solution than blogspot (that is still quick to setup and use)? Blogspot keeps mutilating my code.
You might try code.google (?)

http://code.google.com/intl/fr/opensource/
http://code.google.com/p/pyfilemaker/wi ... MLWrappers

Posted: Fri Sep 04, 2009 7:38 pm
by Trond
Google code isn't meant for sequential publishing and mixing code with text. At least it's not obvious. I always found it confusing.

Posted: Fri Sep 04, 2009 10:40 pm
by jack
Trond, yes please add loop constructs and multiple types. :)

Posted: Fri Sep 04, 2009 11:14 pm
by Xombie
Trond,

Wordpress? http://codex.wordpress.org/Writing_Code_in_Your_Posts

Also - very interesting tutorial. Nice work!

Posted: Sat Sep 05, 2009 1:02 pm
by Trond
Wordpress is even worse than blogspot. Even within code tags, double spaces are stripped, the result is that indentation is lost.

Re: Compiler tutorial: suggest more stuff

Posted: Sat Sep 05, 2009 1:38 pm
by flaith
Trond wrote:So I started a tutorial: http://pbtut.blogspot.com/
That's a really good idea, thanks Trond :D

Re: Compiler tutorial: suggest more stuff

Posted: Sun Sep 06, 2009 8:07 am
by Teng
Trond wrote:Some people wanted to know how to create a programming language in PureBasic: http://www.purebasic.fr/english/viewtopic.php?t=34316

So I started a tutorial: http://pbtut.blogspot.com/

Please post here if there is some particular topic you are interested in a tutorial on, or if something wasn't made clear.

Currently wanted:
functions (Joakim)

Request it if you want any of this:
if statements
loop statements
optimizations (current code is ridiculously bad)
whole assembly file generation (so the compiler becomes usable)
multiple types
anything else you can think of

PS. Can anyone suggest a better publishing solution than blogspot (that is still quick to setup and use)? Blogspot keeps mutilating my code.
Why not just blog it here in the PureBasic forums? With the

Code: Select all

 tags you won't have to worry about formatting problems.

I don't know how far you plan to take this project (are you making a complete interpreter/compiler?) but if it's not too much trouble could you include if statements and also goto statements. Actually I'd like everything you're offering that includes : loop statements, optimizing, multiple types but only if you feel up to it. No pressure here, take it easy.  :)

Re: Compiler tutorial: suggest more stuff

Posted: Sun Sep 06, 2009 9:39 am
by Trond
Teng wrote:
I don't know how far you plan to take this project (are you making a complete interpreter/compiler?)
I will not put all the pieces together, but I will show all the pieces working independently.

The reason I haven't posted a new tutorial part yet, is that I wanted to do functions, but that requires a symbol table. Symbol tables are easy to write, but fast ones require a big amount of code. This is because they need to take into account nested scopes, which is easiest done by a linked tree of hash tables. As you may imagine, this will take a bit of pointer fiddling and a custom hash table implementation, which is really not what we want here.

Instead I'm just going to use one hash table, implemented with PB's map. But that isn't completely working/documented yet. So I have to write a tutorial on another subject in the meantime. Because I think presenting a huge lump of code for a symbol table detracts from the subject.

Re: Compiler tutorial: suggest more stuff

Posted: Mon Sep 07, 2009 11:08 am
by Trond
Trond wrote:
Teng wrote:
I don't know how far you plan to take this project (are you making a complete interpreter/compiler?)
I will not put all the pieces together, but I will show all the pieces working independently.
Or actually, it seems like some things don't make much sense without each other (like ifs without variables and boolean expressions), so I will make certain pieces together.

Posted: Wed Sep 09, 2009 1:01 am
by fsw
@Trond
great job so far.

Wondering about this:

Code: Select all

Procedure IsDigit(C.c)
  If C >= '0' And C <= '9'
    ProcedureReturn 1
  EndIf
EndProcedure

Debug IsDigit(4)
shouldn't it output 1 :?:

Here it only works if the numbers are not using '9' but 9.

using newest beta...

Posted: Wed Sep 09, 2009 8:27 am
by DarkDragon
fsw wrote:@Trond
great job so far.

Wondering about this:

Code: Select all

Procedure IsDigit(C.c)
  If C >= '0' And C <= '9'
    ProcedureReturn 1
  EndIf
EndProcedure

Debug IsDigit(4)
shouldn't it output 1 :?:

Here it only works if the numbers are not using '9' but 9.

using newest beta...
'0' = 48
'9' = '0' + 9 = 57

ASCII ;-) press Alt+A inside the PureBasic IDE for more information.

Posted: Wed Sep 09, 2009 9:46 am
by Trond
fsw wrote:@Trond
great job so far.

Wondering about this:

Code: Select all

Procedure IsDigit(C.c)
  If C >= '0' And C <= '9'
    ProcedureReturn 1
  EndIf
EndProcedure

Debug IsDigit(4)
shouldn't it output 1 :?:

Here it only works if the numbers are not using '9' but 9.

using newest beta...
No, the parameter is a character, not a number. If you could pass a number (like 123, 0, 8193, 573) then the procedure would always have to return 1, and thus be senseless.
The character type is a number type which holds a character in the form of its character number (ascii code or unicode character code). The procedure checks if the passed character number (which will of course always be one or more digits) specifies a character (in the ascii table) that is a digit.

If that didn't help, may blueznl's guide has something about character types?

Posted: Wed Sep 09, 2009 9:52 am
by Teng
Just finished reading section 11 of your tutorial on pre-optimization :) . One thing though, since your tutorial is directed at beginners, you should make passing mention that basic assembler knowledge is required but I guess that is obvious enough if you think about it :lol: . Here's a link to information on assembly for beginners :

http://maven.smith.edu/~thiebaut/ArtOfA ... ofasm.html

A program that also useful for testing basic assembler instructions without messing up your pc (e.g,. if you're trying out interrupts, otherwise just use PureBasic) :

http://www.geocities.com/emu8086/

Update :
Whoops! Sorry don't I look like the ass. :lol:
Totally missed this in the tutorial :
"I recommend that you look here: http://flatassembler.net/docs.php?article=manual to get a description of all x86 assembly instructions. You can also download the same file as a pdf file.". Sorry.

Posted: Wed Sep 09, 2009 10:01 am
by Trond
I learned assembly by writing a compiler, I don't see why anyone else shouldn't. After all, you can only learn something by doing/using it. What I should have mentioned was that if you don't know it, you'll have to make an extra effort to be sure you know what's going on at all times.

What I did was to look a lot on PB's assembly output. When I was unsure of how to translate a certain expression into assembly, I just typed the expression into PB and compiled it to asm (I have a batch file to do it from the tools menu). Very handy.

And again (I mentioned this in the tutorial, but I mention it again), read fasm.pdf on the relevant instructions. It has quite easy to understand info (at least compared to intel's manuals). The book "The art of assembly", I can't recommend, because it uses a different syntax.