TodePond / DreamBerd

perfect programming language
https://dreamberd.computer
Other
11.23k stars 344 forks source link

Add `repeat` keyword #176

Open Braqzen opened 1 year ago

Braqzen commented 1 year ago

Context

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

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.

TodePond commented 1 year ago

sounds helpful!

ThePython10110 commented 2 months ago

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.