Author Topic: meta programming and LPC etc  (Read 5112 times)

Offline silenus

  • BFF
  • ***
  • Posts: 196
    • View Profile
meta programming and LPC etc
« on: April 09, 2007, 03:01:02 PM »
Hi folks,

I have been working on a meta programming tool for MudOS which so far seems to work on DS 2.4.1 but I am sure will work for the most part on other libraries for MudOS. The issue that I am wrestling with is how to further develop this tool. The basic idea I have so far implemented is quite simple-

The code simply hooks into compile_object with a slightly altered syntax. The file is parsed and converted into a generator LPC object which then is used to create the real LPC code which is compiled as an object and returned as object.

The issue here for me is my syntax so far is far from pretty. Currently @@@ tags are used to mark special regions and these are parsed as either text functions or copied wholesale into the constructed object file. The text functions are called by the user in a special block which is used to construct the remainder of the file. I wish however that this syntax could be a bit cleaner.

I am curious if there have been other projects for extending LPC in this manner which might have a nicer syntax I could perhaps emulate. Or if anyone has better ideas of how to mix LPC as data and LPC as generative functions in a single file.

Regards,

Silenus.


Offline chaos

  • BFF
  • ***
  • Posts: 291
  • Job, school, social life, sleep. Pick 2.5.
    • View Profile
    • Lost Souls
Re: meta programming and LPC etc
« Reply #1 on: July 12, 2007, 05:05:14 PM »
You know how PHP uses <?php ?> syntax?  (Often just <? ?>, but that isn't really best practice, for just this reason...)  It's intended that <?lang ?> should be a general syntax, and it's not too objectionable.  So if your preprocessing language were called LPX, say, you could use <?lpx ?>.  Prettier than @@@, and anyone who's been exposed to PHP will probably know what's going on instantly.

Offline silenus

  • BFF
  • ***
  • Posts: 196
    • View Profile
Re: meta programming and LPC etc
« Reply #2 on: July 25, 2007, 09:49:49 AM »
I actually havent worked on this in some time and my version of the code is rather crude and more of a proof of concept thing rather than a robust implementation. It works for what I wanted it it to do but doesnt trap all exception cases properly I probably will go back and fix this. I did thing somewhat backwards from your suggestion initially which was to instead of have a document with embedded LPC tags was to add extra text function blocks which would be processed to create the metaprogrammed file.

Unfortunately unlike something like Ruby you cannot dynamically metaprogram most LPC variants. This would be alot nicer than this approach but I dont see a way to do this without writing your own server. Here is an example file of what I was working on- it's a bit ugly.

Code: [Select]
---VARSTAT(STAT)
private int $STAT$; private nosave int $STAT$_cache; private nosave function array $STAT$_bonuses = ({});
---

---SETSTAT(STAT, DEPENDS_BLOCK)
// need to update dependencies
public void set_$STAT$(int x)
{
$STAT$ = x;
$STAT$_cache = implode( map($STAT$_bonuses, (: evaluate($1, $($STAT$) ) :) ), (: $1 + $2 :), 0 );
}
---

---ADDSTATBONUS(STAT)
// need to update dependencies.....
public int add_$STAT$_bonus(function f)
{
$STAT$_cache += evaluate(f, $STAT$ );
for(int i = 0;i < sizeof( $STAT$_bonuses );++i)
if( $STAT$_bonuses[i] == 0)
{
$STAT$_bonuses[i] = f;
return i;
}
$STAT$_bonuses += ({ f });
return sizeof($STAT$_bonuses) - 1;
}
---

---REMOVESTATBONUS(STAT)
// need to update dependencies.....
public int remove_$STAT$_bonus(int i)
{
$STAT$_cache -= evaluate( $STAT$_bonuses[i], this_object(), $STAT$ );
$STAT$_bonuses[i] = 0;

}
---

---UPDATE(STAT, FORMULA)
public void update_$STAT$
{

}
---

---GETSTAT(STAT)
public int get_$STAT$()
{
return $STAT$_cache;
}
---

---GETSTATBASE(STAT)
public int get_$STAT$_base()
{
return $STAT$;
}
---

---GETSTATBONUS(STAT)
public int get_$STAT$_base()
{
return $STAT$_cache - $STAT$;
}
---

class stat
{
string formula;
string array depends on;
}


private mapping stats

private string array stats = ({"strength","stamina","agility","dexterity","intelligence","will","eloquence","empathy","constitution","appearance"});

string generate(string array args, function f)
{
return implode( map(args, (: evaluate($(f),$1) :) ), (: $1 + $2 :), "" );
}

string generate_code(mixed array args)
{
return generate(stats, (: VARSTAT($1) :) ) + generate(stats, (: SETSTAT($1) :) ) + generate(stats, (: ADDSTATBONUS($1) :) ) + generate(stats, (: GETSTAT($1) :) );
}

 

Offline Tricky

  • BFF
  • ***
  • Posts: 189
  • I like what I code and I code what I like!
    • View Profile
Re: meta programming and LPC etc
« Reply #3 on: July 25, 2007, 10:49:56 AM »
There is actually something called lpscript that is used by Lima. I can't seem to find an manual or help files for it. I think Cratylus tried messing with it when he was in an emulate every mudlib on DS mood.

You could always try asking some of the admin of LimaLib muds.

Code: (An example of lpscript - harry.scr) [Select]
# Do not remove the headers from this file! see /USAGE for more info.

is= monster,actor

name= Harry
gender= male
proper_name= Harry the affectionate
in_room_desc= Harry the affectionate
id= dude
long= Harry has an agreeable look.

periodic[2 to 10]:
  oneof
    !say What are you waiting for?
    !say Hello there!
    !say I don't like winter.
    !say I don't like snow.
    !say I don't like rain.
    !say Who are you?
    !say Why do you look like that?
    !say What are you doing here?
    !say Nice weather, isn't it?
    !smile
    !lsfbog
    !ack
    !rock
  end
end

trigger[*]:
  if (who notequal me)
    nexttrigger

trigger[* smiles.]:
  !smile

trigger[* has left the game.]:
  !say Why did $1 quit the game ?

trigger[* gets *.]:
  !say Why did $1 take $2 ?

trigger[* drops *.]:
  !say Why did $1 drop $2 ?

trigger[* kicks you.]:
  !kick $1

trigger[* kisses you.]:
  !blush

trigger[* nods.]:
  !nod solemnly
  !think did I miss something?
end

trigger[* laughs.]:
  !say Did I miss a joke?

trigger[* chuckles.]:
  !smirk

trigger[* boggles.]:
  !bog

trigger[* enters.]:
  !say Hi $1, nice to see you !

trigger[* leaves *.]:
  !go $2

trigger[* says: *yes*]:
  !say I disagree

trigger[* says: *no*]:
  !say I disagree

trigger[* says: *shut up*]:
  !say Why do you want me to shut up?

trigger[* says: *hello*]:
  !say Pleased to meet you!

trigger[* says: *LPC*]:
  !turing $1

trigger[* says: *stay here*]:
  !say Ok then.

trigger[* says: *not follow*]:
  !say Ok then.

trigger[* says: *get lost*]:
  !say Ok then $1.

trigger[* says: *.]:
  if (chance 20)
    !say Why do you say '$2' ???

Offline silenus

  • BFF
  • ***
  • Posts: 196
    • View Profile
Re: meta programming and LPC etc
« Reply #4 on: July 25, 2007, 12:34:12 PM »
I think the SKOTOS for DGD had some similar scripting language on a scripting language called Merry and ended up writing alot of their lib in a modified script language. The file you posted also reminds me of the UNQ system in Phantasmal even though the goals for that a decidedly more modest (it's a data format like XML which is used to specify object data without having to add any setup code in  create per usual LPC development).

My goals for having a metaprogramable LPCish toy (at least generatively) are probably more modest. I actually wanted to be able to do dynamic metaprogramming of some sort in LPC but this is perhaps mostly impossible currently. I.e. i thought it would be nice to be able to interogate objects (reflectively) and update them on the fly such as adding new functions using LPC code but I ended up settling initially on just having generative style code.

To make it more robust I suspect I would have to do a better job of parsing through the LPC in the file.     

Offline cratylus

  • Your favorite and best
  • Administrator
  • ***
  • Posts: 1020
  • Cratylus@Dead Souls <ds> np
    • View Profile
    • About Cratylus
Re: meta programming and LPC etc
« Reply #5 on: July 25, 2007, 02:19:05 PM »
Quote
I think Cratylus tried messing with it when he was in an emulate every mudlib on DS mood.

Heh...it was, in fact, Lima that knocked me completely off my "emulate every mudlib on DS" kick.
Great lib, etc. But weird. IMO.

I do very much like the idea of lpcscript. I did a little writeup on it
on mudconnector, when people started talking about some sort of
unified mud building standard:

http://www.mudconnect.com/discuss/discuss.cgi?mode=MSG&area=coders&message=24711#24711

The only doc I know of is in the lima help stuff:

http://dead-souls.net/code/libs/lima-1.0b5/lib/help/wizard/coding/lpscript

-Crat

Offline Tricky

  • BFF
  • ***
  • Posts: 189
  • I like what I code and I code what I like!
    • View Profile
Re: meta programming and LPC etc
« Reply #6 on: July 25, 2007, 02:45:10 PM »
I did a grep on LimaLib for lpscript and that file (lima-1.0b5/lib/help/wizard/coding/lpscript) didn't come up ... I forgot to turn case-insensitivity on.  :-[

My goals for having a metaprogramable LPCish toy (at least generatively) are probably more modest. I actually wanted to be able to do dynamic metaprogramming of some sort in LPC but this is perhaps mostly impossible currently. I.e. i thought it would be nice to be able to interogate objects (reflectively) and update them on the fly such as adding new functions using LPC code but I ended up settling initially on just having generative style code.

To make it more robust I suspect I would have to do a better job of parsing through the LPC in the file.     

Wasn't there some sort of programmable object that you created Cratylus? About 6 to 8 months ago I think.

Offline cratylus

  • Your favorite and best
  • Administrator
  • ***
  • Posts: 1020
  • Cratylus@Dead Souls <ds> np
    • View Profile
    • About Cratylus
Re: meta programming and LPC etc
« Reply #7 on: July 25, 2007, 03:07:21 PM »
Quote
Wasn't there some sort of programmable object that you created Cratylus? About 6 to 8 months ago I think.

Yeah...trying to remember who it was, this dude wanted to code an
object that would be like a computer you could add programs
to, adding functions on the fly and such, but he didn't really
understand the complexity of what he was asking for, so I coded an example:

http://dead-souls.net/code/alpha/alpha/lib/domains/default/armor/wristcomp.c
http://dead-souls.net/code/alpha/alpha/lib/domains/default/obj/module_cloak.c

MudOS is not designed to make it easy to add functions to objects that
don't ask for them. Presumably this is because arbitrary function adding
is a security risk. The Yautja wrist computer and the cloaking module for
it are a basic example of an object designed to accept functions from another
object. It's not very pretty, and it's not intended to be en example of how
it *should* be done. Just a kludgy demonstration of how it works.

Writing it gave me a whole new appreciation for the simplicity and ease
of shadows. After contorting myself into pretzels getting the wristcomp to
work, I decided to start working on a formalized lib subsystem for shadows,
because really sometimes things do need functions added, and really
it shouldn't be torture.

The alpha lib contains the basics of this new shadows subsystem,
and this doc describes it:

http://dead-souls.net/code/alpha/alpha/lib/doc/help/creators/shadows

-Crat

Offline silenus

  • BFF
  • ***
  • Posts: 196
    • View Profile
Re: meta programming and LPC etc
« Reply #8 on: July 26, 2007, 10:36:39 AM »
Thanks guys. I think I might have figured out a way to get what I want under MudOS. It probably again as clean as I would like either but a combination of shadows/generative programming might make what appears to be dynamically metaprogrammable objects possible along with some other interesting features. Making it all secure of course might be an interesting challenge.