1use lexer::token::TokenKind;
16use serde::Serialize;
17use shared::span::{GetSpan, Span};
18
19use crate::literal::Literal;
20use crate::StatementList;
21
22#[derive(Debug, Serialize, PartialEq, Clone)]
24#[serde(untagged)]
27pub enum Expression {
28 Identifier(Identifier),
29 Unary(Unary),
30 Binary(Binary),
31 Selection(Selection),
32 FunctionCall(FunctionCall),
33 Array(Array),
34 Index(Index),
35 Literal(Literal),
36}
37
38impl GetSpan for Expression {
39 fn span(&self) -> &Span {
40 match self {
41 Expression::Identifier(identifier) => &identifier.span,
42 Expression::Unary(unary) => &unary.span,
43 Expression::Binary(binary) => &binary.span,
44 Expression::Selection(selection) => &selection.span,
45 Expression::FunctionCall(function_call) => &function_call.span,
46 Expression::Array(array) => &array.span,
47 Expression::Index(index) => &index.span,
48 Expression::Literal(literal) => literal.span(),
49 }
50 }
51}
52
53#[derive(Debug, Serialize, PartialEq, Clone)]
56#[serde(tag = "type")]
57pub struct Identifier {
58 pub name: String,
59 pub span: Span,
60}
61
62#[derive(Debug, Serialize, PartialEq, Clone)]
65#[serde(tag = "type")]
66pub struct Unary {
67 pub operator: TokenKind,
68 pub operand: Box<Expression>,
69 pub span: Span,
70}
71
72#[derive(Debug, Serialize, PartialEq, Clone)]
75#[serde(tag = "type")]
76pub struct Binary {
77 pub operator: TokenKind,
78 pub left: Box<Expression>,
79 pub right: Box<Expression>,
80 pub span: Span,
81}
82
83#[derive(Debug, Serialize, PartialEq, Clone)]
93#[serde(tag = "type")]
94pub struct Selection {
95 pub condition: Box<Expression>,
96 pub conditional: StatementList,
97 pub else_conditional: Option<StatementList>,
98 pub span: Span,
99}
100
101#[derive(Debug, Serialize, PartialEq, Clone)]
104#[serde(tag = "type")]
105pub struct FunctionCall {
106 pub callee: Box<Expression>,
109 pub arguments: Vec<Expression>,
110 pub span: Span,
111}
112
113#[derive(Debug, Serialize, PartialEq, Clone)]
116#[serde(tag = "type")]
117pub struct Array {
118 pub elements: Vec<Expression>,
119 pub span: Span,
120}
121
122#[derive(Debug, Serialize, PartialEq, Clone)]
125#[serde(tag = "type")]
126pub struct Index {
127 pub object: Box<Expression>,
128 pub index: Box<Expression>,
129 pub span: Span,
130}