-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoxInstance.h
44 lines (35 loc) · 862 Bytes
/
LoxInstance.h
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
#pragma once
#include <string>
#include <unordered_map>
#include "LoxClass.h"
#include "Token.h"
#include "Lox.h"
class LoxInstance
{
std::unordered_map<std::string, Object> fields;
LoxClass *loxClass;
public:
LoxInstance(LoxClass *loxClass) : loxClass(loxClass) {}
std::string str()
{
return loxClass->name + " instance";
}
Object get(Token *name)
{
if (fields.count(name->lexeme))
{
return fields[name->lexeme];
}
LoxFunction* method = loxClass->findMethod(name->lexeme);
if (method)
{
return Object(method->bind(this));
}
Lox::runtimeError(*name, "Undefined property '" + name->lexeme + "'.");
return Object();
}
void set(Token *name, Object value)
{
fields[name->lexeme] = value;
}
};