Skip to content
/ Jaskell Public

A compiler for the Jack Programming language, written in Haskell.

Notifications You must be signed in to change notification settings

dmjio/Jaskell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 

Repository files navigation

Jaskell

A lexical analyzer of the Jack language

This project will read in a .jack file, analyze its structure, and print a hierarchy tree along with a list of tokens in XML format. VM Command generation now included! All specs are based on the nand2tetris Hack platform.

Input

/**
 * The Main class initializes a new Snake game and starts it.
 */

class Main {
    /** Initializes a new game and starts it. */    
    function void main() {
        var SnakeGame game;
        let game = SnakeGame.new();
        do game.run();
        do game.dispose();
        return;
    }
}

VM Command Output

function Main.main 1
call SnakeGame.new 0
pop local 0
push local 0
call SnakeGame.run 1
pop temp 0
push local 0
call SnakeGame.dispose 1
pop temp 0
push constant 0
return

XML Token Output

<tokens>
<keyword> class </keyword>
<identifier> Main </identifier>
<symbol> { </symbol>
<keyword> function </keyword>
<keyword> void </keyword>
<identifier> main </identifier>
<symbol> ( </symbol>
<symbol> ) </symbol>
<symbol> { </symbol>
<keyword> var </keyword>
<identifier> SnakeGame </identifier>
<identifier> game </identifier>
<symbol> ; </symbol>
<keyword> let </keyword>
<identifier> game </identifier>
<symbol> = </symbol>
<identifier> SnakeGame </identifier>
<symbol> . </symbol>
<identifier> new </identifier>
<symbol> ( </symbol>
<symbol> ) </symbol>
<symbol> ; </symbol>
<keyword> do </keyword>
<identifier> game </identifier>
<symbol> . </symbol>
<identifier> run </identifier>
<symbol> ( </symbol>
<symbol> ) </symbol>
<symbol> ; </symbol>
<keyword> do </keyword>
<identifier> game </identifier>
<symbol> . </symbol>
<identifier> dispose </identifier>
<symbol> ( </symbol>
<symbol> ) </symbol>
<symbol> ; </symbol>
<keyword> return </keyword>
<symbol> ; </symbol>
<symbol> } </symbol>
<symbol> } </symbol>
</tokens>

XML Class Hierarchy Output

<class>
  <keyword> class </keyword>
  <identifier> Main </identifier>
  <symbol> { </symbol>
  <subroutineDec>
    <keyword> function </keyword>
    <keyword> void </keyword>
    <identifier> main </identifier>
    <symbol> ( </symbol>
    <parameterList>
    </parameterList>
    <symbol> ) </symbol>
    <subroutineBody>
      <symbol> { </symbol>
      <varDec>
        <keyword> var </keyword>
        <identifier> SnakeGame </identifier>
        <identifier> game </identifier>
        <symbol> ; </symbol>
      </varDec>
      <statements>
        <letStatement>
          <keyword> let </keyword>
          <identifier> game </identifier>
          <symbol> = </symbol>
          <expression>
            <term>
              <identifier> SnakeGame </identifier>
              <symbol> . </symbol>
              <identifier> new </identifier>
              <symbol> ( </symbol>
              <expressionList>
              </expressionList>
              <symbol> ) </symbol>
            </term>
          </expression>
          <symbol> ; </symbol>
        </letStatement>
        <doStatement>
          <keyword> do </keyword>
          <identifier> game </identifier>
          <symbol> . </symbol>
          <identifier> run </identifier>
          <symbol> ( </symbol>
          <expressionList>
          </expressionList>
          <symbol> ) </symbol>
          <symbol> ; </symbol>
        </doStatement>
        <doStatement>
          <keyword> do </keyword>
          <identifier> game </identifier>
          <symbol> . </symbol>
          <identifier> dispose </identifier>
          <symbol> ( </symbol>
          <expressionList>
          </expressionList>
          <symbol> ) </symbol>
          <symbol> ; </symbol>
        </doStatement>
        <returnStatement>
          <keyword> return </keyword>
          <symbol> ; </symbol>
        </returnStatement>
      </statements>
      <symbol> } </symbol>
    </subroutineBody>
  </subroutineDec>
  <symbol> } </symbol>
</class>

Instructions

ghc Printer.hs -o printer
ghc VM.hs -o vm
./parser
Please enter the name of your jack file (i.e. Main)
Main
Completed Lexing, MainT.xml created...
Completed Parsing, Main.xml created...
./vm 
Please enter the name of your jack file (i.e. Main)
Main
Completed Parsing, Main.vm created...

About

A compiler for the Jack Programming language, written in Haskell.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published