1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use crate::tokens::{Token, TokenType};
use crate::visitor::ExprVisitor;

#[derive(Clone)]
pub(crate) enum Expr {
	Variable(VariableExpr),
	Logical(LogicalExpr),
	Binary(BinaryExpr),
	Unary(UnaryExpr),
	Call(CallExpr),
	Literal(ExprValue),
}

impl Expr {
	pub(crate) fn new_variable(name: Token) -> Self {
		Self::Variable(VariableExpr { name })
	}
	pub(crate) fn new_logical(left: Expr, operator: TokenType, right: Expr) -> Self {
		Self::Logical(LogicalExpr {
			left: Box::new(left),
			operator,
			right: Box::new(right),
		})
	}

	pub(crate) fn new_binary(left: Expr, operator: TokenType, right: Expr) -> Self {
		Self::Binary(BinaryExpr {
			left: Box::new(left),
			operator,
			right: Box::new(right),
		})
	}

	pub(crate) fn new_unary(operator: TokenType, right: Expr) -> Self {
		Self::Unary(UnaryExpr {
			operator,
			right: Box::new(right),
		})
	}

	pub(crate) fn new_call(name: String, arguments: Vec<Expr>) -> Self {
		Self::Call(CallExpr { name, arguments })
	}

	pub(crate) fn new_literal(value: ExprValue) -> Self {
		Self::Literal(value)
	}

	pub(crate) fn accept<U, V>(&self, visitor: &mut V) -> U
	where
		V: ExprVisitor<U>,
	{
		match self {
			Expr::Variable(expr) => visitor.visit_variable_expr(expr),
			Expr::Logical(expr) => visitor.visit_logical_expr(expr),
			Expr::Binary(expr) => visitor.visit_binary_expr(expr),
			Expr::Unary(expr) => visitor.visit_unary_expr(expr),
			Expr::Call(expr) => visitor.visit_call_expr(expr),
			Expr::Literal(expr) => visitor.visit_literal_expr(expr),
		}
	}
}

#[derive(Clone)]
pub(crate) struct AssignExpr {
	pub(crate) name: Token,
	pub(crate) value: Box<Expr>,
}

#[derive(Clone)]
pub(crate) struct VariableExpr {
	pub(crate) name: Token,
}

#[derive(Clone)]
pub(crate) struct LogicalExpr {
	pub(crate) left: Box<Expr>,
	pub(crate) operator: TokenType,
	pub(crate) right: Box<Expr>,
}

#[derive(Clone)]
pub(crate) struct BinaryExpr {
	pub(crate) left: Box<Expr>,
	pub(crate) operator: TokenType,
	pub(crate) right: Box<Expr>,
}

#[derive(Clone)]
pub(crate) struct UnaryExpr {
	pub(crate) operator: TokenType,
	pub(crate) right: Box<Expr>,
}

#[derive(Clone)]
pub(crate) struct CallExpr {
	pub(crate) name: String,
	pub(crate) arguments: Vec<Expr>,
}

/// Represents an xBASIC value.
#[derive(Clone, PartialEq, Debug)]
pub enum ExprValue {
	Boolean(bool),
	Integer(i64),
	Decimal(f64),
	String(String),
}

impl ExprValue {
	pub fn is_integer(&self) -> bool {
		matches!(self, ExprValue::Boolean(_) | ExprValue::Integer(_))
	}

	pub fn is_decimal(&self) -> bool {
		matches!(self, ExprValue::Decimal(_))
	}

	pub fn is_string(&self) -> bool {
		matches!(self, ExprValue::String(_))
	}

	pub fn into_string(self) -> String {
		match self {
			ExprValue::Boolean(x) => {
				if x {
					"true".to_string()
				} else {
					"false".to_string()
				}
			}
			ExprValue::Integer(x) => x.to_string(),
			ExprValue::Decimal(x) => x.to_string(),
			ExprValue::String(x) => x,
		}
	}

	pub fn into_integer(self) -> i64 {
		match self {
			ExprValue::Boolean(x) => {
				if x {
					1
				} else {
					0
				}
			}
			ExprValue::Integer(x) => x,
			_ => panic!("Not an integer"),
		}
	}

	pub fn into_decimal(self) -> f64 {
		match self {
			ExprValue::Boolean(x) => x as i32 as f64,
			ExprValue::Integer(x) => x as f64,
			ExprValue::Decimal(x) => x,
			ExprValue::String(_) => panic!("Not a decimal"),
		}
	}

	pub fn into_boolean(self) -> Option<bool> {
		match self {
			ExprValue::Boolean(x) => Some(x),
			ExprValue::Integer(x) => Some(x != 0),
			ExprValue::Decimal(x) => Some(x != 0.0),
			ExprValue::String(_) => None,
		}
	}

	pub(crate) fn increment(&mut self) -> Result<(), &'static str> {
		match self {
			ExprValue::Integer(x) => *x += 1,
			ExprValue::Decimal(x) => *x += 1.0,
			_ => return Err("Can only increment numbers."),
		}

		Ok(())
	}
}