1#![doc = include_str!("readme.md")]
2use core::range::Range;
3
4#[derive(Debug, Clone, PartialEq)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct SwiftRoot {
8 pub program: Program,
10 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
12 pub span: Range<usize>,
13}
14
15#[derive(Debug, Clone, PartialEq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub struct Program {
19 pub statements: Vec<Statement>,
21}
22
23#[derive(Debug, Clone, PartialEq)]
25#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
26pub enum Statement {
27 FunctionDef {
29 name: String,
31 parameters: Vec<Parameter>,
33 return_type: Option<Type>,
35 body: Vec<Statement>,
37 },
38 VariableDecl {
40 is_mutable: bool,
42 name: String,
44 type_annotation: Option<Type>,
46 value: Option<Expression>,
48 },
49 Expression(Expression),
51 Return(Option<Expression>),
53 If {
55 test: Expression,
57 body: Vec<Statement>,
59 orelse: Option<Vec<Statement>>,
61 },
62 While {
64 test: Expression,
66 body: Vec<Statement>,
68 },
69 For {
71 variable: String,
73 iterable: Expression,
75 body: Vec<Statement>,
77 },
78 Block(Vec<Statement>),
80}
81
82#[derive(Debug, Clone, PartialEq)]
84#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
85pub enum Expression {
86 Binary {
88 left: Box<Expression>,
90 operator: String,
92 right: Box<Expression>,
94 },
95 Unary {
97 operator: String,
99 operand: Box<Expression>,
101 },
102 Call {
104 callee: Box<Expression>,
106 arguments: Vec<Expression>,
108 },
109 Member {
111 object: Box<Expression>,
113 member: String,
115 },
116 Identifier(String),
118 Literal(Literal),
120}
121
122#[derive(Debug, Clone, PartialEq)]
124#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
125pub enum Literal {
126 Number(String),
128 String(String),
130 Boolean(bool),
132 Nil,
134}
135
136#[derive(Debug, Clone, PartialEq)]
138#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
139pub struct Parameter {
140 pub name: String,
142 pub type_annotation: Type,
144}
145
146#[derive(Debug, Clone, PartialEq)]
148#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
149pub struct Type {
150 pub name: String,
152}