This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tokenizer.prototype.stripComments = function(text) { | |
return text.replace(/\/\/[^\n]+/g, ""); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tokenizer.prototype.trimWhitespace = function(text) { | |
cleanedText = text.replace(/\n+/g, "\n"); | |
cleanedText = cleanedText.replace(/[ ]+/g, " "); | |
cleanedText = cleanedText.trim(); | |
return cleanedText; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tokenizer.prototype.resetNewlines = function(text) { | |
nlText = text.replace(/\n/g, "").replace(/\;/g, ";\n"); | |
nlText = nlText.replace(/\}/g, "}\n").replace(/\{/g, "\n{\n"); | |
nlText = nlText.replace(/\n[ ]+/g, "\n"); | |
nlText = nlText.replace(/\n$/, ""); | |
return nlText; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tokenizer.prototype.padTokens = function(text) { | |
tokenized = text.replace(/ ?([,\(\)\/\[\]\+\*\-]+) ?/g, " $1 "); | |
tokenized = tokenized.replace(/ ?([,]) ?/g, " $1 "); | |
tokenized = tokenized.replace(/- >/g, "->") | |
tokenized = tokenized.replace(/ ?(==) ?/g, " $1 "); | |
tokenized = tokenized.replace(/;/g, " ;"); | |
tokenized = tokenized.replace(/\s{2,}/g, " "); | |
return tokenized; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tokenizer.prototype.getTokenLines = function(text) { | |
lines = text.split("\n"); | |
tokenLines = []; | |
for (var lineId = 0; lineId < lines.length; lineId++) { | |
line = lines[lineId]; | |
tokens = line.split(" "); | |
tokenLines.push(tokens); | |
} | |
return tokenLines; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tokenizer.prototype.run = function(text) { | |
formattedText = this.prepareCodeForTokenization(text); | |
tokens = this.getTokenLines(formattedText); | |
return tokens; | |
} | |
Tokenizer.prototype.prepareCodeForTokenization = function(text) { | |
text = this.stripComments(text); | |
text = this.trimWhitespace(text); | |
text = this.resetNewlines(text); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function LexicalAnalyzer() { | |
this.tokenTypes = { | |
"include": /^include$/, | |
"string": /^\"[^\"]+\"$/, | |
"unaryop": /^(sin|cos|tan|exp|ln|sqrt)$/, | |
"exp": /^(\+|\-|\*|\/|\^)$/, | |
"comma": /^\,$/, | |
"openparen": /^\($/, | |
"closeparen": /^\)$/, | |
"openbracket": /^\[$/, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LexicalAnalyzer.prototype.getClassifiedTokenFromLines = function(tokenLines) { | |
classifiedTokens = []; | |
errors = []; | |
// loop through each line of code | |
for (var lineId = 0; lineId < tokenLines.length; lineId++) { | |
line = tokenLines[lineId]; | |
// loop through each token in each line | |
for (var tokenId = 0; tokenId < line.length; tokenId++) { | |
token = line[tokenId]; | |
classifiedToken = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LexicalAnalyzer.prototype.run = function(tokenLines) { | |
lexOutput = this.getClassifiedTokenFromLines(tokenLines); | |
return lexOutput; | |
}; |
OlderNewer