/
object.rs
213 lines (182 loc) · 5.75 KB
/
object.rs
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::fmt::{Debug, Display};
use std::cell::RefCell;
use std::ops::{Mul, Div, Add, Sub, Not, Neg};
use std::rc::Rc;
use crate::class::{Class, Instance};
use crate::error::RuntimeError;
use crate::function::{Function, NativeFunction};
use crate::literal::Literal;
use crate::interpreter::Interpreter;
/// Represents an object that can be stored in a variable or returned from a function.
/// This is an enum that wraps all the possible types of values in the language.
#[derive(Debug, Clone)]
pub enum Object {
Literal(Literal),
Function(Function),
NativeFunction(NativeFunction),
Class(Rc<RefCell<Class>>),
Instance(Rc<RefCell<Instance>>),
}
impl Object {
/// Returns the object as a boolean if it is a literal.
pub fn as_bool(&self) -> Option<bool> {
match self {
Object::Literal(literal) => Some(literal.as_bool()),
_ => None,
}
}
pub fn type_str(&self) -> &str {
match self {
Object::Literal(literal) => literal.type_str(),
Object::Function(_) => "function",
Object::NativeFunction(_) => "native function",
Object::Class(_) => "class",
Object::Instance(_) => "instance",
}
}
}
impl PartialEq for Object {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Object::Literal(left), Object::Literal(right)) => left == right,
(Object::Function(left), Object::Function(right)) => left == right,
(Object::NativeFunction(left), Object::NativeFunction(right)) => left == right,
(Object::Class(left), Object::Class(right)) => left == right,
(Object::Instance(left), Object::Instance(right)) => left == right,
_ => false,
}
}
}
impl PartialOrd for Object {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match (self, other) {
(Object::Literal(left), Object::Literal(right)) => left.partial_cmp(right),
_ => None
}
}
}
impl Add for Object {
type Output = Option<Self>;
fn add(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(Object::Literal(left), Object::Literal(right)) => left + right,
_ => None,
}.map(|x| x.into())
}
}
impl Sub for Object {
type Output = Option<Self>;
fn sub(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(Object::Literal(left), Object::Literal(right)) => left - right,
_ => None,
}.map(|x| x.into())
}
}
impl Mul for Object {
type Output = Option<Self>;
fn mul(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(Object::Literal(left), Object::Literal(right)) => left * right,
_ => None,
}.map(|x| x.into())
}
}
impl Div for Object {
type Output = Option<Self>;
fn div(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(Object::Literal(left), Object::Literal(right)) => left / right,
_ => None,
}.map(|x| x.into())
}
}
impl Not for Object {
type Output = Option<Self>;
fn not(self) -> Self::Output {
match self {
Object::Literal(right) => !right,
_ => None,
}.map(|x| x.into())
}
}
impl Neg for Object {
type Output = Option<Self>;
fn neg(self) -> Self::Output {
match self {
Object::Literal(right) => -right,
_ => None,
}.map(|x| x.into())
}
}
impl From<Literal> for Object {
fn from(literal: Literal) -> Self {
Object::Literal(literal)
}
}
impl From<f64> for Object {
fn from(number: f64) -> Self {
Object::Literal(Literal::Number(number))
}
}
impl From<bool> for Object {
fn from(boolean: bool) -> Self {
Object::Literal(Literal::Bool(boolean))
}
}
impl From<String> for Object {
fn from(string: String) -> Self {
Object::Literal(Literal::String(string))
}
}
impl From<&str> for Object {
fn from(string: &str) -> Self {
Object::Literal(Literal::String(string.to_owned()))
}
}
impl From<Function> for Object {
fn from(value: Function) -> Self {
Object::Function(value)
}
}
impl From<NativeFunction> for Object {
fn from(value: NativeFunction) -> Self {
Object::NativeFunction(value)
}
}
impl From<Rc<RefCell<Class>>> for Object {
fn from(value: Rc<RefCell<Class>>) -> Self {
Object::Class(value)
}
}
impl From<Instance> for Object {
fn from(value: Instance) -> Self {
Object::Instance(Rc::new(RefCell::new(value)))
}
}
impl From<Rc<RefCell<Instance>>> for Object {
fn from(value: Rc<RefCell<Instance>>) -> Self {
Object::Instance(value)
}
}
impl Display for Object {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Object::Literal(literal) => write!(f, "{literal}"),
Object::Function(function) => write!(f, "{function}"),
Object::NativeFunction(function) => write!(f, "{function}"),
Object::Class(class) => write!(f, "{}", class.borrow()),
Object::Instance(instance) => write!(f, "{}", instance.borrow()),
}
}
}
/// Represents a callable object in the language.
pub trait Callable: Debug {
/// Calls the object with the given arguments and the current state of the interpreter.
/// The interpreter is passed in as a mutable refernece so that the object can access
/// the environment and call other functions. It returns the result of the call or an
/// error if the call failed for some reason.
fn call(&self, interpreter: &mut Interpreter, arguments: Vec<Object>) -> Result<Object, RuntimeError>;
/// Returns the arity of the object.
fn arity(&self) -> usize;
}