microcad_lang/syntax/literal/
number_literal.rs1use crate::{src_ref::*, syntax::*, ty::*, value::*};
7
8#[derive(Clone, PartialEq)]
10pub struct NumberLiteral(pub f64, pub Unit, pub SrcRef);
11
12impl NumberLiteral {
13 pub fn normalized_value(&self) -> f64 {
15 self.1.normalize(self.0)
16 }
17
18 pub fn unit(&self) -> Unit {
20 self.1
21 }
22
23 pub fn value(&self) -> Value {
25 match self.1.ty() {
26 Type::Quantity(quantity_type) => {
27 Value::Quantity(Quantity::new(self.normalized_value(), quantity_type))
28 }
29 _ => unreachable!(),
30 }
31 }
32}
33
34impl crate::ty::Ty for NumberLiteral {
35 fn ty(&self) -> Type {
36 self.1.ty()
37 }
38}
39
40impl SrcReferrer for NumberLiteral {
41 fn src_ref(&self) -> literal::SrcRef {
42 self.2.clone()
43 }
44}
45
46impl std::fmt::Display for NumberLiteral {
47 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
48 write!(f, "{}{}", self.0, self.1)
49 }
50}
51
52impl From<NumberLiteral> for Value {
53 fn from(literal: NumberLiteral) -> Self {
54 literal.value()
55 }
56}