Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const makeToken = function (type, value) {
- return {
- type,
- value
- };
- }
- const EOF = makeToken("(end)", 0);
- const operators = ["+", "-", "/", "*", "^", "(", ")", "="].reduce((aggr, next, index) => {
- aggr[next] = makeToken("(operator)", next);
- return aggr;
- }, {});
- const lexer = module.exports = function (str) {
- const tokens = [];
- let from = 0, to = 0, char, type;
- while (from < str.length) {
- char = str.charAt(from);
- if (char === "\u0020" || char === "\r" ||
- char === "\n" || char === "\t") {
- from++;
- continue;
- }
- /* check if number*/
- if (char >= "0" && char <= "9") {
- type = "(number)";
- to = from + 1;
- let numStr = char;
- let next = str.charAt(to);
- while (to < str.length && next >= "0" && next <= "9") {
- numStr += next;
- to++;
- next = str.charAt(to);
- }
- if (next === ".") {
- numStr += next;
- to += 1;
- next = str.charAt(to);
- while (to < str.length && next >= "0" && next <= "9") {
- numStr += next;
- to++;
- next = str.charAt(to);
- }
- }
- if (next === "E" || next === "e") {
- numStr += next;
- to += 1;
- next = str.charAt(to);
- if (next === "+" || next === "-") {
- numStr += next;
- to += 1;
- next = str.charAt(to);
- }
- while (to < str.length && next >= "0" && next <= "9") {
- numStr += next;
- to++;
- next = str.charAt(to);
- }
- }
- tokens.push(makeToken(type, parseFloat(numStr)));
- from = to;
- continue;
- }
- //check if identifier
- if ((char >= "A" && char <= "Z") || (char >= "a" && char <= "z")) {
- type = "(id)";
- to = from + 1;
- char = str.charAt(to);
- while ((char >= "A" && char <= "Z") || (char >= "a" && char <= "z")) {
- char = str.charAt(to);
- to += 1;
- }
- tokens.push(makeToken(type, str.slice(from, to)));
- from = to;
- continue;
- }
- //check if operator
- if (operators[char]) {
- tokens.push(operators[char]);
- }
- else {
- tokens.push(makeToken("(unknown)", char));
- }
- from += 1;
- }
- tokens.push(EOF);
- return tokens;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement