<commit_before>#![feature(slicing_syntax)]
#![allow(unstable)]
extern crate jit;

use jit::*;
use jit::typecs::UBYTE;
use std::cell::RefCell;
use std::io;
use std::io::fs::File;
use std::mem;
use std::os;
use std::path::Path;
use std::rc::Rc;

macro_rules! count(
    ($func:ident, $code:ident, $curr:ident) => ({
        let mut amount = 1us;
        while $code.peek() == Some(&$curr) {
            amount += 1;
            $code.next();
        }
        $func.insn_of(&amount)
    })
);

static PROMPT:&'static str = "> ";
type WrappedLoop<'a> = Rc<RefCell<Loop<'a>>>;
struct Loop<'a> {
    start: Label<'a>,
    end: Label<'a>,
    parent: Option<WrappedLoop<'a>>
}
impl<'a> Loop<'a> {
    fn new(func: &UncompiledFunction<'a>, current_loop: Option<WrappedLoop<'a>>) -> Loop<'a> {
        let mut new_loop = Loop {
            start: Label::new(func),
            end: Label::new(func),
            parent: current_loop
        };
        func.insn_label(&mut new_loop.start);
        new_loop
    }
    fn end(&mut self, func: &UncompiledFunction<'a>) -> Option<WrappedLoop<'a>> {
        func.insn_branch(&mut self.start);
        func.insn_label(&mut self.end);
        let mut parent = None;
        mem::swap(&mut parent, &mut self.parent);
        parent
    }
}

fn compile<'a>(func: &UncompiledFunction<'a>, code: &str) {
    let ubyte = UBYTE.get();
    let putchar_sig = get::<fn(u8)>();
    let readchar_sig = get::<fn() -> u8>();
    let data = func[0];
    let mut current_loop = None;
    let mut code = code.chars().peekable();
    while let Some(c) = code.next() {
        match c {
            '>' => {
                let amount = count!(func, code, c);
                let new_value = data + amount;
                func.insn_store(data, new_value);
            },
            '<' => {
                let amount = count!(func, code, c);
                let new_value = data - amount;
                func.insn_store(data, new_value);
            },
            '+' => {
                let amount = count!(func, code, c);
                let mut value = func.insn_load_relative(data, 0, ubyte.clone());
                value = value + amount;
                value = func.insn_convert(value, ubyte.clone(), false);
                func.insn_store_relative(data, 0, value)
            },
            '-' => {
                let amount = count!(func, code, c);
                let mut value = func.insn_load_relative(data, 0, ubyte.clone());
                value = value - amount;
                value = func.insn_convert(value, ubyte.clone(), false);
                func.insn_store_relative(data, 0, value)
            },
            '.' => {
                extern fn putchar(c: u8) {
                    io::stdout().write_u8(c).unwrap();<commit_msg>Use `count` as method instead of macro<commit_after>#![feature(slicing_syntax)]
#![allow(unstable)]
extern crate jit;

use jit::*;
use jit::typecs::UBYTE;
use std::cell::RefCell;
use std::io;
use std::io::fs::File;
use std::iter::Peekable;
use std::mem;
use std::os;
use std::path::Path;
use std::rc::Rc;

static PROMPT:&'static str = "> ";
type WrappedLoop<'a> = Rc<RefCell<Loop<'a>>>;
struct Loop<'a> {
    start: Label<'a>,
    end: Label<'a>,
    parent: Option<WrappedLoop<'a>>
}
impl<'a> Loop<'a> {
    fn new(func: &UncompiledFunction<'a>, current_loop: Option<WrappedLoop<'a>>) -> Loop<'a> {
        let mut new_loop = Loop {
            start: Label::new(func),
            end: Label::new(func),
            parent: current_loop
        };
        func.insn_label(&mut new_loop.start);
        new_loop
    }
    fn end(&mut self, func: &UncompiledFunction<'a>) -> Option<WrappedLoop<'a>> {
        func.insn_branch(&mut self.start);
        func.insn_label(&mut self.end);
        let mut parent = None;
        mem::swap(&mut parent, &mut self.parent);
        parent
    }
}

fn count<'a, I>(func: &UncompiledFunction<'a>, code: &mut Peekable<char, I>, curr:char) -> Value<'a> where I:Iterator<Item=char> {
    let mut amount = 1us;
    while code.peek() == Some(&curr) {
        amount += 1;
        code.next();
    }
    func.insn_of(&amount)
}

fn compile<'a>(func: &UncompiledFunction<'a>, code: &str) {
    let ubyte = UBYTE.get();
    let putchar_sig = get::<fn(u8)>();
    let readchar_sig = get::<fn() -> u8>();
    let data = func[0];
    let mut current_loop = None;
    let mut code = code.chars().peekable();
    while let Some(c) = code.next() {
        match c {
            '>' => {
                let amount = count(func, &mut code, c);
                let new_value = data + amount;
                func.insn_store(data, new_value);
            },
            '<' => {
                let amount = count(func, &mut code, c);
                let new_value = data - amount;
                func.insn_store(data, new_value);
            },
            '+' => {
                let amount = count(func, &mut code, c);
                let mut value = func.insn_load_relative(data, 0, ubyte.clone());
                value = value + amount;
                value = func.insn_convert(value, ubyte.clone(), false);
                func.insn_store_relative(data, 0, value)
            },
            '-' => {
                let amount = count(func, &mut code, c);
                let mut value = func.insn_load_relative(data, 0, ubyte.clone());
                value = value - amount;
                value = func.insn_convert(value, ubyte.clone(), false);
                func.insn_store_relative(data, 0, value)
            },
            '.' => {
                extern fn putchar(c: u8) {
                    io::stdout().write_u8(c).unwrap();
{'fraction_non_alphanumeric': 0.16262608387895947, 'fraction_numerical': 0.003893116262608388, 'mean_word_length': 2.019230769230769, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 74, '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': '620216', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1853, 'n_words': 406}