Using Eval in an Application
----------------------------
To use eval as part of an application, just copy the single (20KB) function into
your workspace.
    )copy eval eval
c:\dyalog90\samples\dfns\eval saved ...
Note  that  if eval can't reduce an expression to a number, it returns the char-
acter vector form of the (possibly reduced) expression:
    eval'2+3'           ⍝ numeric result.
5
    eval'1+2+x+4'       ⍝ character result.
3+x+4
We might want to trap such results as errors:
    rslt←eval expr
    0≠⍬⍴0⍴rslt:'bad expr'⎕signal 11
Environment
-----------
An  environment vector of name-value pairs may be passed to eval as a left argu-
ment:
    ('A1' 6)('B2' 7) eval 'A1 * B2'
42
Errors
------
Syntactically  incorrect  expressions  generate  an error (666) with a "helpful"
message.  The message is preceded by an indented marker to show the exact posit-
ion of the problem:
      until''
    2+3+4++5+6+7
          \ missing operand
In  an  application,  we  might want to separate the marker from the text of the
message.
    emsg _ _←⎕dm            ⍝ split diagnostic message vector.
    drop←2++/^\' '=emsg     ⍝ number of chars to drop from message.
    text←drop↓emsg          ⍝ raw error message text.
    indx←⎕io+drop-2         ⍝ index of error in source char vector.
Back to: →Contents←