Skip to content

Commit

Permalink
Merge pull request #421 from Eclalang/issue-#420-struct-dependency-error
Browse files Browse the repository at this point in the history
Issue #420 struct dependency error
  • Loading branch information
mkarten authored Oct 3, 2024
2 parents bed1b47 + 139637a commit 90640c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions parser/Parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Parser struct {
}

var selectorDepth int
var inFunction bool

// Step moves the parser to the next token
func (p *Parser) Step() {
Expand Down Expand Up @@ -1068,6 +1069,12 @@ func (p *Parser) ParseAnonymousFunctionExpr() Expr {
tempAnonymousFunctionDecl.Prototype = p.ParsePrototype()
p.Step()
tempAnonymousFunctionDecl.Body = p.ParseBody()
// check the parsed prototype parameters and remove them from the list of dependencies if they are inside
for _, param := range tempAnonymousFunctionDecl.Prototype.Parameters {
if contains(param.Name, p.CurrentFile.Dependencies) {
p.CurrentFile.RemoveDependency(param.Name)
}
}
p.Step()
//check if it is a call
if p.CurrentToken.TokenType == lexer.LPAREN {
Expand Down Expand Up @@ -1122,12 +1129,20 @@ func (p *Parser) ParseFunctionDecl() Node {
p.HandleFatal("Expected '(' after function name")
return nil
}
inFunction = true
tempFunctionDecl.Prototype = p.ParsePrototype()
p.Step()
tempFunctionDecl.Body = p.ParseBody()
// check the parsed prototype parameters and remove them from the list of dependencies if they are inside
for _, param := range tempFunctionDecl.Prototype.Parameters {
if contains(param.Name, p.CurrentFile.Dependencies) {
p.CurrentFile.RemoveDependency(param.Name)
}
}
p.Step()
p.CurrentFile.FunctionDecl = append(p.CurrentFile.FunctionDecl, tempFunctionDecl.Name)
p.DisableEOLChecking()
inFunction = false
return tempFunctionDecl
}

Expand Down
14 changes: 14 additions & 0 deletions parser/Parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,13 @@ func TestParser_ParseAnonymousFunctionExpr(t *testing.T) {
t.Errorf("ParseAnonymousFunctionExpr() raised an error when it should not")
}
ok = false
// anonymous function expression with structs as parameters and field access in the body
resetWithTokens(&par, lexer.Lexer("function(a : Test, b : Test)( string) {return a.test + b.test;}"))
par.VarTypes["Test"] = "struct"
par.ParseAnonymousFunctionExpr()
if ok {
t.Errorf("ParseAnonymousFunctionExpr() raised an error when it should not")
}

e.RestoreExit()
}
Expand Down Expand Up @@ -1645,6 +1652,13 @@ func TestParser_ParseFunctionDecl(t *testing.T) {
t.Errorf("ParseFunctionDecl() did not raise the missing left parenthesis error")
}
ok = false
// function declaration with structs as parameters and field access in the body
resetWithTokens(&par, lexer.Lexer("function test(a : Test, b : Test)( string) {return a.test + b.test;}"))
par.VarTypes["Test"] = "struct"
par.ParseFunctionDecl()
if ok {
t.Errorf("ParseFunctionDecl() raised an error when it should not")
}

e.RestoreExit()
}
Expand Down

0 comments on commit 90640c6

Please sign in to comment.