<commit_before>part of badger.compiler;

class AstCompilerTarget extends CompilerTarget<String> {
  @override
  String compile(Program program) {
    var ast = new BadgerJsonBuilder(program);
    var encoder = new JsonEncoder.withIndent("  ");

    return encoder.convert(ast.build());
  }
}

class SnapshotCompilerTarget extends CompilerTarget<Future<String>> {
  final Environment env;

  SnapshotCompilerTarget(this.env);

  @override
  Future<String> compile(Program program) async {
    var out = {};
    var locations = [];

    for (ImportDeclaration import in program.declarations.where((it) => it is ImportDeclaration)) {
      locations.add(import.location.components.join());
    }

    for (var location in locations) {
      var p = await env.import(location);
      var m = JSON.decode(await compile(p));
      out[location] = m;
    }

    out["_"] = JSON.decode(new TinyAstCompilerTarget().compile(program));

    return JSON.encode(out);
  }
}

class TinyAstCompilerTarget extends CompilerTarget<String> {
  static final Map<String, String> MAPPING = {
    "type": "a",
    "immutable": "b",
    "reference": "c",
    "identifier": "d",
    "value": "e",
    "declarations": "f",
    "statements": "g",
    "isInitialDefine": "h",
    "op": "i",
    "components": "j",
    "args": "k",
    "method call": "l",
    "access": "m",
    "block": "n",
    "operator": "o",
    "assignment": "p",
    "string literal": "q",
    "left": "r",
    "right": "s",
    "variable reference": "t",
    "for in": "u",
    "if": "v",
    "while": "w",
    "function definition": "x",
    "anonymous function": "y",
    "import declaration": "z",
    "integer literal": "+",
    "double literal": "-",
    "hexadecimal literal": "@",
    "ternary operator": ">",
    "boolean literal": ">",
    "range literal": ".",
    "identifiers": "?"
  };

  static String demap(String key) {
    if (MAPPING.values.contains(key)) {
      return MAPPING.keys.firstWhere((it) => MAPPING[it] == key);
    } else {
      return key;
    }
  }

  static Map expand(Map it) {
    return transformMapStrings(it, (key) {
      return demap(key);
    });
  }

  @override
  String compile(Program program) {
    var ast = new BadgerJsonBuilder(program);
    var result = ast.build();
    return JSON.encode(transformMapStrings(result, (x) {
      if (MAPPING.containsKey(x)) {
        return MAPPING[x];<commit_msg>Tiny AST Compiler: Mapping for List and Map Definition<commit_after>part of badger.compiler;

class AstCompilerTarget extends CompilerTarget<String> {
  @override
  String compile(Program program) {
    var ast = new BadgerJsonBuilder(program);
    var encoder = new JsonEncoder.withIndent("  ");

    return encoder.convert(ast.build());
  }
}

class SnapshotCompilerTarget extends CompilerTarget<Future<String>> {
  final Environment env;

  SnapshotCompilerTarget(this.env);

  @override
  Future<String> compile(Program program) async {
    var out = {};
    var locations = [];

    for (ImportDeclaration import in program.declarations.where((it) => it is ImportDeclaration)) {
      locations.add(import.location.components.join());
    }

    for (var location in locations) {
      var p = await env.import(location);
      var m = JSON.decode(await compile(p));
      out[location] = m;
    }

    out["_"] = JSON.decode(new TinyAstCompilerTarget().compile(program));

    return JSON.encode(out);
  }
}

class TinyAstCompilerTarget extends CompilerTarget<String> {
  static final Map<String, String> MAPPING = {
    "type": "a",
    "immutable": "b",
    "reference": "c",
    "identifier": "d",
    "value": "e",
    "declarations": "f",
    "statements": "g",
    "isInitialDefine": "h",
    "op": "i",
    "components": "j",
    "args": "k",
    "method call": "l",
    "access": "m",
    "block": "n",
    "operator": "o",
    "assignment": "p",
    "string literal": "q",
    "left": "r",
    "right": "s",
    "variable reference": "t",
    "for in": "u",
    "if": "v",
    "while": "w",
    "function definition": "x",
    "anonymous function": "y",
    "import declaration": "z",
    "integer literal": "+",
    "double literal": "-",
    "hexadecimal literal": "@",
    "ternary operator": ">",
    "boolean literal": ">",
    "range literal": ".",
    "identifiers": "?",
    "list definition": "|",
    "map definition": "["
  };

  static String demap(String key) {
    if (MAPPING.values.contains(key)) {
      return MAPPING.keys.firstWhere((it) => MAPPING[it] == key);
    } else {
      return key;
    }
  }

  static Map expand(Map it) {
    return transformMapStrings(it, (key) {
      return demap(key);
    });
  }

  @override
  String compile(Program program) {
    var ast = new BadgerJsonBuilder(program);
    var result = ast.build();
    return JSON.encode(transformMapStrings(result, (x) {
      if (MAPPING.containsKey(x)) {
        return MAPPING[x];
{'fraction_non_alphanumeric': 0.16796235679214402, 'fraction_numerical': 0.0, 'mean_word_length': 3.175064047822374, 'pattern_counts': {'":': 68, '<': 15, '<?xml version=': 0, '>': 23, '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': '5292606', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1553, 'n_words': 387}