This "macro substitution" in Foxpro is not really anything to do with
macros - it is a run-time expression evaluator that parses and evaluates a
string.

Foxpro can do it because it is based on early purely interpreted Bill Gates
BASIC which always parses all statements every time they are executed.
JavaScript also has an eval() function because javascript is (nearly) purely
interpreted. SuperBASIC, SBASIC, VisualBASIC and JAVA cannot because they
program is compiled to execute a fixed sequence of pseudo instructions
written for a "virtual machine" (Java bytecodes), an "engine" (Pascal
p-codes)  or a "run-time interpreter" (S*BASIC, VisualBasic).

In principle, an Eval could be added to SuperBASIC.

Compilation
1) Detect that a statement includes an eval and do not parse / compile it
but extract the "eval" functions replacing them by special variables (e.g.
&01$, &02$ etc)
2) For each eval in the statement, generate an assignment &xx$ = [eval
parameter] and store it in the program
3) Store the modified statement uncompiled after these assignments.

Runtime, when the uncompiled statement is encountered
1) spawn a new set of SBASIC system variables and tables (in principle this
could be done once only)
2) substitute the values of the eval variables into the statement
3) parse and compile the statement
4) now the tricky bit - return to the main set of  SBASIC tables and execute
the statement

This would work (1000s of times slower than normal execution)  for simple
cases such as A="Upper": Print Eval( A & "('Hello'))

But imagine the chaos if someone did

If xxx: A="": B="End For" Else A="End For": B=""
For Index = 1 to 100
  ...
  Exit Index
...
Eval (A)
...
Eval (B)

The end of the loop depends on the value of xxx and can change during
execution, but SBASIC compiles the end of the loop before execution.
This trick does work in Javascript because it is purely and slowly
interpreted.

Any suggestions?

Tony Tebby

----- Original Message -----
From: "François Van Emelen" <[EMAIL PROTECTED]>
To: "QL users list" <[EMAIL PROTECTED]>; "François Van Emelen"
<[EMAIL PROTECTED]>
Sent: mardi 18 février 2003 17:34
Subject: [ql-users] Sbasic and macro substitutions


>
> Hi all,
>
> I've been using Foxpro v2 (programmable database system) for several
> years now and 'macro substitution' is one of those powerful functions I
> use a lot in my programmes. I wonder whether such a function is
> available (possible?) in Sbasic.
> Here's an example of a 'macro substitution':
>
> l='('
> r=')'
> f='UPPER'
> v='"ql-smsq"'
> c=F+L+V+R
> ? c (?=print) displays as expected UPPER('ql-smsq')
> cc=&c (&=macro substitution sign)
> ?cc  displays QL-SMSQ
>
> Is there a way to achieve this in Sbasic (Qliberated Sbasic) ?
> How ? Why not?
>
> François Van Emelen
>

Reply via email to