JSFiddle - React, Tailwind, and code Playground
by orsina
HTML
<textarea id="input" name="Text1" cols="60" rows="10" placeholder="enter script here">
print "Here you can see my age"
age =12
print "My age is" + age
yourAge=0
print "Please enter your age"
read yourAge
</textarea>
<input type="button" value="test" />
<textarea id="output" name="Text2" cols="60" rows="8" placeholder="output"></textarea>
<textarea id="errors" name="Errors" cols="60" rows="7" placeholder="Here you will see the errors to help you with your code" readonly></textarea>
<script id="my-script"></script>
CSS
textarea {
white-space:pre-wrap;
}
JavaScript
function escapeRegExp(s) {
return s.replace(/([.*+?^=!:${}()\]\[\/\\])/g, '\\$1');
}
function replaceAll(p) {
var result1= p['string'].replace(new RegExp(escapeRegExp(p['search']), 'g'), p['replace']);
var result2= result1.slice(1,p['string'].length-1);//to remove "" from the strings
return result2.replace('\\ri','\\n'); //to replace \ri with \n
}
rxAssignment = new RegExp(/^ *([a-zA-Z]+) *=(.+)$/);
rxFormulaPart = new RegExp(/^ *([a-zA-Z]+|[0-9]+) *([-+*\/%]) *(.*)$/);
rxVariable = new RegExp(/^ *([a-zA-Z]+) *$/);
rxInt = new RegExp(/^ *([0-9]+) *$/);
rxString = new RegExp(/^ *"([^"]*)" *$/);
rxPrintString = new RegExp(/^ *print +"([^"]*)" *(.*)$/);
rxPrint = new RegExp(/^ *print +("(?:[^"]*)"|(?:[a-zA-Z]\w*)) *(.*)$/);
rxAfterPrint = new RegExp(/^ *\+ *("(?:[^"]*)"|(?:[a-zA-Z]\w*)) *(.*)$/);
rxErrorPrint = new RegExp(/^(\s*\+\s*(?:"[^"]*"|[a-zA-Z]\w*)\s*)+$/);
rxRead = new RegExp(/^ *read +([a-zA-Z]\w*) *$/);
$('input[type=button]').click(function () {
var lines = $('#input').val().split("\n");
var s = "myProgram=function(){var variables=[];var pOut=''; var errors='';"; // script
for (var i = 0; i < lines.length; i++) {
if (lines[i].trim().length == 0) {
continue;
}
//###############################################################
// To match print //###############################################################
var t = lines[i].match(rxPrint);
if (t !== null) {
var t1 = lines[i].match(rxPrintString);
var error_print =t[2].match(rxErrorPrint);
if(t[2]){//Print statement include multiple strings,variables
if(error_print === null){//means the print statement has errors
s += "errors+='"+"-Error :" + (i+1) + "'; errors+='"+". Syntax not correct\\n'"+ ";";
} else{
if (t1 !== null && error_print !==null ) {//to...