Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-dambovaliev committed Jul 8, 2024
1 parent 9df6662 commit db713cc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 70 deletions.
83 changes: 49 additions & 34 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2806,9 +2806,32 @@ func findUndefined(store Store, last BlockNode, x Expr) (un Name) {
}

// finds the next undefined identifier and returns it if it is global
func findUndefined2SkipLocals(store Store, last BlockNode, x Expr, t Type, st *SymbolTable) Name {
func findUndefined2SkipLocals(store Store, last BlockNode, x Expr, t Type) Name {
name := findUndefined2(store, last, x, t)

existsLocal := func(name Name, bn BlockNode) bool {
curr := bn
for {
currNames := bn.GetBlockNames()

for _, currName := range currNames {
if currName == name {
return true
}
}

if bn.GetStaticBlock().Source == curr {
return false
}

curr = bn.GetStaticBlock().Source

if curr == nil {
return false
}
}
}

if name != "" {
pkg := packageOf(last)

Expand All @@ -2817,7 +2840,7 @@ func findUndefined2SkipLocals(store Store, last BlockNode, x Expr, t Type, st *S
return ""
}

isLocal := st.IdentifierExists(string(name))
isLocal := existsLocal(name, last)

if isLocal {
return ""
Expand All @@ -2827,150 +2850,143 @@ func findUndefined2SkipLocals(store Store, last BlockNode, x Expr, t Type, st *S
return name
}

func findUndefinedStmt(store Store, last BlockNode, stmt Stmt, t Type, st *SymbolTable) Name {
func findUndefinedStmt(store Store, last BlockNode, stmt Stmt, t Type) Name {
switch s := stmt.(type) {
case *BranchStmt:
case *ValueDecl:
for _, rh := range s.Values {
un := findUndefined2SkipLocals(store, last, rh, t, st)
un := findUndefined2SkipLocals(store, last, rh, t)

if un != "" {
return un
}
}

for _, rh := range s.NameExprs {
st.AddIdentifier(string(rh.Name))
}
case *DeclStmt:
for _, rh := range s.Body {
un := findUndefinedStmt(store, last, rh, t, st)
un := findUndefinedStmt(store, last, rh, t)

if un != "" {
return un
}
}
case *IncDecStmt:
un := findUndefined2SkipLocals(store, last, s.X, t, st)
un := findUndefined2SkipLocals(store, last, s.X, t)

if un != "" {
return un
}
case *PanicStmt:
un := findUndefined2SkipLocals(store, last, s.Exception, t, st)
un := findUndefined2SkipLocals(store, last, s.Exception, t)

if un != "" {
return un
}
case *BlockStmt:
for _, rh := range s.Body {
un := findUndefinedStmt(store, last, rh, t, st)
un := findUndefinedStmt(store, s, rh, t)

if un != "" {
return un
}
}
case *DeferStmt:
un := findUndefined2SkipLocals(store, last, s.Call.Func, t, st)
un := findUndefined2SkipLocals(store, last, s.Call.Func, t)

if un != "" {
return un
}

for _, rh := range s.Call.Args {
un = findUndefined2SkipLocals(store, last, rh, t, st)
un = findUndefined2SkipLocals(store, last, rh, t)

if un != "" {
return un
}
}
case *SwitchStmt:
un := findUndefined2SkipLocals(store, last, s.X, t, st)
un := findUndefined2SkipLocals(store, last, s.X, t)
if un != "" {
return un
}

un = findUndefinedStmt(store, last, s.Init, t, st)
un = findUndefinedStmt(store, last, s.Init, t)
if un != "" {
return un
}

for _, b := range s.Clauses {
b := b
un = findUndefinedStmt(store, last, &b, t, st)
un = findUndefinedStmt(store, s, &b, t)

if un != "" {
return un
}
}
case *SwitchClauseStmt:
for _, rh := range s.Cases {
un := findUndefined2SkipLocals(store, last, rh, t, st)
un := findUndefined2SkipLocals(store, last, rh, t)

if un != "" {
return un
}
}

for _, b := range s.Body {
un := findUndefinedStmt(store, last, b, t, st)
un := findUndefinedStmt(store, last, b, t)

if un != "" {
return un
}
}

case *ExprStmt:
return findUndefined2SkipLocals(store, last, s.X, t, st)
return findUndefined2SkipLocals(store, last, s.X, t)
case *AssignStmt:
for i, rh := range s.Rhs {
if s.Op == DEFINE {
st.AddIdentifier(string(s.Lhs[i].(*NameExpr).Name))
}
un := findUndefined2SkipLocals(store, last, rh, t, st)
for _, rh := range s.Rhs {
un := findUndefined2SkipLocals(store, last, rh, t)

if un != "" {
return un
}
}
case *IfStmt:
un := findUndefined2SkipLocals(store, last, s.Cond, t, st)
un := findUndefined2SkipLocals(store, last, s.Cond, t)
if un != "" {
return un
}

un = findUndefinedStmt(store, last, &s.Else, t, st)
un = findUndefinedStmt(store, last, &s.Else, t)
if un != "" {
return un
}

un = findUndefinedStmt(store, last, &s.Then, t, st)
un = findUndefinedStmt(store, last, &s.Then, t)
if un != "" {
return un
}
case *IfCaseStmt:
for _, b := range s.Body {
un := findUndefinedStmt(store, last, b, t, st)
un := findUndefinedStmt(store, last, b, t)

if un != "" {
return un
}
}
case *ReturnStmt:
for _, b := range s.Results {
un := findUndefined2SkipLocals(store, last, b, t, st)
un := findUndefined2SkipLocals(store, last, b, t)
if un != "" {
return un
}
}
case *RangeStmt:
un := findUndefined2SkipLocals(store, last, s.X, t, st)
un := findUndefined2SkipLocals(store, last, s.X, t)
if un != "" {
return un
}

for _, b := range s.Body {
un := findUndefinedStmt(store, last, b, t, st)
un := findUndefinedStmt(store, last, b, t)
if un != "" {
return un
}
Expand Down Expand Up @@ -3095,9 +3111,8 @@ func findUndefined2(store Store, last BlockNode, x Expr, t Type) (un Name) {
ct.String()))
}
case *FuncLitExpr:
st := NewSymbolTable()
for _, stmt := range cx.Body {
un = findUndefinedStmt(store, last, stmt, t, st)
un = findUndefinedStmt(store, last, stmt, t)

if un != "" {
return
Expand Down
36 changes: 0 additions & 36 deletions gnovm/pkg/gnolang/symbols_table.go

This file was deleted.

0 comments on commit db713cc

Please sign in to comment.