<commit_before>#include "../libponyc/ast/parserapi.h"
#include "../libponyc/pkg/package.h"
#include "../libponyc/pass/pass.h"
#include "../libponyc/ds/stringtab.h"
#include "../libponyc/platform/platform.h"

#include "options.h"

#include <stdlib.h>
#include <stdio.h>

#ifdef PLATFORM_IS_POSIX_BASED
#  include <sys/ioctl.h>
#  include <unistd.h>
#endif

enum
{
  OPT_AST,
  OPT_LLVM,
  OPT_OPTLEVEL,
  OPT_PATHS,
  OPT_PASSES,
  OPT_TRACE,
  OPT_WIDTH
};

static arg_t args[] =
{
  {"ast", 'a', ARGUMENT_NONE, OPT_AST},
  {"llvm", 'l', ARGUMENT_NONE, OPT_LLVM},
  {"opt", 'O', ARGUMENT_REQUIRED, OPT_OPTLEVEL},
  {"path", 'p', ARGUMENT_REQUIRED, OPT_PATHS},
  {"pass", 'r', ARGUMENT_REQUIRED, OPT_PASSES},
  {"trace", 't', ARGUMENT_NONE, OPT_TRACE},
  {"width", 'w', ARGUMENT_REQUIRED, OPT_WIDTH},
  ARGUMENTS_FINISH
};

void usage()
{
  printf(
    "ponyc [OPTIONS] <file>\n"
    "  --ast, -a       print the AST\n"
    "  --llvm, -l      print the LLVM IR\n"
    "  --opt, -O       optimisation level (0-3)\n"
    "  --path, -p      add additional colon separated search paths\n"
    "  --pass, -r      restrict phases\n"
    "  --trace, -t     enable parse trace\n"
    "  --width, -w     width to target when printing the AST\n"
    );
}

size_t get_width()
{
  size_t width = 80;
#ifdef _WIN64
  CONSOLE_SCREEN_BUFFER_INFO info;

  if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info))
  {
    int cols = info.srWindow.Right - info.srWindow.Left + 1;

    if(cols > width)
      width = cols;
  }
#else
  struct winsize ws;

  if(ioctl(STDOUT_FILENO, TIOCGWINSZ, ws))
  {
    if(ws.ws_col > width)
      width = ws.ws_col;
  }
#endif
  return width;
}

int main(int argc, char* argv[])
{
  package_init(argv[0]);

  bool ast = false;
  bool llvm = false;
  int opt = 0;
  size_t width = get_width();
  bool error = false;

  parse_state_t s;
  opt_init(args, &s, &argc, argv);

  int id;
  while((id = opt_next(&s)) != -1)
  {
    switch(id)
    {
      case OPT_AST: ast = true; break;
      case OPT_LLVM: llvm = true; break;
      case OPT_PATHS: package_add_paths(s.arg_val); break;
      case OPT_OPTLEVEL: opt = atoi(s.arg_val); break;
      case OPT_PASSES: error = !limit_passes(s.arg_val); break;
      case OPT_TRACE: parse_trace(true); break;
      case OPT_WIDTH: width = atoi(s.arg_val); break;
      default: error = true; break;
    }

    if(error)
    {
      usage();
      return -1;
    }
  }

  if((opt < 0) || (opt > 3))
  {
    usage();
    return -1;
  }

  ast_t* program = program_load((argc > 0) ? argv[0] : ".");
  int ret = 0;

  if(program != NULL)
  {
    if(ast)
    {
      ast_print(program, width);
    }
<commit_msg>reject if too many positional args<commit_after>#include "../libponyc/ast/parserapi.h"
#include "../libponyc/pkg/package.h"
#include "../libponyc/pass/pass.h"
#include "../libponyc/ds/stringtab.h"
#include "../libponyc/platform/platform.h"

#include "options.h"

#include <stdlib.h>
#include <stdio.h>

#ifdef PLATFORM_IS_POSIX_BASED
#  include <sys/ioctl.h>
#  include <unistd.h>
#endif

enum
{
  OPT_AST,
  OPT_LLVM,
  OPT_OPTLEVEL,
  OPT_PATHS,
  OPT_PASSES,
  OPT_TRACE,
  OPT_WIDTH
};

static arg_t args[] =
{
  {"opt", 'O', ARGUMENT_REQUIRED, OPT_OPTLEVEL},
  {"path", 'p', ARGUMENT_REQUIRED, OPT_PATHS},

  {"ast", 'a', ARGUMENT_NONE, OPT_AST},
  {"llvm", 'l', ARGUMENT_NONE, OPT_LLVM},
  {"pass", 'r', ARGUMENT_REQUIRED, OPT_PASSES},
  {"trace", 't', ARGUMENT_NONE, OPT_TRACE},
  {"width", 'w', ARGUMENT_REQUIRED, OPT_WIDTH},
  ARGUMENTS_FINISH
};

void usage()
{
  printf(
    "ponyc [OPTIONS] <file>\n"
    "  --opt, -O       optimisation level (0-3)\n"
    "  --path, -p      add additional colon separated search paths\n"
    "\n"
    "  --ast, -a       print the AST\n"
    "  --llvm, -l      print the LLVM IR\n"
    "  --pass, -r      restrict phases\n"
    "  --trace, -t     enable parse trace\n"
    "  --width, -w     width to target when printing the AST\n"
    "\n"
    );
}

size_t get_width()
{
  size_t width = 80;
#ifdef _WIN64
  CONSOLE_SCREEN_BUFFER_INFO info;

  if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info))
  {
    int cols = info.srWindow.Right - info.srWindow.Left + 1;

    if(cols > width)
      width = cols;
  }
#else
  struct winsize ws;

  if(ioctl(STDOUT_FILENO, TIOCGWINSZ, ws))
  {
    if(ws.ws_col > width)
      width = ws.ws_col;
  }
#endif
  return width;
}

int main(int argc, char* argv[])
{
  package_init(argv[0]);

  bool ast = false;
  bool llvm = false;
  int opt = 0;
  size_t width = get_width();

  parse_state_t s;
  opt_init(args, &s, &argc, argv);

  int id;
  while((id = opt_next(&s)) != -1)
  {
    switch(id)
    {
      case OPT_OPTLEVEL: opt = atoi(s.arg_val); break;
      case OPT_PATHS: package_add_paths(s.arg_val); break;

      case OPT_AST: ast = true; break;
      case OPT_LLVM: llvm = true; break;
      case OPT_TRACE: parse_trace(true); break;
      case OPT_WIDTH: width = atoi(s.arg_val); break;

      case OPT_PASSES:
        if(!limit_passes(s.arg_val))
        {
          usage();
          return -1;
        }
        break;

      default: usage(); return -1;
    }
  }

  if((opt < 0) || (opt > 3))
  {
    usage();
    return -1;
  }

  const char* path;

  switch(argc)
  {
    case 1: path = "."; break;
    case 2: path = argv[1]; break;
    default: usage(); return -1;
  }

  ast_t* program = program_load(path);
  int ret = 0;

  if(program != NULL)
  {
    if(ast)
    {
      ast_print(program, width);
    }

{'fraction_non_alphanumeric': 0.15746458409008354, 'fraction_numerical': 0.006719941881583727, 'mean_word_length': 2.683612040133779, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 20, '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': '7414259', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2210, 'n_words': 495}