Advertisement
Guest User

Lexer

a guest
Sep 15th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     const makeToken = function (type, value) {
  2.       return {
  3.         type,
  4.         value
  5.       };
  6.     }
  7.  
  8.     const EOF = makeToken("(end)", 0);
  9.  
  10.     const operators = ["+", "-", "/", "*", "^", "(", ")", "="].reduce((aggr, next, index) => {
  11.       aggr[next] = makeToken("(operator)", next);
  12.       return aggr;
  13.     }, {});
  14.  
  15.  
  16.     const lexer = module.exports = function (str) {
  17.       const tokens = [];
  18.  
  19.       let from = 0, to = 0, char, type;
  20.  
  21.       while (from < str.length) {
  22.         char = str.charAt(from);
  23.  
  24.         if (char === "\u0020" || char === "\r" ||
  25.           char === "\n" || char === "\t") {
  26.           from++;
  27.           continue;
  28.         }
  29.  
  30.         /* check if number*/
  31.         if (char >= "0" && char <= "9") {
  32.           type = "(number)";
  33.           to = from + 1;
  34.           let numStr = char;
  35.           let next = str.charAt(to);
  36.  
  37.           while (to < str.length && next >= "0" && next <= "9") {
  38.             numStr += next;
  39.             to++;
  40.             next = str.charAt(to);
  41.           }
  42.  
  43.           if (next === ".") {
  44.             numStr += next;
  45.             to += 1;
  46.             next = str.charAt(to);
  47.  
  48.             while (to < str.length && next >= "0" && next <= "9") {
  49.               numStr += next;
  50.               to++;
  51.               next = str.charAt(to);
  52.             }
  53.           }
  54.  
  55.           if (next === "E" || next === "e") {
  56.             numStr += next;
  57.             to += 1;
  58.             next = str.charAt(to);
  59.  
  60.             if (next === "+" || next === "-") {
  61.               numStr += next;
  62.               to += 1;
  63.               next = str.charAt(to);
  64.             }
  65.             while (to < str.length && next >= "0" && next <= "9") {
  66.               numStr += next;
  67.               to++;
  68.               next = str.charAt(to);
  69.             }
  70.           }
  71.  
  72.           tokens.push(makeToken(type, parseFloat(numStr)));
  73.           from = to;
  74.           continue;
  75.         }
  76.  
  77.         //check if identifier
  78.         if ((char >= "A" && char <= "Z") || (char >= "a" && char <= "z")) {
  79.           type = "(id)";
  80.           to = from + 1;
  81.           char = str.charAt(to);
  82.  
  83.           while ((char >= "A" && char <= "Z") || (char >= "a" && char <= "z")) {
  84.             char = str.charAt(to);
  85.             to += 1;
  86.           }
  87.  
  88.           tokens.push(makeToken(type, str.slice(from, to)));
  89.           from = to;
  90.           continue;
  91.         }
  92.  
  93.         //check if operator
  94.         if (operators[char]) {
  95.           tokens.push(operators[char]);
  96.         }
  97.         else {
  98.           tokens.push(makeToken("(unknown)", char));
  99.         }
  100.  
  101.         from += 1;
  102.       }
  103.  
  104.       tokens.push(EOF);
  105.  
  106.       return tokens;
  107.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement