move interesting projects to the root

This commit is contained in:
2023-01-07 20:06:39 +01:00
parent 25c65d210b
commit 535aa6b145
45 changed files with 1 additions and 0 deletions

63
JackCompiler/.gitattributes vendored Normal file
View File

@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp
###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary
###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary
###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain

341
JackCompiler/.gitignore vendored Normal file
View File

@@ -0,0 +1,341 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- Backup*.rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# BeatPulse healthcheck temp database
healthchecksdb
/JackAnalyzer/Properties/launchSettings.json

View File

@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29411.108
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JackAnalyzer", "JackAnalyzer\JackAnalyzer.csproj", "{358F80E6-7CE3-4EE3-B15F-C1D2B1EB3D67}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JackAnalyzerTest", "SymbolTableTest\JackAnalyzerTest.csproj", "{54357789-1274-4FCB-8EEA-80C75EC656CE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{358F80E6-7CE3-4EE3-B15F-C1D2B1EB3D67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{358F80E6-7CE3-4EE3-B15F-C1D2B1EB3D67}.Debug|Any CPU.Build.0 = Debug|Any CPU
{358F80E6-7CE3-4EE3-B15F-C1D2B1EB3D67}.Release|Any CPU.ActiveCfg = Release|Any CPU
{358F80E6-7CE3-4EE3-B15F-C1D2B1EB3D67}.Release|Any CPU.Build.0 = Release|Any CPU
{54357789-1274-4FCB-8EEA-80C75EC656CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{54357789-1274-4FCB-8EEA-80C75EC656CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{54357789-1274-4FCB-8EEA-80C75EC656CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{54357789-1274-4FCB-8EEA-80C75EC656CE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D5BD3FCB-D928-41AD-AD56-948B65966B3A}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace JackAnalyzer
{
class Constants
{
public const string inExt = ".jack";
public static readonly char[] symbols = { '{', '}', '(', ')', '[', ']', '.', ',', ';', '+', '-', '*', '/', '&', '|', '<', '>', '=', '~' };
public static readonly string[] keywords = { "class", "constructor", "function", "method", "field", "static", "var", "int", "char", "boolean", "void", "true", "false", "null", "this", "let", "do", "if", "else", "while", "return" };
public static readonly char[] op = { '+', '-', '*', '/', '&', '|', '<', '>', '=' };
public static readonly char[] unaryOp = { '-', '~' };
public const int indentLevel = 2;
}
}

View File

@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace JackAnalyzer
{
public class MetaSymbolTable
{
private SymbolTable ClassSymbolTable = new SymbolTable();
private SymbolTable FunctionSymbolTable = new SymbolTable();
public MetaSymbolTable()
{
}
public void Define(string name, string type, SymbolKind kind)
{
if (kind == SymbolKind.Field || kind == SymbolKind.Static)
{
ClassSymbolTable.Define(name, type, kind);
}
else
{
FunctionSymbolTable.Define(name, type, kind);
}
}
internal void ResetFieldTable()
{
ClassSymbolTable.ResetFields();
}
public int VarCount(SymbolKind kind)
{
if (kind == SymbolKind.Field || kind == SymbolKind.Static)
{
return ClassSymbolTable.VarCount(kind);
}
else
{
return FunctionSymbolTable.VarCount(kind);
}
}
public SymbolKind KindOf(string name)
{
try
{
return FunctionSymbolTable.KindOf(name);
}
catch
{
return ClassSymbolTable.KindOf(name);
}
}
public string TypeOf(string name)
{
try
{
return FunctionSymbolTable.TypeOf(name);
}
catch
{
return ClassSymbolTable.TypeOf(name);
}
}
public int IndexOf(string name)
{
try
{
return FunctionSymbolTable.IndexOf(name);
}
catch
{
return ClassSymbolTable.IndexOf(name);
}
}
public void ResetFunctionTable()
{
FunctionSymbolTable = new SymbolTable();
}
}
}

View File

@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace JackAnalyzer
{
class Program
{
static int Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Not enough arguments");
return 1;
}
string inPath = args[0];
List<string> inFiles = new List<string>();
if (Directory.Exists(inPath))
{
string[] files = Directory.GetFiles(inPath);
foreach(string file in files)
{
if(Path.GetExtension(file) == Constants.inExt)
{
inFiles.Add(file);
}
}
}
else if (File.Exists(inPath))
{
if (Path.GetExtension(inPath) == Constants.inExt)
{
inFiles.Add(inPath);
}
}
else
{
Console.WriteLine("No input files");
return 2;
}
foreach(string file in inFiles)
{
Tokenizer tokenizer = new Tokenizer(file);
tokenizer.WriteTokens(file);
var tokens = tokenizer.Tokens;
Writer writer = new Writer(file, tokens);
}
return 0;
}
}
}

View File

@@ -0,0 +1,71 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JackAnalyzer
{
public enum SymbolKind
{
Static,
Field,
Arg,
Var,
None
}
public class SymbolTable
{
Dictionary<string, ValueTuple<string, SymbolKind, int>> table = new Dictionary<string, (string, SymbolKind, int)>();
Dictionary<SymbolKind, int> kindCounter = new Dictionary<SymbolKind, int>();
public SymbolTable()
{
}
public void Define(string name, string type, SymbolKind kind)
{
int count;
kindCounter.TryGetValue(kind, out count);
kindCounter[kind] = count + 1;
table.Add(name, (type, kind, count));
}
public int VarCount(SymbolKind kind)
{
try
{
return kindCounter[kind];
} catch
{
return 0;
}
}
public SymbolKind KindOf(string name)
{
return table[name].Item2;
}
public string TypeOf(string name)
{
return table[name].Item1;
}
public int IndexOf(string name)
{
return table[name].Item3;
}
public void ResetFields()
{
kindCounter[SymbolKind.Field] = 0;
var itemsToRemove = table.Where(f => f.Value.Item2 == SymbolKind.Field).ToArray();
foreach (var item in itemsToRemove)
{
table.Remove(item.Key);
}
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace JackAnalyzer
{
enum TokenType
{
keyword,
identifier,
symbol,
integerConstant,
stringConstant
}
}

View File

@@ -0,0 +1,179 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using System.Security;
namespace JackAnalyzer
{
class Tokenizer
{
public List<KeyValuePair<TokenType, string>> Tokens { get; }
public Tokenizer(string file)
{
Tokens = new List<KeyValuePair<TokenType, string>>();
string[] text = File.ReadAllLines(file);
string word = "";
bool readingComment = false;
foreach (string line in text)
{
// Ignore comments and empty lines
if (line.Length > 0)
{
bool readingString = false;
// This is used for comment detection
char prevSymbol = '\0';
foreach (char c in line)
{
if (readingComment)
{
if (c == '/' && prevSymbol == '*')
{
readingComment = false;
continue;
}
else
{
prevSymbol = c;
continue;
}
}
if (c == '"')
{
if (readingString)
{
readingString = false;
PutStringConst(word);
word = "";
continue;
}
else
{
readingString = true;
continue;
}
}
if (readingString)
{
word += c;
continue;
}
if (Constants.symbols.Contains(c))
{
// Detect comments
if (prevSymbol == '/' && c == '/')
{
// If we're looking at a comment, that means
// we need to remove the previous symbol and
// discard the rest of the string
if (Tokens.Count > 0)
{
Tokens.RemoveAt(Tokens.Count - 1);
}
break;
}
if (prevSymbol == '/' && c == '*')
{
// If we're looking at a multi-line, that means
// we need to remove the previous symbol and
// discard the rest of the comment
if (Tokens.Count > 0)
{
Tokens.RemoveAt(Tokens.Count - 1);
}
readingComment = true;
continue;
}
prevSymbol = c;
PutString(word);
PutString(char.ToString(c));
word = "";
continue;
}
word += c;
if (c == ' ')
{
PutString(word);
word = "";
}
}
}
}
}
public void WriteTokens(string path)
{
string filename = Path.GetDirectoryName(path) + '/' + Path.GetFileNameWithoutExtension(path) + "T.xml";
using (StreamWriter file = new StreamWriter(filename))
{
file.WriteLine("<tokens>");
foreach (var token in Tokens)
{
var name = Enum.GetName(typeof(TokenType), token.Key);
var value = token.Value;
file.WriteLine('<' + name + "> " + SecurityElement.Escape(value) + " </" + name + '>');
}
file.WriteLine("</tokens>");
}
}
private void PutStringConst(string token)
{
var str = new KeyValuePair<TokenType, string>(TokenType.stringConstant, token);
Tokens.Add(str);
return;
}
private void PutString(string token)
{
var trimmed = token.Trim();
if (trimmed.Length == 1 && Constants.symbols.Contains(trimmed[0]))
{
var symbol = new KeyValuePair<TokenType, string>(TokenType.symbol, trimmed);
Tokens.Add(symbol);
return;
}
if (Constants.keywords.Contains(trimmed))
{
var keyword = new KeyValuePair<TokenType, string>(TokenType.keyword, trimmed);
Tokens.Add(keyword);
return;
}
if (int.TryParse(trimmed, out _))
{
var integerConstant = new KeyValuePair<TokenType, string>(TokenType.integerConstant, trimmed);
Tokens.Add(integerConstant);
return;
}
if (trimmed.Length > 0)
{
var identifier = new KeyValuePair<TokenType, string>(TokenType.identifier, trimmed);
Tokens.Add(identifier);
return;
}
}
}
}

View File

@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace JackAnalyzer
{
enum Segment
{
Constant, Argument, Local, Static, This, That, Pointer, Temp
}
enum ArithmeticOp
{
Add, Sub, Neg, Eq, Gt, Lt, And, Or, Not
}
class VMWriter
{
StreamWriter file;
List<string> cache = new List<string>();
int funcDef = 0;
public VMWriter(string path)
{
string filename = Path.ChangeExtension(path, ".vm");
file = new StreamWriter(filename);
file.AutoFlush = true;
}
public void Dispose()
{
foreach (string l in cache)
{
file.WriteLine(l);
}
file.Close();
file.Dispose();
}
public void WritePush(Segment segment, int index)
{
cache.Add($"push {Enum.GetName(typeof(Segment),segment).ToLower()} {index}");
}
public void WritePop(Segment segment, int index)
{
cache.Add($"pop {Enum.GetName(typeof(Segment), segment).ToLower()} {index}");
}
public void WriteArithmetic(ArithmeticOp op)
{
cache.Add(Enum.GetName(typeof(ArithmeticOp), op).ToLower());
}
public void WriteLabel(string label)
{
cache.Add($"label {label}");
}
public void WriteGoto(string label)
{
cache.Add($"goto {label}");
}
public void WriteIf(string label)
{
cache.Add($"if-goto {label}");
}
public void WriteCall(string name, int nargs)
{
cache.Add($"call {name} {nargs}");
}
public void WriteFunction(string name, int nlocal)
{
cache.Add($"function {name} {nlocal}");
funcDef = cache.Count - 1;
}
public void RewriteFunctionLocals(string name, int nlocal)
{
cache[funcDef]=($"function {name} {nlocal}");
}
public void WriteReturn()
{
cache.Add("return");
}
}
}

View File

@@ -0,0 +1,914 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security;
using System.Linq;
namespace JackAnalyzer
{
class Writer
{
private Queue<KeyValuePair<TokenType, string>> tokenQueue;
private static MetaSymbolTable MetaSymbolTable;
VMWriter VMWriter;
string ClassName;
int LabelCounter = 0;
public Writer(string path, List<KeyValuePair<TokenType, string>> tokens)
{
if(MetaSymbolTable == null)
{
MetaSymbolTable = new MetaSymbolTable();
} else
{
MetaSymbolTable.ResetFieldTable();
}
VMWriter = new VMWriter(path);
string filename = Path.ChangeExtension(path, ".xml");
ClassName = Path.GetFileNameWithoutExtension(filename);
tokenQueue = new Queue<KeyValuePair<TokenType, string>>(tokens);
using (StreamWriter file = new StreamWriter(filename))
{
var token = tokenQueue.Peek();
if (token.Key == TokenType.keyword)
{
string keyword = token.Value;
if (keyword == "class")
{
string[] classLines = CompileClass();
foreach (string line in classLines)
{
file.WriteLine(line);
}
}
}
}
VMWriter.Dispose();
}
// 'class' className '{' classVarDec* subroutineDec* '}'
private string[] CompileClass()
{
List<string> output = new List<string>();
output.Add(PutOpenTag(0, "class"));
output.Add(PutToken(1));
var token = tokenQueue.Dequeue();
output.Add(PutLineTag(1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "class, defined"));
output.Add(PutToken(1));
// classVarDec*
token = tokenQueue.Peek();
while (token.Value == "static" || token.Value == "field")
{
output.AddRange(CompileClassVarDec(1));
token = tokenQueue.Peek();
}
// subroutineDec*
token = tokenQueue.Peek();
while (token.Value == "function" || token.Value == "constructor" || token.Value == "method" || token.Value == "void")
{
output.AddRange(CompileSubroutine(1));
token = tokenQueue.Peek();
}
// }
output.Add(PutToken(1));
output.Add(PutCloseTag(0, "class"));
return output.ToArray();
}
// ('static' | 'field') type varName (',' varName)* ';'
private string[] CompileClassVarDec(int indent)
{
List<string> output = new List<string>();
output.Add(PutOpenTag(indent, "classVarDec"));
var token = tokenQueue.Dequeue();
SymbolKind kind;
switch (token.Value)
{
case "field":
kind = SymbolKind.Field;
break;
case "static":
kind = SymbolKind.Static;
break;
default:
throw new ArgumentOutOfRangeException();
}
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value));
token = tokenQueue.Dequeue();
string type = token.Value;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value));
token = tokenQueue.Dequeue();
string name = token.Value;
MetaSymbolTable.Define(name, type, kind);
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + type + " " + "kind=" + Enum.GetName(typeof(SymbolKind), kind) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "declared"));
token = tokenQueue.Peek();
while (token.Value == ",")
{
output.Add(PutToken(indent + 1));
token = tokenQueue.Dequeue();
name = token.Value;
MetaSymbolTable.Define(name, type, kind);
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + type + " " + "kind=" + Enum.GetName(typeof(SymbolKind), kind) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "declared"));
token = tokenQueue.Peek();
}
output.Add(PutToken(indent + 1));
output.Add(PutCloseTag(indent, "classVarDec"));
return output.ToArray();
}
// ('constructor' | 'function' | 'method')
// ('void' | type) subroutineName '(' parameterList ')' subroutineBody
private string[] CompileSubroutine(int indent)
{
List<string> output = new List<string>();
output.Add(PutOpenTag(indent, "subroutineDec"));
MetaSymbolTable.ResetFunctionTable();
var token = tokenQueue.Dequeue();
string type = token.Value;
output.Add(PutToken(indent + 1));
token = tokenQueue.Dequeue();
string name = token.Value;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "subroutine, defined"));
output.Add(PutToken(indent + 1));
// parameterList
if(type == "method")
{
MetaSymbolTable.Define("this", ClassName, SymbolKind.Arg);
}
output.AddRange(CompileParameterList(indent + 1));
VMWriter.WriteFunction(ClassName + "." + name, MetaSymbolTable.VarCount(SymbolKind.Var));
output.Add(PutToken(indent + 1));
switch(type)
{
case "constructor":
VMWriter.WritePush(Segment.Constant, MetaSymbolTable.VarCount(SymbolKind.Field));
VMWriter.WriteCall("Memory.alloc", 1);
VMWriter.WritePop(Segment.Pointer, 0);
break;
case "function":
break;
case "method":
VMWriter.WritePush(Segment.Argument, 0);
VMWriter.WritePop(Segment.Pointer, 0);
break;
}
output.AddRange(CompileSubroutineBody(indent + 1));
VMWriter.RewriteFunctionLocals(ClassName + "." + name, MetaSymbolTable.VarCount(SymbolKind.Var));
output.Add(PutCloseTag(indent, "subroutineDec"));
return output.ToArray();
}
// '{' varDec* statements '}'
private string[] CompileSubroutineBody(int indent)
{
List<string> output = new List<string>();
output.Add(PutOpenTag(indent, "subroutineBody"));
output.Add(PutToken(indent + 1));
// varDec*
var token = tokenQueue.Peek();
while (token.Value == "var")
{
output.AddRange(CompileVarDec(indent + 1));
token = tokenQueue.Peek();
}
output.AddRange(CompileStatements(indent + 1));
// }
output.Add(PutToken(indent + 1));
output.Add(PutCloseTag(indent, "subroutineBody"));
return output.ToArray();
}
// statement*
private string[] CompileStatements(int indent)
{
List<string> output = new List<string>();
output.Add(PutOpenTag(indent, "statements"));
bool finished = false;
var token = tokenQueue.Peek();
while (!finished)
{
switch (token.Value)
{
case "do":
output.AddRange(CompileDoStatement(indent + 1));
break;
case "let":
output.AddRange(CompileLetStatement(indent + 1));
break;
case "while":
output.AddRange(CompileWhileStatement(indent + 1));
break;
case "return":
output.AddRange(CompileReturnStatement(indent + 1));
break;
case "if":
output.AddRange(CompileIfStatement(indent + 1));
break;
default:
finished = true;
break;
}
token = tokenQueue.Peek();
}
output.Add(PutCloseTag(indent, "statements"));
return output.ToArray();
}
// 'do' subroutineCall ';'
private string[] CompileDoStatement(int indent)
{
List<string> output = new List<string>();
output.Add(PutOpenTag(indent, "doStatement"));
output.Add(PutToken(indent + 1));
var queueEnumerator = tokenQueue.GetEnumerator();
queueEnumerator.MoveNext();
queueEnumerator.MoveNext();
var nextToken = queueEnumerator.Current;
string name = "";
int ellen = 0;
// subroutineName '(' expressionList ')' | (className |
// varName) '.' subroutineName '(' expressionList ')'
if (nextToken.Value == ".")
{
var token = tokenQueue.Dequeue();
string classname = token.Value;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "class, used"));
output.Add(PutToken(indent + 1));
token = tokenQueue.Dequeue();
string fname = token.Value;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "function, used"));
output.Add(PutToken(indent + 1));
try
{
MetaSymbolTable.KindOf(classname);
Segment seg = Segment.Local;
switch (MetaSymbolTable.KindOf(classname))
{
case SymbolKind.Static:
seg = Segment.Static;
break;
case SymbolKind.Field:
seg = Segment.This;
break;
case SymbolKind.Arg:
seg = Segment.Argument;
break;
case SymbolKind.Var:
seg = Segment.Local;
break;
case SymbolKind.None:
break;
default:
break;
}
VMWriter.WritePush(seg, MetaSymbolTable.IndexOf(classname));
ellen += 1;
}
catch
{
}
ellen += CompileExpressionList(indent + 1);
output.Add(PutToken(indent + 1));
try
{
name = MetaSymbolTable.TypeOf(classname) + "." + fname;
} catch
{
name = classname + "." + fname;
}
}
else if (nextToken.Value == "(")
{
VMWriter.WritePush(Segment.Pointer, 0);
var token = tokenQueue.Dequeue();
name = ClassName + "." + token.Value;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "function, used"));
output.Add(PutToken(indent + 1));
ellen += CompileExpressionList(indent + 1);
ellen += 1;
output.Add(PutToken(indent + 1));
}
// ;
output.Add(PutToken(indent + 1));
VMWriter.WriteCall(name, ellen);
VMWriter.WritePop(Segment.Temp, 0);
output.Add(PutCloseTag(indent, "doStatement"));
return output.ToArray();
}
// 'let' varName ('[' expression ']')? '=' expression ';'
private string[] CompileLetStatement(int indent)
{
List<string> output = new List<string>();
output.Add(PutOpenTag(indent, "letStatement"));
output.Add(PutToken(indent + 1));
var token = tokenQueue.Dequeue();
string name = token.Value;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + MetaSymbolTable.TypeOf(name) + " " + "kind=" + Enum.GetName(typeof(SymbolKind), MetaSymbolTable.KindOf(name)) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "used"));
token = tokenQueue.Peek();
if (token.Value == "[")
{
output.Add(PutToken(indent + 1));
Segment sega = Segment.Local;
switch (MetaSymbolTable.KindOf(name))
{
case SymbolKind.Static:
sega = Segment.Static;
break;
case SymbolKind.Field:
sega = Segment.This;
break;
case SymbolKind.Arg:
sega = Segment.Argument;
break;
case SymbolKind.Var:
sega = Segment.Local;
break;
case SymbolKind.None:
break;
default:
break;
}
VMWriter.WritePush(sega, MetaSymbolTable.IndexOf(name));
output.AddRange(CompileExpression(indent + 1));
VMWriter.WriteArithmetic(ArithmeticOp.Add);
output.Add(PutToken(indent + 1));
output.Add(PutToken(indent + 1));
output.AddRange(CompileExpression(indent + 1));
output.Add(PutToken(indent + 1));
VMWriter.WritePop(Segment.Temp, 0);
VMWriter.WritePop(Segment.Pointer, 1);
VMWriter.WritePush(Segment.Temp, 0);
VMWriter.WritePop(Segment.That, 0);
}
else
{
output.Add(PutToken(indent + 1));
output.AddRange(CompileExpression(indent + 1));
output.Add(PutToken(indent + 1));
Segment seg = Segment.Local;
switch (MetaSymbolTable.KindOf(name))
{
case SymbolKind.Static:
seg = Segment.Static;
break;
case SymbolKind.Field:
seg = Segment.This;
break;
case SymbolKind.Arg:
seg = Segment.Argument;
break;
case SymbolKind.Var:
seg = Segment.Local;
break;
case SymbolKind.None:
break;
default:
break;
}
VMWriter.WritePop(seg, MetaSymbolTable.IndexOf(name));
}
output.Add(PutCloseTag(indent, "letStatement"));
return output.ToArray();
}
// 'while' '(' expression ')' '{' statements '}'
private string[] CompileWhileStatement(int indent)
{
List<string> output = new List<string>();
LabelCounter++;
int localcounter = LabelCounter;
output.Add(PutOpenTag(indent, "whileStatement"));
VMWriter.WriteLabel("WHILE" + localcounter + 0);
output.Add(PutToken(indent + 1));
output.Add(PutToken(indent + 1));
output.AddRange(CompileExpression(indent + 1));
VMWriter.WriteArithmetic(ArithmeticOp.Not);
VMWriter.WriteIf("WHILE" + localcounter + 1);
output.Add(PutToken(indent + 1));
output.Add(PutToken(indent + 1));
output.AddRange(CompileStatements(indent + 1));
VMWriter.WriteGoto("WHILE" + localcounter + 0);
output.Add(PutToken(indent + 1));
VMWriter.WriteLabel("WHILE" + localcounter + 1);
output.Add(PutCloseTag(indent, "whileStatement"));
return output.ToArray();
}
// 'return' expression? ';'
private string[] CompileReturnStatement(int indent)
{
List<string> output = new List<string>();
output.Add(PutOpenTag(indent, "returnStatement"));
output.Add(PutToken(indent + 1));
var token = tokenQueue.Peek();
if (token.Value != ";")
{
output.AddRange(CompileExpression(indent + 1));
} else
{
VMWriter.WritePush(Segment.Constant, 0);
}
output.Add(PutToken(indent + 1));
VMWriter.WriteReturn();
output.Add(PutCloseTag(indent, "returnStatement"));
return output.ToArray();
}
// 'if' '(' expression ')' '{' statements '}' ('else' '{' statements '}')?
private string[] CompileIfStatement(int indent)
{
List<string> output = new List<string>();
LabelCounter++;
int localcounter = LabelCounter;
output.Add(PutOpenTag(indent, "ifStatement"));
output.Add(PutToken(indent + 1));
output.Add(PutToken(indent + 1));
output.AddRange(CompileExpression(indent + 1));
VMWriter.WriteArithmetic(ArithmeticOp.Not);
VMWriter.WriteIf("IF" + localcounter + 0);
output.Add(PutToken(indent + 1));
output.Add(PutToken(indent + 1));
output.AddRange(CompileStatements(indent + 1));
output.Add(PutToken(indent + 1));
VMWriter.WriteGoto("IF" + localcounter + 1);
VMWriter.WriteLabel("IF" + localcounter + 0);
var token = tokenQueue.Peek();
if (token.Value == "else")
{
output.Add(PutToken(indent + 1));
output.Add(PutToken(indent + 1));
output.AddRange(CompileStatements(indent + 1));
output.Add(PutToken(indent + 1));
}
VMWriter.WriteLabel("IF" + localcounter + 1);
output.Add(PutCloseTag(indent, "ifStatement"));
return output.ToArray();
}
// 'var' type varName (',' varName)* ';'
private string[] CompileVarDec(int indent)
{
List<string> output = new List<string>();
output.Add(PutOpenTag(indent, "varDec"));
var token = tokenQueue.Dequeue();
SymbolKind kind = SymbolKind.Var;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value));
token = tokenQueue.Dequeue();
string type = token.Value;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value));
token = tokenQueue.Dequeue();
string name = token.Value;
MetaSymbolTable.Define(name, type, kind);
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + type + " " + "kind=" + Enum.GetName(typeof(SymbolKind), kind) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "declared"));
token = tokenQueue.Peek();
while (token.Value == ",")
{
output.Add(PutToken(indent + 1));
token = tokenQueue.Dequeue();
name = token.Value;
MetaSymbolTable.Define(name, type, kind);
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + type + " " + "kind=" + Enum.GetName(typeof(SymbolKind), kind) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "declared"));
token = tokenQueue.Peek();
}
output.Add(PutToken(indent + 1));
output.Add(PutCloseTag(indent, "varDec"));
return output.ToArray();
}
// ((type varName) (',' type varName)*)?
private string[] CompileParameterList(int indent)
{
List<string> output = new List<string>();
output.Add(PutOpenTag(indent, "parameterList"));
var token = tokenQueue.Peek();
if (token.Key == TokenType.keyword || token.Key == TokenType.identifier)
{
token = tokenQueue.Dequeue();
string type = token.Value;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value));
token = tokenQueue.Dequeue();
string name = token.Value;
SymbolKind kind = SymbolKind.Arg;
MetaSymbolTable.Define(name, type, kind);
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + type + " " + "kind=" + Enum.GetName(typeof(SymbolKind), kind) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "declared"));
token = tokenQueue.Peek();
while (token.Value == ",")
{
output.Add(PutToken(indent + 1));
token = tokenQueue.Dequeue();
type = token.Value;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value));
token = tokenQueue.Dequeue();
name = token.Value;
kind = SymbolKind.Arg;
MetaSymbolTable.Define(name, type, kind);
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + type + " " + "kind=" + Enum.GetName(typeof(SymbolKind), kind) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "declared"));
token = tokenQueue.Peek();
}
}
output.Add(PutCloseTag(indent, "parameterList"));
return output.ToArray();
}
// term (op term)*
private string[] CompileExpression(int indent)
{
List<string> output = new List<string>();
output.Add(PutOpenTag(indent, "expression"));
output.AddRange(CompileTerm(indent + 1));
var token = tokenQueue.Peek();
while (Constants.op.Contains(token.Value[0]))
{
string op = token.Value;
output.Add(PutToken(indent + 1));
output.AddRange(CompileTerm(indent + 1));
switch (op)
{
case "+":
VMWriter.WriteArithmetic(ArithmeticOp.Add);
break;
case "-":
VMWriter.WriteArithmetic(ArithmeticOp.Sub);
break;
case "*":
VMWriter.WriteCall("Math.multiply", 2);
break;
case "/":
VMWriter.WriteCall("Math.divide", 2);
break;
case "&":
VMWriter.WriteArithmetic(ArithmeticOp.And);
break;
case "|":
VMWriter.WriteArithmetic(ArithmeticOp.Or);
break;
case "<":
VMWriter.WriteArithmetic(ArithmeticOp.Lt);
break;
case ">":
VMWriter.WriteArithmetic(ArithmeticOp.Gt);
break;
case "=":
VMWriter.WriteArithmetic(ArithmeticOp.Eq);
break;
default:
break;
}
token = tokenQueue.Peek();
}
output.Add(PutCloseTag(indent, "expression"));
return output.ToArray();
}
// integerConstant | stringConstant | keywordConstant |
// varName | varName '[' expression ']' | subroutineCall |
// '(' expression ')' | unaryOp term
private string[] CompileTerm(int indent)
{
List<string> output = new List<string>();
output.Add(PutOpenTag(indent, "term"));
var token = tokenQueue.Peek();
var queueEnumerator = tokenQueue.GetEnumerator();
queueEnumerator.MoveNext();
queueEnumerator.MoveNext();
var nextToken = queueEnumerator.Current;
if (Constants.unaryOp.Contains(token.Value[0]))
{
string unaryOp = token.Value;
output.Add(PutToken(indent + 1));
output.AddRange(CompileTerm(indent + 1));
switch (unaryOp)
{
case "-":
VMWriter.WriteArithmetic(ArithmeticOp.Neg);
break;
case "~":
VMWriter.WriteArithmetic(ArithmeticOp.Not);
break;
default:
break;
}
}
else if (token.Key == TokenType.identifier && nextToken.Value == ".")
{
// subroutineName '(' expressionList ')' | (className |
// varName) '.' subroutineName '(' expressionList ')'
token = tokenQueue.Dequeue();
string classname = token.Value;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "class, used"));
output.Add(PutToken(indent + 1));
token = tokenQueue.Dequeue();
string funcname = token.Value;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "function, used"));
output.Add(PutToken(indent + 1));
int ellen = CompileExpressionList(indent + 1);
try
{
MetaSymbolTable.KindOf(classname);
Segment seg = Segment.Local;
switch (MetaSymbolTable.KindOf(classname))
{
case SymbolKind.Static:
seg = Segment.Static;
break;
case SymbolKind.Field:
seg = Segment.This;
break;
case SymbolKind.Arg:
seg = Segment.Argument;
break;
case SymbolKind.Var:
seg = Segment.Local;
break;
case SymbolKind.None:
break;
default:
break;
}
VMWriter.WritePush(seg, MetaSymbolTable.IndexOf(classname));
ellen += 1;
}
catch
{
}
try
{
VMWriter.WriteCall(MetaSymbolTable.TypeOf(classname) + "." + funcname, ellen);
} catch
{
VMWriter.WriteCall(classname + "." + funcname, ellen);
}
output.Add(PutToken(indent + 1));
}
else if (token.Key == TokenType.identifier && nextToken.Value == "(")
{
token = tokenQueue.Dequeue();
string funcname = token.Value;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "function, used"));
output.Add(PutToken(indent + 1));
int ellen = CompileExpressionList(indent + 1);
VMWriter.WritePush(Segment.Pointer, 0);
VMWriter.WriteCall(ClassName + "." + funcname, ellen + 1);
output.Add(PutToken(indent + 1));
}
else if (nextToken.Value == "[")
{
token = tokenQueue.Dequeue();
string name = token.Value;
Segment sega = Segment.Local;
switch (MetaSymbolTable.KindOf(name))
{
case SymbolKind.Static:
sega = Segment.Static;
break;
case SymbolKind.Field:
sega = Segment.This;
break;
case SymbolKind.Arg:
sega = Segment.Argument;
break;
case SymbolKind.Var:
sega = Segment.Local;
break;
case SymbolKind.None:
break;
default:
break;
}
VMWriter.WritePush(sega, MetaSymbolTable.IndexOf(name));
output.Add(PutToken(indent + 1));
output.AddRange(CompileExpression(indent + 1));
VMWriter.WriteArithmetic(ArithmeticOp.Add);
output.Add(PutToken(indent + 1));
VMWriter.WritePop(Segment.Pointer, 1);
VMWriter.WritePush(Segment.That, 0);
}
else if (token.Value == "(")
{
output.Add(PutToken(indent + 1));
output.AddRange(CompileExpression(indent + 1));
output.Add(PutToken(indent + 1));
}
else if (token.Key == TokenType.identifier)
{
token = tokenQueue.Dequeue();
string name = token.Value;
output.Add(PutLineTag(indent + 1, Enum.GetName(typeof(TokenType), token.Key), token.Value, "name=" + name + " " + "type=" + MetaSymbolTable.TypeOf(name) + " " + "kind=" + Enum.GetName(typeof(SymbolKind), MetaSymbolTable.KindOf(name)) + " " + "index=" + MetaSymbolTable.IndexOf(name) + " " + "used"));
Segment seg = Segment.Local;
switch (MetaSymbolTable.KindOf(name))
{
case SymbolKind.Static:
seg = Segment.Static;
break;
case SymbolKind.Field:
seg = Segment.This;
break;
case SymbolKind.Arg:
seg = Segment.Argument;
break;
case SymbolKind.Var:
seg = Segment.Local;
break;
case SymbolKind.None:
break;
default:
break;
}
VMWriter.WritePush(seg, MetaSymbolTable.IndexOf(name));
}
else
{
switch (token.Key)
{
case TokenType.keyword:
switch(token.Value)
{
case "this":
VMWriter.WritePush(Segment.Pointer, 0);
break;
case "false":
case "null":
VMWriter.WritePush(Segment.Constant, 0);
break;
case "true":
VMWriter.WritePush(Segment.Constant, 0);
VMWriter.WriteArithmetic(ArithmeticOp.Not);
break;
}
break;
case TokenType.symbol:
break;
case TokenType.integerConstant:
VMWriter.WritePush(Segment.Constant, int.Parse(token.Value));
break;
case TokenType.stringConstant:
string str = token.Value;
VMWriter.WritePush(Segment.Constant, str.Length);
VMWriter.WriteCall("String.new", 1);
foreach(char c in str)
{
VMWriter.WritePush(Segment.Constant, c);
VMWriter.WriteCall("String.appendChar", 2);
}
break;
default:
break;
}
output.Add(PutToken(indent + 1));
}
output.Add(PutCloseTag(indent, "term"));
return output.ToArray();
}
// (expression (',' expression)* )?
private int CompileExpressionList(int indent)
{
List<string> output = new List<string>();
int ExpressionListLength = 0;
output.Add(PutOpenTag(indent, "expressionList"));
var token = tokenQueue.Peek();
if (token.Value != ")")
{
ExpressionListLength = 1;
output.AddRange(CompileExpression(indent + 1));
token = tokenQueue.Peek();
while (token.Value == ",")
{
ExpressionListLength++;
output.Add(PutToken(indent + 1));
output.AddRange(CompileExpression(indent + 1));
token = tokenQueue.Peek();
}
}
output.Add(PutCloseTag(indent, "expressionList"));
return ExpressionListLength;
}
private string PutToken(int indent)
{
var token = tokenQueue.Dequeue();
return PutLineTag(indent, Enum.GetName(typeof(TokenType), token.Key), token.Value);
}
private string PutOpenTag(int indent, string name)
{
return new string(' ', indent * Constants.indentLevel) + '<' + name + '>';
}
private string PutCloseTag(int indent, string name)
{
return new string(' ', indent * Constants.indentLevel) + "</" + name + '>';
}
private string PutLineTag(int indent, string name, string value, string add = "")
{
return new string(' ', indent * Constants.indentLevel) + '<' + name + " " + add + "> " + SecurityElement.Escape(value) + " </" + name + '>';
}
}
}

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\JackAnalyzer\JackAnalyzer.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,43 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using JackAnalyzer;
namespace MetaSymbolTableTest
{
[TestClass]
public class MetaSymbolTableTest
{
[TestMethod]
public void CounterTest()
{
MetaSymbolTable st = new MetaSymbolTable();
st.Define("x", "int", SymbolKind.Var);
Assert.AreEqual(1, st.VarCount(SymbolKind.Var));
Assert.AreEqual(0, st.IndexOf("x"));
st.Define("y", "String", SymbolKind.Var);
Assert.AreEqual(2, st.VarCount(SymbolKind.Var));
Assert.AreEqual(1, st.IndexOf("y"));
st.Define("z", "String", SymbolKind.Field);
Assert.AreEqual(1, st.VarCount(SymbolKind.Field));
Assert.AreEqual(0, st.IndexOf("z"));
}
public void ResetTest()
{
MetaSymbolTable st = new MetaSymbolTable();
st.Define("x", "int", SymbolKind.Var);
Assert.AreEqual(1, st.VarCount(SymbolKind.Var));
Assert.AreEqual(0, st.IndexOf("x"));
st.ResetFunctionTable();
try
{
st.IndexOf("x");
Assert.Fail();
} catch { }
}
}
}

View File

@@ -0,0 +1,29 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using JackAnalyzer;
namespace SymbolTableTest
{
[TestClass]
public class SymbolTableTest
{
[TestMethod]
public void CounterTest()
{
SymbolTable st = new SymbolTable();
st.Define("x", "int", SymbolKind.Var);
Assert.AreEqual(1, st.VarCount(SymbolKind.Var));
Assert.AreEqual(0, st.IndexOf("x"));
st.Define("y", "String", SymbolKind.Var);
Assert.AreEqual(2, st.VarCount(SymbolKind.Var));
Assert.AreEqual(1, st.IndexOf("y"));
st.Define("z", "String", SymbolKind.Field);
Assert.AreEqual(1, st.VarCount(SymbolKind.Field));
Assert.AreEqual(0, st.IndexOf("z"));
Assert.AreEqual(0, st.VarCount(SymbolKind.Static));
}
}
}