Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html lang="en">
- <head>
- <title>Group 7 - Deckcode to JavaScript</title>
- <link rel="stylesheet" type="text/css" href="styleforcompiler.css">
- </head>
- <body>
- <center><h1>Group 7 - Deckcode to JavaScript</h1></center>
- <center><p>Input your deckode below:</p></center>
- <center><p><textarea id="myTextarea" rows="30" cols="50">Enter your deckcode here...</textarea>
- <textarea id="myTextarea" rows="30" cols="50"></textarea></p></center>
- <center><button type="button" onclick="myFunction()">Translate</button></center>
- <center><p id="demo"></p></center>
- <script>
- var Output = "";
- var ARRAY_COMMANDS = ["deck", "movetop", "moveall"];
- var ARRAY_OPERATIONS = ["less," "greater", "equal"];
- //var testDeck1 = ["A", "B", "C"];
- //var testDeck2 = ["d", "e", "f"];
- //var td1 = "in";
- //var td2 = "temp";
- function myFunction()
- {
- var completedArray = getWords();
- document.getElementById("demo").innerHTML = Output;
- }
- function getWords()
- {
- var bufferedString = [];
- var r = [];
- var textAreaValue = document.getElementById("myTextarea").value;
- for (i = 0, len = textAreaValue.length; i <= len; i++)
- {
- if (textAreaValue[i] == " " || textAreaValue[i] == undefined)
- {
- var completedString = bufferedString.join("");
- r.push(completedString);
- // Clear bufferedString
- bufferedString = [];
- }
- else
- {
- bufferedString.push(textAreaValue[i]);
- }
- }
- return r;
- }
- // Handles deck command
- function deck(pvar1, Ivar2)
- {
- if (pvar1 == undefined)
- {
- //error_flag = 1;
- error(1);
- }
- else if (typeof Ivar2 != "2" && Ivar2 != undefined)
- {
- //error_flag = 2;
- error(2);
- }
- else
- {
- if (Ivar2 == undefined)
- {
- // Input was "deck in"
- var returnArray = [];
- Output += ("var " + pvar1 + " = [];\n");
- }
- else
- {
- // (deck in: 1, 2, 3, 4, 5
- var returnArray = Ivar2;
- Output += ("var " + pvar1 + " = [" + Ivar2 + "];\n");
- }
- }
- }
- // Handles movetop command
- // Deckcode: movetop in, temp
- function movetop(Ivar1, Ovar2)
- {
- if (Ivar1 == undefined || Ovar2 == undefined)
- {
- //error_flag = 3;
- error(3);
- }
- // TODO: Add check to make sure parameters are in fact arrays
- else
- {
- Ovar2.unshift(Ivar1.shift());
- Output += (Ovar2 + ".unshift(" + Ivar1 + ".shift());");
- }
- }
- // Handles moveall command
- function moveall(Ivar1, Ovar2)
- {
- if (Ivar1 == undefined || Ovar2 == undefined)
- {
- //error_flag = 3;
- error(3);
- }
- // TODO: Add check to make sure parameters are in fact arrays
- else if (Ivar1.length == 0)
- {
- //error_flag = 5;
- error(5);
- }
- else
- {
- // I could have used Ovar2.concat(Ivar1), but it doesn't empty Ivar1, only copies it and concatenates to Ovar2
- // Plus stackoverflow told be this is apparently the fastest way to do it so who am I to argue
- /*
- while (Ivar1.length > 0)
- {
- Ovar2.push(Ivar1.pop());
- }
- */
- Output += ("while (" + Ivar1 + ".length > 0) { " + Ovar2 + ".push(" + Ivar1 + ".pop()); } ");
- }
- }
- function error(I_error_flag)
- {
- if (I_error_flag == 1)
- {
- console.log("Error 1: Deck initialization does not contain a name input");
- }
- else if (I_error_flag == 2)
- {
- console.log("Error 2: Deck initialization contains an invalid data input");
- }
- else if (I_error_flag == 3)
- {
- console.log("Error 3: Invalid function invocation--check if initial and destination decks are arrays");
- }
- else if (I_error_flag == 4)
- {
- console.log("Error 4: Type mismatch--decks should be arrays, but one or both are not");
- }
- else if (I_error_flag == 5)
- {
- console.log("Error 5: Empty initial array--is there an empty deck where there shouldn't be?");
- }
- }
- </script>
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement