-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
92 lines (74 loc) · 2.38 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace CSharpAlla
{
internal class Program
{
static void Main(string[] args)
{
string code = @"
#class Person {
# function Person(name, family) {
# this.name = name
# this.family = family
# }
# function GetFullname() {
# return name + "" "" + family
# }
#}
#ali = Person(""Ali"", ""Rezaee"")
#writeline(ali.name)
name = ""mahdi""
family = ""khalilzadeh""
fullname = name + "" "" + family
writeline(fullname, fullname == ""mahdi khalilzadeh"")
function sum(a,b) {
return a + b
}
function test(t) {
result = sum(t, t * 2)
writeline(result)
return result
}
writeline(test(10))
";
long totalMiliseconds = 0;
Stopwatch sw = new Stopwatch();
sw.Start();
Lexer lexer = new Lexer(code);
List<Token> tokens = lexer.Tokenize();
Console.WriteLine("[Lexer Finish] " + sw.ElapsedMilliseconds + "ms");
totalMiliseconds += sw.ElapsedMilliseconds;
sw.Reset();
sw.Start();
Parser parser = new Parser(tokens);
parser.Parse();
Console.WriteLine("[Parser Finish] " + sw.ElapsedMilliseconds + "ms");
totalMiliseconds += sw.ElapsedMilliseconds;
sw.Reset();
sw.Start();
try
{
Interpreter interpreter = new Interpreter();
//Console.WriteLine(interpreter.Disassemble(parser));
interpreter.Interpret(parser);
//Console.WriteLine(interpreter.variables.Reverse<object>().FirstOrDefault());
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
Console.WriteLine("[Interpreter Finish] " + sw.ElapsedMilliseconds + "ms");
totalMiliseconds += sw.ElapsedMilliseconds;
sw.Reset();
Console.WriteLine("[Total] " + totalMiliseconds + "ms");
Console.WriteLine(parser.constants.Count + " Constrator");
Console.WriteLine(parser.variables.Count + " Variables");
Console.WriteLine(parser.bytecodes.Count + " Bytecodes");
//Console.WriteLine("========= Disassembled =========");
//Console.WriteLine(interpreter.Disassemble(parser));
}
}
}