Post version history/View/0.1.0 BT2

[previous] &verbar; Post version history &verbar; [next] // Imports, read entered code const fs = require("fs"); const code = fs.readFileSync("code.txt").toString.split("\n");

// Define global variables var logs = []; // Logs for good measure var doing = -1; // The line we're doing var done = false; // The code is all executed or not var hasComment = false; // If the current line we're on has a comment

var module = ""; var command = ""; var params = [];

var olr = []; // One line ram var mlr = []; // Many line ram var installed = []; // Installed modules const showErrors = true; // If false then errors will not be consoleLogged

// Define possible modules const modules = [ "sys","math","tt","time", ];

// Globally used functions function getCurrentTime { // Get the current time in HH:MM:SS format return `${("00" + today.getHours).slice(-2)}:${("00" + today.getMinutes).slice(-2)}:${("00" + today.getSeconds).slice(-2)}`; }

function log(message) { // Logs a message logs.push(`${getCurrentTime}: ${message}`); }

function consoleLog(message) { // Should be used instead of console.log so if this is ported over to other JS launchers we // don't need to go scour all console.logs and change them to the one the new JS launcher supports // We just need to change this one - the single line of code below console.log(message) }

function error(message) { // Shows an error and logs it log(`Error: ${message}`); if (showErrors) { consoleLog(`Error at line ${doing + 1}:\n ${code[doing]}\n  ${message}`); } }

function isBlank(str) { // True if the string is only spaces or completely blank, else false return (!str || /^\s*$/.test(str)); }

function getType(tocheck) { if (isBlank(tocheck)) { return "invalid"; } else if (tocheck.startsWith("<")) { return "varinput"; } else if (tocheck.startsWith("\"")) {   return "str";  } else if (tocheck.startsWith("'")) {    return "num";  } else if (tocheck == "true" || tocheck == "false" || tocheck == "neutral") {    return "bool";  } else if (tocheck == "null") {    return "null";  } else {    return "invalid";  } }

// Function utility functions function param(index,wantedTypes,defaultValue) { if (wantedTypes.includes("any")) { return params[index]; } else if (wantedTypes.includes(getType(params[index]))) { let jstc = getType(params[index]); if (jstc == "num" || jstc == "str") { return params[index].slice(1); } else if (jstc == "varinput") { return ""; // TEMPORARY, until we get a proper variable system } else { return params[index]; } } else { log(`On a param check, expected ${wantedTypes.join(" or ")} but got ${getType(params[index])}.`); return defaultValue; } }

function funcReturn(returned) { consoleLog(`A function returned: ${returned}`) // TEMPORARY, until we have a proper function returning system }

// Function-specific function getRandomInteger(min,max) { return Math.floor(Math.random * (max - min) ) + min; }

var today = new Date; // TEMPORARY - Would this still be ok if its a constant const utilMonthNames = ["January","Febuary"] const utilWeekdayNames = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]

// TEMPORARY - NEEDS REVIEW Date.prototype.getWeek = function { // Credit: https://weeknumber.com/how-to/javascript var date = new Date(this.getTime); date.setHours(0, 0, 0, 0); date.setDate(date.getDate + 3 - (date.getDay + 6) % 7); var week1 = new Date(date.getFullYear, 0, 4); return 1 + Math.round(((date.getTime - week1.getTime) / 86400000 - 3 + (week1.getDay + 6) % 7) / 7); }

// TEMPORARY - NEEDS REVIEW Date.prototype.isLeapYear = function { // Credit: https://stackoverflow.com/a/26426761 var year = this.getFullYear; if((year & 3) != 0) return false; return ((year % 100) != 0 || (year % 400) == 0); };

// TEMPORARY - NEEDS REVIEW Date.prototype.getDOY = function { // Credit: Also https://stackoverflow.com/a/26426761 var dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]; var mn = this.getMonth; var dn = this.getDate; var dayOfYear = dayCount[mn] + dn; if(mn > 1 && this.isLeapYear) dayOfYear++; return dayOfYear; };

// Start main loop while (doing <= code.length) { // Increment the line number that we are on and reset olr doing++; olr = [];

// Check if it's a blank line if (isBlank(code[doing])) { log("Blank line has been passed over."); continue; }

// Escape comment and define module if (code[doing].includes("#")) { // There is a comment params = code[doing].substring(0,code[doing].indexOf("#")).split("."); hasComment = true; } else { // There is no comment params = code[doing].split("."); hasComment = false; } if (params.length == 1) { // There's no module error("There is no command, only the module is found."); continue; } module = params[0];

// Is the given module actually a module and is it installed /* if (module == "install") { // A new module will be installed if (installed.includes(params[1])) { error(`Install failed: ${params[1]} is already installed.`); } else if (modules.includes(params[1])) { installed.push(params[1]); } else { error(`Install failed: ${params[1]} is not a module.`); }   continue; } else if (!installed.includes(module)) { // The module is not installed if (modules.includes(module)) { // The module does exist, its just not installed error(`The module "${module}" is not installed.`); } else { // The module doesn't exist at all error(`The module "${module}" does not exist and thus isn't installed.`); }   continue; } */

// Define command and params command = params[1]; params = params.slice(2); if (hasComment) { log(`Line ${doing + 1}: ${code[doing]}: Module:${module}, Command:${command}, Params:${params}, Has a comment`); } else { log(`Line ${doing + 1}: ${code[doing]}: Module:${module}, Command:${command}, Params:${params}, No comment`); }

// Finally, begin execute the function itself if (module == "sys") { if (command == "print") { consoleLog(param(0,["str"],"")); } else if (command == "log") { log(`${param(0,["str"],"")} (user-created)`); } else if (command == "error") { error(`${param(0,["str"],"")} (user-created)`); } else if (command == "exit") { consoleLog(`Exited with message ${param(0,["str"],"(no message)")}.`); break; } } else if (module == "math") { if (command == "plus") { funcReturn(parseInt(param(0,["num"],"0")) + parseInt(param(1,["num"],"0"))); } else if (command == "minus") { funcReturn(parseInt(param(0,["num"],"0")) - parseInt(param(1,["num"],"0"))); } else if (command == "multiply") { funcReturn(parseInt(param(0,["num"],"0")) * parseInt(param(1,["num"],"0"))); } else if (command == "divide") { funcReturn(parseInt(param(0,["num"],"0")) / parseInt(param(1,["num"],"1"))); } else if (command == "remainder") { funcReturn(parseInt(param(0,["num"],"0")) % parseInt(param(1,["num"],"1"))); } else if (command == "power") { funcReturn(parseInt(param(0,["num"],"0")) ** parseInt(param(1,["num"],"0"))); } else if (command == "sqrt") { funcReturn(Math.sqrt(parseInt(param(0,["num"],"0")))); } else if (command == "cbrt") { funcReturn(Math.cbrt(parseInt(param(0,["num"],"0")))); } else if (command == "incre") { funcReturn(parseInt(param(0,["num"],"0")) + 1); } else if (command == "decre") { funcReturn(parseInt(param(0,["num"],"0")) - 1); } else if (command == "abs") { funcReturn(Math.abs(parseInt(param(0,["num"],"0")))); } else if (command == "round") { funcReturn(Math.round(parseInt(param(0,["num"],"0")))); } else if (command == "floor") { funcReturn(Math.floor(parseInt(param(0,["num"],"0")))); } else if (command == "ceil") { funcReturn(Math.ceil(parseInt(param(0,["num"],"0")))); } else if (command == "type") { // Number is whole or decimal and negative or positive if (param(0,["num"],"0").includes(".0")) { if (param(0,["num"],"0").includes("-")) { funcReturn(["negative","whole","withDotZero"]); } else { funcReturn(["positive","whole","withDotZero"]); }     } else if (param(0,["num"],"0").includes(".")) { if (param(0,["num"],"0").includes("-")) { funcReturn(["negative","decimal","pure"]); } else { funcReturn(["positive","decimal","pure"]); }     } else { if (param(0,["num"],"0").includes("-")) { funcReturn(["negative","whole","pure"]); } else { funcReturn(["positive","whole","pure"]); }     }    } else if (command == "sin") { funcReturn(Math.sin(parseInt(param(0,["num"],"0")))); } else if (command == "cos") { funcReturn(Math.cos(parseInt(param(0,["num"],"0")))); } else if (command == "tan") { funcReturn(Math.tan(parseInt(param(0,["num"],"0")))); } else if (command == "logar") { funcReturn(Math.log(parseInt(param(0,["num"],"0")))); } else if (command == "truefalse") { if (Math.round(Math.random) == 1) { funcReturn("true"); } else { funcReturn("false"); }   } else if (command == "random") { funcReturn(getRandomInteger(parseInt(param(0,["num"],"0")),parseInt(param(1,["num"],"0")))); } } else if (module == "tt") { continue; } else if (module == "time") { if (command == "format") { olr[0] = param(0,["str"],""); olr[0] = olr[0].replace("%c",Math.ceil(today.getFullYear / 100)); olr[0] = olr[0].replace("%z",`${Math.ceil(today.getFullYear.toString.slice(0,-1))}0s`); olr[0] = olr[0].replace("%y",today.getFullYear); olr[0] = olr[0].replace("%m",today.getMonth + 1); olr[0] = olr[0].replace("%M",utilMonthNames[today.getMonth]); olr[0] = olr[0].replace("%w",today.getWeek) olr[0] = olr[0].replace("%o",today.getDay) // Day of week olr[0] = olr[0].replace("%O",utilWeekdayNames[today.getDay]); olr[0] = olr[0].replace("%d",today.getDate); olr[0] = olr[0].replace("%a",today.getDOY) olr[0] = olr[0].replace("%h",today.getHours); olr[0] = olr[0].replace("%n",today.getMinutes); olr[0] = olr[0].replace("%s",today.getSeconds); olr[0] = olr[0].replace("%i",today.getMilliseconds); funcReturn(olr[0]) } else if (command == "isleap") { funcReturn(today.isLeapYear); } else if (command == "timestamp") { funcReturn(today.getTime); } else if (command == "isweekend") { if (today.getDay == 0 || today.getDay == 6) { funcReturn("true"); } else { funcReturn("false"); }   } else if (command == "fromutc") { funcReturn(today.getTimezoneOffset) } else if (command == "wait") { continue; // TEMPORARY - no function seems feasible yet } } } done = true;