-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoxInstance.cpp
More file actions
35 lines (26 loc) · 829 Bytes
/
LoxInstance.cpp
File metadata and controls
35 lines (26 loc) · 829 Bytes
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
// Copyright 2020.2.23 <Copyright hulin>
#include <string>
#include "./LoxInstance.hpp"
#include "./LoxClass.hpp"
#include "./RuntimeError.hpp"
#include "./Token.hpp"
using std::string;
LoxInstance::LoxInstance(LoxClass klass_): klass(klass_) {}
string LoxInstance::toString() {
return klass.name + " instance";
}
Object LoxInstance::get(Token name) {
auto searched = fields.find(name.lexeme);
if (searched != fields.end()) {
return searched->second;
}
shared_ptr<LoxFunction> method = klass.findMethod(name.lexeme);
if (method != nullptr) {
return Object::make_fun_obj(method->bind(shared_from_this()));
}
throw RuntimeError(name,
"Undefined property '" + name.lexeme + "'.");
}
void LoxInstance::set(Token name, Object value) {
fields[name.lexeme] = value;
}