<commit_before>#!/usr/bin/env coffee
fs = require 'fs'
path = require 'path'
readline = require 'readline'
jvm = require '../src/jvm'
util = require '../src/util'
ClassFile = require '../src/class_file'
methods = require '../src/methods'

classpath = [ ".", "#{__dirname}/../third_party/classes" ]

exports.read_binary_file = (filename) ->
  return null unless path.existsSync filename
  util.bytestr_to_array fs.readFileSync(filename, 'binary')

exports.read_classfile = (cls) ->
  for p in classpath
    data = exports.read_binary_file "#{p}/#{cls}.class"
    return new ClassFile data if data?

repl_run = (rs, cname, args) ->
  cname = cname[0...-6] if cname[-6..] is '.class'
  args ?= []
  jvm.run_class rs, cname, args

if require.main == module
  optimist = require 'optimist'
  {argv} = optimist

  optimist.usage '''
  Usage: $0
  Optional flags:
    --classpath=[path1:...:pathn]
    --help
  '''

  return optimist.showHelp() if argv.help

  if argv.classpath?
    classpath = argv.classpath.split ':'
    classpath.push "#{__dirname}/../third_party/classes"

  stdout = process.stdout
  stdin = process.openStdin()

  write_stdout = stdout.write.bind process.stdout
  read_stdin = (n_bytes, resume) ->
    process.stdin.resume()
    process.stdin.once 'data', (data) ->
      process.stdin.pause()
      resume data

  rs = new runtime.RuntimeState(write_stdout, read_stdin, exports.read_classfile)

  repl = readline.createInterface stdin, stdout

  repl.on 'close', ->
    repl.output.write '\n'
    repl.input.destroy()
  repl.on 'line', (line) ->
    toks = line.split /\s+/
    if toks.length > 0
      repl_run rs, toks[0], toks[1..]
    repl.output.write '\n'
    repl.prompt()
  repl.setPrompt 'doppio> '
  repl.prompt()
<commit_msg>clean up, simplify, document, and debug<commit_after>#!/usr/bin/env coffee
readline = require 'readline'
{argv} = require 'optimist'
jvm = require '../src/jvm'
{read_classfile} = require './runner'

repl_run = (rs, cname, args) ->
  cname = cname[0...-6] if cname[-6..] is '.class'
  jvm.run_class rs, cname, args

read_stdin = (n_bytes, resume) ->
  process.stdin.resume()
  process.stdin.once 'data', (data) ->
    process.stdin.pause()
    resume data

if require.main == module
  # initialize the RuntimeState
  write_stdout = process.stdout.write.bind process.stdout
  rs = new runtime.RuntimeState(write_stdout, read_stdin, read_classfile)

  # create the REPL
  stdin = process.openStdin()
  repl = readline.createInterface stdin, process.stdout

  # set up handlers
  repl.on 'close', ->
    repl.output.write '\n'
    repl.input.destroy()
  repl.on 'line', (line) ->
    toks = line.split /\s+/
    if toks?[0]?.length > 0
      repl_run rs, toks[0], toks[1..]
      repl.output.write '\n'
    repl.prompt()

  # set the prompt, display it, and begin the loop
  repl.setPrompt 'doppio> '
  repl.prompt()
{'fraction_non_alphanumeric': 0.15422191207257502, 'fraction_numerical': 0.0052337752965806, 'mean_word_length': 3.209985315712188, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '4183579', 'n_tokens_mistral': 1036, 'n_tokens_neox': 1033, 'n_words': 242}