ast/
statement.rs

1//! This module contains the AST nodes for the SAP language statements.
2//!
3//! A statement is a single line of code that performs an action, such as setting a
4//! variable, returning a value, or defining a function. The AST nodes in this module
5//! represent the different types of statements that can be found in the SAP language.
6//!
7//! Standalone expressions are also considered statements in the SAP language, and are
8//! represented by the `Expression` enum in the `expression` module. This allows
9//! expressions to be used as statements in the language, such as in the case of function
10//! calls.
11use serde::Serialize;
12use shared::span::{GetSpan, Span};
13
14use crate::expression::{Expression, Identifier};
15use crate::StatementList;
16
17/// Represents a single statement in the SAP language.
18#[derive(Debug, Serialize, PartialEq, Clone)]
19// Tell serde not to include this enum in the JSON output, since it only adds clutter and doesn't
20// provide any useful information.
21#[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/// Represents a set statement in the SAP language.
49/// For example: `set x = 5`
50#[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/// Represents a return statement in the SAP language.
59/// For example: `return 5`
60#[derive(Debug, Serialize, PartialEq, Clone)]
61#[serde(tag = "type")]
62pub struct Return {
63    pub value: Expression,
64    pub span: Span,
65}
66
67/// Represents a function declaration in the SAP language.
68/// For example:
69/// ```sap
70/// defineFunction add(x, y)
71///     return x + y
72/// end
73/// ```
74#[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/// Represents a repeat n times statement in the SAP language.
84/// For example:
85/// ```sap
86/// repeat 5 times
87///     display "Hello, world!"
88/// end
89/// ```
90#[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/// Represents a repeat until statement in the SAP language.
99/// For example:
100/// ```sap
101/// repeat until x > 5
102///     set x = x + 1
103/// end
104/// ```
105#[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/// Represents a repeat forever statement in the SAP language.
114/// For example:
115/// ```sap
116/// repeat forever
117///     display "Hello, world!"
118/// end
119/// ```
120#[derive(Debug, Serialize, PartialEq, Clone)]
121#[serde(tag = "type")]
122pub struct RepeatForever {
123    pub body: StatementList,
124    pub span: Span,
125}
126
127/// Represents a display statement in the SAP language.
128/// For example: `display "Hello, world!"`
129#[derive(Debug, Serialize, PartialEq, Clone)]
130#[serde(tag = "type")]
131pub struct Display {
132    pub expressions: Vec<Expression>,
133    pub span: Span,
134}