VAL Virtual Machine v0.2

by Tgwizman

HTML

<canvas></canvas>
<p>Check out the <a href="">VAL Docs</a> and learn how to code in VAL.</p>

CSS

html, body {
    background-color: #000;
    color: #3D3;
    font-size: 8pt;
    font-family: "Inconsolata", "Monaco", "Consolas", "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace;
}

p {
    margin: 2px 5px;
}

a {
    color: #3AD;
    text-decoration: none;
}

JavaScript

1xPos = 4;
yPos = 0;
pointer = [''];
line = 0;
domain = 'normal';

streamTemp = "";

textColor = '#3D3';
systemColor = '#3AD';
errorColor = '#D33';
bgColor = '#000';

canvas = document.querySelector("canvas");
canvas.style.backgroundColor = bgColor;
canvas.width = window.innerWidth - 4;
canvas.height = window.innerHeight - 30;
ctx = canvas.getContext("2d");
ctx.font = '10px "Inconsolata", "Monaco", "Consolas", "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace';

domains = {
    normal : '> ',
    stream_captured : '',
};

color = function (color) {
    ctx.fillStyle = color;
};

print = function (msg) {
    msg = msg || domains[domain];
    for (i=0;i<msg.split("\n").length;i++) {
        xPos = 4;
        yPos += 15;
        ctx.fillText(msg.split("\n")[i], xPos, yPos);
    }
    xPos += ctx.measureText(msg).width;
};

type = function (char) {
    color(textColor);
    ctx.fillText(char, xPos, yPos);
    pointer[line] += char;
    xPos += ctx.measureText(char).width;
};

backspace = function (msg) {
    xPos = ctx.measureText("> ").width + 4;
    color(bgColor);
    ctx.fillRect(xPos, yPos - 12, canvas.height, yPos);
    pointer[line] = '';
    type(msg.slice(0, msg.length - 1));
};

keyEvents = function (event) {
    key = event.charCode || event.keyCode;
    key == 13 ? parse(pointer[line]) : key == 8 ? '' : type(String.fromCharCode(key));
    return false;
};

this.onkeypress = keyEvents;
this.onkeydown = function (event) {
    if (event.keyCode == 8) {
        event.preventDefault();
        backspace(pointer[line]);
    }
};

variables = {};

functions = {};

val = {
    Variable : function(msg) {
        if (msg.split(" ")[2] == "=") {
            if (getType(msg.split(" ")[3]) == "String") {
                name = msg.split(" ")[1];
                value = (/'([^']*?)'/).exec(msg.split(" ")[3])[1];
                variables[name] = "String " + value;
            } else if (getType(msg.split(" ")[3]) == "Variable") {
            ...