(* these should probably be part of the stdlib eventually *)
tuple-map := {func => {tup =>
  tuple-fold.<{xs => {x => concat.<xs, <func.x>>}}, <>, tup>
}};
flatten := {t => tuple-fold.<{ x => { xs => x xs } }, "", t>};

word := {w => flatten.(tuple-map.orthography.w)};
wd := concat.<begin, middle, end>;
middle := syll | concat.<syll, syll> | concat.<syll, syll, syll> | concat.<syll, syll, syll, syll>;
syll := <cons, vowel>;
end := <cons, vowel, final>
     | 2: <cons, ("eː" | "a"), final>
     | <"r"> | <"n"> ;

orthography :=
  { "z"  => "s"
  ; "s"  => "ss"
  ; "dz" => "z"
  ; "kʷ" => "qu"
  ; "k"  => "c"
  ; "x"  => "ch"
  ; "ʎ"  => "lh"
  ; "ɲ"  => "nh"
  ; "ʃ"  => "sc"
  ; "ts" => "tz"
  ; "aː" => "á"
  ; "eː" => "é"
  ; "iː" => "í"
  ; "oː" => "ó"
  ; "uː" => "ú"
  ; "ɛ"  => "è"
  ; "ɔ"  => "o"
  ; "ɾ"  => "r"
  ; "tɾ" => "tr"
  ; "ɾt" => "rt"
  ; x    => x
  };

begin := <vowel> | <initcons, vowel>;

cons ::= b k d d x d f g g ɲ h j l l l ʎ m m m n n n
         p kʷ ɾ z z z s s s ʃ ʃ t t t tg tj ts ts tɾ ɾt v dz;
initcons ::= b k d x d f g ɲ h j l m n p kʷ ɾ s ʃ t v;
vowel := 4: commonvowel | uncommonvowel;
commonvowel ::= a i u e eː o;
uncommonvowel ::= aː ɛ oː ɔ iː uː;
final := "s" | 6: "";


fix wd;
puts (word.wd) " (pronounced /" (flatten.wd) "/)";