Re: Ah've got them Function Pointer blues




"Ben Bacarisse" <ben.usenet@xxxxxxxxx> wrote in message
news:87y72p9lvz.fsf@xxxxxxxxxxxx
"MikeC" <Mike.Best@xxxxxxxxxxxxx> writes:

The commands will be, for example

copy off // don't copy anythying from the input file to the output file
find_forward "a text string"
move_left 6 // characters
copy on // this causes any character scanned by the cursor to be copied
to
the output file
loop 6 // times
{ <more commands>
}
etc....

... you get the idea.
I wanted to run through the command (program) file and compile it into a
forth-like stack, with each stack element being a
structure, which contains, among other things, a pointer to the function
that will execute the command.

This does not follow automatically from what you have said. I have
done similar things and there is no reason why the functions /need/ to
have different types. One way to look at it is that the functions all
modify the state of a "text copying virtual machine". I.e. each one
takes a pointer to a structure that describes that program's state:
the file positions, variable bindings, stack and so on.

While there is a cost here (all the state get piled into one place)
the payoff is that you don't need a big switch effectively doing
run-time type checking. Remember I said that at the time of the call
the function pointer must be cast to the correct type? You end up
with a messy if-then-else chain (or a switch) resolving the correct
way to call each of the various kinds. It is well worth trying to get
them all to be the same.

--
Ben.

Thanks Ben, but I'm not sure I understand what you are thinking about.
The instructions above are a small sub-set of the instructions that will be
available.

find_forward "a text string" takes a string argument.
move_left 6 takes a decimal integer.
add var_2 14 takes a variable and a constant

I will have variables which I can add together, subtract, do simple
arithmetic, etc.

It seems to me all the functions are different. I can't envisage what you
have in mind - but then, you probably have a wealth of experince and
shoulder-rubbing with other programmers. I've been doing it for a long
time, but I have never known anybody else with an interest in it.

I can have a state machine to handle the text, yes, but it will have quite a
lot of buttons to push, and sometimes, two or three buttons in combination
to get it to the next state. Actually, the concept of a state machine had
not occurred to me. Thanks - I'll give that some thought.

What I have in mind would require a degree of pre-compilation, which would
require a switch, but I was planning to use the function pointers to replace
the switch as described in http://www.newty.de/fpt/intro.html. At run-time,
it would use the concepts of the Forth virtual machine, which is a simple,
reverse polish, stack-based machine. Very quick at run time for a
semi-interpreted language.

.... but we're getting quite off-topic. Is there an architecture group that
would be more appropriate?

Thanks,

MikeC


.