Git Product home page Git Product logo

jackcompiler's Introduction

JackCompiler

JackCompiler extends JackAnalyzer (syntax analyzer) to form the frontend compiler to the VMtranslator backend compiler. The Jack compilation process consists of 2 stages:

  1. programs written in the high level Jack language are first compiled down to a virtual machine (VM) language (similar to Java bytecode)
  2. the VM compiler translates it down to the Hack assembly language

API

JackCompiler

Coordinates compiling a single file or directory. Functionality is almost identical to the JackAnalyzer module. A second command line arg can be passed which adds a postfix to the filename (useful for testing).

SymbolTable

Updates the symbol table whenever a class/subroutine variable declaration is found. This allows the compiler to map the variable names to the low level virtual memory segments that the Hack assembly language understands.

Method Arguments Return function
constructor init symbol table
startSubroutine resets the subroutine level symbol table
define name:string,
type:string,
kind: SymbolTableKinds {NONE, STATIC, FIELD, VAR, ARG}
Define a new symbol
varCount kind: SymbolTableKinds int Returns the number of variables in the symbol table for that kind
kindOf name: string SymbolTableKinds Returns the kind for a given variable name
typeOf name: string string Returns the type for a given variable name
indexOf name: string int Returns the index of the variable. This is used to map to a RAM address
exists name: string boolean Returns true if the variable name exists in the table. Else false

VMWriter

Writes the VM commands to file

Method Arguments function
constructor outputFilename Creates a file outputFilename.vm for writing
writePush segment:Segments
{CONST, ARG, LOCAL, STATIC, THIS, THAT, POINTER, TEMP},
index:int
Write a push command to the virtual memory segment
writePop segment:Segments
{ARG, LOCAL, STATIC, THIS, THAT, POINTER, TEMP},
index:int
Write a pop command to the memory segment
writeArithmetic command:Commands
{ADD, SUB, NEG, AND, OR, NOT, EQ, GT, LT}
Write an logical or arithmetic command
writeLabel label:string Write a label command
writeGoto label:string Write a goto command
writeIf label:string Write an if-goto command
writeCall name:string, nArgs:int Write a function call command
writeFunction name:string, nLocals:int Write a function declaration command
writeReturn Write a function return command

CompilationEngine

Compiles VM code. API is identical to the CompilationEngine module found in JackAnalyzer.

Install & Usage

npm install
node JackCompiler.js <src file/dir> [options]

Use --help flag for more options.

Testing

Each of the folders contains Jack programs used to test the compiler. All except ExpressionTest were provided by the nand2tetris course creators.

A bash script regressTest.sh compiles the Jack programs from custom test dirs I've written using this compiler as well as the course supplied JackCompiler and tests that they match. N.B. As development of the compiler continues, I've found the VM code my compiler generates deviates from the course provided one so its use is quite limited.

jackcompiler's People

Contributors

timiscoding avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

analugo uri-nyx

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.