Browse Source

Adding some chemistry stuff, because I don't want to factor out the parsing state code

typed
Weird Constructor 6 months ago
parent
commit
e02a7a4f69
  1. 1
      Cargo.toml
  2. 15
      README.md
  3. 10
      scripts/pack_jsons.wl
  4. 10063
      src/chemical_elements.json
  5. BIN
      src/chemical_elements.json.lzw
  6. 23
      src/chemistry.rs
  7. 62
      src/prelude.rs

1
Cargo.toml

@ -25,6 +25,7 @@ all = ["mqtt", "http", "cursive", "zip", "odbc", "clipboard"]
[dependencies]
fnv = "1.0.7"
weezl = "0.1.7"
rustyline = { version = "6.1.2", optional = true }
zip = { version = "0.6.3", optional = true, features = ["deflate"], default-features = false }
flate2 = { version = "1.0.25", optional = true }

15
README.md

@ -624,4 +624,17 @@ without any additional terms or conditions.
## Contributors
* Cedric Hutchings <cedhut02@gmail.com> (cedric-h on GitHub)
## Attribution
* src/chemical_elements.json was taken from
https://raw.githubusercontent.com/eliaxelang007/Periodic-Table-Rs/master/_data_formatting/final.json
big thanks go to Eli (Github handle eliaxelang007) for collecting this information.
His note about this:
The element data in this library primarily came from
[PubChem](https://pubchem.ncbi.nlm.nih.gov/periodic-table/) with some of its
missing fields filled in by the data from the [Royal Society of Chemistry](https://www.rsc.org/periodic-table).
The combined and parsed element data from both these sources is in
[this json file](https://github.com/eliaxelang007/Periodic-Table-Rs/blob/master/_data_formatting/final.json)
and you can use it in your own projects if all you need is the raw element data :D

10
scripts/pack_jsons.wl

@ -0,0 +1,10 @@
#!wlambda
!packed =
std:bytes:lzw:encode
(std:str:to_bytes
(std:ser:json
(std:deser:json
(std:io:file:read_text "src/chemical_elements.json"))
$true))
7;
std:io:file:write_safe "src/chemical_elements.json.lzw" packed;

10063
src/chemical_elements.json

File diff suppressed because it is too large Load Diff

BIN
src/chemical_elements.json.lzw

Binary file not shown.

23
src/chemistry.rs

@ -0,0 +1,23 @@
use crate::vval::{VVal, Env, VValFun};
use crate::parser::state::State;
use crate::parser::state::{ParseError, ParseErrorKind};
use std::fmt::Write;
pub fn parse_chemical_sum_formula(s: &str) -> Result<VVal, ParseError> {
let mut ps = State::new_verbatim(s, "<selector>");
let res = VVal::map();
while !ps.at_end() {
match ps.peek().unwrap() {
'0' ..= '9' => {
let num = ps.take_while(|c| c.is_digit(10)).to_string();
if ps.at_end() { return Err(ps.err(ParseErrorKind::EOF("mol-count"))); }
}
}
}
Ok(res)
}

62
src/prelude.rs

@ -12964,6 +12964,68 @@ pub fn std_symbol_table() -> SymbolTable {
})
}, Some(1), Some(1), false);
func!(st, "bytes:lzw:encode",
|env: &mut Env, _argc: usize| {
use weezl::{BitOrder, encode::Encoder};
let bitsize = env.arg(1);
let bitsize = if bitsize.is_none() {
9
} else {
bitsize.i() as u8
};
if bitsize < 2 || bitsize > 12 {
return Ok(env.new_err(format!("bytes:lzw:encode: invalid bitsize: {}", bitsize)));
}
let bitorder = if env.arg(2).with_s_ref(|s| s == "lsb") {
BitOrder::Lsb
} else {
BitOrder::Msb
};
env.arg(0).with_bv_ref(|bytes| {
match Encoder::new(bitorder, bitsize).encode(bytes) {
Ok(data) => Ok(VVal::new_byt(data)),
Err(e) =>
Ok(env.new_err(
format!("bytes:lzw:encode: {}", e)))
}
})
}, Some(1), Some(3), false);
func!(st, "bytes:lzw:decode",
|env: &mut Env, _argc: usize| {
use weezl::{BitOrder, decode::Decoder};
let bitsize = env.arg(1);
let bitsize = if bitsize.is_none() {
9
} else {
bitsize.i() as u8
};
if bitsize < 2 || bitsize > 12 {
return Ok(env.new_err(format!("bytes:lzw:decode: invalid bitsize: {}", bitsize)));
}
let bitorder = if env.arg(2).with_s_ref(|s| s == "lsb") {
BitOrder::Lsb
} else {
BitOrder::Msb
};
env.arg(0).with_bv_ref(|bytes| {
match Decoder::new(bitorder, bitsize).decode(bytes) {
Ok(data) => Ok(VVal::new_byt(data)),
Err(e) =>
Ok(env.new_err(
format!("bytes:lzw:decode: {}", e)))
}
})
}, Some(1), Some(3), false);
func!(st, "copy",
|env: &mut Env, _argc: usize| {
Ok(env.arg(0).shallow_clone())

Loading…
Cancel
Save