A Small Stack Machine
Prerequisites: Branches, Indirect Calls, and Inlining, Virtual Functions and Vtables
A build tool stores small automation routines as text instructions, one per line. Build the interpreter that runs them.
- Interpreter: a program that executes another program's instructions one at a time instead of compiling them to machine code.
- Stack machine: instructions take their operands from the top of a stack and push their results back onto it, instead of naming registers or variables. This interpreter is one.
- Program counter (
pc): the index of the next instruction to run.
What to implement
run_program(program)returns the values the program prints, in the order it prints them.- Instruction
iisprogram[i], counting from 0. Every stack value is a string. - On stdin, the first line is the instruction count
Nand the nextNlines are the instructions, read exactly as written. - The problem has two parts. Part 2 adds one instruction on top of Part 1, and one solution must pass all ten tests.
| Instruction | What it does |
|---|---|
push value | Pushes everything after the first space |
pop | Removes the top value and discards it |
printandpop | Removes the top value and prints it |
concat | Pops a value a, then pops a value b, and pushes b + a |
| blank line | Does nothing |
jump index | Sets pc to index (Part 2) |
- An instruction is an opcode (its name), followed for
pushby a space and a value. - A
pushvalue may contain spaces of its own. A barepushpushes the empty string. - In
concat's result, the value that was deeper in the stack comes first. - A blank line, empty or containing only spaces, is a valid instruction.
Part 1: Straight-line programs
- Execution starts with an empty stack and
pc= 0. - Running an instruction updates the stack and moves
pc. Every instruction in this part advancespcto the next line. - The program ends when
pcmoves past the last instruction. - No program pops from an empty stack.
Output: one line per printed value. An empty value prints as an empty line.
Limits
Nis at most 200,000.- The program text and the printed output each contain at most 2,000,000 ASCII characters.
- At most 2,000,000 instructions are executed.
Tests 1–6 and 10 cover this part.
Part 2: Add control flow
Routines now need to skip work and to run blocks out of written order.
jump indexsetspctoindex, a zero-based instruction index, instead of advancing it.- Every other instruction still advances
pcto the next line.
Guarantees
- Jump targets are valid indices from
0toN - 1. - The stack never underflows.
- Every supplied program terminates.
Tests 7–9 cover this part.
9 push build push step 2 push skipped pop concat push done printandpop printandpop
done buildstep 2
push step 2 pushes the whole remainder, step 2, including its space. pop discards skipped, the top value, and the blank line at index 4 does nothing.
concat pops step 2 as a and then build as b, and pushes b + a, which is buildstep 2.
printandpop always takes the top value, so done prints before buildstep 2.