Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance the help command to print info about a given unit #267

Merged
merged 13 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions numbat-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,15 @@ impl Cli {
println!();
}
_ => {
if let Some(keyword) = line.strip_prefix("info") {
let help = self
.context
.lock()
.unwrap()
.print_info_for_keyword(keyword.trim());
println!("{}", ansi_format(&help, true));
continue;
}
let result = self.parse_and_evaluate(
&line,
CodeSource::Text,
Expand Down
13 changes: 13 additions & 0 deletions numbat-cli/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::Path;

use assert_cmd::Command;
use predicates::boolean::PredicateBooleanExt;

fn numbat() -> Command {
let module_path = Path::new(&std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("modules");
Expand Down Expand Up @@ -125,3 +126,15 @@ fn help_text() {
"Energy of red photons: 1.87855 eV",
));
}

#[test]
fn info_text() {
numbat().write_stdin("info g0").assert().success().stdout(
predicates::str::contains("Standard acceleration of gravity on earth")
.and(predicates::str::contains("9.80665 m/s²")),
);

numbat().write_stdin("info C").assert().success().stdout(
predicates::str::contains("Coulomb").and(predicates::str::contains("1 coulomb = ")),
);
}
5 changes: 5 additions & 0 deletions numbat-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ impl Numbat {
self.format(&help_markup(), true).into()
}

pub fn print_info(&mut self, keyword: &str) -> JsValue {
let output = self.ctx.print_info_for_keyword(keyword);
self.format(&output, false).into()
}

pub fn get_completions_for(&self, input: &str) -> Vec<JsValue> {
self.ctx
.get_completions_for(input, false)
Expand Down
10 changes: 8 additions & 2 deletions numbat-wasm/www/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ function interpret(input) {
} else if (input_trimmed == "help" || input_trimmed == "?") {
output = numbat.help();
} else {
var result = numbat.interpret(input);
output = result.output;
var result = {is_error: false};
if (input_trimmed.startsWith("info")) {
var keyword = input_trimmed.substring(4).trim();
output = numbat.print_info(keyword);
} else {
result = numbat.interpret(input);
output = result.output;
}

if (!result.is_error) {
combined_input += input.trim() + "⏎";
Expand Down
84 changes: 75 additions & 9 deletions numbat/modules/math/constants.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@ use core::scalar

### Mathematical

let pi = 3.14159265358979323846264338327950288
let π = pi
@name("Pi")
@url("https://en.wikipedia.org/wiki/Pi")
@aliases(pi)
let π = 3.14159265358979323846264338327950288

@name("Tau")
@url("https://en.wikipedia.org/wiki/Turn_(angle)#Tau_proposals")
let τ = 2 π

@name("Euler's number")
@url("https://en.wikipedia.org/wiki/E_(mathematical_constant)")
let e = 2.71828182845904523536028747135266250

@name("Golden ratio")
@url("https://en.wikipedia.org/wiki/Golden_ratio")
@aliases(golden_ratio)
let φ = 1.61803398874989484820458683436563811
let golden_ratio = φ

### Named numbers

Expand Down Expand Up @@ -41,16 +52,23 @@ unit quadrillion = 10^15
@url("https://en.wikipedia.org/wiki/Quintillion")
unit quintillion = 10^18

@name("Googol")
@url("https://en.wikipedia.org/wiki/Googol")
let googol = 10^100

### Unicode fractions

@name("One half")
@url("https://en.wikipedia.org/wiki/One_half")
@aliases(half, semi)
let ½ = 1 / 2

let ⅓ = 1 / 3
let ⅔ = 2 / 3

@aliases(quarter)
let ¼ = 1 / 4

let ¾ = 3 / 4

let ⅕ = 1 / 5
Expand All @@ -72,11 +90,59 @@ let ⅑ = 1 / 9

let ⅒ = 1 / 10

#### Colloquial
#### Integers and colloquial names

@name("One")
@url("https://en.wikipedia.org/wiki/1")
let one = 1

@name("Two")
@url("https://en.wikipedia.org/wiki/2")
@aliases(double)
let two = 2

@name("Three")
@url("https://en.wikipedia.org/wiki/3")
@aliases(triple)
let three = 3

@name("Four")
@url("https://en.wikipedia.org/wiki/4")
@aliases(quadruple)
let four = 4

@name("Five")
@url("https://en.wikipedia.org/wiki/5")
let five = 5

@name("Six")
@url("https://en.wikipedia.org/wiki/6")
let six = 6

@name("Seven")
@url("https://en.wikipedia.org/wiki/7")
let seven = 7

@name("Eight")
@url("https://en.wikipedia.org/wiki/8")
let eight = 8

@name("Nine")
@url("https://en.wikipedia.org/wiki/9")
let nine = 9

@name("Ten")
@url("https://en.wikipedia.org/wiki/10")
let ten = 10

@name("Eleven")
@url("https://en.wikipedia.org/wiki/11")
let eleven = 11

@name("Twelve")
@url("https://en.wikipedia.org/wiki/12")
let twelve = 12

let quarter = 1 / 4
let half = 1 / 2
let semi = 1 / 2
let double = 2
let triple = 3
@name("Dozen")
@url("https://en.wikipedia.org/wiki/Dozen")
unit dozen = 12
90 changes: 52 additions & 38 deletions numbat/modules/physics/constants.nbt
Original file line number Diff line number Diff line change
@@ -1,80 +1,94 @@
use math::functions
use units::si

### Physics constants

# The speed of light in vacuum
@name("Speed of light in vacuum")
@url("https://en.wikipedia.org/wiki/Speed_of_light")
@aliases(c)
let speed_of_light: Velocity = 299_792_458 m / s
let c = speed_of_light

# The Newtonian constant of gravitation
@name("Newtonian constant of gravitation")
@url("https://en.wikipedia.org/wiki/Gravitational_constant")
@aliases(G)
let gravitational_constant: Force × Length^2 / Mass^2 = 6.67430e-11 m³ / (kg s²)
let G = gravitational_constant

# Standard acceleration of gravity on earth
@name("Standard acceleration of gravity on earth")
@url("https://en.wikipedia.org/wiki/Gravity_of_Earth")
@aliases(g0)
let gravity: Acceleration = 9.80665 m / s²
let g0 = gravity

# The Planck constant
@name("Planck constant")
@url("https://en.wikipedia.org/wiki/Planck_constant")
@aliases(ℎ)
let planck_constant: Action = 6.62607015e-34 J / Hz
let ℎ = planck_constant

# The reduced Planck constant
@name("Reduced Planck constant")
@url("https://en.wikipedia.org/wiki/Planck_constant#Reduced_Planck_constant_%E2%84%8F")
@aliases(h_bar)
let ℏ: AngularMomentum = planck_constant / 2π
let h_bar = ℏ

# Mass of the electron
@name("Electron mass")
@url("https://en.wikipedia.org/wiki/Electron_mass")
let electron_mass: Mass = 9.1093837015e-31 kg

# Elementary charge (charge of the electron)
@name("Elementary charge")
@url("https://en.wikipedia.org/wiki/Elementary_charge")
@aliases(electron_charge)
let elementary_charge: ElectricCharge = 1.602176634e-19 C
let electron_charge: ElectricCharge = elementary_charge

# Magnetic constant (vacuum magnetic permeability)
@name("Vacuum permeability / magnetic constant")
@url("https://en.wikipedia.org/wiki/Vacuum_permeability")
@aliases(µ0,mu0)
let magnetic_constant: MagneticPermeability = 1.25663706212e-6 N / A²
let µ0 = magnetic_constant
let mu0 = magnetic_constant

# Electric constant ( vacuum electric permittivity)
@name("Vacuum electric permittivity / electric constant")
@url("https://en.wikiversity.org/wiki/Electric_constant")
@aliases(ε0,eps0)
let electric_constant: ElectricPermittivity = 8.8541878128e-12 F / m
let ε0 = electric_constant
let eps0 = electric_constant

# Bohr magneton
@name("Bohr magneton")
@aliases(µ_B)
@url("https://en.wikipedia.org/wiki/Bohr_magneton")
let bohr_magneton: Energy / MagneticFluxDensity = 9.2740100783e-24 J / T
let µ_B = bohr_magneton

# Fine structure constant
@name("Fine structure constant")
@url("https://en.wikipedia.org/wiki/Fine-structure_constant")
@aliases(α, alpha)
let fine_structure_constant: Scalar = 7.2973525693e-3
let α = fine_structure_constant
let alpha = fine_structure_constant

# Proton mass
@name("Proton mass")
@url("https://en.wikipedia.org/wiki/Proton")
let proton_mass: Mass = 1.67262192369e-27 kg

# Neutron mass
@name("Neutron mass")
@url("https://en.wikipedia.org/wiki/Neutron")
let neutron_mass: Mass = 1.67492749804e-27 kg

# Avogadro constant
@name("Avogadro constant")
@url("https://en.wikipedia.org/wiki/Avogadro_constant")
@aliases(N_A)
let avogadro_constant: 1 / AmountOfSubstance = 6.02214076e23 / mol
let N_A = avogadro_constant

# Boltzmann constant
@name("Boltzmann constant")
@url("https://en.wikipedia.org/wiki/Boltzmann_constant")
@aliases(k_B)
let boltzmann_constant: Energy / Temperature = 1.380649e-23 J / K
let k_B = boltzmann_constant

# Stefan-Boltzmann constant
@name("Stefan-Boltzmann constant")
@url("https://en.wikipedia.org/wiki/Stefan%E2%80%93Boltzmann_law")
let stefan_boltzmann_constant: Power / (Area × Temperature^4) = 2 π^5 k_B^4 / (15 planck_constant^3 c^2)

# Ideal gas constant
@name("Ideal gas constant")
@url("https://en.wikipedia.org/wiki/Gas_constant")
@aliases(R)
let gas_constant: Energy / (AmountOfSubstance × Temperature) = 8.31446261815324 J / (K mol)
let R = gas_constant

# Bohr radius
@name("Bohr radius")
@url("https://en.wikipedia.org/wiki/Bohr_radius")
@aliases(a0)
let bohr_radius: Length = 4 pi ε0 ℏ^2 / (electron_charge^2 electron_mass)
let a0 = bohr_radius

# Rydberg constant
@name("Rydberg constant")
@url("https://en.wikipedia.org/wiki/Rydberg_constant")
let rydberg_constant: Wavenumber = (electron_mass electron_charge^4) / (8 ε0^2 ℎ^3 c)

@name("Rydberg unit of energy")
Expand Down
3 changes: 3 additions & 0 deletions numbat/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ pub enum Statement {
identifier: String,
expr: Expression,
type_annotation: Option<TypeAnnotation>,
decorators: Vec<Decorator>,
},
DefineFunction {
function_name_span: Span,
Expand Down Expand Up @@ -440,11 +441,13 @@ impl ReplaceSpans for Statement {
identifier,
expr,
type_annotation,
decorators,
} => Statement::DefineVariable {
identifier_span: Span::dummy(),
identifier: identifier.clone(),
expr: expr.replace_spans(),
type_annotation: type_annotation.as_ref().map(|t| t.replace_spans()),
decorators: decorators.clone(),
},
Statement::DefineFunction {
function_name_span: _,
Expand Down
Loading