Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
RednibCoding committed Jun 25, 2024
1 parent d4b8f9b commit 4a63a2c
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 241 deletions.
66 changes: 33 additions & 33 deletions ast.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
package runevm

type ExprType string
type exprType string

const (
Num ExprType = "num"
Str ExprType = "str"
Bool ExprType = "bool"
Var ExprType = "var"
Assign ExprType = "assign"
Binary ExprType = "binary"
Unary ExprType = "unary"
Fun ExprType = "fun"
If ExprType = "if"
Block ExprType = "block"
Call ExprType = "call"
Return ExprType = "return"
While ExprType = "while"
Break ExprType = "break"
Continue ExprType = "continue"
Array ExprType = "array"
Table ExprType = "table"
Pair ExprType = "pair"
Index ExprType = "Index"
Import ExprType = "import"
numExpr exprType = "num"
strExpr exprType = "str"
boolExpr exprType = "bool"
varExpr exprType = "var"
assignExpr exprType = "assign"
binaryExpr exprType = "binary"
unaryExpr exprType = "unary"
funExpr exprType = "fun"
ifExpr exprType = "if"
blockExpr exprType = "block"
callExpr exprType = "call"
returnExpr exprType = "return"
whileExpr exprType = "while"
breakExpr exprType = "break"
continueExpr exprType = "continue"
arrayExpr exprType = "array"
tableExpr exprType = "table"
pairExpr exprType = "pair"
indexExpr exprType = "Index"
importExpr exprType = "import"
)

type Expr struct {
Type ExprType
type expression struct {
Type exprType
// Multipurpose field for storing a value
Value interface{}

Left *Expr
Right *Expr
Left *expression
Right *expression

// Operator of binary expressions
Operator string

// If/While
Cond *Expr
Then *Expr
Else *Expr
Cond *expression
Then *expression
Else *expression

// Function decl
Func *Expr
Func *expression
// Function decl param names
Params []string

// Entire block
Block []*Expr
Block []*expression

// Function call arguments
Args []*Expr
Args []*expression

// Function / while bodies
Body *Expr
Body *expression

// Index access / Field access
Index *Expr
Index *expression

// Token infos
File string
Expand Down
19 changes: 10 additions & 9 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ type Environment struct {
parent *Environment
}

func NewEnvironment(parent *Environment) *Environment {
func newEnvironment(parent *Environment) *Environment {
vars := make(map[string]interface{})
return &Environment{vars: vars, parent: parent}
}

func (env *Environment) Extend() *Environment {
return NewEnvironment(env)
func (env *Environment) extend() *Environment {
return newEnvironment(env)
}

func (env *Environment) Lookup(name string) *Environment {
func (env *Environment) lookup(name string) *Environment {
for scope := env; scope != nil; scope = scope.parent {
if _, found := scope.vars[name]; found {
return scope
Expand All @@ -23,19 +23,20 @@ func (env *Environment) Lookup(name string) *Environment {
return nil
}

func (env *Environment) Get(name string, exp *Expr) interface{} {
func (env *Environment) get(name string, exp *expression) interface{} {
if value, found := env.vars[name]; found {
return value
}
if env.parent != nil {
return env.parent.Get(name, exp)
return env.parent.get(name, exp)
}
Error(exp, "Undefined variable '%s'", name)
return nil
}

func (env *Environment) Set(name string, value interface{}, exp *Expr) interface{} {
scope := env.Lookup(name)
// func (env *Environment) set(name string, value interface{}, exp *Expr) interface{} {
func (env *Environment) set(name string, value interface{}) interface{} {
scope := env.lookup(name)
// if scope == nil && env.parent != nil {
// Error(exp, "Undefined variable '%s'", name)
// }
Expand All @@ -47,7 +48,7 @@ func (env *Environment) Set(name string, value interface{}, exp *Expr) interface
return value
}

func (env *Environment) Def(name string, value interface{}) interface{} {
func (env *Environment) def(name string, value interface{}) interface{} {
env.vars[name] = value
return value
}
Loading

0 comments on commit 4a63a2c

Please sign in to comment.