From fd2d48519345aec4ae0ca3d5543cb23b69af5fd2 Mon Sep 17 00:00:00 2001 From: kudzu_cp <6d05c8c8ef5431987001abfdb2eadc9593ac9498> Date: Wed, 13 Jun 2012 14:48:33 +0000 Subject: [PATCH] X# --- source/Cosmos.Compiler.html | 17 ++++++++++ source2/Compiler/Cosmos.XSharp/Parser.cs | 33 +++++-------------- source2/Compiler/Cosmos.XSharp/Token.cs | 32 ++++++++++++++---- .../Compiler/Cosmos.XSharp/TokenPattern.cs | 17 +++------- .../Compiler/Cosmos.XSharp/TokenPatterns.cs | 3 ++ source2/Users/Kudzu/Kudzu-Notes.html | 5 ++- source2/VSIP/Cosmos.VS.XSharp/Scanner.cs | 9 ++--- 7 files changed, 69 insertions(+), 47 deletions(-) diff --git a/source/Cosmos.Compiler.html b/source/Cosmos.Compiler.html index 6471bf769..0d5e1d1d3 100644 --- a/source/Cosmos.Compiler.html +++ b/source/Cosmos.Compiler.html @@ -8,5 +8,22 @@

TODO: Rename Cosmos.IL2CPU to Cosmos.Compiler

+

+  

+

+ Projects

+

+ Cosmos.Compiler.XSharp

+

+ Compiles X# to C# assembler methods.

+

+ Cosmos.Compiler.XSharp.Test

+

+ Test for Cosmos.Compiler.XSharp

+

+ Cosmos.Compiler.XSharp.XSC

+

+ Future command line compiler for X#.

+ diff --git a/source2/Compiler/Cosmos.XSharp/Parser.cs b/source2/Compiler/Cosmos.XSharp/Parser.cs index 5ff350232..ebafcaa35 100644 --- a/source2/Compiler/Cosmos.XSharp/Parser.cs +++ b/source2/Compiler/Cosmos.XSharp/Parser.cs @@ -47,33 +47,19 @@ namespace Cosmos.Compiler.XSharp { mStart = rPos; return; } - } else if (xString == "[") { - xToken.Type = TokenType.BracketLeft; - } else if (xString == "]") { - xToken.Type = TokenType.BracketRight; - } else if (xString == "+") { - xToken.Type = TokenType.Plus; - } else if (xString == "-") { - xToken.Type = TokenType.Minus; - } else if (xString == "=") { - xToken.Type = TokenType.Assignment; } else if (char.IsLetter(xChar1)) { - if (xString.EndsWith(":")) { - xToken.Type = TokenType.Label; + string xUpper = xString.ToUpper(); + if (mRegisters.Contains(xUpper)) { + xToken.Type = TokenType.Register; + } else if (mOps.Contains(xUpper)) { + xToken.Type = TokenType.OpCode; } else { - string xUpper = xString.ToUpper(); - if (mRegisters.Contains(xUpper)) { - xToken.Type = TokenType.Register; - } else if (mOps.Contains(xUpper)) { - xToken.Type = TokenType.OpCode; - } else { - xToken.Type = TokenType.AlphaNum; - } + xToken.Type = TokenType.AlphaNum; } } else if (char.IsDigit(xChar1)) { - xToken.Type = TokenType.ValueNumberInt; + xToken.Type = TokenType.ValueInt; } else { - xToken.Type = TokenType.Unknown; + xToken.Type = Token.GetTypeForSymbol(xString); } } xToken.Value = xString; @@ -95,8 +81,7 @@ namespace Cosmos.Compiler.XSharp { xChar = mData[i]; if (char.IsWhiteSpace(xChar)) { xCharType = CharType.WhiteSpace; - } else if (char.IsLetterOrDigit(xChar) || xChar == ':') { - // : is for labels + } else if (char.IsLetterOrDigit(xChar)) { xCharType = CharType.Identifier; } else { xCharType = CharType.Symbol; diff --git a/source2/Compiler/Cosmos.XSharp/Token.cs b/source2/Compiler/Cosmos.XSharp/Token.cs index e0048ae49..37466882d 100644 --- a/source2/Compiler/Cosmos.XSharp/Token.cs +++ b/source2/Compiler/Cosmos.XSharp/Token.cs @@ -6,17 +6,17 @@ using System.Text; namespace Cosmos.Compiler.XSharp { public enum TokenType { // Line based - Comment, LiteralAsm, + Comment, LiteralAsm // - Register, Label, OpCode, AlphaNum, + , Register, OpCode, AlphaNum // Values - ValueNumberInt, + , ValueInt // Symbols - Assignment, BracketLeft, BracketRight, Plus, Minus, + , Assignment, BracketLeft, BracketRight, Plus, Minus, Colon, Dollar // - WhiteSpace, + , WhiteSpace // For now used during scanning while user is typing, but in future could be user methods we have to find etc - Unknown + , Unknown } public class Token { @@ -24,5 +24,25 @@ namespace Cosmos.Compiler.XSharp { public int SrcPosStart; public int SrcPosEnd; public string Value; + + public static TokenType GetTypeForSymbol(string aSymbol) { + if (aSymbol == "[") { + return TokenType.BracketLeft; + } else if (aSymbol == "]") { + return TokenType.BracketRight; + } else if (aSymbol == "+") { + return TokenType.Plus; + } else if (aSymbol == "-") { + return TokenType.Minus; + } else if (aSymbol == "=") { + return TokenType.Assignment; + } else if (aSymbol == ":") { + return TokenType.Colon; + } else if (aSymbol == "$") { + return TokenType.Dollar; + } else { + return TokenType.Unknown; + } + } } } diff --git a/source2/Compiler/Cosmos.XSharp/TokenPattern.cs b/source2/Compiler/Cosmos.XSharp/TokenPattern.cs index aa3491c30..055d8d70c 100644 --- a/source2/Compiler/Cosmos.XSharp/TokenPattern.cs +++ b/source2/Compiler/Cosmos.XSharp/TokenPattern.cs @@ -26,19 +26,12 @@ namespace Cosmos.Compiler.XSharp { } else if (string.Compare(xPart, "ABC") == 0) { xTokenType = TokenType.AlphaNum; } else if (char.IsDigit(xPart[0])) { - xTokenType = TokenType.ValueNumberInt; - } else if (xPart == "=") { - xTokenType = TokenType.Assignment; - } else if (xPart == "[") { - xTokenType = TokenType.BracketLeft; - } else if (xPart == "]") { - xTokenType = TokenType.BracketRight; - } else if (xPart == "+") { - xTokenType = TokenType.Plus; - } else if (xPart == "-") { - xTokenType = TokenType.Minus; + xTokenType = TokenType.ValueInt; } else { - throw new Exception("Unrecognized string token: " + xPart); + xTokenType = Token.GetTypeForSymbol(xPart); + if (xTokenType == TokenType.Unknown) { + throw new Exception("Unrecognized string token: " + xPart); + } } xTokenTypes.Add(xTokenType); } diff --git a/source2/Compiler/Cosmos.XSharp/TokenPatterns.cs b/source2/Compiler/Cosmos.XSharp/TokenPatterns.cs index 6f18e7c46..af43246ce 100644 --- a/source2/Compiler/Cosmos.XSharp/TokenPatterns.cs +++ b/source2/Compiler/Cosmos.XSharp/TokenPatterns.cs @@ -15,6 +15,9 @@ namespace Cosmos.Compiler.XSharp { Add(new TokenType[] { TokenType.Comment }, "new Comment(\"{0}\");" ); + Add("ABC:" , + "new Label(\"{0}\");" + ); Add("REG = 123", "new Mov{{ DestinationReg = RegistersEnum.{0}, SourceValue = {2} }};" diff --git a/source2/Users/Kudzu/Kudzu-Notes.html b/source2/Users/Kudzu/Kudzu-Notes.html index e4bc60d92..f618da184 100644 --- a/source2/Users/Kudzu/Kudzu-Notes.html +++ b/source2/Users/Kudzu/Kudzu-Notes.html @@ -30,7 +30,10 @@ To Do