<commit_before>/*
 * Whitespace-JS / inst/io.js
 * copyright (c) 2016 Susisu
 */

"use strict";

function end() {
    module.exports = Object.freeze({
        IO,
        PutChar,
        Print,
        GetChar,
        Read
    });
}

const co = require("co");

const inst = require("./inst.js");

class IO extends inst.Instruction {
    constructor(pos) {
        super(pos);
    }

    toWhitespace() {
        return inst.TAB + inst.LF + this.toSubWhitespace();
    }

    toSubWhitespace() {
        throw new Error("unknown instruction");
    }
}

class PutChar extends IO {
    constructor(pos) {
        super(pos);
    }

    toSubWhitespace() {
        return inst.SPACE + inst.SPACE;
    }

    toUnWhitespace() {
        return "putchar";
    }

    doSub(vm) {
        let c = vm.state.pop(this.pos);
        vm.output(String.fromCharCode(c));
    }
}

class Print extends IO {
    constructor(pos) {
        super(pos);
    }

    toSubWhitespace() {
        return inst.SPACE + inst.TAB;
    }

    toUnWhitespace() {
        return "print";
    }

    doSub(vm) {
        let n = vm.state.pop(this.pos);
        vm.output(n.toString());
    }
}

class GetChar extends IO {
    constructor(pos) {
        super(pos);
    }

    toSubWhitespace() {
        return inst.TAB + inst.SPACE;
    }

    toUnWhitespace() {
        return "getchar";
    }

    doSub(vm) {
        return vm.input().then(c => {
            vm.state.push(c.charCodeAt(0));
        });
    }
}

class Read extends IO {
    constructor(pos) {
        super(pos);
    }

    toSubWhitespace() {
        return inst.TAB + inst.TAB;
    }

    toUnWhitespace() {
        return "read";
    }

    doSub(vm) {
        return co(function * () {
            let str = "";
            while (true) {
                let c = yield vm.input();
                if (c === "\n") {
                    break;
                }
                str += c;
            }
            vm.state.push(parseInt(str) | 0);<commit_msg>Fix GetChar and Read instructions<commit_after>/*
 * Whitespace-JS / inst/io.js
 * copyright (c) 2016 Susisu
 */

"use strict";

function end() {
    module.exports = Object.freeze({
        IO,
        PutChar,
        Print,
        GetChar,
        Read
    });
}

const co = require("co");

const inst = require("./inst.js");

class IO extends inst.Instruction {
    constructor(pos) {
        super(pos);
    }

    toWhitespace() {
        return inst.TAB + inst.LF + this.toSubWhitespace();
    }

    toSubWhitespace() {
        throw new Error("unknown instruction");
    }
}

class PutChar extends IO {
    constructor(pos) {
        super(pos);
    }

    toSubWhitespace() {
        return inst.SPACE + inst.SPACE;
    }

    toUnWhitespace() {
        return "putchar";
    }

    doSub(vm) {
        let c = vm.state.pop(this.pos);
        vm.output(String.fromCharCode(c));
    }
}

class Print extends IO {
    constructor(pos) {
        super(pos);
    }

    toSubWhitespace() {
        return inst.SPACE + inst.TAB;
    }

    toUnWhitespace() {
        return "print";
    }

    doSub(vm) {
        let n = vm.state.pop(this.pos);
        vm.output(n.toString());
    }
}

class GetChar extends IO {
    constructor(pos) {
        super(pos);
    }

    toSubWhitespace() {
        return inst.TAB + inst.SPACE;
    }

    toUnWhitespace() {
        return "getchar";
    }

    doSub(vm) {
        let loc = vm.state.pop();
        return vm.input().then(c => {
            vm.state.store(loc, c.charCodeAt(0));
        });
    }
}

class Read extends IO {
    constructor(pos) {
        super(pos);
    }

    toSubWhitespace() {
        return inst.TAB + inst.TAB;
    }

    toUnWhitespace() {
        return "read";
    }

    doSub(vm) {
        let loc = vm.state.pop();
        return co(function * () {
            let str = "";
            while (true) {
                let c = yield vm.input();
                if (c === "\n") {
                    break;
                }
                str += c;
            }
            vm.state.store(loc, parseInt(str) | 0);
{'fraction_non_alphanumeric': 0.138671875, 'fraction_numerical': 0.0029296875, 'mean_word_length': 1.543140906269398, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4461356', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1335, 'n_words': 261}