{"size": 2399, "ext": "js", "max_stars_repo_path": "Parser.js", "max_stars_repo_name": "sooham/interactive-brainfuck", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "Parser.js", "max_issues_repo_name": "sooham/interactive-brainfuck", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "Parser.js", "max_forks_repo_name": "sooham/interactive-brainfuck", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.9866666667, "max_line_length": 68, "alphanum_fraction": 0.4939558149}
"use strict";
/* This module parses preprocessed brainfuck code
 * and returns a logic object representing
 * the sequence of functions to execute.
 */

/* Gets the loop body of first loop in source */
var get_loop_body = function(source) {
    var open_brace_index = source.indexOf('[');
    for (var i = open_brace_index; i < source.length; i++) {
        if (source[i] == ']') {
            var body = source.slice(open_brace_index + 1, i);
            if (is_syntax_valid(body))
                return body;
        }
    }
}

// TODO: Bind arguments to the action functions
/*
 * Iterates over the source code and returns an array
 * of action objects.
 */
var translate = function(source) {
    var actions = [];
    var instruction_pointer = 0;
    while (instruction_pointer < source.length) {
        var action = {};
        action.type = "statement";
        var opchar = source[instruction_pointer];
        switch (opchar) {
            case '+':
            case '-':
                action.func = update_memory;
                action.args = (opchar == '+') ? [1]: [-1];
                break;
            case '>':
            case '<':
                action.func = update_pointer;
                action.args = (opchar == '>') ? [1]: [-1];
                break;
            case '.':
                action.func = print_memory;
                action.args = [];
                break;
            case ',':
                action.func = input_memory;
                // TODO: pass in input from user interface
                action.args = [];
                break;
            case '[':
                action.type = "loop";
                // in a loop, get the body of the loop and
                // recursively run translate on the body
                var loop_body = get_loop_body(
                    source.slice(instruction_pointer, source.length)
                );
                action.actions = translate(loop_body);
                // set instruction pointer to the corresponding
                // ]
                instruction_pointer += loop_body.length + 1;
                break;
            case ']':
                throw new Error("Detected: " + opchar);
                break;
            default:
                throw new Error("Detected: " + opchar);
        }
        actions.push(action);
        instruction_pointer++;
    }
    return actions;
}

