This commit is contained in:
bQUARKz 2026-02-04 19:07:05 +00:00
parent 18c9ebceae
commit a1053eefd5
Signed by: bquarkz
SSH Key Fingerprint: SHA256:Z7dgqoglWwoK6j6u4QC87OveEq74WOhFN+gitsxtkf8
3 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1 @@
pub mod symbols;

View File

@ -0,0 +1,85 @@
use crate::common::spans::Span;
use prometeu_analysis::NameId;
use serde::{Deserialize, Serialize};
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct SymbolId(pub u32);
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum SymbolKind {
Type,
Value,
Service,
Function,
Struct,
Contract,
ErrorType,
Local,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Symbol {
pub name: NameId,
pub kind: SymbolKind,
pub exported: bool,
pub module: u32,
pub decl_span: Span,
}
#[derive(Debug, Default, Clone)]
pub struct SymbolArena {
pub symbols: Vec<Symbol>,
}
impl SymbolArena {
pub fn new() -> Self {
Self { symbols: Vec::new() }
}
pub fn insert(&mut self, symbol: Symbol) -> SymbolId {
let id = SymbolId(self.symbols.len() as u32);
self.symbols.push(symbol);
id
}
pub fn get(&self, id: SymbolId) -> &Symbol {
&self.symbols[id.0 as usize]
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_symbol(name: NameId, module: u32) -> Symbol {
Symbol {
name,
kind: SymbolKind::Function,
exported: false,
module,
decl_span: Span::new(0, 1, 2),
}
}
#[test]
fn insert_returns_incremental_ids() {
let mut arena = SymbolArena::new();
let id0 = arena.insert(sample_symbol(NameId(0), 0));
let id1 = arena.insert(sample_symbol(NameId(1), 0));
assert_eq!(id0, SymbolId(0));
assert_eq!(id1, SymbolId(1));
}
#[test]
fn get_returns_correct_symbol() {
let mut arena = SymbolArena::new();
let symbol = sample_symbol(NameId(7), 3);
let id = arena.insert(symbol.clone());
assert_eq!(arena.get(id).name, symbol.name);
assert_eq!(arena.get(id).kind, symbol.kind);
assert_eq!(arena.get(id).module, symbol.module);
assert_eq!(arena.get(id).decl_span, symbol.decl_span);
}
}

View File

@ -49,6 +49,7 @@ pub mod deps;
pub mod sources; pub mod sources;
pub mod building; pub mod building;
pub mod semantics; pub mod semantics;
pub mod analysis;
use anyhow::Result; use anyhow::Result;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};