-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.rs
More file actions
211 lines (194 loc) · 6.51 KB
/
Copy pathobject.rs
File metadata and controls
211 lines (194 loc) · 6.51 KB
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
use crate::ast::{BlockStatement, Identifier};
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt;
use std::rc::Rc;
/// Represents a compiled function's bytecode metadata.
#[derive(Debug, PartialEq, Clone)]
pub struct CompiledFunction {
pub instructions_offset: usize,
pub num_locals: usize,
pub num_params: usize,
}
#[derive(Debug, PartialEq, Clone)]
pub enum Object {
Integer(i64),
Boolean(bool),
Null,
ReturnValue(Box<Object>),
Error(String),
Function(Vec<Identifier>, BlockStatement, Rc<RefCell<Environment>>),
String(String),
Array(Vec<Object>),
Class(Rc<RefCell<Class>>),
ClassInstance(Rc<RefCell<ClassInstance>>),
Struct(Rc<RefCell<Struct>>),
StructInstance(Rc<RefCell<StructInstance>>),
Interface(Rc<RefCell<Interface>>),
Method(Rc<RefCell<Method>>),
CompiledFunction(CompiledFunction),
Closure(Box<CompiledFunction>, Vec<Object>),
BuiltinFunction {
name: String,
num_params: i32,
handler: fn(Vec<Object>) -> Object,
},
}
impl fmt::Display for Object {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Object::Integer(value) => write!(f, "{}", value),
Object::Boolean(value) => write!(f, "{}", value),
Object::Null => write!(f, "null"),
Object::ReturnValue(value) => write!(f, "{}", value),
Object::Error(message) => write!(f, "ERROR: {}", message),
Object::Function(parameters, body, _) => {
let params: Vec<String> = parameters.iter().map(|p| p.value.clone()).collect();
write!(f, "fn({}) {{\n{}\n}}", params.join(", "), body)
}
Object::String(value) => write!(f, "{}", value),
Object::Array(elements) => {
let elements: Vec<String> = elements.iter().map(|e| e.to_string()).collect();
write!(f, "[{}]", elements.join(", "))
}
Object::Class(c) => write!(f, "class {}", c.borrow().name),
Object::ClassInstance(i) => write!(f, "instance of {}", i.borrow().class.borrow().name),
Object::Struct(s) => write!(f, "struct {}", s.borrow().name),
Object::StructInstance(i) => write!(
f,
"instance of struct {}",
i.borrow().struct_def.borrow().name
),
Object::Interface(i) => write!(f, "interface {}", i.borrow().name),
Object::Method(m) => write!(f, "method {}", m.borrow().name),
Object::CompiledFunction(cf) => write!(
f,
"compiled fn(offset={}, locals={}, params={})",
cf.instructions_offset, cf.num_locals, cf.num_params
),
Object::Closure(cf, free) => write!(
f,
"closure(offset={}, free={})",
cf.instructions_offset,
free.len()
),
Object::BuiltinFunction { name, .. } => write!(f, "builtin fn {}", name),
}
}
}
#[allow(dead_code)]
const INTEGER: &str = "INTEGER";
#[allow(dead_code)]
const BOOLEAN: &str = "BOOLEAN";
#[allow(dead_code)]
const NULL: &str = "NULL";
#[allow(dead_code)]
const RETURN_VALUE: &str = "RETURN_VALUE";
#[allow(dead_code)]
const ERROR: &str = "ERROR";
#[allow(dead_code)]
const FUNCTION: &str = "FUNCTION";
#[allow(dead_code)]
const STRING: &str = "STRING";
#[allow(dead_code)]
const ARRAY: &str = "ARRAY";
#[allow(dead_code)]
const CLASS: &str = "CLASS";
#[allow(dead_code)]
const CLASS_INSTANCE: &str = "CLASS_INSTANCE";
#[allow(dead_code)]
const STRUCT: &str = "STRUCT";
#[allow(dead_code)]
const STRUCT_INSTANCE: &str = "STRUCT_INSTANCE";
#[allow(dead_code)]
const INTERFACE: &str = "INTERFACE";
#[allow(dead_code)]
const METHOD: &str = "METHOD";
impl Object {
pub fn type_str(&self) -> &str {
match self {
Object::Integer(_) => INTEGER,
Object::Boolean(_) => BOOLEAN,
Object::Null => NULL,
Object::ReturnValue(_) => RETURN_VALUE,
Object::Error(_) => ERROR,
Object::Function(_, _, _) => FUNCTION,
Object::String(_) => STRING,
Object::Array(_) => ARRAY,
Object::Class(_) => "CLASS",
Object::ClassInstance(_) => "CLASS_INSTANCE",
Object::Struct(_) => "STRUCT",
Object::StructInstance(_) => "STRUCT_INSTANCE",
Object::Interface(_) => "INTERFACE",
Object::Method(_) => "METHOD",
Object::CompiledFunction(_) => "COMPILED_FUNCTION",
Object::Closure(_, _) => "CLOSURE",
Object::BuiltinFunction { .. } => "BUILTIN_FUNCTION",
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct Class {
pub name: String,
pub super_class: Option<Rc<RefCell<Class>>>,
pub interfaces: Vec<Rc<RefCell<Interface>>>,
pub properties: HashMap<String, Object>,
pub methods: HashMap<String, Rc<RefCell<Method>>>,
}
#[derive(Debug, PartialEq, Clone)]
pub struct ClassInstance {
pub class: Rc<RefCell<Class>>,
pub fields: HashMap<String, Object>,
}
#[derive(Debug, PartialEq, Clone)]
pub struct Struct {
pub name: String,
pub properties: HashMap<String, Object>,
}
#[derive(Debug, PartialEq, Clone)]
pub struct StructInstance {
pub struct_def: Rc<RefCell<Struct>>,
pub fields: HashMap<String, Object>,
}
#[derive(Debug, PartialEq, Clone)]
pub struct Interface {
pub name: String,
pub method_signatures: HashMap<String, MethodSignature>,
}
#[derive(Debug, PartialEq, Clone)]
pub struct Method {
pub name: String,
pub parameters: Vec<Identifier>,
pub body: BlockStatement,
pub env: Rc<RefCell<Environment>>,
pub this: Option<Rc<RefCell<ClassInstance>>>,
}
#[derive(Debug, PartialEq, Clone)]
pub struct MethodSignature {
pub name: String,
pub parameters: Vec<Identifier>,
}
#[derive(Debug, PartialEq, Clone, Default)]
pub struct Environment {
store: HashMap<String, Object>,
outer: Option<Rc<RefCell<Environment>>>,
}
impl Environment {
pub fn new() -> Self {
Default::default()
}
pub fn new_enclosed(outer: Rc<RefCell<Environment>>) -> Self {
let mut env = Environment::new();
env.outer = Some(outer);
env
}
pub fn get(&self, name: &str) -> Option<Object> {
match self.store.get(name) {
Some(obj) => Some(obj.clone()),
None => self.outer.as_ref().and_then(|o| o.borrow().get(name)),
}
}
pub fn set(&mut self, name: String, val: Object) {
self.store.insert(name, val);
}
}