Branch: Tag:

2011-11-04

2011-11-04 14:51:43 by Per Hedbor <ph@opera.com>

Added support for dynamic compile-time macros.

Some simple examples;

> master()->add_predefine("RND()", lambda( string arg1 ) { return random((int)arg1); });
> RND(10);
(2) Result: 9
> cpp("RND(123)")
(3) Result: "19"

It can also be used to override the default builtin macros. If you
want "fun" debug information (not used for backtraces, but code using
__LINE__ will be confused):

> master()->add_predefine("__LINE__", lambda() { return random(4711); });
> __LINE__;
(6) Result: 2593
> __LINE__;
(7) Result: 1780

3198:       //! Add a define (without arguments) which will be implicitly    //! defined in @[cpp] calls. -  void add_predefine (string name, string value) +  void add_predefine (string name, mixed value)    {    predefines[name] = value;    }
3216:    return predefines;    }    +  string evaluate_define(string def, string arguments) +  { +  mixed val = arguments ? predefines[def+"()"] : predefines[def]; +  if( callablep(val) ) +  { +  object C = resolv("Parser.C"); +  array args; +  if( arguments ) +  args = map( +  map(C.group(C.split(arguments))/({","}),C.simple_reconstitute), +  resolv("String.trim_all_whites")); +  else +  args = ({}); +  +  val = val( @args ); +  } +  return (string)val; +  } +     //! Instantiate static modules in the same way that dynamic modules    //! are instantiated.    protected mapping(string:mixed) instantiate_static_modules(object|mapping static_modules)