<commit_before>#!/usr/bin/env node
const process = require('process');
const fs = require('fs');
const path = require('path');
const PEG = require('pegjs');

const compile = require('../lib/compiler').compile;
const defaultOut = './song.asm';

let program = require('commander');

program
  .usage('[options] <file>')
  .option('-o, --output [file]',
    `output file (default: ${defaultOut})`, defaultOut)
  .option('-s, --source', 'generate assembly source code')
  .option('--player',
    'generate a C64 executable player .prg instead of .sid file')
  .parse(process.argv);

if (!process.argv.slice(2).length) {
  program.outputHelp();
  process.exit(1);
}

if (program.args.length === 0) {
  console.error(`Missing source file`)
  process.exit(1);
}

if (program.args.length > 1) {
  console.error(`Invalid arguments: ${program.args}`)
  process.exit(1);
}

let inputPath = program.args[0];
let outputPath = program.output;

const source = fs.readFileSync(inputPath).toString();

const templatePath = path.join(__dirname, program.player ?
  '../lib/player/prg_player.asm' : '../lib/player/sid_player.asm');
const template = fs.readFileSync(templatePath).toString();

let out = compile(source, { template: template });

if (program.source) {
  fs.writeFileSync(outputPath, out);
} else {
  console.error('Assembler not implemented yet. Use --source for now.');
  process.exit(1);
}<commit_msg>Use Assembler to build .sid or .prg file on executable<commit_after>#!/usr/bin/env node
const process = require('process');
const fs = require('fs');
const path = require('path');

const compile = require('../lib/compiler').compile;
const Assembler = require('6502asm').Assembler;
const defaultOut = './song.sid';

let program = require('commander');

program
  .usage('[options] <file>')
  .option('-o, --output [file]', `output file`)
  .option('-s, --source', 'generate assembly source code')
  .option('--player',
    'generate a C64 executable player .prg instead of .sid file')
  .parse(process.argv);

if (!process.argv.slice(2).length) {
  program.outputHelp();
  process.exit(1);
}

if (program.args.length === 0) {
  console.error(`Missing source file`)
  process.exit(1);
}

if (program.args.length > 1) {
  console.error(`Invalid arguments: ${program.args}`)
  process.exit(1);
}


function abort(err) {
  if (err.location) {
    let loc = err.location.start;
    console.error(`${err.message}, at ${loc.line}:${loc.column}`);
  } else {
    console.error(err);
  }
  process.exit(1);
}

function outputPathFromOptions(program) {
  let inputPath = program.args[0];
  let basename = path.basename(inputPath, path.extname(inputPath));

  let newExt;
  if (program.source) {
    newExt = '.asm';
  } else if (program.player) {
    newExt = '.prg';
  } else {
    newExt = '.sid';
  }

  return `${basename}${newExt}`;
}

let inputPath = program.args[0];
let outputPath = program.output || outputPathFromOptions(program);

try {
  const source = fs.readFileSync(inputPath).toString();

  const templatePath = path.join(__dirname, program.player ?
    '../lib/player/prg_player.asm' : '../lib/player/sid_player.asm');
  const template = fs.readFileSync(templatePath).toString();

  let out = compile(source, { template: template });

  if (program.source) {
    fs.writeFileSync(outputPath, out);
  } else {
    let assembler = new Assembler();
    let {objectCode} = assembler.assemble(out);
    let buf = Buffer.from(objectCode);
    fs.writeFileSync(outputPath, buf);
  }
} catch(err) {
  abort(err);
}
{'fraction_non_alphanumeric': 0.17782844115132515, 'fraction_numerical': 0.007124536905101168, 'mean_word_length': 4.131578947368421, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1620833', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1156, 'n_words': 265}