1use serde::Serialize;
12use shared::span::{GetSpan, Span};
13
14use crate::expression::{Expression, Identifier};
15use crate::StatementList;
16
17#[derive(Debug, Serialize, PartialEq, Clone)]
19#[serde(untagged)]
22pub enum Statement {
23 Set(Set),
24 Return(Return),
25 Expression(Expression),
26 FunctionDeclaration(FunctionDeclaration),
27 RepeatNTimes(RepeatNTimes),
28 RepeatUntil(RepeatUntil),
29 RepeatForever(RepeatForever),
30 Display(Display),
31}
32
33impl GetSpan for Statement {
34 fn span(&self) -> &Span {
35 match self {
36 Statement::Set(set_stmt) => &set_stmt.span,
37 Statement::Return(ret_stmt) => &ret_stmt.span,
38 Statement::Expression(expr) => expr.span(),
39 Statement::FunctionDeclaration(function_declaration) => &function_declaration.span,
40 Statement::RepeatNTimes(repeat_n_times) => &repeat_n_times.span,
41 Statement::RepeatUntil(repeat_until) => &repeat_until.span,
42 Statement::RepeatForever(repeat_forever) => &repeat_forever.span,
43 Statement::Display(display) => &display.span,
44 }
45 }
46}
47
48#[derive(Debug, Serialize, PartialEq, Clone)]
51#[serde(tag = "type")]
52pub struct Set {
53 pub ident: Identifier,
54 pub expr: Expression,
55 pub span: Span,
56}
57
58#[derive(Debug, Serialize, PartialEq, Clone)]
61#[serde(tag = "type")]
62pub struct Return {
63 pub value: Expression,
64 pub span: Span,
65}
66
67#[derive(Debug, Serialize, PartialEq, Clone)]
75#[serde(tag = "type")]
76pub struct FunctionDeclaration {
77 pub name: Identifier,
78 pub parameters: Vec<Identifier>,
79 pub body: StatementList,
80 pub span: Span,
81}
82
83#[derive(Debug, Serialize, PartialEq, Clone)]
91#[serde(tag = "type")]
92pub struct RepeatNTimes {
93 pub n: Expression,
94 pub body: StatementList,
95 pub span: Span,
96}
97
98#[derive(Debug, Serialize, PartialEq, Clone)]
106#[serde(tag = "type")]
107pub struct RepeatUntil {
108 pub condition: Expression,
109 pub body: StatementList,
110 pub span: Span,
111}
112
113#[derive(Debug, Serialize, PartialEq, Clone)]
121#[serde(tag = "type")]
122pub struct RepeatForever {
123 pub body: StatementList,
124 pub span: Span,
125}
126
127#[derive(Debug, Serialize, PartialEq, Clone)]
130#[serde(tag = "type")]
131pub struct Display {
132 pub expressions: Vec<Expression>,
133 pub span: Span,
134}