Interpret pseudocode
Wikipedia says pseudocode
is intended for human reading rather than machine reading.
and
A program in pseudocode is not an executable program.
I don't care.
Make a pseudocode interpreter that can run pseudocode that fits the rules described below*. This is based on the IB pseudocode guide, but it is simplified quite a bit to make it fit for the challenge.
Pseudocode specifications
This is a simplified pseudocode to make the challenge less tedious. The pseudocode language has no strings, no arrays, no classes, no methods, and no variables other than integers.
Basic syntax
Comments that start at //
and end at a newline (like java one-line comments). //
is not necessarily followed by a space, and the comment may be empty. Example:
A = 2 + 3 // I can't write five because my keyboard is broken
Statements are separated by newlines. Lines may be empty (without statements). The exact number of spaces doesn't matter, and spaces are not required. The language is case sensitive.
Variables
All variables are global, and can be accessed anywhere. They do not need to be declared. To keep things simple, all variables can be assumed to be integers. All variable names are UPPERCASE, and consist only of letters. Your program should at least handle integers from -256 to 256. A wider range is not a requirement.
Variables are assigned values using this syntax:
VARIABLE = Expression
Where VARIABLE can be any uppercase name and expression can be any integer expression, as discussed below.
Examples:
A = 5
B = A + 3
NUMBER = A * B
Expressions
An expression can be:
- An integer, like
42
- A variable, like
NUMBER
- A binary operation on two other expressions, like NUMBER + 5. There are only four operations:
+
, -
, *
, /
. Division rounds integers down.
Expressions can be surrounded by parentheses to indicate that they need to be evaluated first. To keep things simple, all expressions are evaluated from left to right no matter what the operations are (unless there are parentheses that specify otherwise), so
A = 2 - RM * 9 + 3 / NUMBER
B = 1 + 2 * (3 - 4) / 6
is equivalent to
A = (((2 - RM) * 9) + 3) / NUMBER
B = ((1 + 2) * (3 - 4)) / 6
Boolean expressions
Boolean expressions can compare two expressions using ==
(equality), !=
(not equal to), <
(less than), and >
(greater than). They are only used for control flow, as discussed below (there are no boolean variables).
Control flow
There are four types of control flow. They can be infinitely nested in all combinations.
If
if (booleanExpression) then
// statements (discussed below)
endif
If-else
if (booleanExpression) then
// statements (discussed below)
else
// other statements
endif
Loop while
loop while (booleanExpression)
// do stuff
endloop
Where booleanExpression
s are boolean expressions. The if
s work the same as in normal programming languages. The while
loop is a simple while loop.
The booleanExpressions will always be surrounded by ()
. The pseudocode is very flexible with spaces, and any number of spaces is valid.
Loop for
loop VARIABLE from Expression1 to Expression2
// things to do over and over again
end loop
Where Expression1
and Expression2
are expressions that are evaluated before the loop begins and their values are stored until the loop finishes. The content of the loop is executed for every integer from the result of Expression1
to that of Expression1
, inclusive. At every iteration, the index variable (VARIABLE
in this case) is updated.
Example:
loop I from 3 to 5
output(I)
endloop
Outputs:
3
4
5
Statements
Output
output(Expression)
outputs the evaluated expression. It's like println
in programming languages. So:
output(1+1)
prints 2
, followed by a newline.
output()
with no arguments should print a newline.
Other statements
If the interpreter encounters any other statement that looks like a method call with no arguments, it should pretend it's executing it. For example,
lightsoff()
gohome()
should print (together with a newline):
executing lightsoff
executing gohome
In other words, executing [Method name]
should be printed. All statements will be lowercase and will consist entirely of letters.
Keywords cannot be statements. You do not have to deal with the following (it will not appear in the pseudocode):
- if()
- endif()
- loop()
- while()
- etc.
However, statements that start with keywords are valid. For example, loophole() should print executing loophole
, even though loop()
itself is not valid.
Challenge rules
- Your program should take a string as input. It can also take something equivalent, like an array of characters. But you can't take an array of strings; your program must itself separate the lines and tokens. You can also take a file as input.
- Your program should print the output of the pseudocode in any reasonable form.
- No standard loopholes.
- There are no restrictions on what your program should do when given invalid pseudocode.
- This is code golf. The shortest code in bytes wins.
Example output
1
A = 3
output(A) // prints 3
B = 4 + A * 2
output(B)
helloworld()
output(A + B + 1 * 3)
Should give:
3
14
executing helloworld
54
2
loop NUM from 2 to 20 // cycle through possible prime numbers
COUNT = 0
loop DIV from 2 to NUM // cycle through possible divisors
if(NUM/DIV*DIV == NUM) then // if the number is exactly divisible
COUNT = COUNT + 1
endif
endloop
if (COUNT == 2) then // if number is prime
output(NUM)
endif
endloop
Should give:
2
3
5
7
11
13
17
19
3
Tricky cases that your interpreter should handle:
// empty comment:
//
// empty line:
// more comment testing // ///
////
if (3<4) then
endoftheworld() // a statement
ifff()
endifnot()
// endif in a comment doesn't count
endif
// loops can be empty:
loop I from 0 to 10
endloop
output(I) // variables are global
if(1<2)
if(3<4) // nesting is ok
ok()
endif
endif
// spacing doesn't matter:
output (2+ 8 - 1 )
loop while(2<1)
neverhappened()
endloop
Should output:
executing endoftheworld
executing ifff
executing endifnot
10
excecuting ok
9
*Technically, once pseudocode follows rules as strict as those described here, it is arguably not pseudocode anymore. Wikipedia says it's called skeleton code.
Any suggestions?
I double-checked all the specifications, but if anything seems reasonably unclear, please let me know.