Skip to content

Commit

Permalink
[prakriya] Support upasarga/sanadi lists in WASM
Browse files Browse the repository at this point in the history
Additionally:

- Add support for 8.4.59
- Add more interesting krdanta tests.
- Fix various small bugs when using sanAdi dhatus with krt and sup
  pratyayas.
  • Loading branch information
akprasad committed Dec 9, 2024
1 parent b585bb4 commit 30bbe8f
Show file tree
Hide file tree
Showing 23 changed files with 720 additions and 503 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion vidyut-prakriya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ serde = { version = "1.0.150", features = ["derive"] }
sha2 = "0.10.6"
sha256 = "1.1.1"
rayon = "1.6.1"
wasm-bindgen = "0.2"
wasm-bindgen = { version = "0.2", features = ["serde-serialize"]}
serde-wasm-bindgen = "0.4"
console_error_panic_hook = "0.1.7"
rustc-hash = "2.0.0"

[features]
default = ["serde"]
serde = []

[dev-dependencies]
criterion = "0.5.1"
test_utils = { path = "test_utils" }
Expand Down
67 changes: 41 additions & 26 deletions vidyut-prakriya/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,23 @@ coverage:
create_test_files:
cargo build --release
../target/release/create_tinantas --prayoga kartari > test-files/tinantas-basic-kartari.csv
../target/release/create_tinantas --sanadi Ric --prayoga kartari > test-files/tinantas-nic-kartari.csv
../target/release/create_tinantas --sanadi san --prayoga kartari > test-files/tinantas-san-kartari.csv
../target/release/create_tinantas --sanadi yaN --prayoga kartari > test-files/tinantas-yan-kartari.csv
../target/release/create_tinantas --sanadi yaNluk --prayoga kartari > test-files/tinantas-yan-luk-kartari.csv
../target/release/create_tinantas --sanadi san,Ric --prayoga kartari > test-files/tinantas-san-nic-kartari.csv
../target/release/create_tinantas --sanadi Ric,san --prayoga kartari > test-files/tinantas-nic-san-kartari.csv
../target/release/create_tinantas --prayoga kartari --sanadi Ric > test-files/tinantas-nic-kartari.csv
../target/release/create_tinantas --prayoga kartari --sanadi san > test-files/tinantas-san-kartari.csv
../target/release/create_tinantas --prayoga kartari --sanadi yaN > test-files/tinantas-yan-kartari.csv
../target/release/create_tinantas --prayoga kartari --sanadi yaNluk > test-files/tinantas-yan-luk-kartari.csv
../target/release/create_tinantas --prayoga kartari --sanadi san,Ric > test-files/tinantas-san-nic-kartari.csv
../target/release/create_tinantas --prayoga kartari --sanadi Ric,san > test-files/tinantas-nic-san-kartari.csv
../target/release/create_tinantas --prayoga karmani > test-files/tinantas-basic-karmani.csv
../target/release/create_tinantas --sanadi Ric --prayoga karmani > test-files/tinantas-nic-karmani.csv
../target/release/create_tinantas --sanadi san --prayoga karmani > test-files/tinantas-san-karmani.csv
../target/release/create_tinantas --sanadi yaN --prayoga karmani > test-files/tinantas-yan-karmani.csv
../target/release/create_tinantas --sanadi san,Ric --prayoga karmani > test-files/tinantas-san-nic-karmani.csv
../target/release/create_tinantas --sanadi Ric,san --prayoga karmani > test-files/tinantas-nic-san-karmani.csv
../target/release/create_krdantas --krt ktvA > test-files/krdantas-ktvA.csv
../target/release/create_krdantas --krt kta > test-files/krdantas-kta.csv
../target/release/create_tinantas --prayoga karmani --sanadi Ric > test-files/tinantas-nic-karmani.csv
../target/release/create_tinantas --prayoga karmani --sanadi san > test-files/tinantas-san-karmani.csv
../target/release/create_tinantas --prayoga karmani --sanadi yaN > test-files/tinantas-yan-karmani.csv
../target/release/create_tinantas --prayoga karmani --sanadi san,Ric > test-files/tinantas-san-nic-karmani.csv
../target/release/create_tinantas --prayoga karmani --sanadi Ric,san > test-files/tinantas-nic-san-karmani.csv
../target/release/create_krdantas > test-files/krdantas-basic.csv
../target/release/create_krdantas --sanadi Ric > test-files/krdantas-nic.csv
../target/release/create_krdantas --sanadi san > test-files/krdantas-san.csv
../target/release/create_krdantas --sanadi yaN > test-files/krdantas-yan.csv
../target/release/create_krdantas --sanadi yaNluk > test-files/krdantas-yan-luk.csv
../target/release/create_dhatus > test-files/dhatus.csv

# Runs a full evaluation over all forms generated by vidyut-prakriya. `hash` is
Expand All @@ -63,7 +66,7 @@ test_tinantas:
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-nic-kartari.csv \
--hash "0df6d10e8ac4628ccc10a719d1e5022cb364575ce6c10e26ab1e04ac3114b163"
--hash "503fe8ace23a847f8ea02c23263112a0bbb484aa1971fdba5a811a5096dd2681"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-san-kartari.csv \
Expand All @@ -79,19 +82,19 @@ test_tinantas:
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-san-nic-kartari.csv \
--hash "4379daf9d118797bd085b03d57cac210ada84ece3972c0d4458221cd2098b0f7"
--hash "c5977158acab3b00601ef4b78c29c045a6d9586fb9aa0502856fdab73e630bf5"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-nic-san-kartari.csv \
--hash "309d08d11718a2fc54e61d9edb976d7fe09aca13ec88a75879f1f5803cf69aca"
--hash "c292a9b75da8feaa4c4f318900bc257ab703ece640f691ac9c7ac52a683679d2"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-basic-karmani.csv \
--hash "a30032f2b32cd8008c91c95b7e03e3e9f11386179bb70fd26f5eccf8748d0afe"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-nic-karmani.csv \
--hash "5de5d1f252df630448bb2af0403c15b5f31b398a76234882e47ebeb34f94bf4f"
--hash "7a5358df11a14351e771b917742a83df10d4013e844419ddeadc91c8cc206548"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-san-karmani.csv \
Expand All @@ -103,29 +106,41 @@ test_tinantas:
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-san-nic-karmani.csv \
--hash "085f3486d19af249b955f1a0f534cec5f3cdd953bf3f6307a612f423313af207"
--hash "8c3925ad25154ad44ceb3f726843fcb0a453a71b93fb2d4a1c55d20a905dd292"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-nic-san-karmani.csv \
--hash "e1c50923278a24c447e2a17552dc882ccefe613c820a8fd4ee60ee24658f0e01"
--hash "d679a4a2d3487a99899cb2793c8d890f570016a60d8b5460a62cde92a0dfec76"

test_krdantas:
cargo build --release --bin test_results
../target/release/test_results \
--data-type krdanta \
--test-cases test-files/krdantas-ktvA.csv \
--hash "1f41354393e7490afe73a2cb7f873bc75c6495a6188f739f5ecac08a2ee725a4"
--test-cases test-files/krdantas-basic.csv \
--hash "c3daa8186c4bbe2e9a34f0aeeb8af9c7c81e823f9c4925e0c4cbfef04cf89c2a"
../target/release/test_results \
--data-type krdanta \
--test-cases test-files/krdantas-kta.csv \
--hash "aa72a9d4cec0f82248de228108ab881789962c12fe0094134657a55340822247"
--test-cases test-files/krdantas-nic.csv \
--hash "961375118a3cd6ed548fa316885e3c4cdb2efb08e31648076fd45669d7019dbb"
../target/release/test_results \
--data-type krdanta \
--test-cases test-files/krdantas-san.csv \
--hash "d66a8b1f9931325f9a66f57599226464e7ba42e7a8b21a8e7faec08bd0768593"
../target/release/test_results \
--data-type krdanta \
--test-cases test-files/krdantas-yan.csv \
--hash "7b1eb0ddd6e359aee21bb111983d61d5e997f09d09a27f1e4fa19b3d859edfb0"
../target/release/test_results \
--data-type krdanta \
--test-cases test-files/krdantas-yan-luk.csv \
--hash "9c5f46647f8c8da61745515e8b948150fb691da806b8f70f774205ca8394307d"

test_dhatus:
cargo build --release --bin test_results
../target/release/test_results \
--data-type dhatu \
--test-cases test-files/dhatus.csv \
--hash "398f83acdac10be88098bf89284369e08a714471a4007c42f694a8fb685d2d4f"
--hash "813ceb212c3d500ed4e4a2b892c8c72504be8c0f3602831135698bcd54e62b5f"


test_subantas:
Expand Down Expand Up @@ -161,14 +176,14 @@ docs:
# This is a release build. Creating it will be slower but smaller and (perhaps)
# more performant.
wasm_release:
wasm-pack build --target web --release
wasm-pack build --target web --release -- --features serde

# Builds WASM module and JS wrapper (pkg/vidyut_prakriya.js).
#
# This is a dev build. Creating it will be faster, but size and performance
# will not be optimized.
wasm_dev:
wasm-pack build --target web
wasm-pack build --target web -- --features serde

# Builds a simple web debugger.
debugger:
Expand Down
2 changes: 1 addition & 1 deletion vidyut-prakriya/scripts/run-debugger.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fi
# build by default instead. Creating this release build is slow, but the debug
# build seems to have issues with enum parsing. So, stick with the release
# build.
wasm-pack build --target web --release
wasm-pack build --target web --release -- --features serde
mkdir -p www/static/wasm && cp pkg/* www/static/wasm
mkdir -p www/static/data && cp data/* www/static/data
cd www && python3 -m http.server
5 changes: 3 additions & 2 deletions vidyut-prakriya/src/angasya.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,9 +777,10 @@ pub fn run_before_dvitva(p: &mut Prakriya, is_lun: bool) -> Option<()> {

// Check explicitly that ni-pratyaya is the *last* term so that we don't try applying these
// rules again after adding a tin/krt pratyaya.
let ni = p.terms().last()?;
let is_ni = p.has(i + 1, |t| t.is_ni_pratyaya());
let is_dhatu = p.terms().last().map_or(false, |t| t.is_dhatu());

if !ni.is_ni_pratyaya() {
if !is_ni || !is_dhatu {
return None;
}

Expand Down
1 change: 1 addition & 0 deletions vidyut-prakriya/src/args/dhatu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl Sanadi {
matches!(self, kAmyac | kyaN | kyac)
}

/// Returns the *aupadeśika* form of this *pratyaya*.
pub(crate) fn aupadeshika(&self) -> &'static str {
self.as_str()
}
Expand Down
3 changes: 2 additions & 1 deletion vidyut-prakriya/src/args/krt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ impl Krt {
}

impl BaseKrt {
pub(crate) fn aupadeshika(&self) -> &'static str {
/// Returns the *aupadeśika* form of this *pratyaya*.
pub fn aupadeshika(&self) -> &'static str {
self.as_str()
}
}
Expand Down
3 changes: 2 additions & 1 deletion vidyut-prakriya/src/args/taddhita.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,8 @@ enum_boilerplate!(Taddhita, {
});

impl Taddhita {
pub(crate) fn aupadeshika(&self) -> &'static str {
/// Returns the *aupadeśika* form of this *pratyaya*.
pub fn aupadeshika(&self) -> &'static str {
self.as_str()
}
}
Expand Down
3 changes: 2 additions & 1 deletion vidyut-prakriya/src/args/tin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ enum_boilerplate!(Lakara, {
});

impl Lakara {
pub(crate) fn aupadeshika(&self) -> &'static str {
/// Returns the *aupadeśika* form of this *pratyaya*.
pub fn aupadeshika(&self) -> &'static str {
use Lakara::*;
match self {
Lat => "la~w",
Expand Down
41 changes: 25 additions & 16 deletions vidyut-prakriya/src/ashtadhyayi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::atmanepada;
use crate::caching::{calculate_hash, Cache};
use crate::core::errors::*;
use crate::core::prakriya_stack::PrakriyaStack;
use crate::core::{Prakriya, PrakriyaTag as PT, Tag, Term};
use crate::core::{Prakriya, PrakriyaTag as PT, Stage, Tag, Term};
use crate::dhatu_karya;
use crate::dvitva;
use crate::it_agama;
Expand All @@ -56,7 +56,6 @@ use crate::tin_pratyaya;
use crate::tripadi;
use crate::uttarapade;
use crate::vikarana;
use crate::RuleChoice;
use core::cell::RefCell;

/// Enough to hold a term changed by up to 4 optional rules (2^4 = 16), plus an extra 2x seems to
Expand Down Expand Up @@ -100,9 +99,10 @@ impl MainArgs {
}
}

#[derive(Debug)]
enum CachedPrakriya {
Good(Prakriya),
Fail(Vec<RuleChoice>),
Fail(Prakriya),
}

// Returns whether this prakriya and its args trigger the `saMscaNoH` condition, which affects
Expand Down Expand Up @@ -149,7 +149,10 @@ fn prepare_dhatu(p: &mut Prakriya, dhatu: &Dhatu, args: MainArgs) -> Result<()>
if let Some(val) = cache.read(&cache_key) {
match val {
CP::Good(val) => *p = val.clone(),
CP::Fail(choices) => cache_ret = Err(Error::Abort(choices.clone())),
CP::Fail(val) => {
*p = val.clone();
cache_ret = Err(Error::Abort(p.rule_choices().to_vec()));
}
}
cache_hit = true;
}
Expand All @@ -161,7 +164,7 @@ fn prepare_dhatu(p: &mut Prakriya, dhatu: &Dhatu, args: MainArgs) -> Result<()>
let ret = prepare_dhatu_inner(p, dhatu, args);
let payload = match ret {
Ok(()) => CP::Good(p.clone()),
Err(_) => CP::Fail(p.rule_choices().to_vec()),
Err(_) => CP::Fail(p.clone()),
};
CACHE.with_borrow_mut(|cache| cache.write(cache_key, payload));
ret
Expand Down Expand Up @@ -282,7 +285,10 @@ fn prepare_pratipadika(p: &mut Prakriya, pratipadika: &Pratipadika) -> Result<()
if let Some(payload) = cache.read(&cache_key) {
match payload {
CP::Good(val) => *p = val.clone(),
CP::Fail(choices) => cache_ret = Err(Error::Abort(choices.clone())),
CP::Fail(val) => {
*p = val.clone();
cache_ret = Err(Error::Abort(p.rule_choices().to_vec()));
}
}
cache_hit = true;
}
Expand All @@ -294,7 +300,7 @@ fn prepare_pratipadika(p: &mut Prakriya, pratipadika: &Pratipadika) -> Result<()
let ret = prepare_pratipadika_inner(p, pratipadika);
let payload = match ret {
Ok(()) => CP::Good(p.clone()),
Err(_) => CP::Fail(p.rule_choices().to_vec()),
Err(_) => CP::Fail(p.clone()),
};
CACHE.with_borrow_mut(|cache| cache.write(cache_key, payload));
ret
Expand Down Expand Up @@ -528,6 +534,17 @@ fn run_main_rules(p: &mut Prakriya, dhatu_args: Option<&Dhatu>, args: MainArgs)
samasa::try_sup_luk(p);
misc::run_pad_adi(p);

if p.stage != Stage::Vakya {
// Add strI-pratyayas. This should be done after adding the sup-pratyaya so that we satisfy the
// following constraints:
//
// - su~ must be added before sup-luk (7.1.23)
// - sup-luk must be checked before changing adas to ada (7.2.102)
// - ada must be in place before running stritva (4.1.4)
angasya::run_before_stritva(p);
stritva::run(p);
}

angasya::maybe_do_jha_adesha(p);

ac_sandhi::try_sup_sandhi_before_angasya(p);
Expand Down Expand Up @@ -628,15 +645,6 @@ pub fn derive_subanta(mut prakriya: Prakriya, args: &Subanta) -> Result<Prakriya

samasa::run_rules_for_avyayibhava(p);

// Add strI-pratyayas. This should be done after adding the sup-pratyaya so that we satisfy the
// following constraints:
//
// - su~ must be added before sup-luk (7.1.23)
// - sup-luk must be checked before changing adas to ada (7.2.102)
// - ada must be in place before running stritva (4.1.4)
angasya::run_before_stritva(p);
stritva::run(p);

run_main_rules(p, None, MainArgs::default());
tripadi::run(p);

Expand Down Expand Up @@ -766,6 +774,7 @@ pub fn derive_vakya(mut prakriya: Prakriya, padas: &[Pada]) -> Result<Prakriya>
}

let p = &mut prakriya;
p.stage = Stage::Vakya;
samjna::try_pragrhya_rules(p);
run_main_rules(p, None, MainArgs::default());
tripadi::run(p);
Expand Down
Loading

0 comments on commit 30bbe8f

Please sign in to comment.