Giter Site home page Giter Site logo

dzui42unit / abstractvm Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 1.4 MB

AbstractVM is a machine that uses a stack to compute simple arithmetic expressions. These arithmetic expressions are provided to the machine as basic assembly programs.

C++ 98.01% Makefile 1.99%
avm lexer parser factory-method exception-handling cpp11

abstractvm's Introduction

AbstractVM

In order to run it use make

./avm (with no params to get theinput from standart input ";;" to stop input)

./avm some_file[0] ... some_file[n];

As for any assembly language, the language of AbstractVM is composed of a series of instructions, with one instruction per line. However, AbstractVM’s assembly language has a limited type system, which is a major difference from other real world assembly languages.

Description

• Comments: Comments start with a ’;’ and finish with a newline. A comment can be either at the start of a line, or after an instruction.

• 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. If the stack is empty, the program execution must stop with an error.

• dump: Displays each value of the stack, from the most recent one to the oldest one WITHOUT CHANGING the stack. Each value is separated from the next one by a newline.

• 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 must stop with an error. The value v has the same form that those passed as parameters to the instruction push.

• add: Unstacks the first two values on the stack, adds them together and stacks the result. If the number of values on the stack is strictly inferior to 2, the program execution must stop with an error.

• sub: Unstacks the first two values on the stack, subtracts them, then stacks the result. If the number of values on the stack is strictly inferior to 2, the program execution must stop with an error.

• mul: Unstacks the first two values on the stack, multiplies them, then stacks the result. If the number of values on the stack is strictly inferior to 2, the program execution must stop with an error.

• div: Unstacks the first two values on the stack, divides them, then stacks the result. If the number of values on the stack is strictly inferior to 2, the program execution must stop with an error. Moreover, if the divisor is equal to 0, the program execution must stop with an error too. Chatting about floating point values is relevant a this point. If you don’t understand why, some will understand. The linked question is an open one, there’s no definitive answer.

• mod: Unstacks the first two values on the stack, calculates the modulus, then stacks the result. If the number of values on the stack is strictly inferior to 2, the program execution must stop with an error. Moreover, if the divisor is equal to 0, the program execution must stop with an error too. Same note as above about fp values.

• print: Asserts that the value at the top of the stack is an 8-bit integer. (If not, see the instruction assert), 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.

Grammar

screen shot 2017-12-06 at 7 49 41 pm

Errors

When one of the following cases is encountered, AbstractVM must raise an exception and stop the execution of the program cleanly.

  • The assembly program includes one or several lexical errors or syntactic errors.
  • An instruction is unknown
  • Overflow on a value or the result of an operation
  • Underflow on a value or the result of an operation
  • Instruction pop on an empty stack
  • Division/modulo by 0
  • The program doesn’t have an exit instruction
  • An assert instruction is not true
  • The stack is composed of strictly less that two values when an arithmetic instruction is executed.

Execution

screen shot 2017-12-06 at 7 52 10 pm

IOoerand interface

AbstractVM uses 5 operand classes that you must declare and define:

  • Int8 : Representation of a signed integer coded on 8bits.
  • Int16 : Representation of a signed integer coded on 16bits.
  • Int32 : Representation of a signed integer coded on 32bits.
  • Float : Representation of a float.
  • Double : Representation of a double.

Precision

When an operation happens between two operands of the same type, there is no problem. However, what about when the types are different ? The usual method is to order types using their precision. For this machine you should use the following order: Int8 < Int16 < Int32 < Float < Double. This order may be represented as an enum, as enum values evaluate to integers.

The pure method getPrecision from the interface IOperand allows to get the precision of an operand. When an operation uses two operands from two different types, the comparison of theirs precisions allows to figure out the result type of the operation.

Stack

AbstractVM is a stack based virtual machine. Whereas the stack is an actual stack or another container that behaves like a stack is up to you. Whatever the container, it MUST only contain pointers to the abstract type IOperand.

abstractvm's People

Contributors

dzui42unit 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.