-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJackProgram.cs
More file actions
executable file
·49 lines (45 loc) · 1.58 KB
/
Copy pathJackProgram.cs
File metadata and controls
executable file
·49 lines (45 loc) · 1.58 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleCompiler
{
public class JackProgram : JackProgramElement
{
public List<VarDeclaration> Globals { get; private set; }
public List<Function> Functions { get; private set; }
public Function Main { get; private set; }
public override void Parse(TokensStack sTokens)
{
Globals = new List<VarDeclaration>();
while ((sTokens.Peek() is Statement) && ((Statement)sTokens.Peek()).Name == "var")
{
VarDeclaration global = new VarDeclaration();
global.Parse(sTokens);
Globals.Add(global);
}
Main = new Function();
Main.Parse(sTokens);
Functions = new List<Function>();
while (sTokens.Count > 0)
{
if (!(sTokens.Peek() is Statement) || ((Statement)sTokens.Peek()).Name != "function")
throw new SyntaxErrorException("Expected function", sTokens.Peek());
Function f = new Function();
f.Parse(sTokens);
Functions.Add(f);
}
}
public override string ToString()
{
string sProgram = "";
foreach (VarDeclaration v in Globals)
sProgram += "\t" + v + "\n";
sProgram += "\t" + Main + "\n";
foreach (Function f in Functions)
sProgram += "\t" + f + "\n";
return sProgram;
}
}
}