Skip to content

Commit

Permalink
Update Internals.md
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jan 23, 2023
1 parent 0675733 commit e92442f
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions docs/Internals.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Internals

Expr is a stack based virtual machine. It compiles expressions to bytecode and
runs it.
Expr is a stack based virtual machine. It compiles expressions to bytecode and
runs it.

Compilation is done in a few steps:

- Parse expression to AST
- Type check AST
- Apply operator overloading
- Apply custom AST patching
- Apply operator overloading
- Apply custom AST patching
- Optimize AST
- Compile AST to a bytecode

Expand All @@ -19,7 +20,7 @@ Compiler has a bunch of optimization which will produce a more optimal program.
value in ['foo', 'bar', 'baz']
```

If Expr finds an `in` or `not in` expression with an array, it will be
If Expr finds an `in` or `not in` expression with an array, it will be
transformed into:

```js
Expand All @@ -28,11 +29,11 @@ value in {"foo": true, "bar": true, "baz": true}

## Constant folding

Arithmetic expressions with constants is computed on compile step and replaced
Arithmetic expressions with constants is computed on compile step and replaced
with the result.

```js
-(2-5)**3-2/(+4-3)+-2
-(2 - 5) ** 3 - 2 / (+4 - 3) + -2
```

Will be compiled to just single number:
Expand All @@ -44,7 +45,8 @@ Will be compiled to just single number:
## In range

```js
user.Age in 18..32
user.Age in 18.
.32
```

Will be replaced with a binary operator:
Expand All @@ -56,7 +58,8 @@ Will be replaced with a binary operator:
## Const range

```js
1..10_000
1.
.10_000
```

Ranges computed on compile stage, replaced with precreated slices.
Expand All @@ -77,3 +80,5 @@ fib(42)
Will be replaced with result of `fib(42)` on the compile step.

[ConstExpr Example](https://pkg.go.dev/github.com/antonmedv/expr?tab=doc#ConstExpr)

* Next: [Tips](Tips.md)

0 comments on commit e92442f

Please sign in to comment.