Skip to content

Instantly share code, notes, and snippets.

@backupbrain
backupbrain / 0_reuse_code.js
Created November 15, 2016 00:29
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
Tokenizer.prototype.stripComments = function(text) {
return text.replace(/\/\/[^\n]+/g, "");
}
Tokenizer.prototype.trimWhitespace = function(text) {
cleanedText = text.replace(/\n+/g, "\n");
cleanedText = cleanedText.replace(/[ ]+/g, " ");
cleanedText = cleanedText.trim();
return cleanedText;
}
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;
}
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;
}
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;
}
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);
function LexicalAnalyzer() {
this.tokenTypes = {
"include": /^include$/,
"string": /^\"[^\"]+\"$/,
"unaryop": /^(sin|cos|tan|exp|ln|sqrt)$/,
"exp": /^(\+|\-|\*|\/|\^)$/,
"comma": /^\,$/,
"openparen": /^\($/,
"closeparen": /^\)$/,
"openbracket": /^\[$/,
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;
LexicalAnalyzer.prototype.run = function(tokenLines) {
lexOutput = this.getClassifiedTokenFromLines(tokenLines);
return lexOutput;
};