示例#1
0
 public CodeToken Id(string t)
 {
     foreach (string k in ConvTable.Keys)
     {
         CodeToken at = ConvTable[k];
         if (at.Text == t)
         {
             return(at.Clone( ));
         }
     }
     return(new CodeToken(TokenClass.Id, Token.Id, t));
 }
示例#2
0
 public void AddConv(string tok, TokenClass cls, Token t)
 {
     ConvTable [tok] = new CodeToken(cls, t, tok);
 }
示例#3
0
        public TokenStream ParseString2(string code)
        {
            //code = code.Replace ( "   ", "" );

            while (false)
            {
                int find = code.IndexOf("  ");
                if (find == -1)
                {
                    break;
                }

                string code1 = code.Substring(0, find);
                string code2 = code.Substring(find + 3);
                code1 = code1.Trim( );
                code2 = code2.Trim( );
                code  = code1 + " " + code2;
            }

            List <string> elements  = new List <string>();
            string        cur       = "";
            bool          string_on = false;
            bool          num_on    = false;

            for (int c = 0; c < code.Length; c++)
            {
                string ch = code[c].ToString();

                if (ch[0] >= "0"[0] && ch[0] <= "9"[0])
                {
                    if (string_on == false)
                    {
                        num_on = true;
                    }
                }
                if (ch == "\"")
                {
                    if (!string_on)
                    {
                        string_on = true;
                        if (cur.Length > 0)
                        {
                            elements.Add(cur);
                        }
                        cur    = "";
                        num_on = false;
                        continue;
                    }
                    else
                    {
                        elements.Add("\"" + cur + "\"");
                        string_on = false;
                        cur       = "";
                        num_on    = false;
                        continue;
                    }
                }
                if (string_on)
                {
                    cur = cur + ch;
                    continue;
                }

                if (num_on)
                {
                    if (ch == ".")
                    {
                        cur = cur + ch;
                        continue;
                    }
                }



                switch (ch)
                {
                case "\n":

                    int lg = 0;
                    for (int nn = 0; nn < cur.Length; nn++)
                    {
                        if (cur [nn].ToString( ) != " " && cur [nn].ToString( ) != " ")
                        {
                            lg = nn;
                        }
                    }
                    cur = cur.Substring(0, lg);

                    break;

                case " ":
                    if (cur.Length > 0)
                    {
                        elements.Add(cur);
                    }
                    cur    = "";
                    num_on = false;
                    continue;
                    break;

                case "+":
                case "/":
                case "-":
                case "*":
                case "<":
                case ">":
                case "=":
                case ".":
                case ",":
                case ":":
                case "[":
                case "]":
                case "(":
                case ")":
                case "!":
                case "{":
                case "}":
                case ";":

                    if (cur.Length > 0)
                    {
                        elements.Add(cur);
                    }

                    elements.Add(ch);
                    cur    = "";
                    num_on = false;
                    continue;
                    break;
                }
                cur += ch;
                // Console.WriteLine ( ch );
            }
            if (cur.Length > 0)
            {
                elements.Add(cur);
            }
            List <string> final_elements = new List <string>();

            foreach (string ele in elements)
            {
                //Console.WriteLine ( "E:" + ele );
                bool keep = false;
                for (int i = 0; i < good.Length; i++)
                {
                    if (ele.Contains(good [i].ToString( )))
                    {
                        keep = true;
                        continue;
                    }
                }
                string ne = "";
                ne = ele.Trim( );
                if (keep)
                {
                    final_elements.Add(ne);
                }
            }
            TokenStream rs = new TokenStream();

            foreach (string word in final_elements)
            {
                CodeToken tok = Id(word);
                if (tok.Text [0].ToString( ) == "\"")
                {
                    tok.Text  = tok.Text.Substring(1, tok.Text.Length - 2);
                    tok.Class = TokenClass.Value;
                    tok.Token = Token.String;
                }
                if (tok.Text == ";")
                {
                    tok.Class = TokenClass.Flow;
                    tok.Token = Token.EndLine;
                }
                if (tok.Text [0] >= "0" [0] && tok.Text [0] <= "9" [0])
                {
                    tok.Class = TokenClass.Value;
                    tok.Token = Token.Int;
                    if (tok.Text.Contains("."))
                    {
                        tok.Token = Token.Float;
                        tok.FVal  = float.Parse(tok.Text);
                    }
                }
                if (tok.Text == "=")
                {
                    tok.Class = TokenClass.Op;
                    tok.Token = Token.Equal;
                }
                //for(int tc = 0l; tc < tok.Token)
                //{
                rs.Add(tok);
                //  Console.WriteLine ( "Tok:" + tok );
            }

            return(rs);
        }
示例#4
0
 public void AddConv(string tok, CodeToken t)
 {
     ConvTable [tok] = t;
 }
示例#5
0
        public CodeToken Clone( )
        {
            CodeToken nt = new CodeToken(Class, Token, Text);

            return(nt);
        }