-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpreter.cs
98 lines (70 loc) · 1.92 KB
/
Interpreter.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
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
using System;
using System.Collections.Generic;
using LuaV.VM;
namespace LuaV {
public class LuaInterpreter {
public LuaParser Parser;
public LuaVM VM = new LuaVM();
public LuaObject Expression(Expr expr) {
if (expr.Type == "function") {
LuaFunction value = new LuaFunction();
for (int i = 0; i < expr.DefinedArgs.Count; i++) {
value.Upvalues[i] = new LuaObject(LuaType.Nil, null);
}
value.Func = expr;
return value;
}
return Math(expr);
}
public LuaObject Math(Expr expr) {
if (expr.Type == "binary") {
LuaObject left = Math(expr.Left);
LuaObject right = Math(expr.Right);
if (expr.Operator == "+") {
if (left.Type == LuaType.Number) {
if (right.Type == LuaType.Number) {
return LuaObject.Number(left.ToFloat() + right.ToFloat());
}
}
}
return null;
}
return Literal(expr);
}
public LuaObject Literal(Expr expr) {
if (expr.Type == "number") {
return LuaObject.Number(float.Parse(expr.Value.Value));
}
if (expr.Type == "string") {
return LuaObject.String(expr.Value.Value);
}
if (expr.Type == "boolean") {
// return LuaObject.Boolean(expr.Value.Value == "true");
}
if (expr.Type == "nil") {
return LuaObject.Nil();
}
if (expr.Type == "group") {
return Expression(expr.Left);
}
if (expr.Type == "table") {
LuaTable table = new LuaTable();
VM.Push(table);
for (int i = 0; i < expr.TKeys.Count; i++) {
VM.Push(Math(expr.TKeys[i]));
VM.Push(Math(expr.TValues[i]));
VM.SetTable(-3);
}
return VM.Pop();
}
if (expr.Type == "call") {
LuaFunction func = Expression(expr.Left) as LuaFunction;
if (func == null) {
throw new Exception("Attempt to call non-function");
}
Console.WriteLine("Call " + func.Func.Name);
}
return null;
}
}
}