Skip to content

Commit

Permalink
Merge branch 'issue-#418-calling-lib-function-error' into issue-#417-…
Browse files Browse the repository at this point in the history
…testing-pixel-graphics-engine
  • Loading branch information
mkarten committed Oct 2, 2024
2 parents 1b4b43e + ce67ecc commit 245a9a5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
9 changes: 9 additions & 0 deletions DEMO/Test/struct.ecla
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@ p.printSelf(p);
# access embedded struct fields
console.println(p.test.test);

# use a struct field in a binary operation
var p2 int = p.x + p.y;
console.println(p2);

# use a field of a struct field in a binary operation
var p3 string = p.test.test + p.test.test;
console.println(p3);


2 changes: 1 addition & 1 deletion interpreter/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ func RunSelectorExpr(expr parser.SelectorExpr, env *Env, Struct eclaType.Type) [
}
return []*Bus{NewMainBus(*result)}
default:
env.ErrorHandle.HandleError(expr.StartLine(), expr.StartPos(), "struct cannot have filed of type "+prev.GetType(), errorHandler.LevelFatal)
env.ErrorHandle.HandleError(expr.StartLine(), expr.StartPos(), "struct cannot have field of type "+prev.GetType(), errorHandler.LevelFatal)
}
default:
env.ErrorHandle.HandleError(expr.StartLine(), expr.StartPos(), "type "+prev.GetType()+" has no fields", errorHandler.LevelFatal)
Expand Down
16 changes: 15 additions & 1 deletion parser/Parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,21 @@ func (p *Parser) ParseSelector(x Expr) Expr {
p.HandleFatal("Expected field name after '.'")
return nil
}
selector := p.ParseExpr()
// check if the field is a function call
var selector Expr
if p.Peek(1).TokenType == lexer.LPAREN {
selector = p.ParseFunctionCallExpr()
} else {
selector = p.ParseVariableAccess()
p.Step()
}
// check if there is a period after the selector to see if it is a selector
if p.CurrentToken.TokenType == lexer.PERIOD {
p.Step()
selectorDepth++
selector = p.ParseSelector(selector)
selectorDepth--
}
return SelectorExpr{Field: p.CurrentToken, Expr: x, Sel: selector}
}

Expand Down
17 changes: 17 additions & 0 deletions parser/Parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,23 @@ func TestParser_ParseSelector(t *testing.T) {
}
ok = false

// selector with a function call
resetWithTokens(&par, lexer.Lexer("test.test()"))
par.MultiStep(2)
par.ParseSelector(nil)
if ok {
t.Errorf("ParseSelector() raised an error when it should not")
}
ok = false
// selector used in a binary expression
resetWithTokens(&par, lexer.Lexer("test.test + 1"))
par.MultiStep(2)
par.ParseSelector(nil)
if ok {
t.Errorf("ParseSelector() raised an error when it should not")
}
ok = false

e.RestoreExit()
}

Expand Down

0 comments on commit 245a9a5

Please sign in to comment.