eval, reval¶
Evaluate parse trees
eval¶
Evaluate a parse tree
eval x eval[x]
Where x is a parse tree, returns the result of evaluating it.
The eval function is the complement of parse and can evaluate the parse trees it returns, as well as parse trees you construct yourself.
q)parse "2+3"
+
2
3
q)eval parse "2+3"
5
q)eval (+;2;3) / constructed explicitly
5
A string is data, not a parse tree, so eval returns it unchanged. To evaluate a string, use value.
q)eval "2+3"
"2+3"
q)value "2+3"
5
reval¶
Restricted evaluation of a parse tree
reval x reval[x]
The reval function is similar to eval, and behaves as if the command-line options -u 1 and -b were active during evaluation.
It also blocks all system calls that change state.
That is, when it evaluates a message from a client, reval
- prevents amendment of globals
- blocks all writes to the file system
- allows read access only to files in the working directory and below
- blocks
exitand opening a file withhopen
A typical use is inside the message handler .z.pg, for access control. In the following example, a process listening on port 5000 defines:
q).z.pg:{reval(value;enlist x)}
A client process on the same host then gets:
q)h:hopen 5000
q)h"1+2"
3
q)h"a:4"
'noupdate: `. `a
q)h"read0`:/etc/hosts"
'access: /etc/hosts
q)h"hopen`:somefile"
'access: somefile
q)h"system\"pwd\""
'sys
Like -b, the restrictions apply only to messages from clients
When you call reval from the console or a script, it doesn't apply all of them. For example, reval(value;enlist "zz:4") updates zz.