Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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 i is program[i], counting from 0. Every stack value is a string.
  • On stdin, the first line is the instruction count N and the next N lines 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.
InstructionWhat it does
push valuePushes everything after the first space
popRemoves the top value and discards it
printandpopRemoves the top value and prints it
concatPops a value a, then pops a value b, and pushes b + a
blank lineDoes nothing
jump indexSets pc to index (Part 2)
  • An instruction is an opcode (its name), followed for push by a space and a value.
  • A push value may contain spaces of its own. A bare push pushes 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

  1. Execution starts with an empty stack and pc = 0.
  2. Running an instruction updates the stack and moves pc. Every instruction in this part advances pc to the next line.
  3. The program ends when pc moves past the last instruction.
  4. No program pops from an empty stack.

Output: one line per printed value. An empty value prints as an empty line.

Limits

  • N is 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.

  1. jump index sets pc to index, a zero-based instruction index, instead of advancing it.
  2. Every other instruction still advances pc to the next line.

Guarantees

  • Jump targets are valid indices from 0 to N - 1.
  • The stack never underflows.
  • Every supplied program terminates.

Tests 7–9 cover this part.

Example
Input
9
push build
push step 2
push skipped
pop

concat
push done
printandpop
printandpop
Output
done
buildstep 2
Explanation

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.