compileLoopControl compiles loop control statements.
(control *ast.LoopControl)
| 8 | |
| 9 | // compileLoopControl compiles loop control statements. |
| 10 | func (f *Function) compileLoopControl(control *ast.LoopControl) error { |
| 11 | call := control.Expression |
| 12 | |
| 13 | if call.Token.Kind != token.Call { |
| 14 | return errors.New(InvalidExpression, f.File, control.Expression.Source()) |
| 15 | } |
| 16 | |
| 17 | if len(call.Children) != 1 { |
| 18 | return errors.New(InvalidExpression, f.File, control.Expression.Source()) |
| 19 | } |
| 20 | |
| 21 | loop := f.loopStack.Current() |
| 22 | |
| 23 | if loop == nil { |
| 24 | return errors.New(LoopControlWithoutLoop, f.File, control.Expression.Source()) |
| 25 | } |
| 26 | |
| 27 | name := call.Children[0].Token.StringFrom(f.File.Bytes) |
| 28 | |
| 29 | switch name { |
| 30 | case "next": |
| 31 | f.loopNext(loop) |
| 32 | case "restart": |
| 33 | // reserved |
| 34 | case "stop": |
| 35 | f.jump(loop.Exit) |
| 36 | default: |
| 37 | correctName := "" |
| 38 | |
| 39 | if name == "break" { |
| 40 | correctName = "stop" |
| 41 | } |
| 42 | |
| 43 | return errors.New(&InvalidLoopControl{Name: name, CorrectName: correctName}, f.File, call.Children[0].Source()) |
| 44 | } |
| 45 | |
| 46 | return nil |
| 47 | } |
no test coverage detected