Compiler tutorial: suggest more stuff
Compiler tutorial: suggest more stuff
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)
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.
Looks very interesting, i will have a look.
http://code.google.com/intl/fr/opensource/
http://code.google.com/p/pyfilemaker/wi ... MLWrappers
You might try code.google (?)PS. Can anyone suggest a better publishing solution than blogspot (that is still quick to setup and use)? Blogspot keeps mutilating my code.
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
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Trond,
Wordpress? http://codex.wordpress.org/Writing_Code_in_Your_Posts
Also - very interesting tutorial. Nice work!
Wordpress? http://codex.wordpress.org/Writing_Code_in_Your_Posts
Also - very interesting tutorial. Nice work!
- flaith
- 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
That's a really good idea, thanks TrondTrond wrote:So I started a tutorial: http://pbtut.blogspot.com/

“Fear is a reaction. Courage is a decision.” - WC
Re: Compiler tutorial: suggest more stuff
Why not just blog it here in the PureBasic forums? With theTrond 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.
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
I will not put all the pieces together, but I will show all the pieces working independently.Teng wrote:
I don't know how far you plan to take this project (are you making a complete interpreter/compiler?)
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
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.Trond wrote:I will not put all the pieces together, but I will show all the pieces working independently.Teng wrote:
I don't know how far you plan to take this project (are you making a complete interpreter/compiler?)
@Trond
great job so far.
Wondering about this:
shouldn't it output 1 
Here it only works if the numbers are not using '9' but 9.
using newest beta...
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)

Here it only works if the numbers are not using '9' but 9.
using newest beta...
-
- Addict
- Posts: 2344
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
'0' = 48fsw wrote:@Trond
great job so far.
Wondering about this:shouldn't it output 1Code: Select all
Procedure IsDigit(C.c) If C >= '0' And C <= '9' ProcedureReturn 1 EndIf EndProcedure Debug IsDigit(4)
Here it only works if the numbers are not using '9' but 9.
using newest beta...
'9' = '0' + 9 = 57
ASCII

bye,
Daniel
Daniel
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.fsw wrote:@Trond
great job so far.
Wondering about this:shouldn't it output 1Code: Select all
Procedure IsDigit(C.c) If C >= '0' And C <= '9' ProcedureReturn 1 EndIf EndProcedure Debug IsDigit(4)
Here it only works if the numbers are not using '9' but 9.
using newest beta...
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?
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
. 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.
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.


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.

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