You could probably write this code in an afternoon, if you are a slow coder. A simple
language with nested if-then-else isn't hard (include an endif, fi, or some other
terminator and your job is much easier). I once wrote a scripting language not dissimilar
to this in five hours. A simple recursive-descent parser and a tirival tokenizer can give
you a tree you interpret, then you just do an LRN treewalk. If I weren't leaving on Sunday
for a couple weeks, I'd have time to find an old piece of code I wrote, but it is backed
up to 5 1/4" floppies and I don't have a machine that can read them any longer.
Here's the sketch:
write a simple FSM parser that recognizes words like IF, identifiers, constants, and
operators. It should return something like this
class lexeme{
public:
CString token;
};
class op : public lexeme{
public:
op(const CString & name) { token = name; }
};
and from this
class plus : public op {
public:
virtual void Execute() {
int a = stack.pop();
int b = stack.pop();
stack.push(a + b);
};
and so on.
class keyword : public lexeme{
public:
keyword(const CString & name) { token = name; }
};
class identifier : public lexeme{
public:
identifier(const CString & name) { token = name; }
void Execute() { stack.push(lookup(token)); }
};
Now this sketch obviously skips over error handling and other niceities; that's why it
takes an afternoon instead of an hour. Use throw to handle errors; if there is a script
error, do something like
class CScriptExeception : public CException { }
and have subclasses for undefined variables, divide-by-zero, and all the usual cases you
might care about.
class constant : public lexeme{
public:
identifier(const CString & name) { token = name; }
};
class Tree {}
class BinaryOp : public Tree {
public:
Tree * left;
Tree * right;
op * operator;
};
class UnaryOp : public Tree {
public:
Tree * left;
};
class Leaf : public Tree {
public:
lexeme * leaf;
}
Now you can add to those tree nodes a virtual method execute(); for a binary node, it
turns out to be
virtual void Execute() {
left->Execute();
right->Execute();
operator ->Execute();
};
I hope this helps. Otherwise you have a fairly long task to integrate VBScript,
JavaScript, etc. into your app, which in your case doesn't sound justified.
joe
Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web:
http://www.flounder.com
MVP Tips:
http://www.flounder.com/mvp_tips.htm