Skip to main content

oak_go/ast/
mod.rs

1use core::range::Range;
2use serde::{Deserialize, Serialize};
3use std::{boxed::Box, string::String, vec::Vec};
4
5/// Go 语言强类型 AST 根
6#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
7pub struct GoRoot {
8    pub package: Option<String>,
9    pub imports: Vec<Import>,
10    pub declarations: Vec<Declaration>,
11}
12
13#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
14pub struct Import {
15    pub path: String,
16    pub alias: Option<String>,
17    #[serde(with = "oak_core::serde_range")]
18    pub span: Range<usize>,
19}
20
21#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
22pub enum Declaration {
23    Function(Function),
24    Variable(Variable),
25    Type(TypeDecl),
26    Const(Const),
27}
28
29#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
30pub struct Function {
31    pub name: String,
32    pub params: Vec<Parameter>,
33    pub return_type: Option<String>,
34    pub body: Block,
35    #[serde(with = "oak_core::serde_range")]
36    pub span: Range<usize>,
37}
38
39#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
40pub struct Parameter {
41    pub name: String,
42    pub param_type: String,
43    #[serde(with = "oak_core::serde_range")]
44    pub span: Range<usize>,
45}
46
47#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
48pub struct Block {
49    pub statements: Vec<Statement>,
50    #[serde(with = "oak_core::serde_range")]
51    pub span: Range<usize>,
52}
53
54#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
55pub enum Statement {
56    Expression(Expression),
57    Assignment {
58        target: String,
59        value: Expression,
60        #[serde(with = "oak_core::serde_range")]
61        span: Range<usize>,
62    },
63    Return {
64        value: Option<Expression>,
65        #[serde(with = "oak_core::serde_range")]
66        span: Range<usize>,
67    },
68    If {
69        condition: Expression,
70        then_block: Block,
71        else_block: Option<Block>,
72        #[serde(with = "oak_core::serde_range")]
73        span: Range<usize>,
74    },
75    For {
76        condition: Option<Expression>,
77        body: Block,
78        #[serde(with = "oak_core::serde_range")]
79        span: Range<usize>,
80    },
81}
82
83#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
84pub enum Expression {
85    Identifier {
86        name: String,
87        #[serde(with = "oak_core::serde_range")]
88        span: Range<usize>,
89    },
90    Literal {
91        value: String,
92        #[serde(with = "oak_core::serde_range")]
93        span: Range<usize>,
94    },
95    Binary {
96        left: Box<Expression>,
97        op: String,
98        right: Box<Expression>,
99        #[serde(with = "oak_core::serde_range")]
100        span: Range<usize>,
101    },
102    Call {
103        func: Box<Expression>,
104        args: Vec<Expression>,
105        #[serde(with = "oak_core::serde_range")]
106        span: Range<usize>,
107    },
108}
109
110#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
111pub struct Variable {
112    pub name: String,
113    pub var_type: Option<String>,
114    pub value: Option<Expression>,
115    #[serde(with = "oak_core::serde_range")]
116    pub span: Range<usize>,
117}
118
119#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
120pub struct TypeDecl {
121    pub name: String,
122    pub definition: String,
123    #[serde(with = "oak_core::serde_range")]
124    pub span: Range<usize>,
125}
126
127#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
128pub struct Const {
129    pub name: String,
130    pub const_type: Option<String>,
131    pub value: Expression,
132    #[serde(with = "oak_core::serde_range")]
133    pub span: Range<usize>,
134}