Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var script = "";
- var hasErrored = false;
- function addLineToScript(newLine) {
- script += newLine + "\n";
- }
- function replaceAll(str, find, replace) {
- return str.replace(new RegExp(find, "g"), replace);
- }
- function parseConditionalExpression(expression) {
- expression = replaceAll(expression, " е идентично с", " === ");
- expression = replaceAll(expression, " е равно на", " == ");
- expression = replaceAll(expression, " не е равно на ", " != ");
- expression = replaceAll(expression, " и ", " && ");
- expression = replaceAll(expression, " или ", " || ");
- expression = expression.replace(/([a-zA-Z0-9]+) е вътре в ([a-zA-Z0-9]+)/g, "$2.indexOf($1) > -1");
- return expression;
- }
- function showError(error, parseError) {
- document.getElementById("error").className = "alert alert-danger";
- document.getElementById("error-text").innerHTML += error + "<br>";
- if(parseError) {
- document.getElementById("error-title").innerHTML = "О не! Има грешки в кода ти:<br>";
- } else {
- document.getElementById("error-title").innerHTML = "О не! Получи се грешка, когато се опитахме да пуснем кода ти:<br>";
- }
- hasErrored = true;
- }
- function hideError() {
- document.getElementById("error").className = "hidden";
- document.getElementById("error-text").innerHTML = "";
- hasErrored = false;
- }
- function parseFunctionCalls(expression) {
- return expression.replace(/<([^<>]+?) с параметри ([^<>]+?)>/, function(x,y,z) {
- return intoFunctionName(y) + "(" + z + ")";
- });
- }
- function removeIndent(expression) {
- return expression.replace(/^ +/, "");
- }
- function intoFunctionName(expression) {
- return replaceAll(expression, " ", "_");
- }
- function checkForError(matches, lineNumber) {
- if (matches == null) {
- showError("Не разбрах ред номер " + (lineNumber + 1), true);
- return false;
- }
- return true;
- }
- function execute() {
- var x = document.getElementById("text").value.split("\n");
- script = "";
- hideError();
- for (var i = 0; i < x.length; i++) {
- x[i] = parseFunctionCalls(x[i]);
- x[i] = removeIndent(x[i]);
- var line = x[i].split(" ");
- var matches = "";
- if (line[0] === "") {
- // Do nothing
- } else if (line[0] === "Задай" && line[1] === "стойност" && line[2] === "на") {
- matches = /Задай стойност на (.+) да е (.+)/g.exec(x[i]);
- if(checkForError(matches, i)) {
- addLineToScript(matches[1] + " = " + matches[2]);
- }
- } else if (line[0] === "Покажи") {
- matches = /Покажи (.+)/g.exec(x[i]);
- if(checkForError(matches, i)) {
- addLineToScript("alert(" + matches[1] + ")");
- }
- } else if (line[0] === "Попитай") {
- matches = /Попитай (".+") за (.+)/g.exec(x[i]);
- if(checkForError(matches, i)) {
- addLineToScript("var " + matches[2] + " = prompt(" + matches[1] + ")");
- }
- } else if (line[0] === "Ако") {
- matches = /Ако (.+), тогава направи:/g.exec(x[i]);
- if(checkForError(matches, i)) {
- addLineToScript("if(" + parseConditionalExpression(matches[1]) + ") {");
- }
- } else if (line[0] === "Край") {
- addLineToScript("}");
- } else if (line[0] === "Или") {
- if(line[1] == "в" && line[2] == "противен" && line[3] == "случай:") {
- addLineToScript("} else {");
- } else {
- matches = /Или ако (.+), тогава направи:/g.exec(x[i]);
- if(checkForError(matches, i)) {
- addLineToScript("} else if(" + parseConditionalExpression(matches[1]) + ") {");
- }
- }
- } else if (line[0] === "Докато") {
- matches = /Докато (.+) прави:/g.exec(x[i]);
- if(checkForError(matches, i)) {
- addLineToScript("while(" + parseConditionalExpression(matches[1]) + ") {");
- }
- } else if (line[0] === "Брой") {
- matches = /Брой докато (.+) стигне (-?\d+):/g.exec(x[i]);
- if(checkForError(matches, i)) {
- var incrementor = "--";
- if (parseInt(matches[2]) > 0) {
- incrementor = "++";
- }
- addLineToScript("for(" + matches[1] + " = 0; " + matches[1] + " != " + matches[2] + "; " + matches[1] + incrementor + ") {");
- }
- } else if (x[i].charAt(0) === "(" && x[i].charAt(x[i].length - 1) === ")") {
- matches = /\((.+)\)/g.exec(x[i]);
- if(checkForError(matches, i)) {
- addLineToScript("// " + matches[1]);
- }
- } else if (line[0] === "Въведи") {
- matches = /Въведи (.+) в (.+)/g.exec(x[i]);
- if(checkForError(matches, i)) {
- addLineToScript(matches[2] + ".push(" + matches[1] + ");");
- }
- } else if (line[0] === "Изтрий") {
- matches = /Изтрий (.+) от (.+)/g.exec(x[i]);
- if(checkForError(matches, i)) {
- addLineToScript(matches[2] + ".splice((" + matches[2] + ".indexOf(" + matches[1] + ")), 1)");
- }
- } else if (line[0] === "За") {
- matches = /За всеки (.+) в (.+) прави:/g.exec(x[i]);
- if(checkForError(matches, i)) {
- addLineToScript("for (var ___ = 0; ___ < " + matches[2] + ".length; ___++) {");
- addLineToScript("var " + matches[1] + " = " + matches[2] + "[___];");
- }
- } else if (line[0] === "Създай") {
- if(line[1] === "променливата") {
- matches = /Създай променливата (.+)/g.exec(x[i]);
- if(checkForError(matches, i)) {
- addLineToScript("var " + matches[1] + ";");
- }
- } else {
- matches = /Създай функция (.+) с параметри (.+):/g.exec(x[i]);
- if(checkForError(matches, i)) {
- matches[1] = intoFunctionName(matches[1]);
- addLineToScript("function " + matches[1] + "(" + matches[2] + ") {");
- }
- }
- } else if (x[i].slice(-2) === ").") {
- matches = /^([^ ]+?\(.+?\))/g.exec(x[i]);
- if(checkForError(matches, i)) {
- addLineToScript(matches[1]);
- }
- } else if (line[0] === "Върни резултат") {
- matches = /Върни резултат (.+)/g.exec(x[i]);
- console.log(matches)
- if(checkForError(matches, i)) {
- addLineToScript("return " + matches[1]);
- }
- } else {
- showError("Не разбрах ред номер " + (i + 1), true);
- return
- }
- }
- if(!hasErrored) {
- hideError();
- console.log(script);
- try {
- new Function(script)();
- } catch(err) {
- showError(err, false);
- }
- }
- }
- ----
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Kolev Lang</title>
- <script src="interpreter.js"></script>
- <link rel="stylesheet" type="text/css" href="https://bootswatch.com/3/lumen/bootstrap.min.css">
- <link rel="stylesheet" type="text/css" href="style.css">
- </head>
- <body>
- <div class="container">
- <div class="row">
- <div class="col-md-6 col-md-offset-3 col-header">
- <h1>Elusive</h1>
- <h3>Programming for everybody.</h3>
- </div>
- <div class="col-md-6 col-md-offset-3">
- <textarea class="block" rows="4" id="text" placeholder="Започи да пишеш кода си и ще се случи чудо!"></textarea>
- <div class="btn btn-success" id="run" onclick="execute();">Компилирай</div>
- <div class="alert alert-danger hidden" role="alert" id="error">
- <strong id="error-title">О не! Има грешки в кода ти:
- <br>
- </strong>
- <span id="error-text"></span>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
- ----
- @import url('https://fonts.googleapis.com/css?family=Merriweather:400,400i');
- * {
- font-family: "Merriweather", Georgia, 'Times New Roman', Times, serif
- }
- .block {
- min-width: 100%;
- border: none;
- background-color: #eee;
- padding: 10px;
- }
- .highlight {
- background-color: red;
- }
- .alert-danger {
- margin-top: 30px;
- }
- .col-header {
- padding: 50px;
- text-align: center;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement