PLC Laboratory 10

From Marek Běhálek Wiki
Jump to navigation Jump to search

Virtual Machine Interpreting Arithmetic Expressions

In our previous laboratory ( Laboratory 9), we have generated a stack-based code defining the computation for arithmetic expressions.

The target stack-based code consist of following instructions:

  • ADD, SUB, MUL, DIV, MOD - it takes two values from the stack, computes the appropriate values, and stores the result on stack. If necessary, integers are automatically casted into floating-point numbers. MOD works only for integers.
  • PUSH (I|F) n - it stores the value n on stack. It will be either int or float.
  • LOAD a - it loads the value of variable a to the stack.
  • SAVE (I|F) a - it takes the value from the stack and stores in into variable a of given type.
  • PRINT (I|F) - it takes a value from stack and based on its type, prints it on output.

Input specification

Input is a correct program composed from instructions described above.

Output specification

Interpret the instructions from the input and write the result on the standard output.

Example

  • Input
PUSH I 15
SAVE b
LOAD b
SAVE a
LOAD a
PRINT I
LOAD a
LOAD b
ADD
PRINT I
LOAD a
LOAD b
MOD
PRINT I
LOAD a
LOAD b
ADD
SAVE c
LOAD c
PRINT F
LOAD c
LOAD a
ADD
PRINT F
LOAD a
SAVE c
LOAD c
PRINT F
LOAD c
PUSH F 1.1
ADD
PRINT F
PUSH I 10
LOAD a
MOD
PRINT I
  • Output

Solution