#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 sux_str_ends(el_val_t s, el_val_t suf);
el_val_t sux_str_drop_last(el_val_t s, el_val_t n);
el_val_t sux_str_last_char(el_val_t s);
el_val_t sux_str_last2(el_val_t s);
el_val_t sux_slot(el_val_t person, el_val_t number);
el_val_t sux_ergative_suffix(el_val_t person, el_val_t number);
el_val_t sux_absolutive_suffix(el_val_t person, el_val_t number);
el_val_t sux_map_canonical(el_val_t verb);
el_val_t sux_personal_suffix(el_val_t slot);
el_val_t sux_me_present(el_val_t slot);
el_val_t sux_me_past(el_val_t slot);
el_val_t sux_dug4_present(el_val_t slot);
el_val_t sux_dug4_past(el_val_t slot);
el_val_t sux_du_present(el_val_t slot);
el_val_t sux_du_past(el_val_t slot);
el_val_t sux_igibar_present(el_val_t slot);
el_val_t sux_igibar_past(el_val_t slot);
el_val_t sux_ak_present(el_val_t slot);
el_val_t sux_ak_past(el_val_t slot);
el_val_t sux_tum2_present(el_val_t slot);
el_val_t sux_tum2_past(el_val_t slot);
el_val_t sux_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
el_val_t sux_is_animate(el_val_t noun);
el_val_t sux_case_suffix(el_val_t gram_case);
el_val_t sux_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
el_val_t sux_noun_phrase(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite);
el_val_t sux_verb_chain(el_val_t agent, el_val_t verb, el_val_t patient, el_val_t tense);
el_val_t sux_realize_sentence(el_val_t intent, el_val_t agent, el_val_t predicate, el_val_t patient, el_val_t tense);

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

el_val_t sux_str_drop_last(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 sux_str_last_char(el_val_t s) {
  el_val_t n = str_len(s);
  if (n == 0) {
    return EL_STR("");
  }
  return str_slice(s, (n - 1), n);
  return 0;
}

el_val_t sux_str_last2(el_val_t s) {
  el_val_t n = str_len(s);
  if (n < 2) {
    return s;
  }
  return str_slice(s, (n - 2), n);
  return 0;
}

el_val_t sux_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 sux_ergative_suffix(el_val_t person, el_val_t number) {
  if (str_eq(person, EL_STR("first"))) {
    if (str_eq(number, EL_STR("singular"))) {
      return EL_STR("-en");
    }
    return EL_STR("-enden");
  }
  if (str_eq(person, EL_STR("second"))) {
    if (str_eq(number, EL_STR("singular"))) {
      return EL_STR("-en");
    }
    return EL_STR("-enzen");
  }
  if (str_eq(number, EL_STR("singular"))) {
    return EL_STR("-e");
  }
  return EL_STR("-e\xc5\xa1");
  return 0;
}

el_val_t sux_absolutive_suffix(el_val_t person, el_val_t number) {
  if (str_eq(person, EL_STR("first"))) {
    if (str_eq(number, EL_STR("singular"))) {
      return EL_STR("-en");
    }
    return EL_STR("-enden");
  }
  if (str_eq(person, EL_STR("second"))) {
    if (str_eq(number, EL_STR("singular"))) {
      return EL_STR("-en");
    }
    return EL_STR("-enzen");
  }
  return EL_STR("");
  return 0;
}

el_val_t sux_map_canonical(el_val_t verb) {
  if (str_eq(verb, EL_STR("be"))) {
    return EL_STR("me");
  }
  if (str_eq(verb, EL_STR("say"))) {
    return EL_STR("dug4");
  }
  if (str_eq(verb, EL_STR("go"))) {
    return EL_STR("du");
  }
  if (str_eq(verb, EL_STR("see"))) {
    return EL_STR("igi-bar");
  }
  if (str_eq(verb, EL_STR("do"))) {
    return EL_STR("ak");
  }
  if (str_eq(verb, EL_STR("make"))) {
    return EL_STR("ak");
  }
  if (str_eq(verb, EL_STR("bring"))) {
    return EL_STR("tum2");
  }
  if (str_eq(verb, EL_STR("build"))) {
    return EL_STR("d\xc3\xb9");
  }
  if (str_eq(verb, EL_STR("give"))) {
    return EL_STR("\xc5\xa1um2");
  }
  if (str_eq(verb, EL_STR("know"))) {
    return EL_STR("zu");
  }
  if (str_eq(verb, EL_STR("hear"))) {
    return EL_STR("\xc4\x9d""e\xc5\xa1tug2 \xc4\x9d""ar");
  }
  if (str_eq(verb, EL_STR("love"))) {
    return EL_STR("ki-a\xc4\x9d""2");
  }
  if (str_eq(verb, EL_STR("sit"))) {
    return EL_STR("tu\xc5\xa1");
  }
  if (str_eq(verb, EL_STR("stand"))) {
    return EL_STR("gub");
  }
  if (str_eq(verb, EL_STR("come"))) {
    return EL_STR("\xc4\x9d""en");
  }
  if (str_eq(verb, EL_STR("eat"))) {
    return EL_STR("gu7");
  }
  if (str_eq(verb, EL_STR("drink"))) {
    return EL_STR("na\xc4\x9d");
  }
  if (str_eq(verb, EL_STR("write"))) {
    return EL_STR("sar");
  }
  return verb;
  return 0;
}

el_val_t sux_personal_suffix(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("en");
  }
  if (slot == 1) {
    return EL_STR("en");
  }
  if (slot == 2) {
    return EL_STR("");
  }
  if (slot == 3) {
    return EL_STR("enden");
  }
  if (slot == 4) {
    return EL_STR("enzen");
  }
  return EL_STR("e\xc5\xa1");
  return 0;
}

el_val_t sux_me_present(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("me-en");
  }
  if (slot == 1) {
    return EL_STR("me-en");
  }
  if (slot == 2) {
    return EL_STR("");
  }
  if (slot == 3) {
    return EL_STR("me-en-d\xc3\xa8");
  }
  if (slot == 4) {
    return EL_STR("me-en-z\xc3\xa8-en");
  }
  return EL_STR("me-e\xc5\xa1");
  return 0;
}

el_val_t sux_me_past(el_val_t slot) {
  if (slot == 0) {
    return EL_STR("ba-me-en");
  }
  if (slot == 1) {
    return EL_STR("ba-me-en");
  }
  if (slot == 2) {
    return EL_STR("ba-me");
  }
  if (slot == 3) {
    return EL_STR("ba-me-en-d\xc3\xa8");
  }
  if (slot == 4) {
    return EL_STR("ba-me-en-z\xc3\xa8-en");
  }
  return EL_STR("ba-me-e\xc5\xa1");
  return 0;
}

el_val_t sux_dug4_present(el_val_t slot) {
  el_val_t suf = sux_personal_suffix(slot);
  if (str_eq(suf, EL_STR(""))) {
    return EL_STR("e");
  }
  return el_str_concat(EL_STR("e-"), suf);
  return 0;
}

el_val_t sux_dug4_past(el_val_t slot) {
  el_val_t suf = sux_personal_suffix(slot);
  if (str_eq(suf, EL_STR(""))) {
    return EL_STR("mu-un-dug4");
  }
  return el_str_concat(EL_STR("mu-un-dug4-"), suf);
  return 0;
}

el_val_t sux_du_present(el_val_t slot) {
  el_val_t suf = sux_personal_suffix(slot);
  if (str_eq(suf, EL_STR(""))) {
    return EL_STR("i-du");
  }
  return el_str_concat(EL_STR("i-du-"), suf);
  return 0;
}

el_val_t sux_du_past(el_val_t slot) {
  el_val_t suf = sux_personal_suffix(slot);
  if (str_eq(suf, EL_STR(""))) {
    return EL_STR("mu-un-du");
  }
  return el_str_concat(EL_STR("mu-un-du-"), suf);
  return 0;
}

el_val_t sux_igibar_present(el_val_t slot) {
  el_val_t suf = sux_personal_suffix(slot);
  if (str_eq(suf, EL_STR(""))) {
    return EL_STR("igi i-bar");
  }
  return el_str_concat(EL_STR("igi i-bar-"), suf);
  return 0;
}

el_val_t sux_igibar_past(el_val_t slot) {
  el_val_t suf = sux_personal_suffix(slot);
  if (str_eq(suf, EL_STR(""))) {
    return EL_STR("igi mu-un-bar");
  }
  return el_str_concat(EL_STR("igi mu-un-bar-"), suf);
  return 0;
}

el_val_t sux_ak_present(el_val_t slot) {
  el_val_t suf = sux_personal_suffix(slot);
  if (str_eq(suf, EL_STR(""))) {
    return EL_STR("i-ak");
  }
  return el_str_concat(EL_STR("i-ak-"), suf);
  return 0;
}

el_val_t sux_ak_past(el_val_t slot) {
  el_val_t suf = sux_personal_suffix(slot);
  if (str_eq(suf, EL_STR(""))) {
    return EL_STR("mu-un-ak");
  }
  return el_str_concat(EL_STR("mu-un-ak-"), suf);
  return 0;
}

el_val_t sux_tum2_present(el_val_t slot) {
  el_val_t suf = sux_personal_suffix(slot);
  if (str_eq(suf, EL_STR(""))) {
    return EL_STR("i-tum2");
  }
  return el_str_concat(EL_STR("i-tum2-"), suf);
  return 0;
}

el_val_t sux_tum2_past(el_val_t slot) {
  el_val_t suf = sux_personal_suffix(slot);
  if (str_eq(suf, EL_STR(""))) {
    return EL_STR("mu-un-tum2");
  }
  return el_str_concat(EL_STR("mu-un-tum2-"), suf);
  return 0;
}

el_val_t sux_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number) {
  el_val_t v = sux_map_canonical(verb);
  el_val_t slot = sux_slot(person, number);
  if (str_eq(v, EL_STR("me"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return sux_me_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return sux_me_past(slot);
    }
    return sux_me_present(slot);
  }
  if (str_eq(v, EL_STR("dug4"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return sux_dug4_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return sux_dug4_past(slot);
    }
    return sux_dug4_past(slot);
  }
  if (str_eq(v, EL_STR("du"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return sux_du_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return sux_du_past(slot);
    }
    return sux_du_past(slot);
  }
  if (str_eq(v, EL_STR("igi-bar"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return sux_igibar_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return sux_igibar_past(slot);
    }
    return sux_igibar_past(slot);
  }
  if (str_eq(v, EL_STR("ak"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return sux_ak_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return sux_ak_past(slot);
    }
    return sux_ak_past(slot);
  }
  if (str_eq(v, EL_STR("tum2"))) {
    if (str_eq(tense, EL_STR("present"))) {
      return sux_tum2_present(slot);
    }
    if (str_eq(tense, EL_STR("past"))) {
      return sux_tum2_past(slot);
    }
    return sux_tum2_past(slot);
  }
  el_val_t suf = sux_personal_suffix(slot);
  if (str_eq(tense, EL_STR("present"))) {
    if (str_eq(suf, EL_STR(""))) {
      return el_str_concat(EL_STR("i-"), v);
    }
    return el_str_concat(el_str_concat(el_str_concat(EL_STR("i-"), v), EL_STR("-")), suf);
  }
  if (str_eq(suf, EL_STR(""))) {
    return el_str_concat(EL_STR("mu-"), v);
  }
  return el_str_concat(el_str_concat(el_str_concat(EL_STR("mu-"), v), EL_STR("-")), suf);
  return 0;
}

el_val_t sux_is_animate(el_val_t noun) {
  if (sux_str_ends(noun, EL_STR("di\xc4\x9dir"))) {
    return 1;
  }
  if (sux_str_ends(noun, EL_STR("dingir"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("lugal"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("nin"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("en"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("ensi2"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("dumu"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("dam"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("ama"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("ad"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("a2-dam"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("lu2"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("munus"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("ur"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("sa\xc4\x9d"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("gudu4"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("sanga"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("ugula"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("dub-sar"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("nar"))) {
    return 1;
  }
  if (str_eq(noun, EL_STR("sukkal"))) {
    return 1;
  }
  if (sux_str_ends(noun, EL_STR("d-"))) {
    return 1;
  }
  return 0;
  return 0;
}

el_val_t sux_case_suffix(el_val_t gram_case) {
  if (str_eq(gram_case, EL_STR("absolutive"))) {
    return EL_STR("");
  }
  if (str_eq(gram_case, EL_STR("ergative"))) {
    return EL_STR("-e");
  }
  if (str_eq(gram_case, EL_STR("genitive"))) {
    return EL_STR("-ak");
  }
  if (str_eq(gram_case, EL_STR("dative"))) {
    return EL_STR("-ra");
  }
  if (str_eq(gram_case, EL_STR("locative"))) {
    return EL_STR("-a");
  }
  if (str_eq(gram_case, EL_STR("ablative"))) {
    return EL_STR("-ta");
  }
  if (str_eq(gram_case, EL_STR("comitative"))) {
    return EL_STR("-da");
  }
  if (str_eq(gram_case, EL_STR("equative"))) {
    return EL_STR("-gin");
  }
  if (str_eq(gram_case, EL_STR("terminative"))) {
    return EL_STR("-\xc5\xa1""e");
  }
  return EL_STR("");
  return 0;
}

el_val_t sux_decline(el_val_t noun, el_val_t gram_case, el_val_t number) {
  el_val_t csuf = sux_case_suffix(gram_case);
  if (str_eq(number, EL_STR("singular"))) {
    if (str_eq(gram_case, EL_STR("absolutive"))) {
      return noun;
    }
    el_val_t suf_len = str_len(csuf);
    el_val_t bare_suf = str_slice(csuf, 1, suf_len);
    return el_str_concat(noun, bare_suf);
  }
  el_val_t animate = sux_is_animate(noun);
  el_val_t plural_stem = EL_STR("");
  if (animate) {
    plural_stem = el_str_concat(noun, EL_STR("ene"));
  }
  if (!animate) {
    plural_stem = el_str_concat(noun, EL_STR("a"));
  }
  if (str_eq(gram_case, EL_STR("absolutive"))) {
    return plural_stem;
  }
  el_val_t suf_len2 = str_len(csuf);
  el_val_t bare_suf2 = str_slice(csuf, 1, suf_len2);
  return el_str_concat(plural_stem, bare_suf2);
  return 0;
}

el_val_t sux_noun_phrase(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite) {
  return sux_decline(noun, gram_case, number);
  return 0;
}

el_val_t sux_verb_chain(el_val_t agent, el_val_t verb, el_val_t patient, el_val_t tense) {
  el_val_t conjugated = sux_conjugate(verb, tense, EL_STR("third"), EL_STR("singular"));
  if (str_eq(patient, EL_STR(""))) {
    return el_str_concat(el_str_concat(agent, EL_STR(" ")), conjugated);
  }
  el_val_t agent_erg = el_str_concat(agent, EL_STR("e"));
  return el_str_concat(el_str_concat(el_str_concat(el_str_concat(agent_erg, EL_STR(" ")), patient), EL_STR(" ")), conjugated);
  return 0;
}

el_val_t sux_realize_sentence(el_val_t intent, el_val_t agent, el_val_t predicate, el_val_t patient, el_val_t tense) {
  if (str_eq(intent, EL_STR("assert"))) {
    return sux_verb_chain(agent, predicate, patient, tense);
  }
  if (str_eq(intent, EL_STR("question"))) {
    el_val_t assertion = sux_verb_chain(agent, predicate, patient, tense);
    return el_str_concat(assertion, EL_STR("-a"));
  }
  if (str_eq(intent, EL_STR("describe"))) {
    if (str_eq(patient, EL_STR(""))) {
      return el_str_concat(el_str_concat(el_str_concat(agent, EL_STR(" ")), predicate), EL_STR("-am3"));
    }
    return el_str_concat(el_str_concat(el_str_concat(agent, EL_STR(" ")), patient), EL_STR("-am3"));
  }
  return sux_verb_chain(agent, predicate, patient, tense);
  return 0;
}

