1

I read this paragraph from the Delphi Tools Site

Changes since the last SVN update are:
  Added support for FreePascal-like compile-time $INCLUDE “macros”:
    %FILE% and %LINE% insert the current filename and line number into the source
    %FUNCTION% inserts the current function name, or class.method name into the source
    %DATE% and %TIME% allow inserting the compile date/time

Is there a way we can define macros in DWScript (other than these functions) just like people define macors in excel (using VBscript) in a simple way, where the name of the script will be the name of the function that will be used later, without adding {$Include XXX} in the executed script?

N.B.: I konw this can be done by managing the written script to be saved in a certain file called functions for ex. then save the added function with its name to be used (Add), then the user will write Add(1,2) to get the result; but my boss at work wants it to be something that looks like vbscript in excel.

Zeina
  • 1,573
  • 2
  • 24
  • 34
  • 1
    I fail to see any similarities between Office macros and the ones outlined above. – OnTheFly Mar 13 '12 at 08:24
  • 1
    from what I know this is not possible out of the box, but hey, you got the source, see how it's done, modify it to fit your needs. –  Mar 13 '12 at 08:24
  • @Dorin, Thanks, I love having the code... – Zeina Mar 13 '12 at 08:37

1 Answers1

4

I'm not sure to understand the question, so I'll list various answers to various possible interpretations...

  • If you want to declare functions that are implicitly supported by the scripting engine without having to "{$include}" or "uses" them, you can declare them via a TdwsUnit component, and attach it to the script component. If you don't have the "coExplicitUses" option set, they'll be available automatically, and you get design-time support in the IDE.

  • If you want to add internal functions (that are always there), use one of the RegisterInternalFunction overloads, you can check any of the "dwsXxxxFunctions.pas" units for examples. That's potentially more efficient, but also more cumbersome.

  • If you want to pre-process custom source-level macros in the source code (ala C's macros), you can use the filters functionality (check the HTML or JS filters as example of how a filter can be implemented).

  • If you want to react dynamically to "unknown" names, so you can declare them on the spot or bind them to something dynamically, you can use TdwsLanguageExtension.FindUnknownName, that's how the RTTI environment works f.i. (see TRTTIEnvironment in dwsRTTIConnector).

  • If you want to parse completely custom areas of code in a completely custom way, you can use language extensions too, override ReadInstr and check how asmLib & the JSLibModule do it to support "asm".

Eric Grange
  • 5,931
  • 1
  • 39
  • 61
  • The one I need, is the "unknown names" to work with dynamically. I'll check the RTTIConnector and respond soon. Thanks Eric. – Zeina Mar 14 '12 at 07:03