Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated interpreter error and replaced parseExpr with the appropriate allowed expression #419

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading