Skip to content

Commit 7cd9fa6

Browse files
authored
Add ast tree to program (#528)
* Add pro holder * explain
1 parent 7ba66ee commit 7cd9fa6

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
*.out
88
*.html
99
custom_tests.json
10+
pro/

Diff for: compiler/compiler.go

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err erro
4949

5050
program = NewProgram(
5151
tree.Source,
52+
tree.Node,
5253
c.locations,
5354
c.variables,
5455
c.constants,

Diff for: vm/program.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"text/tabwriter"
1111

12+
"github.com/expr-lang/expr/ast"
1213
"github.com/expr-lang/expr/builtin"
1314
"github.com/expr-lang/expr/file"
1415
"github.com/expr-lang/expr/vm/runtime"
@@ -21,6 +22,7 @@ type Program struct {
2122
Constants []any
2223

2324
source *file.Source
25+
node ast.Node
2426
locations []file.Location
2527
variables int
2628
functions []Function
@@ -30,6 +32,7 @@ type Program struct {
3032
// NewProgram returns a new Program. It's used by the compiler.
3133
func NewProgram(
3234
source *file.Source,
35+
node ast.Node,
3336
locations []file.Location,
3437
variables int,
3538
constants []any,
@@ -40,6 +43,7 @@ func NewProgram(
4043
) *Program {
4144
return &Program{
4245
source: source,
46+
node: node,
4347
locations: locations,
4448
variables: variables,
4549
Constants: constants,
@@ -50,6 +54,16 @@ func NewProgram(
5054
}
5155
}
5256

57+
// Source returns origin file.Source.
58+
func (program *Program) Source() *file.Source {
59+
return program.source
60+
}
61+
62+
// Node returns origin ast.Node.
63+
func (program *Program) Node() ast.Node {
64+
return program.node
65+
}
66+
5367
// Disassemble returns opcodes as a string.
5468
func (program *Program) Disassemble() string {
5569
var buf bytes.Buffer
@@ -346,8 +360,3 @@ func (program *Program) DisassembleWriter(w io.Writer) {
346360
}
347361
}
348362
}
349-
350-
// Source returns origin file.Source.
351-
func (program *Program) Source() *file.Source {
352-
return program.source
353-
}

0 commit comments

Comments
 (0)