JSFiddle - React, Tailwind, and code Playground
by orsina
HTML
<textarea id="input" name="Text1" cols="40" rows="5" placeholder="enter script here">
print "This \iri" + "is" + "my first program"
a = 240
b=120
print a + c
print "a = "+ a +"is number" 'dsdafs' 23mds
</textarea>
<input type="button" value="test" />
<textarea id="output" name="Text2" cols="40" rows="5" placeholder="output"></textarea>
<textarea id="errors" name="Errors" cols="40" rows="5" 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('\\iri','\\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*)+$/);
$('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;
}
var t = lines[i].match(rxPrint);
if (t !== null) {
//alert(t);
//alert(t[2]);
var t1 = lines[i].match(rxPrintString);
var error_print =t[2].match(rxErrorPrint);
if(error_print === null){
s += "errors+='"+"You have error in row :" + (i+1) + "';";
} else{
if (t1 !== null && error_print !==null ) {
s += "pOut+='" + replaceAll({string: t[1], search: "'", replace: "\\'"}) +" "+ "';";
} else {
s += "pOut+=" + "variables['" + t[1] + "']" + " "+";";
s += "pOut+='" + " "+ "';";
}
var t2 = t[2].match(rxAfterPrint);
while (t2...