Skip to main content

oxilean_codegen/go_backend/
golit_traits.rs

1//! # GoLit - Trait Implementations
2//!
3//! This module contains trait implementations for `GoLit`.
4//!
5//! ## Implemented Traits
6//!
7//! - `Display`
8//!
9//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
10
11use crate::lcnf::*;
12
13use super::functions::format_stmts;
14use super::types::GoLit;
15use std::fmt;
16
17impl fmt::Display for GoLit {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        match self {
20            GoLit::Int(n) => write!(f, "{}", n),
21            GoLit::Float(v) => {
22                if v.fract() == 0.0 && v.is_finite() {
23                    write!(f, "{:.1}", v)
24                } else {
25                    write!(f, "{}", v)
26                }
27            }
28            GoLit::Bool(b) => write!(f, "{}", b),
29            GoLit::Str(s) => {
30                write!(f, "\"")?;
31                for c in s.chars() {
32                    match c {
33                        '"' => write!(f, "\\\"")?,
34                        '\\' => write!(f, "\\\\")?,
35                        '\n' => write!(f, "\\n")?,
36                        '\r' => write!(f, "\\r")?,
37                        '\t' => write!(f, "\\t")?,
38                        c => write!(f, "{}", c)?,
39                    }
40                }
41                write!(f, "\"")
42            }
43            GoLit::Nil => write!(f, "nil"),
44        }
45    }
46}