From a1053eefd53574bf4055e079bb25d6d4a4ae5200 Mon Sep 17 00:00:00 2001 From: bQUARKz Date: Wed, 4 Feb 2026 19:07:05 +0000 Subject: [PATCH] pr 04.1 --- crates/prometeu-compiler/src/analysis/mod.rs | 1 + .../src/analysis/symbols/mod.rs | 85 +++++++++++++++++++ crates/prometeu-compiler/src/lib.rs | 1 + 3 files changed, 87 insertions(+) create mode 100644 crates/prometeu-compiler/src/analysis/mod.rs create mode 100644 crates/prometeu-compiler/src/analysis/symbols/mod.rs diff --git a/crates/prometeu-compiler/src/analysis/mod.rs b/crates/prometeu-compiler/src/analysis/mod.rs new file mode 100644 index 00000000..3f159a96 --- /dev/null +++ b/crates/prometeu-compiler/src/analysis/mod.rs @@ -0,0 +1 @@ +pub mod symbols; \ No newline at end of file diff --git a/crates/prometeu-compiler/src/analysis/symbols/mod.rs b/crates/prometeu-compiler/src/analysis/symbols/mod.rs new file mode 100644 index 00000000..7b7b194b --- /dev/null +++ b/crates/prometeu-compiler/src/analysis/symbols/mod.rs @@ -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, +} + +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); + } +} \ No newline at end of file diff --git a/crates/prometeu-compiler/src/lib.rs b/crates/prometeu-compiler/src/lib.rs index c4bc559d..db50b3b8 100644 --- a/crates/prometeu-compiler/src/lib.rs +++ b/crates/prometeu-compiler/src/lib.rs @@ -49,6 +49,7 @@ pub mod deps; pub mod sources; pub mod building; pub mod semantics; +pub mod analysis; use anyhow::Result; use clap::{Parser, Subcommand};