U operator

This commit is contained in:
ゆめ 2023-08-22 21:43:31 -05:00
parent 979951b940
commit 9a1247ec3d
6 changed files with 41 additions and 1 deletions

View file

@ -179,7 +179,7 @@ function App() {
)
case 'message':
return (
<div key={index}>Message: {cell.text}</div>
<pre key={index}>{cell.text}</pre>
)
}
})

View file

@ -127,6 +127,7 @@ export function Keyboard(props: KeyboardProps) {
</div>
<div className="keyboard-col">
<UiActionButton action="clear" text="CLR" />
<TokenButton token="U" tokentype="operator" />
{
["m", "mol", "M", "Da"].map((token) => {
return (

View file

@ -86,6 +86,7 @@ impl<'a> Interpreter<'a> {
Token::Operator('d') => self.op_d()?,
Token::Operator('r') => self.op_r()?,
Token::Operator('s') => self.op_s()?,
Token::Operator('U') => self.op_upper_u()?,
Token::VarRecall(name) => self.op_recall(&name)?,
Token::VarStore(name) => self.op_store(&name)?,
Token::MacroInvoke((name, args)) => match name.as_str() {

View file

@ -144,6 +144,27 @@ impl<'a> Interpreter<'a> {
Ok(())
}
pub fn op_upper_u(&mut self) -> InterpreterResult<()> {
let mut output = String::from("Base units:\n");
for u in &self.unit_system.base_units() {
output.push_str(&format!("{}, ", u.symbol));
}
output.pop();
output.pop();
output.push_str("\n\nDerived units:\n");
for u in &self.unit_system.derived_units() {
output.push_str(&format!(
"{} = {} ({}) + {}\n",
u.symbol, u.scale, u.exponents, u.offset
));
}
(self.output)(Output::Message(output));
Ok(())
}
pub fn op_s(&mut self) -> InterpreterResult<()> {
let target = self.stack.pop().ok_or(InterpreterError::StackUnderflow)?;

View file

@ -55,6 +55,22 @@ impl UnitSystem {
pub fn push_derived_unit(&mut self, unit: DerivedUnit) {
self.derived_units.insert(unit.symbol.clone(), unit);
}
pub fn base_units(&self) -> Vec<BaseUnit> {
let mut base_units = Vec::new();
for unit in self.base_units.values() {
base_units.push(unit.clone());
}
base_units.sort_by(|a, b| a.symbol.cmp(&b.symbol));
base_units
}
pub fn derived_units(&self) -> Vec<DerivedUnit> {
let mut derived_units = Vec::new();
for unit in self.derived_units.values() {
derived_units.push(unit.clone());
}
derived_units.sort_by(|a, b| format!("{}", a.exponents).cmp(&format!("{}", b.exponents)));
derived_units
}
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]

View file

@ -152,6 +152,7 @@ impl<R: std::io::Read> Tokenizer<R> {
Some('d') => Ok(Some(Token::Operator('d'))),
Some('r') => Ok(Some(Token::Operator('r'))),
Some('s') => Ok(Some(Token::Operator('s'))),
Some('U') => Ok(Some(Token::Operator('U'))),
Some('#') => {
while let Some(c) = self.next_char().map_err(TokenizerError::IOError)? {
match c {