-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compiler.cs
116 lines (104 loc) · 3.79 KB
/
Compiler.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using Muftec.Lib.CompilerStates;
namespace Muftec.Lib
{
/// <summary>
/// Class that describes a Muftec compiler.
/// </summary>
public static class Compiler
{
/// <summary>
/// Class that describes compiler output.
/// </summary>
public class CompilerOutput
{
/// <summary>
/// Gets or sets the variable list.
/// </summary>
public List<string> Variables { get; set; }
/// <summary>
/// Gets or sets the function dictionary.
/// </summary>
public Dictionary<string, Queue<MuftecStackItem>> Functions { get; set; }
/// <summary>
/// Gets or sets the name of the main function.
/// </summary>
public string MainFunctionName { get; set; }
/// <summary>
/// Gets or sets a stack item pointing to the main function.
/// </summary>
public MuftecStackItem MainFunction { get; set; }
}
/// <summary>
/// Parse a file into a stack queue.
/// </summary>
/// <param name="text">Text to parse.</param>
/// <returns>Returns compiler output.</returns>
public static CompilerOutput ParseString(string text)
{
var variables = new List<string>();
var functions = new Dictionary<string, Queue<MuftecStackItem>>();
var defines = new Dictionary<string, string>();
var appState = new ApplicationCore(variables, functions, defines);
var evaluator = new EvaluatorState(appState);
var lineNum = 1;
// Get each line to count
foreach (var line in text.Replace("\r", "").Split('\n'))
{
try
{
evaluator.EvaluateLine(line, lineNum);
lineNum++;
}
catch (Exception e)
{
Console.WriteLine("Error parsing at line " + lineNum);
Console.WriteLine("-----");
Console.WriteLine(line);
Console.WriteLine("-----");
#if DEBUG
throw;
#else
Console.WriteLine(e);
Console.WriteLine("-----");
return null;
#endif
}
}
// Return compiler output
return new CompilerOutput { Variables = variables, Functions = functions, MainFunctionName = evaluator.LastFunction, MainFunction = new MuftecStackItem(evaluator.LastFunction, MuftecAdvType.Function) };
}
/// <summary>
/// Save a compiled application into a byte array.
/// </summary>
/// <param name="compiled">Compiled application to save.</param>
/// <returns>Saved queue.</returns>
public static byte[] SaveApplication(CompilerOutput compiled)
{
using (var ms = new MemoryStream())
{
var bf = new BinaryFormatter();
bf.Serialize(ms, compiled);
return ms.ToArray();
}
}
/// <summary>
/// Load a compiled application from a byte array.
/// </summary>
/// <param name="data">Saved queue to decode.</param>
/// <returns>Original queue.</returns>
public static CompilerOutput LoadApplication(byte[] data)
{
using (var ms = new MemoryStream(data))
{
var bf = new BinaryFormatter();
var stack = bf.Deserialize(ms) as CompilerOutput;
return stack;
}
}
}
}