# See https://www.reddit.com/r/ProgrammingLanguages/comments/d076go/how_to_write_a_turingcomplete_programming/ezaqcs2
grammar Shane's-Turing-complete-language {
rule TOP { <.ws> <stmts> }
rule stmts { <expr>+ }
rule expr { <ifExpr> | <whileExpr> | <opExpr> | <storeSet> | <storeGet> | <value> }
rule ifExpr { 'if' <stmts> 'then' <stmts> [ 'else' <stmts> ]? 'end' }
rule whileExpr { 'while' <stmts> 'do' <stmts> 'end' }
rule storeSet { [ '[' <stmts> ']' | <var> ] '=' <expr> }
rule storeGet { '[' <stmts> ']' | <var> }
my @keywords = < if then else while do end >;
token var { :i <!before @keywords> <[_a..z]> \w* }
my @ops = << <= >= == - + * / < > >>;
token op { @ops }
rule opExpr { <op> <expr> <expr> }
token value { '-'? <[0..9]>+ }
}
given Shane's-Turing-complete-language.parse: slurp 'sieve-code' { .Str.say for .<stmts><expr> }
maxPrime = 30
[0] = 0
i = 1
while < i maxPrime do
[i] = i
i = + i 1
end
i = 2
while < i maxPrime do
if [i] then
j = * i 2
while < j maxPrime do
[j] = 0
j = + j i
end
end
i = + i 1
end