testing performance

This commit is contained in:
Jim 2025-10-20 20:39:48 -04:00
parent a587f1be24
commit bff45e9215
Signed by: jim
GPG key ID: 3236D2F059A7C0AC
9 changed files with 1010926 additions and 234 deletions

500
Cargo.lock generated
View file

@ -17,6 +17,36 @@ version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]]
name = "anes"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
[[package]]
name = "anstyle"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78"
[[package]]
name = "autocfg"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]]
name = "bumpalo"
version = "3.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43"
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.2.41" version = "1.2.41"
@ -40,13 +70,135 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6cd3ef0a728f561e3b4157213d178ae7523cbc405423f862da757447588ae103" checksum = "6cd3ef0a728f561e3b4157213d178ae7523cbc405423f862da757447588ae103"
dependencies = [ dependencies = [
"hashbrown", "hashbrown",
"regex-automata", "regex-automata 0.3.9",
"serde", "serde",
"stacker", "stacker",
"unicode-ident", "unicode-ident",
"unicode-segmentation", "unicode-segmentation",
] ]
[[package]]
name = "ciborium"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
dependencies = [
"ciborium-io",
"ciborium-ll",
"serde",
]
[[package]]
name = "ciborium-io"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
[[package]]
name = "ciborium-ll"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
dependencies = [
"ciborium-io",
"half",
]
[[package]]
name = "clap"
version = "4.5.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c2cfd7bf8a6017ddaa4e32ffe7403d547790db06bd171c1c53926faab501623"
dependencies = [
"clap_builder",
]
[[package]]
name = "clap_builder"
version = "4.5.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a4c05b9e80c5ccd3a7ef080ad7b6ba7d6fc00a985b8b157197075677c82c7a0"
dependencies = [
"anstyle",
"clap_lex",
]
[[package]]
name = "clap_lex"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
[[package]]
name = "criterion"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1c047a62b0cc3e145fa84415a3191f628e980b194c2755aa12300a4e6cbd928"
dependencies = [
"anes",
"cast",
"ciborium",
"clap",
"criterion-plot",
"itertools",
"num-traits",
"oorandom",
"plotters",
"rayon",
"regex",
"serde",
"serde_json",
"tinytemplate",
"walkdir",
]
[[package]]
name = "criterion-plot"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b1bcc0dc7dfae599d84ad0b1a55f80cde8af3725da8313b528da95ef783e338"
dependencies = [
"cast",
"itertools",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
[[package]]
name = "crunchy"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
[[package]]
name = "either"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]] [[package]]
name = "equivalent" name = "equivalent"
version = "1.0.2" version = "1.0.2"
@ -65,6 +217,17 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
[[package]]
name = "half"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b"
dependencies = [
"cfg-if",
"crunchy",
"zerocopy",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.15.5" version = "0.15.5"
@ -76,6 +239,21 @@ dependencies = [
"foldhash", "foldhash",
] ]
[[package]]
name = "itertools"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
[[package]] [[package]]
name = "jang" name = "jang"
version = "0.1.0" version = "0.1.0"
@ -84,10 +262,35 @@ dependencies = [
] ]
[[package]] [[package]]
name = "libc" name = "jang_cli"
version = "0.2.172" version = "0.1.0"
dependencies = [
"chumsky",
"criterion",
"jang",
]
[[package]]
name = "js-sys"
version = "0.3.81"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" checksum = "ec48937a97411dcb524a265206ccd4c90bb711fca92b2792c407f268825b9305"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.177"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
[[package]]
name = "log"
version = "0.4.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432"
[[package]] [[package]]
name = "memchr" name = "memchr"
@ -96,10 +299,59 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
[[package]] [[package]]
name = "proc-macro2" name = "num-traits"
version = "1.0.95" version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "oorandom"
version = "11.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
[[package]]
name = "plotters"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747"
dependencies = [
"num-traits",
"plotters-backend",
"plotters-svg",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "plotters-backend"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a"
[[package]]
name = "plotters-svg"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670"
dependencies = [
"plotters-backend",
]
[[package]]
name = "proc-macro2"
version = "1.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@ -115,13 +367,45 @@ dependencies = [
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.40" version = "1.0.41"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rayon"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "368f01d005bf8fd9b1206fb6fa653e6c4a81ceb1466406b81792d87c5677a58f"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]]
name = "regex"
version = "1.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata 0.4.13",
"regex-syntax 0.8.8",
]
[[package]] [[package]]
name = "regex-automata" name = "regex-automata"
version = "0.3.9" version = "0.3.9"
@ -130,7 +414,18 @@ checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
"regex-syntax", "regex-syntax 0.7.5",
]
[[package]]
name = "regex-automata"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax 0.8.8",
] ]
[[package]] [[package]]
@ -139,6 +434,33 @@ version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da"
[[package]]
name = "regex-syntax"
version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58"
[[package]]
name = "rustversion"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
[[package]]
name = "ryu"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.228" version = "1.0.228"
@ -169,6 +491,19 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "serde_json"
version = "1.0.145"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
"serde_core",
]
[[package]] [[package]]
name = "shlex" name = "shlex"
version = "1.3.0" version = "1.3.0"
@ -185,14 +520,14 @@ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
"psm", "psm",
"windows-sys", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.101" version = "2.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" checksum = "2a26dbd934e5451d21ef060c018dae56fc073894c5a7896f882928a76e6d081b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -200,10 +535,20 @@ dependencies = [
] ]
[[package]] [[package]]
name = "unicode-ident" name = "tinytemplate"
version = "1.0.18" version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "unicode-ident"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d"
[[package]] [[package]]
name = "unicode-segmentation" name = "unicode-segmentation"
@ -211,6 +556,100 @@ version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1da10c01ae9f1ae40cbfac0bac3b1e724b320abfcf52229f80b547c0d250e2d"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "671c9a5a66f49d8a47345ab942e2cb93c7d1d0339065d4f8139c486121b43b19"
dependencies = [
"bumpalo",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ca60477e4c59f5f2986c50191cd972e3a50d8a95603bc9434501cf156a9a119"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f07d2f20d4da7b26400c9f4a0511e6e0345b040694e8a75bd41d578fa4421d7"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bad67dc8b2a1a6e5448428adec4c3e84c43e561d8c9ee8a9e5aabeb193ec41d1"
dependencies = [
"unicode-ident",
]
[[package]]
name = "web-sys"
version = "0.3.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9367c417a924a74cae129e6a2ae3b47fabb1f8995595ab474029da749a8be120"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "winapi-util"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.59.0" version = "0.59.0"
@ -220,6 +659,15 @@ dependencies = [
"windows-targets", "windows-targets",
] ]
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
[[package]] [[package]]
name = "windows-targets" name = "windows-targets"
version = "0.52.6" version = "0.52.6"
@ -283,3 +731,23 @@ name = "windows_x86_64_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "zerocopy"
version = "0.8.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0894878a5fa3edfd6da3f88c4805f4c8558e2b996227a3d864f47fe11e38282c"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88d2b8d9c68ad2b9e4340d7832716a4d21a22a1154777ad56ea55c51a9cf3831"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View file

@ -1,7 +1,15 @@
[package] [package]
name = "jang" name = "jang_cli"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
chumsky = "0.11.1" chumsky = "0.11.1"
jang = { path = "crates/jang" }
[dev-dependencies]
criterion = "0.7.0"
[[bench]]
name = "parser"
harness = false

27
benches/parser.rs Normal file
View file

@ -0,0 +1,27 @@
use chumsky::Parser;
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use jang::*;
fn bench_parsers(c: &mut Criterion) {
let mut group = c.benchmark_group("Fibonacci");
for input_path in ["programs/medium.jang", "programs/test.jang"].iter() {
let src = std::fs::read_to_string(input_path).unwrap();
group.bench_with_input(BenchmarkId::new("Combo", input_path), &src, |b, src| {
b.iter(|| create_combo_parser().parse(src))
});
group.bench_with_input(BenchmarkId::new("Separate", input_path), &src, |b, src| {
b.iter(|| {
let lex_result = create_lexer().parse(src);
let Ok(lexed) = lex_result.into_result() else {
return;
};
create_parser().parse(&lexed);
})
});
}
group.finish();
}
criterion_group!(benches, bench_parsers);
criterion_main!(benches);

7
crates/jang/Cargo.toml Normal file
View file

@ -0,0 +1,7 @@
[package]
name = "jang"
version = "0.1.0"
edition = "2024"
[dependencies]
chumsky = "0.11.1"

381
crates/jang/src/lib.rs Normal file
View file

@ -0,0 +1,381 @@
use chumsky::prelude::*;
use std::{collections::HashMap, rc::Rc};
#[derive(Clone)]
pub enum IdentValue<'src> {
Var(f64),
Func {
name: &'src str,
params: Vec<&'src str>,
body: Rc<Expr<'src>>,
},
}
pub fn eval<'src>(
expr: &Expr<'src>,
vars: &mut HashMap<&'src str, Vec<IdentValue<'src>>>,
) -> Result<f64, String> {
fn bind_var<'src>(
vars: &mut HashMap<&'src str, Vec<IdentValue<'src>>>,
name: &'src str,
val: IdentValue<'src>,
) {
vars.entry(name).or_insert(Vec::new()).push(val);
}
fn unbind_var<'src>(vars: &mut HashMap<&'src str, Vec<IdentValue<'src>>>, name: &'src str) {
vars.get_mut(name).unwrap().pop();
}
match expr {
Expr::Num(x) => Ok(*x),
Expr::Neg(a) => Ok(-eval(a, vars)?),
Expr::Add(a, b) => Ok(eval(a, vars)? + eval(b, vars)?),
Expr::Sub(a, b) => Ok(eval(a, vars)? - eval(b, vars)?),
Expr::Mul(a, b) => Ok(eval(a, vars)? * eval(b, vars)?),
Expr::Div(a, b) => Ok(eval(a, vars)? / eval(b, vars)?),
Expr::Var(name) => {
let ident_value = vars.get(name).and_then(|v| v.last());
match ident_value {
Some(IdentValue::Var(v)) => Ok(*v),
Some(IdentValue::Func { .. }) => Err("Expected function to be called".to_string()),
None => Err(format!("Undefined variable `{}`", name)),
}
}
Expr::Let { name, rhs, then } => {
let val = eval(rhs, vars)?;
bind_var(vars, name, IdentValue::Var(val));
let output = eval(then, vars);
unbind_var(vars, name);
output
}
Expr::Call(name, args) => {
let ident_value = vars.get(name).and_then(|v| v.last()).cloned();
match ident_value {
Some(IdentValue::Func { name, params, body }) => {
if params.len() != args.len() {
Err(format!(
"expected {} arguments but only {} were provided to {}",
params.len(),
args.len(),
name
))
} else {
let binds = params
.iter()
.zip(args)
.map(|(param, arg)| (param, eval(arg, vars)))
.collect::<Vec<_>>();
for (param, bind) in binds {
bind_var(vars, param, IdentValue::Var(bind.unwrap()));
}
let return_value = eval(&body, vars);
for param in params.iter() {
unbind_var(vars, param);
}
return_value
}
}
Some(IdentValue::Var(_)) => {
Err("Variable cannot be called like a function".to_string())
}
None => Err(format!("Undefined variable `{}`", name)),
}
}
Expr::Fn {
name,
args,
body,
then,
} => {
vars.entry(name)
.or_insert(Vec::new())
.push(IdentValue::Func {
name,
params: args.clone(),
body: body.clone(),
});
let output = eval(then, vars);
vars.get_mut(name).unwrap().pop();
output
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Token<'src> {
KWLet,
KWFn,
Plus,
Minus,
FSlash,
Asterisk,
LParen,
RParen,
Semicolon,
Comma,
Equals,
Number(u64),
// todo: string internment
Ident(&'src str),
}
// todo - either hand write this or move it to `logos`. The perf is HORRIBLE (4.6 MB/s!!!!!! WOW!!!!)
// probably would want a different parsing lib too with how slow that is
pub fn create_lexer<'src>()
-> impl Parser<'src, &'src str, Vec<Token<'src>>, extra::Err<Rich<'src, char>>> {
choice((
text::ascii::keyword("let").padded().to(Token::KWLet),
text::ascii::keyword("fn").padded().to(Token::KWFn),
just('+').to(Token::Plus),
just('-').to(Token::Minus),
just('/').to(Token::FSlash),
just('*').to(Token::Asterisk),
just('(').to(Token::LParen),
just(')').to(Token::RParen),
just(';').to(Token::Semicolon),
just(',').to(Token::Comma),
just('=').to(Token::Equals),
text::int(10).map(|s: &str| Token::Number(s.parse().unwrap())),
text::ident().map(Token::Ident),
))
.padded()
.repeated()
.collect::<Vec<_>>()
}
#[derive(Debug, Clone)]
pub enum Expr<'src> {
Num(f64),
Var(&'src str),
Neg(Box<Expr<'src>>),
Add(Box<Expr<'src>>, Box<Expr<'src>>),
Sub(Box<Expr<'src>>, Box<Expr<'src>>),
Mul(Box<Expr<'src>>, Box<Expr<'src>>),
Div(Box<Expr<'src>>, Box<Expr<'src>>),
Call(&'src str, Vec<Expr<'src>>),
Let {
name: &'src str,
rhs: Box<Expr<'src>>,
then: Box<Expr<'src>>,
},
Fn {
name: &'src str,
args: Vec<&'src str>,
body: Rc<Expr<'src>>,
then: Box<Expr<'src>>,
},
}
//does both lex + parsing
pub fn create_combo_parser<'src>() -> impl Parser<'src, &'src str, Expr<'src>> {
let ident = text::ascii::ident().padded();
let expr = recursive(|expr| {
let int = text::int(10)
.map(|s: &str| Expr::Num(s.parse().unwrap()))
.padded();
let call = ident
.then(
expr.clone()
.separated_by(just(','))
.allow_trailing()
.collect::<Vec<_>>()
.delimited_by(just('('), just(')')),
)
.map(|(f, args)| Expr::Call(f, args));
let atom = int
.or(expr.delimited_by(just('('), just(')')))
.or(call)
.or(ident.map(Expr::Var))
.padded();
let op = |c| just(c).padded();
let unary = op('-')
.repeated()
.foldr(atom, |_op, rhs| Expr::Neg(Box::new(rhs)));
let product = unary.clone().foldl(
choice((
op('*').to(Expr::Mul as fn(_, _) -> _),
op('/').to(Expr::Div as fn(_, _) -> _),
))
.then(unary)
.repeated(),
|lhs, (op, rhs)| op(Box::new(lhs), Box::new(rhs)),
);
let sum = product.clone().foldl(
choice((
op('+').to(Expr::Add as fn(_, _) -> _),
op('-').to(Expr::Sub as fn(_, _) -> _),
))
.then(product)
.repeated(),
|lhs, (op, rhs)| op(Box::new(lhs), Box::new(rhs)),
);
sum
});
let decl = recursive(|decl| {
// ex: let x = 5*2
let decl_let = text::ascii::keyword("let")
.ignore_then(ident)
.then_ignore(just('='))
.then(expr.clone())
.then_ignore(just(';'))
.then(decl.clone())
.map(|((name, rhs), then)| Expr::Let {
name,
rhs: Box::new(rhs),
then: Box::new(then),
});
// ex: fn f x y = x + y
let decl_fn = text::ascii::keyword("fn")
.ignore_then(ident)
// collect params
.then(ident.repeated().collect::<Vec<_>>())
.then_ignore(just('='))
.then(expr.clone())
.then_ignore(just(';'))
.then(decl)
.map(|(((name, params), body), then)| Expr::Fn {
name,
args: params,
body: Rc::new(body),
then: Box::new(then),
});
// must parse 'let' keyword first so it doesn't become an identifier
decl_fn.or(decl_let).or(expr).padded()
});
decl
}
pub fn create_parser<'src>()
-> impl Parser<'src, &'src [Token<'src>], Expr<'src>, extra::Err<Rich<'src, Token<'src>>>> {
let ident = any().filter(|tok| matches!(tok, Token::Ident(_)));
let int = any()
.filter(|tok| matches!(tok, Token::Number(_)))
.map(|tok| match tok {
Token::Number(n) => Expr::Num(n as f64),
_ => unreachable!("alredy filtered on number"),
});
let expr = recursive(|expr| {
let call = ident
.then(
expr.clone()
.separated_by(just(Token::Comma))
.allow_trailing()
.collect::<Vec<_>>()
.delimited_by(just(Token::LParen), just(Token::RParen)),
)
.try_map(|(f, args), span| match f {
Token::Ident(f_name) => Ok(Expr::Call(f_name, args)),
_ => Err(Rich::custom(
span,
format!("expected identifier, found {:?}", f),
)),
});
let atom = int
.or(expr.delimited_by(just(Token::LParen), just(Token::RParen)))
.or(call)
.or(ident.map(|tok| match tok {
Token::Ident(str) => Expr::Var(str),
_ => unreachable!("alredy filtered on string"),
}));
let unary = just(Token::Minus)
.repeated()
.foldr(atom, |_op, rhs| Expr::Neg(Box::new(rhs)));
let product = unary.clone().foldl(
choice((
just(Token::Asterisk).to(Expr::Mul as fn(_, _) -> _),
just(Token::FSlash).to(Expr::Div as fn(_, _) -> _),
))
.then(unary)
.repeated(),
|lhs, (op, rhs)| op(Box::new(lhs), Box::new(rhs)),
);
let sum = product.clone().foldl(
choice((
just(Token::Plus).to(Expr::Add as fn(_, _) -> _),
just(Token::Minus).to(Expr::Sub as fn(_, _) -> _),
))
.then(product)
.repeated(),
|lhs, (op, rhs)| op(Box::new(lhs), Box::new(rhs)),
);
sum
});
let decl = recursive(|decl| {
// ex: let x = 5*2
let decl_let = just(Token::KWLet)
.ignore_then(ident)
.then_ignore(just(Token::Equals))
.then(expr.clone())
.then_ignore(just(Token::Semicolon))
.then(decl.clone())
.try_map(|((name, rhs), then), span| match name {
Token::Ident(str) => Ok(Expr::Let {
name: str,
rhs: Box::new(rhs),
then: Box::new(then),
}),
_ => Err(Rich::custom(
span,
format!("expected identifier, found {:?}", name),
)),
});
// ex: fn f x y = x + y
let decl_fn = just(Token::KWFn)
.ignore_then(ident)
// collect params
.then(
ident
.try_map(|name, span| match name {
Token::Ident(str) => Ok(str),
_ => Err(Rich::custom(
span,
format!("expected identifier, found {:?}", name),
)),
})
.repeated()
.collect::<Vec<_>>(),
)
.then_ignore(just(Token::Equals))
.then(expr.clone())
.then_ignore(just(Token::Semicolon))
.then(decl)
.try_map(|(((name, params), body), then), span| match name {
Token::Ident(str) => Ok(Expr::Fn {
name: str,
args: params,
body: Rc::new(body),
then: Box::new(then),
}),
_ => Err(Rich::custom(
span,
format!("expected identifier, found {:?}", name),
)),
});
// must parse 'let' keyword first so it doesn't become an identifier
decl_fn.or(decl_let).or(expr)
});
decl
}

1000001
programs/long.jang Normal file

File diff suppressed because it is too large Load diff

10000
programs/medium.jang Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,38 +1,27 @@
use std::{collections::HashMap, rc::Rc};
use chumsky::prelude::*; use chumsky::prelude::*;
use jang::*;
#[derive(Debug, Clone)] use std::collections::HashMap;
enum Expr<'src> {
Num(f64),
Var(&'src str),
Neg(Box<Expr<'src>>),
Add(Box<Expr<'src>>, Box<Expr<'src>>),
Sub(Box<Expr<'src>>, Box<Expr<'src>>),
Mul(Box<Expr<'src>>, Box<Expr<'src>>),
Div(Box<Expr<'src>>, Box<Expr<'src>>),
Call(&'src str, Vec<Expr<'src>>),
Let {
name: &'src str,
rhs: Box<Expr<'src>>,
then: Box<Expr<'src>>,
},
Fn {
name: &'src str,
args: Vec<&'src str>,
body: Rc<Expr<'src>>,
then: Box<Expr<'src>>,
},
}
fn main() { fn main() {
let path = std::env::args().nth(1).unwrap(); let path = std::env::args().nth(1).unwrap();
let src = std::fs::read_to_string(&path).unwrap(); let src = std::fs::read_to_string(&path).unwrap();
println!("Parsing file at {path}"); println!("Parsing file at {path}");
println!("src:\n{}\n---", src); println!("src:\n{}\n---", src);
let parse_result = create_parser().parse(&src); let combo_parse_result = create_combo_parser().parse(&src);
println!("combo parsed:\n{:?}\n---", combo_parse_result);
if let Ok(ast) = combo_parse_result.into_result() {
let mut vars = HashMap::new();
let output = eval(&ast, &mut vars);
println!("{:?}", output);
}
println!("----\n");
let lex_result = create_lexer().parse(&src);
println!("lexed:\n{:?}\n---", lex_result);
let Ok(lexed) = lex_result.into_result() else {
return;
};
let parse_result = create_parser().parse(&lexed);
println!("parsed:\n{:?}\n---", parse_result); println!("parsed:\n{:?}\n---", parse_result);
if let Ok(ast) = parse_result.into_result() { if let Ok(ast) = parse_result.into_result() {
let mut vars = HashMap::new(); let mut vars = HashMap::new();
@ -40,192 +29,3 @@ fn main() {
println!("{:?}", output); println!("{:?}", output);
} }
} }
#[derive(Clone)]
enum IdentValue<'src> {
Var(f64),
Func {
name: &'src str,
params: Vec<&'src str>,
body: Rc<Expr<'src>>,
},
}
fn eval<'src>(
expr: &Expr<'src>,
vars: &mut HashMap<&'src str, Vec<IdentValue<'src>>>,
) -> Result<f64, String> {
fn bind_var<'src>(
vars: &mut HashMap<&'src str, Vec<IdentValue<'src>>>,
name: &'src str,
val: IdentValue<'src>,
) {
vars.entry(name).or_insert(Vec::new()).push(val);
}
fn unbind_var<'src>(vars: &mut HashMap<&'src str, Vec<IdentValue<'src>>>, name: &'src str) {
vars.get_mut(name).unwrap().pop();
}
match expr {
Expr::Num(x) => Ok(*x),
Expr::Neg(a) => Ok(-eval(a, vars)?),
Expr::Add(a, b) => Ok(eval(a, vars)? + eval(b, vars)?),
Expr::Sub(a, b) => Ok(eval(a, vars)? - eval(b, vars)?),
Expr::Mul(a, b) => Ok(eval(a, vars)? * eval(b, vars)?),
Expr::Div(a, b) => Ok(eval(a, vars)? / eval(b, vars)?),
Expr::Var(name) => {
let ident_value = vars.get(name).and_then(|v| v.last());
match ident_value {
Some(IdentValue::Var(v)) => Ok(*v),
Some(IdentValue::Func { .. }) => Err("Expected function to be called".to_string()),
None => Err(format!("Undefined variable `{}`", name)),
}
}
Expr::Let { name, rhs, then } => {
let val = eval(rhs, vars)?;
bind_var(vars, name, IdentValue::Var(val));
let output = eval(then, vars);
unbind_var(vars, name);
output
}
Expr::Call(name, args) => {
let ident_value = vars.get(name).and_then(|v| v.last()).cloned();
match ident_value {
Some(IdentValue::Func { name, params, body }) => {
if params.len() != args.len() {
Err(format!(
"expected {} arguments but only {} were provided to {}",
params.len(),
args.len(),
name
))
} else {
let binds = params
.iter()
.zip(args)
.map(|(param, arg)| (param, eval(arg, vars)))
.collect::<Vec<_>>();
for (param, bind) in binds {
bind_var(vars, param, IdentValue::Var(bind.unwrap()));
}
let return_value = eval(&body, vars);
for param in params.iter() {
unbind_var(vars, param);
}
return_value
}
}
Some(IdentValue::Var(_)) => {
Err("Variable cannot be called like a function".to_string())
}
None => Err(format!("Undefined variable `{}`", name)),
}
}
Expr::Fn {
name,
args,
body,
then,
} => {
vars.entry(name)
.or_insert(Vec::new())
.push(IdentValue::Func {
name,
params: args.clone(),
body: body.clone(),
});
let output = eval(then, vars);
vars.get_mut(name).unwrap().pop();
output
}
}
}
fn create_parser<'src>() -> impl Parser<'src, &'src str, Expr<'src>> {
let ident = text::ascii::ident().padded();
let expr = recursive(|expr| {
let int = text::int(10)
.map(|s: &str| Expr::Num(s.parse().unwrap()))
.padded();
let call = ident
.then(
expr.clone()
.separated_by(just(','))
.allow_trailing()
.collect::<Vec<_>>()
.delimited_by(just('('), just(')')),
)
.map(|(f, args)| Expr::Call(f, args));
let atom = int
.or(expr.delimited_by(just('('), just(')')))
.or(call)
.or(ident.map(Expr::Var))
.padded();
let op = |c| just(c).padded();
let unary = op('-')
.repeated()
.foldr(atom, |_op, rhs| Expr::Neg(Box::new(rhs)));
let product = unary.clone().foldl(
choice((
op('*').to(Expr::Mul as fn(_, _) -> _),
op('/').to(Expr::Div as fn(_, _) -> _),
))
.then(unary)
.repeated(),
|lhs, (op, rhs)| op(Box::new(lhs), Box::new(rhs)),
);
let sum = product.clone().foldl(
choice((
op('+').to(Expr::Add as fn(_, _) -> _),
op('-').to(Expr::Sub as fn(_, _) -> _),
))
.then(product)
.repeated(),
|lhs, (op, rhs)| op(Box::new(lhs), Box::new(rhs)),
);
sum
});
let decl = recursive(|decl| {
// ex: let x = 5*2
let decl_let = text::ascii::keyword("let")
.ignore_then(ident)
.then_ignore(just('='))
.then(expr.clone())
.then_ignore(just(';'))
.then(decl.clone())
.map(|((name, rhs), then)| Expr::Let {
name,
rhs: Box::new(rhs),
then: Box::new(then),
});
// ex: fn f x y = x + y
let decl_fn = text::ascii::keyword("fn")
.ignore_then(ident)
// collect params
.then(ident.repeated().collect::<Vec<_>>())
.then_ignore(just('='))
.then(expr.clone())
.then_ignore(just(';'))
.then(decl)
.map(|(((name, params), body), then)| Expr::Fn {
name,
args: params,
body: Rc::new(body),
then: Box::new(then),
});
// must parse 'let' keyword first so it doesn't become and identifier
decl_fn.or(decl_let).or(expr).padded()
});
decl
}