示例#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 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>();
            List <int>    elements_s = new List <int>();
            string        cur        = "";
            bool          string_on  = false;
            bool          num_on     = false;
            int           c_s        = 0;
            bool          minus      = false;
            string        ptok       = "";

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

                if (ch[0] >= "0"[0] && ch[0] <= "9"[0])
                {
                    if (cur.Length == 0)
                    {
                        if (string_on == false)
                        {
                            num_on = true;
                            if (minus)
                            {
                                cur   = "-";
                                minus = false;
                            }
                        }
                    }
                    else if (cur.Length == 1)
                    {
                        if (string_on == false)
                        {
                        }
                    }
                }
                if (ch == "\"")
                {
                    if (!string_on)
                    {
                        string_on = true;
                        if (cur.Length > 0)
                        {
                            ptok = cur;
                            elements.Add(cur);
                            elements_s.Add(c_s);
                        }
                        cur    = "";
                        c_s    = c;
                        num_on = false;
                        continue;
                    }
                    else
                    {
                        ptok = cur;
                        elements.Add("\"" + cur + "\"");
                        elements_s.Add(c_s);
                        string_on = false;
                        cur       = "";
                        c_s       = c;
                        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)
                    {
                        ptok = cur;
                        elements.Add(cur);
                        elements_s.Add(c_s);
                    }
                    cur    = "";
                    c_s    = c;
                    num_on = false;
                    continue;
                    break;

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

                    if (ch == "-")
                    {
                        bool skip = true;

                        switch (ptok)
                        {
                        case "(":
                        case "=":
                        case ",":
                        case "+":
                        case "/":
                        case "*":
                        case "-":
                            if (cur.Length > 0)
                            {
                                switch (cur)
                                {
                                case "(":
                                case "=":
                                case ",":
                                case "+":
                                case "/":
                                case "*":
                                case "-":
                                    skip = true;
                                    break;
                                }
                                break;
                            }

                            skip = false;

                            break;
                        }
                        if (!skip)
                        {
                            string ch2 = code[c + 1].ToString();
                            ch = "";
                            if (ch2[0] >= "0"[0] && ch2[0] <= "9"[0])
                            {
                                minus = true;
                                if (cur.Length > 0)
                                {
                                    ptok = cur;
                                    elements.Add(cur);
                                    elements_s.Add(c_s);
                                }


                                cur    = "";
                                c_s    = c;
                                num_on = false;

                                break;
                            }
                        }
                    }

                    if (cur.Length > 0)
                    {
                        ptok = cur;
                        elements.Add(cur);
                        elements_s.Add(c_s);
                    }
                    ptok = ch;
                    elements.Add(ch);
                    elements_s.Add(c_s);
                    cur    = "";
                    c_s    = c;
                    num_on = false;
                    continue;
                    break;
                }
                cur += ch;


                // Console.WriteLine ( ch );
            }
            if (cur.Length > 0)
            {
                ptok = cur;
                elements.Add(cur);
                elements_s.Add(c_s);
            }
            foreach (var el in elements)
            {
                if (el == "tokestop")
                {
                }
            }
            List <string> final_elements   = new List <string>();
            List <int>    final_elements_s = new List <int>();
            int           s_i = 0;

            foreach (string ele in elements)
            {
                int c_s2 = elements_s[s_i];
                s_i++;
                System.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);
                    final_elements_s.Add(c_s2);
                }
            }
            TokenStream rs   = new TokenStream();
            int         s_i2 = 0;

            foreach (string word in final_elements)
            {
                int c_s1 = final_elements_s[s_i2];
                s_i2++;
                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])
                {
                    if (tok.Text.Length > 1)
                    {
                        if (tok.Text[1] >= "0"[0] && tok.Text[1] <= "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[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;
                }
                tok.LineC = c_s1;
                //for(int tc = 0l; tc < tok.Token)
                //{
                rs.Add(tok);
                //  Console.WriteLine ( "Tok:" + tok );
            }

            return(rs);
        }
示例#3
0
 public void AddConv(string tok, TokenClass cls, Token t)
 {
     ConvTable[tok] = new CodeToken(cls, t, tok);
 }
示例#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;
 }