-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoxClass.cs
55 lines (47 loc) · 1.58 KB
/
LoxClass.cs
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
namespace cslox;
public class LoxClass : ILoxCallable
{
public readonly string Name;
public readonly LoxClass? Superclass;
private readonly Dictionary<string, LoxFunction> _methods;
public LoxClass(string name, LoxClass? superclass, Dictionary<string, LoxFunction> methods)
{
Name = name;
Superclass = superclass;
_methods = methods;
}
public override string ToString() => Name;
public int Arity => FindMethod("init")?.Arity ?? 0;
public object Call(Interpreter interpreter, List<object?> arguments)
{
var instance = new LoxInstance(this);
var initializer = FindMethod("init");
initializer?.Bind(instance).Call(interpreter, arguments);
return instance;
}
public LoxFunction? FindMethod(string name)
{
return _methods.TryGetValue(name, out var method) ? method : Superclass?.FindMethod(name);
}
}
public class LoxInstance
{
private LoxClass _loxClass;
private readonly Dictionary<string, object?> _fields = new();
public LoxInstance(LoxClass loxClass)
{
_loxClass = loxClass;
}
public override string ToString() => $"{_loxClass.Name} instance";
public object? Get(Token token)
{
if (_fields.TryGetValue(token.Lexeme, out var value)) return value;
var method = _loxClass.FindMethod(token.Lexeme);
if (method != null) return method.Bind(this);
throw new RuntimeException(token, $"Undefined property {token.Lexeme}.");
}
public void Set(Token token, object? value)
{
_fields[token.Lexeme] = value;
}
}