-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProgram.cs
125 lines (100 loc) · 4.71 KB
/
Program.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
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
using IronWren.AutoMapper;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace IronWren.ConsoleTesting
{
internal class Program
{
private static void alloc(WrenVM vm)
{
Console.WriteLine("Allocator called!");
vm.SetSlotNewForeign(0, 1);
}
private static void Main(string[] args)
{
using (var vm = new WrenVM())
{
vm.Write += (vm, text) => Console.Write(text);
vm.Error += (vm, type, module, line, message) => Console.WriteLine($"Error [{type}] in module [{module}] at line {line}:{Environment.NewLine}{message}");
vm.LoadModule += (vm, module) => new WrenLoadModuleResult { Source = $"System.print(\"Module [{module}] loaded!\")" };
vm.BindForeignMethod += (vm, module, className, isStatic, signature) =>
{
Console.WriteLine($"BindForeignMethod called: It's called {signature}, is part of {className} and is {(isStatic ? "static" : "not static")}.");
return (signature == "sayHi(_)" ? sayHi : (WrenForeignMethod)null);
};
vm.BindForeignClass += (vm, module, className) => className == "Test" ? new WrenForeignClassMethods { Allocate = alloc } : null;
Console.WriteLine("Running Wren Version " + WrenVM.GetVersionNumber());
var result = vm.Interpret("System.print(\"Hi from Wren!\")");
result = vm.Interpret("var helloTo = Fn.new { |name|\n" +
"System.print(\"Hello, %(name)!\")\n" +
"}");
result = vm.Interpret("helloTo.call(\"IronWren\")");
var someFnHandle = vm.MakeCallHandle("call(_)");
vm.EnsureSlots(2);
vm.GetVariable(WrenVM.MainModule, "helloTo", 0);
vm.SetSlotString(1, "foreign method");
result = vm.Call(someFnHandle);
result = vm.Interpret("foreign class Test {\n" +
"construct new() { }\n" +
"isForeign { true }\n" +
"foreign sayHi(to)\n" +
"}\n" +
"var test = Test.new()\n" +
"test.sayHi(\"wren\")\n" +
"\n" +
"import \"TestModule\"\n");
vm.EnsureSlots(1);
vm.GetVariable(WrenVM.MainModule, "test", 0);
result = vm.Call(vm.MakeCallHandle("isForeign"));
var isTestClassForeign = vm.GetSlotBool(0);
Console.WriteLine("Test class is foreign: " + isTestClassForeign);
vm.AutoMap(typeof(WrenMath));
vm.Interpret("System.print(\"The sine of pi is: %(Math.sin(Math.pi))!\")");
Console.WriteLine($"And C# says it's: {Math.Sin(Math.PI)}");
Console.WriteLine();
var sw = new Stopwatch();
for (var i = 0; i < 3; ++i)
{
sw.Restart();
vm.Interpret("for (i in 1..1000000) Math.sin(Math.pi)");
sw.Stop();
Console.WriteLine("1000000 iterations took " + sw.ElapsedMilliseconds + "ms.");
}
var results = new double[1000000];
sw.Restart();
for (var i = 0; i < 1000000; ++i)
{
results[i] = Math.Sin(Math.PI);
}
sw.Stop();
Console.WriteLine("1000000 iterations in C# took " + sw.ElapsedMilliseconds + "ms.");
vm.AutoMap<WrenVector>();
vm.Interpret("var vec = Vector.new(1, 2)");
vm.Interpret("vec.print()");
vm.Interpret("System.print(\"Vector's X is: %(vec.x)\")");
vm.Interpret("System.print(\"Vector's Y is: %(vec.y)\")");
Console.ReadLine();
Console.Clear();
Console.WriteLine("You may now write Wren code that will be interpreted!");
Console.WriteLine("Use file:[path] to interpret a file!");
Console.WriteLine();
while (true)
{
var input = Console.ReadLine();
if (input.StartsWith("file:"))
vm.Interpret(File.ReadAllText(input.Substring(5)));
else
vm.Interpret(input);
}
}
}
private static void sayHi(WrenVM vm)
{
var to = vm.GetSlotString(1);
Console.WriteLine("Foreign method says hi to " + to + "!");
}
}
}