Giter Site home page Giter Site logo

clj-compiler-interpreter's Introduction

clj-compiler-interpreter

Compiler & Interpreter for a few simple languages. Written in Clojure for a Languages and Compilers course.

Usage

$ lein run

Authors

Kristoffer Ek

Vicent Dolz Martinez (Vicentico22)

Documenation

File structure

+-- src/
| +-- project/
| | +-- addMulCompiler.clj
| | +-- addMulInterpreter.clj
| | +-- addSubCompiler.clj
| | +-- addSubInterpreter.clj
| | +-- arithInterpreter.clj
| | +-- constCompiler.clj
| | +-- constInterpreter.clj
| | +-- core.clj
| | +-- lang0Compiler.clj
| | +-- lang0Interpreter.clj
| | +-- lang1Compiler.clj
| | +-- lang1Interpreter.clj
+-- doc/
+-- project.clj

Languages

  • ConstMod
  • AddSubMod
  • AddMultMod
  • ArithMod
  • ArithExpMod
  • Lang0Mod
  • Lang1Mod

Int -> Long

In order for us to handle big numbers we converted both the interpreter and the compiler to work with the primitive type Long instead of the initial type int. This meant we now parse all numbers in the AST as Longs, changing the method signature of the generated bytecode as well as the operation bytecodes (Opcodes/IADD -> Opcodes/LADD). Another thing we had to modify due to this was the handling of arguments since they were coded as int's and not as long's. Int's only take up one block in memory and long take up 2 blocks. We solved this by adding another increment to where the variable indexes (in local memory) are calculated.

Modulo

The methods for handling the modulo operation is located in constCompiler.clj as well as in constInterpreter.clj. In our language, the modulo operand is represented with a "%". Since the the language should be able to handle very big numbers(more than what long can represent) we choose to create a function called mod-exp to convert the numbers as biginteger and perform modulo on each number separately before we parse the full expresison.

Interpreter mod-parser, mod-interpreter, and mod-eval are the three methods responsible for interpreting the "%". The parser will transform it to the keyword :mod and it then evaluated by using clojure's mod function.

Compiler We use the Opcodes/LREM to perform the remainder, which it works like modulo for positive numbers. For the negatives we create a function called rem-sign which transform a negative result to the correct positive one.

Exponentials

We use a java method called modPow to calculate modulo of big numbers which we use before passing the expression to the parser. In the case of variable name management in the exponential expressions, we didn't succeed to implement it. One possible way to solve this is defining a function in bytecode for exponential calculus.

clj-compiler-interpreter's People

Contributors

kristofferek avatar vicentdm avatar

Watchers

 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.