Skip to content

Commit 4a63a2c

Browse files
committed
refactoring
1 parent d4b8f9b commit 4a63a2c

File tree

8 files changed

+240
-241
lines changed

8 files changed

+240
-241
lines changed

ast.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
11
package runevm
22

3-
type ExprType string
3+
type exprType string
44

55
const (
6-
Num ExprType = "num"
7-
Str ExprType = "str"
8-
Bool ExprType = "bool"
9-
Var ExprType = "var"
10-
Assign ExprType = "assign"
11-
Binary ExprType = "binary"
12-
Unary ExprType = "unary"
13-
Fun ExprType = "fun"
14-
If ExprType = "if"
15-
Block ExprType = "block"
16-
Call ExprType = "call"
17-
Return ExprType = "return"
18-
While ExprType = "while"
19-
Break ExprType = "break"
20-
Continue ExprType = "continue"
21-
Array ExprType = "array"
22-
Table ExprType = "table"
23-
Pair ExprType = "pair"
24-
Index ExprType = "Index"
25-
Import ExprType = "import"
6+
numExpr exprType = "num"
7+
strExpr exprType = "str"
8+
boolExpr exprType = "bool"
9+
varExpr exprType = "var"
10+
assignExpr exprType = "assign"
11+
binaryExpr exprType = "binary"
12+
unaryExpr exprType = "unary"
13+
funExpr exprType = "fun"
14+
ifExpr exprType = "if"
15+
blockExpr exprType = "block"
16+
callExpr exprType = "call"
17+
returnExpr exprType = "return"
18+
whileExpr exprType = "while"
19+
breakExpr exprType = "break"
20+
continueExpr exprType = "continue"
21+
arrayExpr exprType = "array"
22+
tableExpr exprType = "table"
23+
pairExpr exprType = "pair"
24+
indexExpr exprType = "Index"
25+
importExpr exprType = "import"
2626
)
2727

28-
type Expr struct {
29-
Type ExprType
28+
type expression struct {
29+
Type exprType
3030
// Multipurpose field for storing a value
3131
Value interface{}
3232

33-
Left *Expr
34-
Right *Expr
33+
Left *expression
34+
Right *expression
3535

3636
// Operator of binary expressions
3737
Operator string
3838

3939
// If/While
40-
Cond *Expr
41-
Then *Expr
42-
Else *Expr
40+
Cond *expression
41+
Then *expression
42+
Else *expression
4343

4444
// Function decl
45-
Func *Expr
45+
Func *expression
4646
// Function decl param names
4747
Params []string
4848

4949
// Entire block
50-
Block []*Expr
50+
Block []*expression
5151

5252
// Function call arguments
53-
Args []*Expr
53+
Args []*expression
5454

5555
// Function / while bodies
56-
Body *Expr
56+
Body *expression
5757

5858
// Index access / Field access
59-
Index *Expr
59+
Index *expression
6060

6161
// Token infos
6262
File string

environment.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ type Environment struct {
55
parent *Environment
66
}
77

8-
func NewEnvironment(parent *Environment) *Environment {
8+
func newEnvironment(parent *Environment) *Environment {
99
vars := make(map[string]interface{})
1010
return &Environment{vars: vars, parent: parent}
1111
}
1212

13-
func (env *Environment) Extend() *Environment {
14-
return NewEnvironment(env)
13+
func (env *Environment) extend() *Environment {
14+
return newEnvironment(env)
1515
}
1616

17-
func (env *Environment) Lookup(name string) *Environment {
17+
func (env *Environment) lookup(name string) *Environment {
1818
for scope := env; scope != nil; scope = scope.parent {
1919
if _, found := scope.vars[name]; found {
2020
return scope
@@ -23,19 +23,20 @@ func (env *Environment) Lookup(name string) *Environment {
2323
return nil
2424
}
2525

26-
func (env *Environment) Get(name string, exp *Expr) interface{} {
26+
func (env *Environment) get(name string, exp *expression) interface{} {
2727
if value, found := env.vars[name]; found {
2828
return value
2929
}
3030
if env.parent != nil {
31-
return env.parent.Get(name, exp)
31+
return env.parent.get(name, exp)
3232
}
3333
Error(exp, "Undefined variable '%s'", name)
3434
return nil
3535
}
3636

37-
func (env *Environment) Set(name string, value interface{}, exp *Expr) interface{} {
38-
scope := env.Lookup(name)
37+
// func (env *Environment) set(name string, value interface{}, exp *Expr) interface{} {
38+
func (env *Environment) set(name string, value interface{}) interface{} {
39+
scope := env.lookup(name)
3940
// if scope == nil && env.parent != nil {
4041
// Error(exp, "Undefined variable '%s'", name)
4142
// }
@@ -47,7 +48,7 @@ func (env *Environment) Set(name string, value interface{}, exp *Expr) interface
4748
return value
4849
}
4950

50-
func (env *Environment) Def(name string, value interface{}) interface{} {
51+
func (env *Environment) def(name string, value interface{}) interface{} {
5152
env.vars[name] = value
5253
return value
5354
}

0 commit comments

Comments
 (0)