-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeWriter.cs
More file actions
34 lines (28 loc) · 1.16 KB
/
Copy pathCodeWriter.cs
File metadata and controls
34 lines (28 loc) · 1.16 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
using System.Collections.Generic;
namespace JackCompiling
{
public partial class CodeWriter
{
private readonly List<string> resultVmCode = new List<string>();
private readonly Dictionary<string, VarInfo> classSymbols;
private IReadOnlyDictionary<string, VarInfo> methodSymbols;
private string currentClassName = "";
public CodeWriter(Dictionary<string, VarInfo>? classSymbols = null, Dictionary<string, VarInfo>? methodSymbols = null)
{
this.classSymbols = classSymbols ?? new Dictionary<string, VarInfo>();
this.methodSymbols = methodSymbols ?? new Dictionary<string, VarInfo>();
}
public IReadOnlyList<string> ResultVmCode => resultVmCode;
private VarInfo? FindVarInfo(string varName)
{
if (!methodSymbols.TryGetValue(varName, out var varInfo) && !classSymbols.TryGetValue(varName, out varInfo))
return null;
return varInfo;
}
public void Write(string vmCodeLine)
{
resultVmCode.Add(vmCodeLine);
}
public string GetResult() => string.Join("\n", resultVmCode);
}
}