This commit is contained in:
kudzu_cp 2012-06-13 12:37:31 +00:00
parent c69df84763
commit 31df5a706f

View file

@ -5,30 +5,30 @@ using System.Text;
namespace Cosmos.Compiler.XSharp { namespace Cosmos.Compiler.XSharp {
public class TokenPatterns { public class TokenPatterns {
protected class CodeFunc : Func<Token[], string> { } public delegate string CodeFunc(Token[] aTokens);
protected Dictionary<TokenPattern, Func<Token[], string>> mList = new Dictionary<TokenPattern, Func<Token[], string>>(); protected Dictionary<TokenPattern, CodeFunc> mList = new Dictionary<TokenPattern, CodeFunc>();
public TokenPatterns() { public TokenPatterns() {
Add(new TokenType[] { TokenType.LiteralAsm }, delegate(Token[] aTokens) { Add(new TokenType[] { TokenType.LiteralAsm },
return "new LiteralAssemblerCode(\"{0}\");"; "new LiteralAssemblerCode(\"{0}\");"
}); );
Add(new TokenType[] { TokenType.Comment }, delegate(Token[] aTokens) { Add(new TokenType[] { TokenType.Comment },
return "new Comment(\"{0}\");"; "new Comment(\"{0}\");"
}); );
Add("REG = 123", delegate(Token[] aTokens) { Add("REG = 123",
return "new Mov{{ DestinationReg = RegistersEnum.{0}, SourceValue = {2} }};"; "new Mov{{ DestinationReg = RegistersEnum.{0}, SourceValue = {2} }};"
}); );
Add("REG = REG", delegate(Token[] aTokens) { Add("REG = REG",
return "new Mov{{ DestinationReg = RegistersEnum.{0}, SourceReg = RegistersEnum.{2} }};"; "new Mov{{ DestinationReg = RegistersEnum.{0}, SourceReg = RegistersEnum.{2} }};"
}); );
Add("REG = REG[0]", delegate(Token[] aTokens) { Add("REG = REG[0]",
return "//new ;"; "//new ;"
}); );
Add("ABC = REG", delegate(Token[] aTokens) { Add("ABC = REG",
return "//new ;"; "//new ;"
}); );
// TODO: Allow asm to optimize these to Inc/Dec // TODO: Allow asm to optimize these to Inc/Dec
Add("REG + 1", delegate(Token[] aTokens) { Add("REG + 1", delegate(Token[] aTokens) {
@ -39,25 +39,33 @@ namespace Cosmos.Compiler.XSharp {
return "new Sub {{ DestinationReg = RegistersEnum.{0}, SourceValue = {2} }};"; return "new Sub {{ DestinationReg = RegistersEnum.{0}, SourceValue = {2} }};";
}); });
Add(new TokenType[] { TokenType.OpCode }, delegate(Token[] aTokens) { Add(new TokenType[] { TokenType.OpCode },
return "//new ;"; "//new ;"
}); );
} }
public string GetCode(TokenType[] aPattern) { public string GetCode(TokenType[] aPattern) {
Func<Token[], string> xAction; CodeFunc xAction;
if (!mList.TryGetValue(new TokenPattern(aPattern), out xAction)) { if (!mList.TryGetValue(new TokenPattern(aPattern), out xAction)) {
throw new Exception("Token pattern not found."); throw new Exception("Token pattern not found.");
} }
return xAction(new Token[0]); return xAction(new Token[0]);
} }
public void Add(string aPattern, Func<Token[], string> aCode) { public void Add(string aPattern, CodeFunc aCode) {
mList.Add(new TokenPattern(aPattern), aCode); mList.Add(new TokenPattern(aPattern), aCode);
} }
public void Add(TokenType[] aPattern, Func<Token[], string> aCode) { public void Add(TokenType[] aPattern, CodeFunc aCode) {
mList.Add(new TokenPattern(aPattern), aCode); mList.Add(new TokenPattern(aPattern), aCode);
} }
public void Add(string aPattern, string aCode) {
Add(aPattern, delegate(Token[] aTokens) { return aCode; });
}
public void Add(TokenType[] aPattern, string aCode) {
Add(aPattern, delegate(Token[] aTokens) { return aCode; });
}
} }
} }