diff --git a/source2/Compiler/Cosmos.XSharp/Generator.cs b/source2/Compiler/Cosmos.XSharp/Generator.cs index 541b4d89c..296ea1b7d 100644 --- a/source2/Compiler/Cosmos.XSharp/Generator.cs +++ b/source2/Compiler/Cosmos.XSharp/Generator.cs @@ -65,10 +65,7 @@ namespace Cosmos.Compiler.XSharp { var xTokens = xParser.Tokens; var xPattern = xTokens.Select(c => c.Type).ToArray(); - string xCode; - if (!mPatterns.TryGetValue(new TokenPattern(xPattern), out xCode)) { - throw new Exception("Invalid token pattern in X# file."); - } + string xCode = mPatterns.GetCode(xPattern); mOutput.WriteLine(xCode, xTokens.Select(c => c.Value).ToArray()); } diff --git a/source2/Compiler/Cosmos.XSharp/TokenPatterns.cs b/source2/Compiler/Cosmos.XSharp/TokenPatterns.cs index 6b3f617c8..d52f4f191 100644 --- a/source2/Compiler/Cosmos.XSharp/TokenPatterns.cs +++ b/source2/Compiler/Cosmos.XSharp/TokenPatterns.cs @@ -4,7 +4,9 @@ using System.Linq; using System.Text; namespace Cosmos.Compiler.XSharp { - public class TokenPatterns : Dictionary { + public class TokenPatterns { + protected Dictionary mList = new Dictionary(); + public TokenPatterns() { Add(new TokenType[] { TokenType.LiteralAsm }, "new LiteralAssemblerCode(\"{0}\");"); Add(new TokenType[] { TokenType.Comment }, "new Comment(\"{0}\");"); @@ -22,12 +24,20 @@ namespace Cosmos.Compiler.XSharp { Add(new TokenType[] { TokenType.OpCode }, "//new ;"); } - public void Add(string aTokenTypes, string aCode) { - Add(new TokenPattern(aTokenTypes), aCode); + public string GetCode(TokenType[] aPattern) { + string xResult; + if (!mList.TryGetValue(new TokenPattern(aPattern), out xResult)) { + throw new Exception("Token pattern not found."); + } + return xResult; } - public void Add(TokenType[] aTokenTypes, string aCode) { - Add(new TokenPattern(aTokenTypes), aCode); + public void Add(string aPattern, string aCode) { + mList.Add(new TokenPattern(aPattern), aCode); + } + + public void Add(TokenType[] aPattern, string aCode) { + mList.Add(new TokenPattern(aPattern), aCode); } } }