Call script from another

Suppose this function is stored in some script:

function Add(a,b:integer):integer; begin result:=a+b; end; 

and I have another script that looks like this:

 var a,b,c:integer; a:=1; b:=2; c:=Add(a,b); println(inttoStr(c)); 

how can I compile both scripts and call the first script from the second using dwscript in Delphi ?

+4
source share
2 answers

assuming a single file is called "file1.extension" and its contents:

 function Add(a,b:integer):integer; begin result:=a+b; end; 

and another file called "main.extension" with content:

 var a,b,c:integer; a:=1; b:=2; c:=Add(a,b); println(inttoStr(c)); 

you need to add the following line at the beginning of main.extension:

 // note that file name is case sensitive // file1.extension <> FILE1.EXTENSION // include_once is to solve cycle-includes // ie file1.extension includes main.extension and vice-versa {$include_once 'file1.extension'} // or include if file1.extension does not require functions/objects/variables/etc. // from main.extension {$include 'file1.extension'} 

I suggest using {$ include_once ...} instead of {$ include ...}.

+4
source

In addition to using $ include / $ include_once, as indicated by Dorinโ€™s answer, you can also use more traditional units with the โ€œusesโ€ operator if you are in SVN version (2.3).

The units can be either โ€œclassicโ€ units with an interface / implementation, or mixed units that follow the extended script syntax (just omit the โ€œinterfaceโ€ keyword and start declaring / embedding).

The easiest way to pass the source to the compiler is to use events (OnInclude for include source, OnNeedUnit for the source code), but you can also pass them by specifying CompileFileSystem.

+3
source

Source: https://habr.com/ru/post/1399937/


All Articles