#include <stdint.h>
#include <stdlib.h>
#include "el_runtime.h"

el_val_t str_ends(el_val_t s, el_val_t suf);
el_val_t str_last_char(el_val_t s);
el_val_t str_last2(el_val_t s);
el_val_t str_last3(el_val_t s);
el_val_t str_drop_last(el_val_t s, el_val_t n);
el_val_t is_vowel(el_val_t c);
el_val_t morph_apply_suffix(el_val_t base, el_val_t suffix);
el_val_t en_irregular_plural(el_val_t word);
el_val_t en_irregular_singular(el_val_t word);
el_val_t en_irregular_verb(el_val_t base);
el_val_t en_verb_3sg(el_val_t base);
el_val_t en_should_double_final(el_val_t base);
el_val_t en_verb_past(el_val_t base);
el_val_t en_verb_gerund(el_val_t base);
el_val_t en_pluralize_regular(el_val_t singular);
el_val_t en_verb_form(el_val_t base, el_val_t tense, el_val_t person, el_val_t number);
el_val_t agree_determiner(el_val_t det, el_val_t noun);
el_val_t morph_pluralize(el_val_t noun, el_val_t profile);
el_val_t morph_map_canonical(el_val_t verb, el_val_t code);
el_val_t morph_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number, el_val_t profile);
el_val_t morph_inflect(el_val_t word, el_val_t features, el_val_t profile);
el_val_t pluralize(el_val_t singular);
el_val_t singularize(el_val_t plural);
el_val_t verb_form(el_val_t base, el_val_t tense, el_val_t person, el_val_t number);
el_val_t irregular_plural(el_val_t word);
el_val_t irregular_singular(el_val_t word);
el_val_t enm_str_ends(el_val_t s, el_val_t suf);
el_val_t enm_drop(el_val_t s, el_val_t n);
el_val_t enm_first_char(el_val_t s);
el_val_t enm_slot(el_val_t person, el_val_t number);
el_val_t enm_been_present(el_val_t slot);
el_val_t enm_been_past(el_val_t slot);
el_val_t enm_haven_present(el_val_t slot);
el_val_t enm_haven_past(el_val_t slot);
el_val_t enm_goon_present(el_val_t slot);
el_val_t enm_goon_past(el_val_t slot);
el_val_t enm_seen_present(el_val_t slot);
el_val_t enm_seen_past(el_val_t slot);
el_val_t enm_seyen_present(el_val_t slot);
el_val_t enm_seyen_past(el_val_t slot);
el_val_t enm_comen_present(el_val_t slot);
el_val_t enm_comen_past(el_val_t slot);
el_val_t enm_maken_present(el_val_t slot);
el_val_t enm_maken_past(el_val_t slot);
el_val_t enm_map_canonical(el_val_t verb);
el_val_t enm_weak_stem(el_val_t verb);
el_val_t enm_weak_present(el_val_t stem, el_val_t slot);
el_val_t enm_weak_past(el_val_t stem, el_val_t slot);
el_val_t enm_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
el_val_t enm_irregular_plural(el_val_t noun);
el_val_t enm_make_plural(el_val_t noun);
el_val_t enm_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
el_val_t enm_is_vowel_initial(el_val_t s);
el_val_t enm_indef_article(el_val_t noun_phrase);
el_val_t enm_noun_phrase(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite);

el_val_t enm_str_ends(el_val_t s, el_val_t suf) {
  return str_ends_with(s, suf);
  return 0;
}

el_val_t enm_drop(el_val_t s, el_val_t n) {
  el_val_t len = str_len(s);
  if (n >= len) {
    return EL_STR("");
  }
  return str_slice(s, 0, (len - n));
  return 0;
}

el_val_t enm_first_char(el_val_t s) {
  if (str_len(s) == 0) {
    return EL_STR("");
  }
  return str_slice(s, 0, 1);
  return 0;
}

el_val_t enm_slot(el_val_t person, el_val_t number) {
  if (str_eq(person, EL_STR("first"))) {
    if (str_eq(number, EL_STR("singular"))) {
      return 0;
    }
    return 3;
  }
  if (str_eq(person, EL_STR("second"))) {
    if (str_eq(number, EL_STR("singular"))) {
      return 1;
    }
    return 4;
  }
  if (str_eq(number, EL_STR("singular"))) {
    return 2;
  }
  return 5;
  return 0;
}

el_val_t enm_been_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("am");
  }
  if (slot == 1) {
    return EL_STR("art");
  }
  if (slot == 2) {
    return EL_STR("is");
  }
  if (slot == 3) {
    return EL_STR("aren");
  }
  if (slot == 4) {
    return EL_STR("been");
  }
  return EL_STR("been");
  return 0;
}

el_val_t enm_been_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("was");
  }
  if (slot == 1) {
    return EL_STR("were");
  }
  if (slot == 2) {
    return EL_STR("was");
  }
  if (slot == 3) {
    return EL_STR("were");
  }
  if (slot == 4) {
    return EL_STR("were");
  }
  return EL_STR("were");
  return 0;
}

el_val_t enm_haven_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("have");
  }
  if (slot == 1) {
    return EL_STR("hast");
  }
  if (slot == 2) {
    return EL_STR("hath");
  }
  if (slot == 3) {
    return EL_STR("have");
  }
  if (slot == 4) {
    return EL_STR("have");
  }
  return EL_STR("have");
  return 0;
}

el_val_t enm_haven_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("hadde");
  }
  if (slot == 1) {
    return EL_STR("haddest");
  }
  if (slot == 2) {
    return EL_STR("hadde");
  }
  if (slot == 3) {
    return EL_STR("hadden");
  }
  if (slot == 4) {
    return EL_STR("hadden");
  }
  return EL_STR("hadden");
  return 0;
}

el_val_t enm_goon_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("go");
  }
  if (slot == 1) {
    return EL_STR("goost");
  }
  if (slot == 2) {
    return EL_STR("gooth");
  }
  if (slot == 3) {
    return EL_STR("goon");
  }
  if (slot == 4) {
    return EL_STR("goon");
  }
  return EL_STR("goon");
  return 0;
}

el_val_t enm_goon_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("wente");
  }
  if (slot == 1) {
    return EL_STR("wentest");
  }
  if (slot == 2) {
    return EL_STR("wente");
  }
  if (slot == 3) {
    return EL_STR("wenten");
  }
  if (slot == 4) {
    return EL_STR("wenten");
  }
  return EL_STR("wenten");
  return 0;
}

el_val_t enm_seen_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("see");
  }
  if (slot == 1) {
    return EL_STR("seest");
  }
  if (slot == 2) {
    return EL_STR("seeth");
  }
  if (slot == 3) {
    return EL_STR("seen");
  }
  if (slot == 4) {
    return EL_STR("seen");
  }
  return EL_STR("seen");
  return 0;
}

el_val_t enm_seen_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("saugh");
  }
  if (slot == 1) {
    return EL_STR("sawest");
  }
  if (slot == 2) {
    return EL_STR("saugh");
  }
  if (slot == 3) {
    return EL_STR("sawen");
  }
  if (slot == 4) {
    return EL_STR("sawen");
  }
  return EL_STR("sawen");
  return 0;
}

el_val_t enm_seyen_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("seye");
  }
  if (slot == 1) {
    return EL_STR("seyst");
  }
  if (slot == 2) {
    return EL_STR("seith");
  }
  if (slot == 3) {
    return EL_STR("seyen");
  }
  if (slot == 4) {
    return EL_STR("seyen");
  }
  return EL_STR("seyen");
  return 0;
}

el_val_t enm_seyen_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("seide");
  }
  if (slot == 1) {
    return EL_STR("seidest");
  }
  if (slot == 2) {
    return EL_STR("seide");
  }
  if (slot == 3) {
    return EL_STR("seiden");
  }
  if (slot == 4) {
    return EL_STR("seiden");
  }
  return EL_STR("seiden");
  return 0;
}

el_val_t enm_comen_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("come");
  }
  if (slot == 1) {
    return EL_STR("comest");
  }
  if (slot == 2) {
    return EL_STR("cometh");
  }
  if (slot == 3) {
    return EL_STR("comen");
  }
  if (slot == 4) {
    return EL_STR("comen");
  }
  return EL_STR("comen");
  return 0;
}

el_val_t enm_comen_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("cam");
  }
  if (slot == 1) {
    return EL_STR("come");
  }
  if (slot == 2) {
    return EL_STR("cam");
  }
  if (slot == 3) {
    return EL_STR("comen");
  }
  if (slot == 4) {
    return EL_STR("comen");
  }
  return EL_STR("comen");
  return 0;
}

el_val_t enm_maken_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("make");
  }
  if (slot == 1) {
    return EL_STR("makest");
  }
  if (slot == 2) {
    return EL_STR("maketh");
  }
  if (slot == 3) {
    return EL_STR("maken");
  }
  if (slot == 4) {
    return EL_STR("maken");
  }
  return EL_STR("maken");
  return 0;
}

el_val_t enm_maken_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("made");
  }
  if (slot == 1) {
    return EL_STR("madest");
  }
  if (slot == 2) {
    return EL_STR("made");
  }
  if (slot == 3) {
    return EL_STR("maden");
  }
  if (slot == 4) {
    return EL_STR("maden");
  }
  return EL_STR("maden");
  return 0;
}

el_val_t enm_map_canonical(el_val_t verb) {
  if (str_eq(verb, EL_STR("be"))) {
    return EL_STR("been");
  }
  if (str_eq(verb, EL_STR("have"))) {
    return EL_STR("haven");
  }
  if (str_eq(verb, EL_STR("go"))) {
    return EL_STR("goon");
  }
  if (str_eq(verb, EL_STR("see"))) {
    return EL_STR("seen");
  }
  if (str_eq(verb, EL_STR("say"))) {
    return EL_STR("seyen");
  }
  if (str_eq(verb, EL_STR("come"))) {
    return EL_STR("comen");
  }
  if (str_eq(verb, EL_STR("make"))) {
    return EL_STR("maken");
  }
  return verb;
  return 0;
}

el_val_t enm_weak_stem(el_val_t verb) {
  if (enm_str_ends(verb, EL_STR("en"))) {
    return enm_drop(verb, 2);
  }
  if (enm_str_ends(verb, EL_STR("e"))) {
    return enm_drop(verb, 1);
  }
  return verb;
  return 0;
}

el_val_t enm_weak_present(el_val_t stem, el_val_t slot) {
  if (slot == 0) {
    return el_str_concat(stem, EL_STR("e"));
  }
  if (slot == 1) {
    return el_str_concat(stem, EL_STR("est"));
  }
  if (slot == 2) {
    return el_str_concat(stem, EL_STR("eth"));
  }
  if (slot == 3) {
    return el_str_concat(stem, EL_STR("en"));
  }
  if (slot == 4) {
    return el_str_concat(stem, EL_STR("en"));
  }
  return el_str_concat(stem, EL_STR("en"));
  return 0;
}

el_val_t enm_weak_past(el_val_t stem, el_val_t slot) {
  if (slot == 0) {
    return el_str_concat(stem, EL_STR("ede"));
  }
  if (slot == 1) {
    return el_str_concat(stem, EL_STR("edest"));
  }
  if (slot == 2) {
    return el_str_concat(stem, EL_STR("ede"));
  }
  if (slot == 3) {
    return el_str_concat(stem, EL_STR("eden"));
  }
  if (slot == 4) {
    return el_str_concat(stem, EL_STR("eden"));
  }
  return el_str_concat(stem, EL_STR("eden"));
  return 0;
}

el_val_t enm_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number) {
  el_val_t v = enm_map_canonical(verb);
  el_val_t slot = enm_slot(person, number);
  if (str_eq(v, EL_STR("been"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return enm_been_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return enm_been_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("haven"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return enm_haven_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return enm_haven_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("goon"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return enm_goon_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return enm_goon_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("seen"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return enm_seen_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return enm_seen_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("seyen"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return enm_seyen_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return enm_seyen_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("comen"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return enm_comen_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return enm_comen_past(slot);
    }
    return v;
  }
  if (str_eq(v, EL_STR("maken"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return enm_maken_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return enm_maken_past(slot);
    }
    return v;
  }
  el_val_t stem = enm_weak_stem(v);
  if (str_eq(tense, EL_STR("present"))) {
    return enm_weak_present(stem, slot);
  }
  if (str_eq(tense, EL_STR("past"))) {
    return enm_weak_past(stem, slot);
  }
  return v;
  return 0;
}

el_val_t enm_irregular_plural(el_val_t noun) {
  if (str_eq(noun, EL_STR("man"))) {
    return EL_STR("men");
  }
  if (str_eq(noun, EL_STR("woman"))) {
    return EL_STR("wommen");
  }
  if (str_eq(noun, EL_STR("child"))) {
    return EL_STR("children");
  }
  if (str_eq(noun, EL_STR("ox"))) {
    return EL_STR("oxen");
  }
  if (str_eq(noun, EL_STR("foot"))) {
    return EL_STR("feet");
  }
  if (str_eq(noun, EL_STR("tooth"))) {
    return EL_STR("teeth");
  }
  if (str_eq(noun, EL_STR("goose"))) {
    return EL_STR("gees");
  }
  if (str_eq(noun, EL_STR("mouse"))) {
    return EL_STR("mees");
  }
  if (str_eq(noun, EL_STR("louse"))) {
    return EL_STR("lees");
  }
  return EL_STR("");
  return 0;
}

el_val_t enm_make_plural(el_val_t noun) {
  el_val_t irreg = enm_irregular_plural(noun);
  if (!str_eq(irreg, EL_STR(""))) {
    return irreg;
  }
  if (enm_str_ends(noun, EL_STR("e"))) {
    return el_str_concat(noun, EL_STR("s"));
  }
  return el_str_concat(noun, EL_STR("es"));
  return 0;
}

el_val_t enm_decline(el_val_t noun, el_val_t gram_case, el_val_t number) {
  if (str_eq(number, EL_STR("plural"))) {
    return enm_make_plural(noun);
  }
  if (str_eq(gram_case, EL_STR("genitive"))) {
    return el_str_concat(noun, EL_STR("es"));
  }
  return noun;
  return 0;
}

el_val_t enm_is_vowel_initial(el_val_t s) {
  el_val_t c = enm_first_char(s);
  if (str_eq(c, EL_STR("a"))) {
    return 1;
  }
  if (str_eq(c, EL_STR("e"))) {
    return 1;
  }
  if (str_eq(c, EL_STR("i"))) {
    return 1;
  }
  if (str_eq(c, EL_STR("o"))) {
    return 1;
  }
  if (str_eq(c, EL_STR("u"))) {
    return 1;
  }
  return 0;
  return 0;
}

el_val_t enm_indef_article(el_val_t noun_phrase) {
  if (enm_is_vowel_initial(noun_phrase)) {
    return EL_STR("an");
  }
  return EL_STR("a");
  return 0;
}

el_val_t enm_noun_phrase(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite) {
  el_val_t form = enm_decline(noun, gram_case, number);
  if (str_eq(definite, EL_STR("true"))) {
    return el_str_concat(EL_STR("the "), form);
  }
  if (str_eq(number, EL_STR("plural"))) {
    return form;
  }
  el_val_t art = enm_indef_article(form);
  return el_str_concat(el_str_concat(art, EL_STR(" ")), form);
  return 0;
}

