Last active
August 29, 2015 13:56
-
-
Save travisby/8901142 to your computer and use it in GitHub Desktop.
Basic idea for a Lexer flow.. not taking into account quotes or comments
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
// the first few dozen lines are more boilerplate that we will probably already have built-in with OCaml | |
// In addition, the Token prototype is really a do-nothing. A real token prototype/datatype will be much, much cooler | |
Object.prototype.keys = function () { | |
var keys = []; | |
for (var key in this) { | |
keys.push(key); | |
} | |
return keys; | |
}; | |
Array.prototype.filter = function (item) { | |
return this.reduce( | |
function (previousValue, currentValue) { | |
if (currentValue != item) return previousValue.concat(currentValue); | |
return previousValue; | |
}, | |
[] | |
); | |
}; | |
function replaceAll (str, charToReplace, charToBeReplacedWith) { | |
return str.split(charToReplace).join(charToBeReplacedWith); | |
} | |
var OPTIONAL_SPACE_TOKEN_WORDS_REPLACEMENTS = { | |
'(': ' ( ', | |
')': ' ) ', | |
'{': ' { ', | |
'}': ' } ', | |
'+': ' + ', | |
'=': ' = ', | |
// notice the hack here. Assignment will separate these two first. Then we can rebuild it with this | |
'= =': ' == ' | |
}; | |
function addSpaceAroundOptional(str, optional) { | |
return replaceAll(str, optional, OPTIONAL_SPACE_TOKEN_WORDS_REPLACEMENTS[optional]); | |
} | |
function addSpacesAroundAllOptionals (str) { | |
var partialAssignment = function (char) { | |
return addSpaceAroundOptional(str, char); | |
}; | |
return OPTIONAL_SPACE_TOKEN_WORDS_REPLACEMENTS.keys().reduce( | |
function (previousValue, currentValue) { | |
return addSpaceAroundOptional(previousValue, currentValue); | |
}, | |
str | |
); | |
} | |
function Token(tokenName) { | |
this.name = tokenName; | |
} | |
function tokenizeOne(tokenString) { | |
// TODO | |
return new Token(tokenString); | |
} | |
var program = "{print() while if int string boolean == != false true +}"; | |
console.log(addSpacesAroundAllOptionals(program).split(' ').filter('').map(tokenizeOne)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment