portal_jsc_swift_backend/
lib.rs

1#![no_std]
2
3use core::fmt::Display;
4
5use alloc::{
6    collections::btree_map::BTreeMap,
7    format,
8    string::{String, ToString},
9    sync::Arc,
10    vec::Vec,
11};
12use itertools::Itertools;
13use symbolism::Symbol;
14extern crate alloc;
15#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
16#[repr(transparent)]
17pub struct SwiftName {
18    pub id: u64,
19}
20impl Display for SwiftName {
21    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22        write!(
23            f,
24            "`js_{}`",
25            Symbol::from_int(self.id).to_string().replace(" ", "$")
26        )
27    }
28}
29#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
30#[repr(transparent)]
31pub struct TargetBlock {
32    pub name: SwiftName,
33}
34impl Display for TargetBlock {
35    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36        write!(
37            f,
38            "label = .{}
39            break",
40            &self.name
41        )
42    }
43}
44#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
45#[repr(transparent)]
46pub struct Return<T> {
47    pub value: T,
48}
49impl<T: Display> Display for Return<T> {
50    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
51        write!(f, "return {};", &self.value)
52    }
53}
54
55#[derive(Clone)]
56pub struct Blocks<K> {
57    pub map: BTreeMap<TargetBlock, Term<K>>,
58    pub entry: TargetBlock,
59}
60impl<K: Display> Display for Blocks<K> {
61    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
62        write!(
63            f,
64            "var label = .unused;while 1{{switch label{{{}\ndefault:{}}}}}",
65            self.map
66                .iter()
67                .map(|(t, k)| format!("case .{}: {k}", t.name))
68                .join("\n"),
69            self.entry
70        )
71    }
72}
73pub use portal_jsc_mob_backend_common::*;