Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiecooro committed Oct 10, 2019
1 parent 1d4f84b commit 3646662
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 53 deletions.
80 changes: 39 additions & 41 deletions 11/CompilationEngine.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const JackTokenizer = require('./JackTokenizer');
const XMLWritter = require('./XMLWritter');
const VMWriter = require('./VMWriter');

const subroutineStarters = [
JackTokenizer.KEYWORDS.FUNCTION,
Expand Down Expand Up @@ -27,9 +28,10 @@ const keywordConstants = [
];

class CompilationEngine {
constructor(sourceFilePath, destWritter) {
constructor(sourceFilePath, destPath) {
this.tokenizer = new JackTokenizer(sourceFilePath);
this.xmlWritter_old = new XMLWritter(destWritter);
this.xmlWriter_old = new XMLWritter(destPath + '.xml');
this.vmWriter = new VMWriter(destPath);
}

_includes(expected, found) {
Expand Down Expand Up @@ -72,29 +74,25 @@ class CompilationEngine {
}

_writeCurrentToken() {
console.log('Writing');
const tokenType = this.tokenizer.token();
switch (tokenType) {
case JackTokenizer.TOKENTYPES.IDENTIFIER:
this.xmlWritter_old.writeTag('identifier', this.tokenizer.identifier());
this.xmlWriter_old.writeTag('identifier', this.tokenizer.identifier());
break;
case JackTokenizer.TOKENTYPES.KEYWORD:
this.xmlWritter_old.writeTag(
this.xmlWriter_old.writeTag(
'keyword',
this.tokenizer.keyword().toLowerCase()
);
break;
case JackTokenizer.TOKENTYPES.SYMBOL:
this.xmlWritter_old.writeTag('symbol', this.tokenizer.symbol());
this.xmlWriter_old.writeTag('symbol', this.tokenizer.symbol());
break;
case JackTokenizer.TOKENTYPES.INT_CONST:
this.xmlWritter_old.writeTag(
'integerConstant',
this.tokenizer.intVal()
);
this.xmlWriter_old.writeTag('integerConstant', this.tokenizer.intVal());
break;
case JackTokenizer.TOKENTYPES.STRING_CONST:
this.xmlWritter_old.writeTag(
this.xmlWriter_old.writeTag(
'stringConstant',
this.tokenizer.stringVal()
);
Expand Down Expand Up @@ -187,7 +185,7 @@ class CompilationEngine {
}

compileClass() {
this.xmlWritter_old.openTag('class');
this.xmlWriter_old.openTag('class');

this._compileKeyword(JackTokenizer.KEYWORDS.CLASS);

Expand All @@ -197,7 +195,7 @@ class CompilationEngine {
this._compileClassBody();
this._compileSymbol('}');

this.xmlWritter_old.closeTag('class');
this.xmlWriter_old.closeTag('class');
}

compileType() {
Expand Down Expand Up @@ -226,10 +224,10 @@ class CompilationEngine {
}

compileVarDec() {
this.xmlWritter_old.openTag('varDec');
this.xmlWriter_old.openTag('varDec');
this._compileKeyword(JackTokenizer.KEYWORDS.VAR);
this.compileRestVarDec();
this.xmlWritter_old.closeTag('varDec');
this.xmlWriter_old.closeTag('varDec');
}

compileRestVarDec() {
Expand All @@ -241,13 +239,13 @@ class CompilationEngine {
}

compileClassVarDec() {
this.xmlWritter_old.openTag('classVarDec');
this.xmlWriter_old.openTag('classVarDec');

this._compileKeyword(classVarDecStarters);

this.compileRestVarDec();

this.xmlWritter_old.closeTag('classVarDec');
this.xmlWriter_old.closeTag('classVarDec');
}

_compileTermIdentifier() {
Expand All @@ -268,7 +266,7 @@ class CompilationEngine {
}

compileTerm() {
this.xmlWritter_old.openTag('term');
this.xmlWriter_old.openTag('term');
if (this.tokenizer.token() === JackTokenizer.TOKENTYPES.INT_CONST) {
this._compileIntegerConstant();
} else if (
Expand All @@ -287,21 +285,21 @@ class CompilationEngine {
} else if (this.tokenizer.token() === JackTokenizer.TOKENTYPES.IDENTIFIER) {
this._compileTermIdentifier();
}
this.xmlWritter_old.closeTag('term');
this.xmlWriter_old.closeTag('term');
}

compileOperation() {
this._compileSymbol();
}

compileExpression() {
this.xmlWritter_old.openTag('expression');
this.xmlWriter_old.openTag('expression');
this.compileTerm();
while (this._includes(operations, this.tokenizer.symbol())) {
this.compileOperation();
this.compileTerm();
}
this.xmlWritter_old.closeTag('expression');
this.xmlWriter_old.closeTag('expression');
}

compileExpressionList() {
Expand All @@ -321,11 +319,11 @@ class CompilationEngine {
}

this._compileSymbol('(');
this.xmlWritter_old.openTag('expressionList');
this.xmlWriter_old.openTag('expressionList');
if (this.tokenizer.symbol() !== ')') {
this.compileExpressionList();
}
this.xmlWritter_old.closeTag('expressionList');
this.xmlWriter_old.closeTag('expressionList');
this._compileSymbol(')');
}

Expand All @@ -336,29 +334,29 @@ class CompilationEngine {
}

compileReturnStatement() {
this.xmlWritter_old.openTag('returnStatement');
this.xmlWriter_old.openTag('returnStatement');

this._compileKeyword(JackTokenizer.KEYWORDS.RETURN);
if (this.tokenizer.symbol() !== ';') {
this.compileExpression();
}
this._compileSymbol(';');

this.xmlWritter_old.closeTag('returnStatement');
this.xmlWriter_old.closeTag('returnStatement');
}

compileDoStatement() {
this.xmlWritter_old.openTag('doStatement');
this.xmlWriter_old.openTag('doStatement');

this._compileKeyword(JackTokenizer.KEYWORDS.DO);
this.compileSubroutineCall();
this._compileSymbol(';');

this.xmlWritter_old.closeTag('doStatement');
this.xmlWriter_old.closeTag('doStatement');
}

compileWhileStatement() {
this.xmlWritter_old.openTag('whileStatement');
this.xmlWriter_old.openTag('whileStatement');

this._compileKeyword(JackTokenizer.KEYWORDS.WHILE);

Expand All @@ -370,11 +368,11 @@ class CompilationEngine {

this._compileBody();

this.xmlWritter_old.closeTag('whileStatement');
this.xmlWriter_old.closeTag('whileStatement');
}

compileIfStatement() {
this.xmlWritter_old.openTag('ifStatement');
this.xmlWriter_old.openTag('ifStatement');
this._compileKeyword(JackTokenizer.KEYWORDS.IF);

this._compileSymbol('(');
Expand All @@ -389,11 +387,11 @@ class CompilationEngine {
this._compileKeyword(JackTokenizer.KEYWORDS.ELSE);
this._compileBody();
}
this.xmlWritter_old.closeTag('ifStatement');
this.xmlWriter_old.closeTag('ifStatement');
}

compileLetStatement() {
this.xmlWritter_old.openTag('letStatement');
this.xmlWriter_old.openTag('letStatement');
this._compileKeyword(JackTokenizer.KEYWORDS.LET);
this._compileIdentifier();

Expand All @@ -409,11 +407,11 @@ class CompilationEngine {

this._compileSymbol(';');

this.xmlWritter_old.closeTag('letStatement');
this.xmlWriter_old.closeTag('letStatement');
}

compileStatements() {
this.xmlWritter_old.openTag('statements');
this.xmlWriter_old.openTag('statements');
while (this.tokenizer.symbol() !== '}') {
this._expectKeyword(statementStarters);
switch (this.tokenizer.keyword()) {
Expand All @@ -434,7 +432,7 @@ class CompilationEngine {
break;
}
}
this.xmlWritter_old.closeTag('statements');
this.xmlWriter_old.closeTag('statements');
}

_compileParameter() {
Expand All @@ -443,7 +441,7 @@ class CompilationEngine {
}

compileParameterList() {
this.xmlWritter_old.openTag('parameterList');
this.xmlWriter_old.openTag('parameterList');
if (this.tokenizer.symbol() !== ')') {
this._compileParameter();

Expand All @@ -452,11 +450,11 @@ class CompilationEngine {
this._compileParameter();
}
}
this.xmlWritter_old.closeTag('parameterList');
this.xmlWriter_old.closeTag('parameterList');
}

compileSubroutineBody() {
this.xmlWritter_old.openTag('subroutineBody');
this.xmlWriter_old.openTag('subroutineBody');
this._compileSymbol('{');

this._expectKeyword([...statementStarters, JackTokenizer.KEYWORDS.VAR]);
Expand All @@ -468,11 +466,11 @@ class CompilationEngine {
this.compileStatements();

this._compileSymbol('}');
this.xmlWritter_old.closeTag('subroutineBody');
this.xmlWriter_old.closeTag('subroutineBody');
}

compileSubroutine() {
this.xmlWritter_old.openTag('subroutineDec');
this.xmlWriter_old.openTag('subroutineDec');
this._compileKeyword(subroutineStarters);

if (this.tokenizer.keyword() === JackTokenizer.KEYWORDS.VOID) {
Expand All @@ -489,7 +487,7 @@ class CompilationEngine {

this.compileSubroutineBody();

this.xmlWritter_old.closeTag('subroutineDec');
this.xmlWriter_old.closeTag('subroutineDec');
}
}

Expand Down
6 changes: 3 additions & 3 deletions 11/JackCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ if (isDir) {
}

function compile(path) {
const destinationFile = fs.createWriteStream(
path.replace(/\.jack/, '.debug.xml')
const compilation = new CompilationEngine(
path,
path.replace(/\.jack/, '.vm')
);
const compilation = new CompilationEngine(path, destinationFile);
compilation.compile();
// const xmlWritter = new XMLWritter(destinationFile);
// let i = 0;
Expand Down
1 change: 0 additions & 1 deletion 11/JackTokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ class JackTokenizer {
}
}
}
console.log('New token:', this.currentToken, this.currentTokenDescriptor);
}

token() {
Expand Down
11 changes: 8 additions & 3 deletions 11/VMWriter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const fs = require('fs');

const arithmeticCommands = [
'add',
'sub',
Expand All @@ -21,16 +23,19 @@ const segments = [
];

class VMWriter {
constructor(destWriter) {
this.writer = destWriter;
constructor(destPath) {
this.writer = fs.createWriteStream(destPath, { encoding: 'utf8' });
}

writePush(segment, index) {
if (!segments.includes(segment)) {
throw new Error('Unrecognized segment: ' + segment);
}
this.writer.write(`push ${segment} ${index}`);
}

writePop(segment, index) {
if (!segment.includes(segment)) {
if (!segments.includes(segment)) {
throw new Error('Unrecognized segment: ' + segment);
}
this.writer.write(`pop ${segment} ${index}`);
Expand Down
11 changes: 6 additions & 5 deletions 11/XMLWritter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
class XMLWritter {
constructor(writter) {
this.writter = writter;
constructor(destPath) {
this.writer = fs.createWriteStream(destPath, { encoding: 'utf8' });
this.openTags = [];
}

Expand All @@ -15,13 +16,13 @@ class XMLWritter {
writeTag(tagName, tagContents) {
tagName = this._sanitize(tagName);
tagContents = this._sanitize(tagContents);
this.writter.write(`<${tagName}> ${tagContents} </${tagName}>\n`);
this.writer.write(`<${tagName}> ${tagContents} </${tagName}>\n`);
}

openTag(tagName) {
this.openTags.push(tagName);
tagName = this._sanitize(tagName);
this.writter.write(`<${tagName}>\n`);
this.writer.write(`<${tagName}>\n`);
}

closeTag(tagName) {
Expand All @@ -30,7 +31,7 @@ class XMLWritter {
!found && tag === tagName ? tags : [tag, ...tags]
);
tagName = this._sanitize(tagName);
this.writter.write(`</${tagName}>\n`);
this.writer.write(`</${tagName}>\n`);
}

closeAllOpenTags() {
Expand Down

0 comments on commit 3646662

Please sign in to comment.