This commit is contained in:
kudzu_cp 2012-06-13 12:05:29 +00:00
parent 80beff8cdd
commit c7fbdbc2da
2 changed files with 16 additions and 9 deletions

View file

@ -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());
}

View file

@ -4,7 +4,9 @@ using System.Linq;
using System.Text;
namespace Cosmos.Compiler.XSharp {
public class TokenPatterns : Dictionary<TokenPattern, string> {
public class TokenPatterns {
protected Dictionary<TokenPattern, string> mList = new Dictionary<TokenPattern, string>();
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);
}
}
}