Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
68 lines (53 loc) · 1.48 KB
/
Copy pathProgram.cs
File metadata and controls
68 lines (53 loc) · 1.48 KB
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
using HERMESLANG.Lexer;
using HERMESLANG.Parser;
using HERMESLANG.Interpreter;
using HERMESLANG.Runtime.Functions;
using HERMESLANG.Runtime;
string codigo = """
abrir_portao()
"""; //essa parte "codigo" é onde aparece no terminal no final dele, onde testa se funciona
Lexer lexer = new Lexer(codigo);
List<Token> tokens = lexer.ScanTokens();
Parser parser = new Parser(tokens);
try
{
List<Statement> statements = parser.Parse();
Console.WriteLine("=== AST ===");
ASTPrinter printer = new ASTPrinter();
printer.Print(statements);
Console.WriteLine();
Console.WriteLine("=== INTERPRETER ===");
Interpreter interpreter = new Interpreter();
//abaixo é onde registra funções
interpreter.RegisterFunction(
new MostrarFunction()
);
interpreter.RegisterFunction(
new AbrirPortaFunction()
);
interpreter.Interpret(statements);
foreach (var variable in interpreter.GetVariables())
{
Console.WriteLine(
$"{variable.Key} = {variable.Value}"
);
}
Console.WriteLine("Programa executado com sucesso!");
}
catch (ParserException ex)
{
Console.WriteLine($"Erro de Parser: {ex.Message}");
}
catch (HermesException ex)
{
Console.WriteLine("=== ERRO HERMES ===");
if (ex.Line.HasValue)
{
Console.WriteLine($"Linha: {ex.Line.Value}");
}
Console.WriteLine($"Mensagem: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Erro de execução: {ex.Message}");
}