Main Page | Report this Page
Computers Forum Index  »  Computer Compilers  »  A novice in compiler construction wants feedback...
Page 1 of 1    

A novice in compiler construction wants feedback...

Author Message
Marcus Johansson...
Posted: Thu Sep 03, 2009 6:27 pm
Guest
Hi,

In real life I work as programmer at a company that produces casual download
games. But for a year or so I've spent my friday nights on writing my own
little programming language. It's just a simple BASIC style thing, but I'm
still kind of proud of it since I had no experience in compiler construction
before I wrote the very first line of code. The compiler translates the
source code into an assembler-like instruction set and then adds these
instructions to an executable file that interprets itself (is there a name
for this technique?).

But why am I writing? I guess I simply want some feedback, since I have no
one to discuss this kind of work with. The compiler can be downloaded from
naalaa.webs.com/naalaa.zip. It comes with an editor with syntax highlithting
and some example code. And, yeah, it's for windows.

/Marcus
 
Derek...
Posted: Fri Sep 04, 2009 5:29 pm
Guest
On Sep 3, 8:27 am, "Marcus Johansson" <program... at (no spam) telia.com> wrote:
Quote:
In real life I work as programmer at a company that produces casual
download
games. But for a year or so I've spent my friday nights on writing my own
little programming language. It's just a simple BASIC style thing, but I'm
still kind of proud of it since I had no experience in compiler
construction
before I wrote the very first line of code. The compiler translates the
source code into an assembler-like instruction set and then adds these
instructions to an executable file that interprets itself (is there a name
for this technique?).

But why am I writing? I guess I simply want some feedback, since I have no
one to discuss this kind of work with. The compiler can be downloaded from
naalaa.webs.com/naalaa.zip. It comes with an editor with syntax highlithting
and some example code. And, yeah, it's for windows.

We'd love to comment but unfortunately there isn't much to comment on.
The problem is that you've not published the source code to the
compiler itself and hence we can only comment on whether it works or
not. Congratulations-- it does -- but you already knew that. We can't
give you the more useful feedback of commenting on how it works or
give any feedback on possible improvements or different methods, since
we have no way of knowing at the moment short of disassembling the
compile.exe file or just plain guessing. And I doubt that many of us
are going to go to that amount of trouble.

So, if you want useful feedback, you'll need to show us the source.

Cheers

Derek
 
Marcus Johansson...
Posted: Sat Sep 05, 2009 12:35 pm
Guest
Quote:
But why am I writing? I guess I simply want some feedback, since I have
no one to discuss this kind of work with. The compiler can be downloaded

The problem is that you've not published the source code to the
compiler itself and hence we can only comment on whether it works or
not.

The source? That would probably cause permanent mass laughter. But
there are a couple of things that I'm really curious of. Please have
understanding if I'm naive.

My intepreter is written in C. It has got 8 registers and a stack. They're
of a union type, something like:
typedef union {
int i;
float f;
DString *s;
Array *a;
}AnyType;

The compiled instructions are loaded into an array of, let's say:
typedef struct {
int command;
int dst;
int src;
} Instruction;

I've set up an array with function pointers matching the command values of
the instructions:
void CMD_MOVE_RR(void);
void CMD_MOVE_RC(void);
...
gCommands[MOVE_RR] = CMD_MOVE_RR;
gCommands[MOVE_RC] = CMD_MOVE_RC;
...

I then interpret the program with:

while (gProgramRunning) {
gCommands[gInstructions[gCurrentInstruction].command]();
gCurrentInstruction++;
}

Is this the usual way of doing it? And, above all, is there a smarter and
faster way of interpreting the instructions? Can I somehow get rid of all
those function calls above?

/Marcus
[That seems like an entirely reasonable way of doing a stack-based
interpreter. There are faster ways of doing interpreter dispatch, but
most are pretty kludgy. One thing you could do at the cost of
readability is to move the various commands into one routine and use a
switch to dispatch, since function calls tend to be slow, e.g.:

while (gProgramRunning) {
switch(gInstructions[gCurrentInstruction].command) {
MOVE_RR: do something ; break;
MOVE_RC: do somethng else; break;
...
}
gCurrentInstruction++;
}

We were all novices one time, your code isn't likely to
be any worse than our early attempts. -John]
 
 
Page 1 of 1    
All times are GMT
The time now is Sun Nov 29, 2009 3:56 pm