--
You received this message because you are subscribed to the Google Groups "4tH-compiler" group.
To post to this group, send email to 4th-co...@googlegroups.com.
To unsubscribe from this group, send email to 4th-compiler...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/4th-compiler?hl=en.
When exiting this function ErrLine will contain the address of
the word in the Code Segment where the error occured.
The member "ErrLine" of an Hcode header always points to the
next available word in the Code Segment. That is the place where
we will compile our 'BRANCH' instruction. When the instruction
pointer is set to that location, it will be automatically
incremented and get inside the definition.
24.19 Useful variables
We've already seen that dump_4th() can provide you with a lot of
information about Hcode. If you need this information, you don't
have to call dump_4th(). The dump_4th() function simply uses the
information that is already available. This small program shows
you how to obtain it:
Hcode *Program;
Program = comp_4th (strdup (".\" Hello world\" cr"));
if (Program == NULL)
printf ("Not enough memory\n");
else {
printf ("Error# : %u\n", Program->ErrNo);
printf ("Error at word: %d\n", Program->ErrLine);
printf ("Object size : %d\n", Program->CodeSiz);
printf ("String size : %u\n", Program->StringSiz);
printf ("Var. offset : %u\n", Program->Offset);
printf ("Variables : %u\n", Program->Variables);
printf ("Strings : %u\n", Program->Strings);
printf ("Reliable : %s\n", Program->Reliable ? "Yes" :
"No");
}
The labels are kept the same as in dump_4th(), so if you need
more information, read that section again.
25.18 <sec:Adding-words-with>Adding words with arguments
The very first thing you have to do is to make sure that your
code can be saved and loaded again. Words that only consist of a
token are saved without the argument. That reduces the size of
the HX file. If you want to save the argument you have to add a
line to both load_4th() and save_4th():
case (LITERAL):
case (PRINT):
case (BRANCH):
case (BRANCH0):
Now we have to add code to exec_4th() in order to execute '."'.
The first problem we encounter is: how do we access the argument?
Accessing an argument is quite an expression:
Object->CodeSeg [Object->ErrLine].Value
In which:
Object = Hcode pointer
CodeSeg = Member of Hcode, pointing to the Code Segment
ErrLine = Member of Hcode, pointing to the current word
Value = Member of word, holding the argument
In plain English it means: give me the argument of the currently
executed word in the Code Segment.
If you want to create your own branch instructions, you'll have
to define their behaviour in exec_4th(). If the argument of the
token contains the address you want to jump to in the end, you'll
have to define it like this:
JUMP (OPERAND);
That is pretty easy. This macro changes the Program Counter,
which is part of the Hcode header:
Object->ErrLine
Of course, we've defined a macro for that:
PROGCOUNT
If the address you want to jump to is issued by the user, you
probably want to check whether it is a valid execution token.
The member "ErrLine" of an Hcode header always points to the
next available word in the Code Segment. That is the place where
we will compile our 'BRANCH' instruction. When the instruction
pointer is set to that location, it will be automatically
incremented and get inside the definition.
Hans Bezemer
--
Absolutely no trees were killed to produce this sig. Well, OK, we had to tie
one up and torture it. That's it.
Visit our website! http://thebeez.home.xs4all.nl/4tH/
*** Home of the 4tH compiler! ***