Skip to main content

oak_nim/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use crate::language::NimLanguage;
3use core::range::Range;
4use oak_core::tree::RedNode;
5use std::sync::Arc;
6
7/// A Nim module.
8#[derive(Debug, Clone, PartialEq)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub struct NimModule {
11    /// Items in the module.
12    pub items: Vec<Item>,
13}
14
15/// A top-level item in a Nim source file.
16#[derive(Debug, Clone, PartialEq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub enum Item {
19    /// A statement.
20    Statement(Statement),
21    /// A declaration.
22    Declaration(Arc<Declaration>),
23}
24
25/// Represents a statement in Nim.
26#[derive(Debug, Clone, PartialEq)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28pub enum Statement {
29    /// An expression statement.
30    Expression(Expression),
31    /// A `static:` block for compile-time execution.
32    Static(Arc<Block>),
33    /// A `when` statement for compile-time conditional.
34    When {
35        /// Branches of the when statement.
36        branches: Vec<WhenBranch>,
37        /// Optional else branch.
38        else_branch: Option<Arc<Block>>,
39    },
40    // Add more variants as needed
41}
42
43/// Represents a branch in a `when` statement.
44#[derive(Debug, Clone, PartialEq)]
45#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
46pub struct WhenBranch {
47    /// Condition for the branch.
48    pub condition: Expression,
49    /// Body of the branch.
50    pub body: Arc<Block>,
51}
52
53/// Represents a declaration in Nim.
54#[derive(Debug, Clone, PartialEq)]
55#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
56pub struct Declaration {
57    /// Name of the declaration.
58    pub name: String,
59    /// Span of the declaration.
60    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
61    pub span: Range<usize>,
62}
63
64/// Represents an expression in Nim.
65#[derive(Debug, Clone, PartialEq)]
66#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67pub enum Expression {
68    /// A literal value.
69    Literal(String),
70    /// An identifier.
71    Identifier(String),
72    // Add more variants as needed
73}
74
75/// Represents a block of code in Nim.
76#[derive(Debug, Clone, PartialEq)]
77#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
78pub struct Block {
79    /// Statements in the block.
80    pub statements: Vec<Statement>,
81    /// Span of the block.
82    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
83    pub span: Range<usize>,
84}