Compiler tutorial: suggest more stuff

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Compiler tutorial: suggest more stuff

Post 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)
Last edited by Trond on Sun Jan 10, 2010 3:48 pm, edited 6 times in total.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

Trond, yes please add loop constructs and multiple types. :)
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

Trond,

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

Also - very interesting tutorial. Nice work!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Wordpress is even worse than blogspot. Even within code tags, double spaces are stripped, the result is that indentation is lost.
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Re: Compiler tutorial: suggest more stuff

Post by flaith »

Trond wrote:So I started a tutorial: http://pbtut.blogspot.com/
That's a really good idea, thanks Trond :D
“Fear is a reaction. Courage is a decision.” - WC
Teng
User
User
Posts: 21
Joined: Thu Aug 27, 2009 12:13 pm

Re: Compiler tutorial: suggest more stuff

Post 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.  :)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Compiler tutorial: suggest more stuff

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Compiler tutorial: suggest more stuff

Post 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.
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post 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...
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post 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.
bye,
Daniel
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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?
Teng
User
User
Posts: 21
Joined: Thu Aug 27, 2009 12:13 pm

Post 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.
Last edited by Teng on Wed Sep 09, 2009 11:00 am, edited 1 time in total.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Post Reply