From 80b5401dbc0fd14d9f82bdef4256384b60a7390d Mon Sep 17 00:00:00 2001 From: rafa_br34 <66086623+rafabr34@users.noreply.github.com> Date: Sun, 6 Feb 2022 01:56:21 -0300 Subject: [PATCH] Add files via upload --- rfcs/syntax-goto-statement.md | 115 ++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 rfcs/syntax-goto-statement.md diff --git a/rfcs/syntax-goto-statement.md b/rfcs/syntax-goto-statement.md new file mode 100644 index 00000000..5c201a42 --- /dev/null +++ b/rfcs/syntax-goto-statement.md @@ -0,0 +1,115 @@ +# Feature name + +Goto statement + +## Summary + +goto statement for jumps and making scripting a bit easier. + +## Motivation + +### Problems + +#### Breaking in nested loops +Let's say for example: +```lua +while true do + for i=1, 100 do + if (i == 10) then + break -- This will only break the current for loop + end + end +end +``` + +In Luau there isn't a easy/simple way to break nested loops(without a variable)
+ +Current solution we have: +```lua +local RunLoop = true +while RunLoop do + for i=1, 100 do + if (i == 10) then + RunLoop = false + break + end + end +end +``` +The problems that this solution has are: +Messy: In a bigger scale this will get messy and confusing.
+Uneeded checks: Why to do those checks when we could just jump out directly? + + + + +### Possible solution + +A goto statement would make Luau more easy to script and cleaner, if used properly.
+```lua +while true do + for i=1, 100 do + if (i == 10) then + goto LoopEnd + end + end +end +LoopEnd: -- Could follow Lua syntax(:LABEL:) +``` + + +## Design + + + +`:` Sets a label
+Notes:
+A label can't be used in another scope +```lua +function A() + FunctionStart: + + do + if Y == Y then + goto FunctionStart -- However, this would work + end + end + + if X == X then + goto FunctionStart -- This would act the same as a while loop. + end +end + +function B() + goto FunctionStart -- Undefined label compile error +end +``` + +`goto ` Jumps to a set label + + +`goto ` Jumps to X line +Notes:
+If the number is a constant number, the compiler should optmize it
+Otherwise if the number is a variable, any registers needed should be saved before the jump
+If the X line is in another scope a error should be thrown
+ + +## Drawbacks + +Cross thread jumping could be a problem(but it would be very interesting if implemented and used correctly).
+Lambdas/Functions that are called could bypass the scope boundary without runtime checks.
+ + +## Alternatives + +A break for nested loops +```lua +while true do + for i=1, 100 do + if (i == 10) then + :break -- The ':' means "break depth" + end + end +end +``` \ No newline at end of file