The first "general purpose" codemonkey plugin
Posted: Tue Jun 27, 2006 5:41 am
Hi all.
Some of you may know that, in my quest for Clicker, i've been developing a PERL-based code rewriting tool: "CodeMonkey"
The general idea is that anyone should be able to write code the way he prefers rather than the way you're expected to. It's a sort of programmable pre-processor (aimed at C-like language, though i haven't investigated whether other languages could be supported as well). CodeMonkey is just a text processing framework that will read your sources, tokenize it and do a few "managing" operations, but the 'rewriting' proper is delegated to "plugins".
You tell in your sources what plugin you want to apply, e.g.
or
Then the plugin registers "keyword" on which CodeMonkey will call it back, for instance a "@synchronized" block to provide Java-like protection, etc. The most common ways to use those keywords are the "type modifiers" (e.g. having a 'public' keyword for C that add the function declaration in a .h file automatically) or "special blocks" such as @synchronized.
The reason why i'm talking about this now is that i'm just done with the first plugin that may have interest beyond my own research, e.g. processing arguments on command line. I don't know how you feel about "argc" and "argv", but honnestly, i never liked them. If you look into clicker's building tools, you'll notice weird stuff such as
which is a data block to be given to a "command line preprocessor" library of my own (clp.c/clp.h) each of those setXyz names are actually functions that will be invoked once the "preprocessor" (poor name, i agree) has recognized one of the stuff (e.g. it will call setName() if it finds "--name <xxx>" on the command line).
Even with my best cpp skills, i couldn't achieve something prettier than
That's small enough, but really boring to write ... and anything you find boring to write should be written by the CodeMonkey
Now, i can use e.g.
This code will default the values of max and kbps to 5, and the value of ip to 127.0.0.1 ... I only have three types pre-defined atm (that is, decimal ints, strings and 'flags'), but anyone could define new ones provided that you edit "args.pl" and describes them in the "%types" hash:
And a nice by-product is that it also automates the generation of "--help" messages
So ... what's your general feeling about it ... Is there any things where you think you might want to have a CodeMonkey plugin or is it just overcomplication on top of an already-bloated compiler for an imperfect programming language ?
Some of you may know that, in my quest for Clicker, i've been developing a PERL-based code rewriting tool: "CodeMonkey"
The general idea is that anyone should be able to write code the way he prefers rather than the way you're expected to. It's a sort of programmable pre-processor (aimed at C-like language, though i haven't investigated whether other languages could be supported as well). CodeMonkey is just a text processing framework that will read your sources, tokenize it and do a few "managing" operations, but the 'rewriting' proper is delegated to "plugins".
You tell in your sources what plugin you want to apply, e.g.
Code: Select all
@plugin "objects.pl","synchronized.pl"
Code: Select all
@plugin "GTK.pl"
The reason why i'm talking about this now is that i'm just done with the first plugin that may have interest beyond my own research, e.g. processing arguments on command line. I don't know how you feel about "argc" and "argv", but honnestly, i never liked them. If you look into clicker's building tools, you'll notice weird stuff such as
Code: Select all
// options locales
clpSet clpLocal={
"",4,"target options",{
{"--target",setStart,"sets the default target address"},
{"--mode",setType,"sets the module type. Valid arguments are "
"'kernal', 'system', 'intermediate' or 'endlevel'"
},
{"--name",setName,"sets the module name."},
},
&clpExperimental
};
Even with my best cpp skills, i couldn't achieve something prettier than
Code: Select all
CLPFILTER(setName) {
CHK_ARG(1,"setting module name");
App.name=GET_ARG(1);
CLN_ARG(2);
}
Now, i can use e.g.
Code: Select all
@plugin "args.pl"
// some #include here
@argument int kbps=(5,"emission rate");
@argument int max=(5,"amount of packets to be sent");
@argument str ip=("127.0.0.1","target IP address");
int main(int argc, char* argv[])
{
// local variables
@argument.prepare;
// now you can use kbps, max and ip ;)
}
Code: Select all
# how to process the existing types in @argument.
# new types can be added with new entries in this hash, e.g.
# - real C type for the argument (e.g. Person_t *)
# - number of arguments to retrieve on the command line (e.g. 2)
# - function or expression to create a value from the arguments
# from strings. (e.g. newPerson(%) which will be expanded into
# newPerson("Sylvain", "Martin") at run time.
# that would mean
# 'person' => "struct Person_t *::2::newPerson(%)"
my %types=(
'int'=>"int::1::atoi(%)",
'str'=>"char*::1::%",
'flag'=>"int::0::1",
);
If you're interested, you can download a "demo" package here.cmdemo> ./collect --help
Available Options :
Group "@args.pl auto-generated options":
* -kbps :emission rate(int)
* -max :amount of packets to be sent(int)
* -ip :target IP address(str)
Group "generic options" (--...):
* help :Display this help
So ... what's your general feeling about it ... Is there any things where you think you might want to have a CodeMonkey plugin or is it just overcomplication on top of an already-bloated compiler for an imperfect programming language ?