[READ-ONLY] Mirror of https://github.com/probablykasper/limp. Programming language made in JS, for fun (unfinished) limp.kasper.space
language
0

Configure Feed

Select the types of activity you want to include in your feed.

Completely rewritten engine

Following this tutorial: http://lisperator.net/pltut/

KH (May 30, 2017, 12:14 AM +0200) cb377112 a958a62c

+59 -71
+57 -70
js/limp.js
··· 1 1 "use strict"; 2 - var limpErrors = { 3 - "u01": "Invalid JavaScript data type passed to limp(). Expected string." 4 - } 2 + // var limpErrors = { 3 + // "u01": "Invalid JavaScript data type passed to limp(). Expected string." 4 + // } 5 5 6 6 function limpLog(type, msg) { 7 7 var html = document.createElement("div"); ··· 19 19 } 20 20 } 21 21 22 - function wordType(word) { 23 - var type; 24 - if ( /(^\d*$|^\d*\.\d+$)/.test(word) ) { // NUMBER, digit OR optional digit . digit 25 - type = "number"; 26 - } else if (word == "true" || word == "false") { // BOOLEAN 27 - type = "boolean"; 28 - } else if ( /^".*"$/.test(word) ) { // STRING, starts & ends with " 29 - type = "string"; 30 - } else if ( /^[+\-*/%]$|^[+-]{2}$/.test(word) ) { // OPERATOR, + - * % ++ -- 31 - type = "operator"; 32 - } else if ( /^\$.*/.test(word) ) { 33 - type = "variable"; 34 - } else { // OTHER 35 - type = "instruction"; 22 + function limp(input) { 23 + var pos = 0, line = 1, col = 0; 24 + 25 + function currentChar(offset = 0) { 26 + return input.charAt(pos+offset); 36 27 } 37 - return type; 38 - } 39 28 40 - function limp(script) { 41 - var words = script.trim().match( /[^\s"]+|"[^"]*"/g ); // trim & split into array where there is whitespace, except in quotes 29 + function jumpChar(offset = 1) { 30 + pos = pos+offset; 31 + } 42 32 43 - var currentWord, currentType; 44 - for (var wordIndex = 0; wordIndex < words.length; wordIndex++) { // loop through words array 45 - currentWord = words[wordIndex]; 46 - currentType = wordType(currentWord); 47 - limpLog("inf", "["+currentType+"] "+currentWord); 48 - switch (currentType) { 49 - case "number": 50 - // do code... 51 - break; 52 - case "boolean": 53 - // do code... 54 - break; 55 - case "string": 56 - // do code... 57 - break; 58 - case "operator": 59 - switch (currentWord) { 60 - case "+": 61 - case "-": 62 - case "*": 63 - case "/": 64 - case "%": 65 - if ( wordType(words[wordIndex-1]) == "number" && wordType(words[wordIndex+1]) == "number" ) { 66 - limpLog("inf", eval(words[wordIndex-1]+currentWord+words[wordIndex+1])); 67 - } else { 68 - limpLog("err", "not number"); 69 - } 70 - break; 71 - case "++": 72 - limpLog("inf", words[wordIndex-1]+1); 73 - break; 74 - case "--": 75 - limpLog("inf", words[wordIndex-1]-1); 76 - break; 77 - } 78 - break; 79 - case "variable": 80 - // do code... 81 - break; 82 - case "instruction": 83 - // do code... 84 - break; 33 + function jumpLine() { 34 + var nextNewline = input.indexOf("\n", currentChar()); 35 + pos = nextNewline+1; 36 + } 37 + 38 + function isWhitespace(char) { 39 + return /^\s*$/.test(char); 40 + } 41 + 42 + function isDigit(char) { 43 + return /[0-9]/.test(char); 44 + } 45 + 46 + function readNumber() { 47 + var keepRunning = true, firstPeriod = true, number = ""; 48 + while (keepRunning == true) { 49 + keepRunning = false; 50 + if ( isDigit(currentChar()) ) { 51 + keepRunning = true; 52 + } else if ( currentChar() == "." && firstPeriod == true && isDigit(currentChar(1)) ) { 53 + keepRunning = true; 54 + firstPeriod = false; 55 + } 56 + if (keepRunning) { 57 + number += currentChar(); 58 + jumpChar(1); 59 + } else { 60 + jumpChar(-1); 61 + } 85 62 } 63 + return number; 86 64 } 87 65 88 - var nextWord = function() { 89 - if (wordIndex >= words.length) { 90 - return null; 91 - } else { 92 - return words[next++]; 66 + function readNext() { 67 + if ( isWhitespace(currentChar()) ) { 68 + jumpChar(); 69 + readNext(); 70 + } else if (currentChar() == "/" && currentChar(1) == "/" ) { 71 + jumpLine(); 72 + readNext(); 73 + } else if (currentChar() == '"') { 74 + readString(); 75 + jumpChar(); 76 + readNext(); 77 + } else if ( isDigit(currentChar()) ) { 78 + readNumber(); 79 + jumpChar(); 80 + readNext(); 93 81 } 94 82 } 95 - 96 - 83 + readNext(); 97 84 }
+2 -1
js/script.limp
··· 1 1 limp(` 2 - 2 + 2 2 + 233872-- 3 + then here we go 3 4 `);