Skip to main content
change "natural" to "positive"
Source Link
RubenVerg
  • 954
  • 4
  • 17

In this challenge, you implement an interpreter for a simple stack-based programming language. Your language must provide the following instructions:

  • push an arbitrary integera positive number
  • pop two numbers and push their sum
  • pop two numbers and push their difference (second number - first number)
  • pop a number and push it twice (dup)
  • pop two numbers and push them so that they are in opposite order (swap)
  • pop a number and discard it (drop)

You may assume instructions will never be called with less arguments on the stack than are needed.

The actual instructions can be chosen for each implementation, please specify how each instruction is called in the solution. Your program/function must output/return the stack after all instructions are performed sequentially. Output the stack in whatever format you prefer. The stack must be empty at the start of your program.

Examples

For these examples, the stack is represented bottom-to-top.

1 2 + -> [3]
1 drop -> []
10 2 - 3 + -> [11]
9 dup - -> [0]
1 2 3 -> [1 2 3]
1 dup 2 + -> [1 3]
3 2 5 swap -> [3 5 2]

In this challenge, you implement an interpreter for a simple stack-based programming language. Your language must provide the following instructions:

  • push an arbitrary integer
  • pop two numbers and push their sum
  • pop two numbers and push their difference (second number - first number)
  • pop a number and push it twice (dup)
  • pop two numbers and push them so that they are in opposite order (swap)
  • pop a number and discard it (drop)

You may assume instructions will never be called with less arguments on the stack than are needed.

The actual instructions can be chosen for each implementation, please specify how each instruction is called in the solution. Your program/function must output/return the stack after all instructions are performed sequentially. Output the stack in whatever format you prefer. The stack must be empty at the start of your program.

Examples

For these examples, the stack is represented bottom-to-top.

1 2 + -> [3]
1 drop -> []
10 2 - 3 + -> [11]
9 dup - -> [0]
1 2 3 -> [1 2 3]
1 dup 2 + -> [1 3]
3 2 5 swap -> [3 5 2]

In this challenge, you implement an interpreter for a simple stack-based programming language. Your language must provide the following instructions:

  • push a positive number
  • pop two numbers and push their sum
  • pop two numbers and push their difference (second number - first number)
  • pop a number and push it twice (dup)
  • pop two numbers and push them so that they are in opposite order (swap)
  • pop a number and discard it (drop)

You may assume instructions will never be called with less arguments on the stack than are needed.

The actual instructions can be chosen for each implementation, please specify how each instruction is called in the solution. Your program/function must output/return the stack after all instructions are performed sequentially. Output the stack in whatever format you prefer. The stack must be empty at the start of your program.

Examples

For these examples, the stack is represented bottom-to-top.

1 2 + -> [3]
1 drop -> []
10 2 - 3 + -> [11]
9 dup - -> [0]
1 2 3 -> [1 2 3]
1 dup 2 + -> [1 3]
3 2 5 swap -> [3 5 2]
Became Hot Network Question
Source Link
RubenVerg
  • 954
  • 4
  • 17

Implement a simple stack language

In this challenge, you implement an interpreter for a simple stack-based programming language. Your language must provide the following instructions:

  • push an arbitrary integer
  • pop two numbers and push their sum
  • pop two numbers and push their difference (second number - first number)
  • pop a number and push it twice (dup)
  • pop two numbers and push them so that they are in opposite order (swap)
  • pop a number and discard it (drop)

You may assume instructions will never be called with less arguments on the stack than are needed.

The actual instructions can be chosen for each implementation, please specify how each instruction is called in the solution. Your program/function must output/return the stack after all instructions are performed sequentially. Output the stack in whatever format you prefer. The stack must be empty at the start of your program.

Examples

For these examples, the stack is represented bottom-to-top.

1 2 + -> [3]
1 drop -> []
10 2 - 3 + -> [11]
9 dup - -> [0]
1 2 3 -> [1 2 3]
1 dup 2 + -> [1 3]
3 2 5 swap -> [3 5 2]