Skip to content

Commit

Permalink
updated interpreter error and replaced parseExpr with the appropriate…
Browse files Browse the repository at this point in the history
… allowed expression
  • Loading branch information
mkarten committed Oct 1, 2024
1 parent e4455f6 commit 98e9634
Show file tree
Hide file tree
Showing 3 changed files with 25 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

0 comments on commit 98e9634

Please sign in to comment.