1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (C) 2024 Ethan Uppal. All rights reserved.
use pulsar_frontend::ty::Type;
use std::fmt::{Display, Formatter};

/// If one exists, the start symbol for a pulsar program is guaranteed to begin
/// with the following.
pub const MAIN_SYMBOL_PREFIX: &str = "_pulsar_Smain";

pub struct LabelName {
    unmangled: String,
    mangled: String,
    is_native: bool
}

impl LabelName {
    pub fn from_native(
        value: String, args: &Vec<Type>, ret: &Box<Type>
    ) -> Self {
        let mut mangled = String::new();
        mangled.push_str("_pulsar");
        mangled.push_str(&format!("_S{}", value));
        for arg in args {
            mangled.push_str(&format!("_{}", arg.mangle()));
        }
        mangled.push_str(&format!("_{}", ret.mangle()));
        Self {
            unmangled: value,
            mangled,
            is_native: true
        }
    }

    pub fn mangle(&self) -> &String {
        &self.mangled
    }
}

impl Display for LabelName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.is_native {
            write!(f, "@native({})", self.unmangled)?;
        } else {
            self.mangled.fmt(f)?;
        }

        Ok(())
    }
}

pub enum LabelVisibility {
    Public,
    Private,
    External
}

impl Display for LabelVisibility {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match &self {
            LabelVisibility::Public => "public",
            LabelVisibility::Private => "private",
            LabelVisibility::External => "external"
        }
        .fmt(f)
    }
}

pub struct Label {
    pub name: LabelName,
    pub visibility: LabelVisibility
}

impl Label {
    pub fn from(name: LabelName, visibility: LabelVisibility) -> Label {
        Label { name, visibility }
    }
}

impl Display for Label {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} {}", self.visibility, self.name)
    }
}