DreamBerd icon indicating copy to clipboard operation
DreamBerd copied to clipboard

Add `repeat` keyword

Open Braqzen opened this issue 2 years ago • 2 comments

Context

Programming takes effort and the language has a lot of syntax that is repeated for example:

  • const const const
  • multiple quotation marks
  • using next, previous, reverse etc. multiple times in a row

The language requires a new keyword, that is syntactic sugar, for unpacking or executing the same line multiple times in a row.

Specification

Abstract

Use the repeat keyword followed by the item that should be repeated followed by the number of times the item should be repeated (with some clever syntax this sentence could probably be factored out using the repeat keyword, we can implement that after the MVP)

repeat <item> <number of times>

Concrete example

repeat const 3 name = repeat " 2 Luke repeat " 2

The example above may be used to call the same function multiple times instead of using multiple lines, in a row, to call the same function.

This

// Could use "repeat" here but that is more characters so use the plain syntax
var var x = add(x, y)!
// Take the previous value of x and update it
x = add(x, y)!

becomes

repeat var var x = add(3, 2)! 2

Considerations

We do not allow loops but we need to consider the usage of the reverse keyword. We do not want to accidentally get stuck ping-ponging between multiple lines of code - effectively a loop.

Braqzen avatar Jun 21 '23 22:06 Braqzen

sounds helpful!

TodePond avatar Jun 22 '23 08:06 TodePond

The repeat keyword should be mandatory. Even if it's more characters. If you want to use anything multiple times in a row, you must use the repeat keyword. For example:

repeat const  2 name = "Lu" repeat ! 2
repeat const  2 name = "Luke"!
print(name)! repeat / 2 "Lu"

repeat const  2 name = "Lu or Luke (either is fine)" repeat ! 9
print(name)! repeat / 2 "Lu or Luke (either is fine)"

This applies to comments and exclamation points as well, so // becomes repeat / 2 and !!!!!!!!! becomes repeat ! 9.

Also notice the additional space in repeat const 2. Without it, it would expand to constconst, and ~~nobody wants that~~ although everyone wants that, it would confuse new programmers.

Obviously, this would add a step to the compilation, since the repeating !'s would have to be parsed before the main compilation.

ThePython10110 avatar Jul 06 '24 17:07 ThePython10110