Code Monkey home page Code Monkey logo

abstract-vm's Introduction

Abstract-VM

MacOS - Build

This program provide a stack to compute simple arithmetic expressions.

Operations:

  • push v: Pushes the value v at the top of the stack. The value v must have one of the following form:
    • int8(n) : Creates an 8-bit integer with value n.
    • int16(n) : Creates a 16-bit integer with value n.
    • int32(n) : Creates a 32-bit integer with value n.
    • float(z) : Creates a float with value z.
    • double(z) : Creates a double with value z.

  • pop: Unstacks the value from the top of the stack.

  • dump: Displays each value of the stack.

  • assert v: Asserts that the value at the top of the stack is equal to the one passed as parameter for this instruction. If it is not the case, the program execution stop with an error.

  • add: Unstacks the first two values on the stack, adds them together and stacks the result.

  • sub: Unstacks the first two values on the stack, subtracts them, then stacks the result.

  • mul: Unstacks the first two values on the stack, multiplies them, then stacks the result.

  • div: Unstacks the first two values on the stack, divides them, then stacks the result.

  • mod: Unstacks the first two values on the stack, calculates the modulus, then stacks the result.

  • print: Asserts that the value at the top of the stack is an 8-bit integer, then interprets it as an ASCII value and displays the corresponding character on the standard output.

  • exit: Terminate the execution of the current program. If this instruction does not appears while all others instruction has been processed, the execution must stop with an error.

  • ;;: pass that command to finish writing to standart output

Grammar

S := INSTR [SEP INSTR]* #

INSTR :=
  push VALUE
  | pop
  | dump
  | assert VALUE
  | add
  | sub
  | mul
  | div
  | mod
  | print
  | exit

VALUE :=
  int8(N)
  | int16(N)
  | int32(N)
  | float(Z)
  | double(Z)

N := [-]?[0..9]+

Z := [-]?[0..9]+.[0..9]+

SEP := '\n'+

Build and run on MacOS:

cmake .
make

./abstract_vm example.txt
or
./abstract_vm
push int32(124)
push int8(12)
sub
assert int32(112)
exit
;;

(You can pass file with operations into first argument or you can write operations into standart output)

example.txt:

push int32(124)
push int8(12)
sub
assert int32(112)
exit

Examples:

./abstract_vm
push int8(33)
push int8(112)
;comment
push int8(111)
push int8(108)
push int8(112)
print
pop
print
pop
print
pop
print
pop
print
pop
exit
;;
p
l
o
p
!
example.txt:

push int32(124)
push int8(12)
push int16(5324)
push float (412.5213)
push float(.9)
push double(512.)
dump
add
add
mul
add
dump
exit

./abstract_vm example.txt
512.00
0.90
412.52
5324
12
124
-4926831.01

abstract-vm's People

Contributors

dolovnyak avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.