# `eval`, `reval`

_Evaluate parse trees_

## `eval`

_Evaluate a parse tree_
```syntax
eval x     eval[x]
```

Where `x` is a parse tree, returns the result of evaluating it.

The `eval` function is the complement of [`parse`](parse.md) and can evaluate the parse trees it returns, as well as parse trees you construct yourself.
```q
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`](value.md).
```q
q)eval "2+3"
"2+3"
q)value "2+3"
5
```

## `reval`

_Restricted evaluation of a parse tree_
```syntax
reval x     reval[x]
```

The `reval` function is similar to [`eval`](#eval), and behaves as if the [command-line options `-u 1`](cmdline.md#-u-disable-syscmds) and [`-b`](cmdline.md#-b-blocked) 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 [`exit`](exit.md) and opening a file with [`hopen`](hopen.md)

A typical use is inside the message handler [`.z.pg`](dotz.md#zpg-get), for access control. In the following example, a process listening on port 5000 defines:
```q
q).z.pg:{reval(value;enlist x)}
```

A client process on the same host then gets:
```q
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
```

> **Note — 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`.
>
----

[`parse`](parse.md),
[`value`](value.md)


[Table counts in a partitioned database](../how_to/interact_with_databases/partition.md#table-counts)
