コード例 #1
0
ファイル: Parser.cs プロジェクト: Northcode/NXCODE
        private ISTMT ParseStmt()
        {
            try
            {
                if (i + 1 < tokens.Length && tokens[i + 1].val.ToString() == "(")
                {
                    if (isfunction)
                    {
                        string name = Current.val.ToString();
                        i++;
                        i++;
                        FuncCall f = new FuncCall() { method = name, args = ParseArgs() };
                        i++;
                        return f;
                    }
                    else
                    {
                        string name = Current.val.ToString();
                        i++;
                        i++;
                        DefineFunc f = new DefineFunc() { name = name, Args = ParseFuncArgs() };
                        isfunction = true;
                        i++;
                        f.body = ParseBody();
                        isfunction = false;
                        if (isClass)
                        {
                            cfs.Add(f);
                        }
                        return f;
                    }
                }
                else if (i < tokens.Length && Current.type == TokenType.Base)
                {
                    BaseCode bc = new BaseCode() { val = Current.val.ToString() };
                    i++;
                    return bc;
                }
                else if (i < tokens.Length && Current.val.ToString() == "open")
                {
                    i++;
                    Open o = new Open() { name = Current.val.ToString() };
                    i++;
                    return o;
                }
                else if (i + 1 < tokens.Length && Current.val.ToString() == "xmlgen")
                {
                    i++;
                    string XML = Current.val.ToString();
                    Xml x = new Xml() { text = XML };
                    i++;
                    return x;
                }
                else if (i + 1 < tokens.Length && tokens[i + 1].val.ToString() == "=")
                {
                    string name = Current.val.ToString();
                    i++;
                    i++;
                    SetVar s = new SetVar() { name = name, value = ParseExpr(false) };
                    i++;
                    return s;
                }
                else if (i + 1 < tokens.Length && Current.val.ToString() == "if")
                {
                    If f = new If();
                    i++;
                    f.eval = ParseExpr(false);
                    i++;
                    f.truebody = ParseBody();
                    i--;
                    if (Current.val.ToString() == "else")
                    {
                        i++;
                        f.falsebody = ParseBody();
                    }
                    else
                    {
                        i++;
                    }
                    return f;
                }
                else if (i < tokens.Length && Current.val.ToString() == "from")
                {
                    i++;
                    string inc = Current.val.ToString();
                    i++; //name
                    i++; //,
                    IEXPR to = ParseExpr(false);
                    i++; //expr
                    i++; //,
                    IEXPR by = ParseExpr(false);
                    i++; //by

                    ISTMT[] body = ParseBody();
                    Loop l = new Loop() { from = inc, to = to, by = by, body = body };
                    return l;
                }
                else if (i < tokens.Length && Current.val.ToString() == "while")
                {
                    i++;
                    IEXPR expr = ParseExpr(false);
                    i++;
                    ISTMT[] body = ParseBody();
                    While w = new While() { expr = expr, body = body };
                    return w;
                }
                else if (i < tokens.Length && Current.val.ToString() == "return")
                {
                    i++;
                    IEXPR v = ParseExpr(false);
                    i++;
                    Return r = new Return() { value = v };
                    return r;
                }
                else if (i < tokens.Length && Current.val.ToString() == "class")
                {
                    i++;
                    string name = Current.val.ToString();
                    i++;
                    DefClass d = new DefClass();
                    d.Name = name;
                    isClass = true;
                    cref = d;
                    cfs = new List<DefineFunc>();
                    cvs = new List<DefVar>();
                    ParseBody();
                    d.funcs = cfs.ToArray();
                    d.vars = cvs.ToArray();
                    isClass = false;
                    return d;
                }
                else if (i < tokens.Length && Current.val.ToString() == "public")
                {
                    i++;
                    string name = Current.val.ToString();
                    i++;
                    DefVar v = new DefVar() { access = "public", name = name };
                    if (isClass)
                    {
                        cvs.Add(v);
                    }
                    return v;
                }
                else if (i < tokens.Length && Current.val.ToString() == "private")
                {
                    i++;
                    string name = Current.val.ToString();
                    i++;
                    DefVar v = new DefVar() { access = "private", name = name };
                    if (isClass)
                    {
                        cvs.Add(v);
                    }
                    return v;
                }
                else if (i < tokens.Length && Current.val.ToString() == "\n")
                {
                    i++;
                    return null;
                }
                else if (i < tokens.Length && Current.val.ToString() == "end")
                {
                    i++;
                    return new End();
                }
                else if (i < tokens.Length && Current.val.ToString() == "else")
                {
                    i++;
                    return new Else();
                }
                else
                {
                    throw new Exception("Unkown token");
                }
            }
            catch (IndexOutOfRangeException)
            {
                MessageBox.Show("Unexpected end of file","Error");
                return null;
            }
        }
コード例 #2
0
        private ISTMT ParseStmt()
        {
            try
            {
                if (i + 1 < tokens.Length && tokens[i + 1].val.ToString() == "(")
                {
                    if (isfunction)
                    {
                        string name = Current.val.ToString();
                        i++;
                        i++;
                        FuncCall f = new FuncCall()
                        {
                            method = name, args = ParseArgs()
                        };
                        i++;
                        return(f);
                    }
                    else
                    {
                        string name = Current.val.ToString();
                        i++;
                        i++;
                        DefineFunc f = new DefineFunc()
                        {
                            name = name, Args = ParseFuncArgs()
                        };
                        isfunction = true;
                        i++;
                        f.body     = ParseBody();
                        isfunction = false;
                        if (isClass)
                        {
                            cfs.Add(f);
                        }
                        return(f);
                    }
                }
                else if (i < tokens.Length && Current.type == TokenType.Base)
                {
                    BaseCode bc = new BaseCode()
                    {
                        val = Current.val.ToString()
                    };
                    i++;
                    return(bc);
                }
                else if (i < tokens.Length && Current.val.ToString() == "open")
                {
                    i++;
                    Open o = new Open()
                    {
                        name = Current.val.ToString()
                    };
                    i++;
                    return(o);
                }
                else if (i + 1 < tokens.Length && Current.val.ToString() == "xmlgen")
                {
                    i++;
                    string XML = Current.val.ToString();
                    Xml    x   = new Xml()
                    {
                        text = XML
                    };
                    i++;
                    return(x);
                }
                else if (i + 1 < tokens.Length && tokens[i + 1].val.ToString() == "=")
                {
                    string name = Current.val.ToString();
                    i++;
                    i++;
                    SetVar s = new SetVar()
                    {
                        name = name, value = ParseExpr(false)
                    };
                    i++;
                    return(s);
                }
                else if (i + 1 < tokens.Length && Current.val.ToString() == "if")
                {
                    If f = new If();
                    i++;
                    f.eval = ParseExpr(false);
                    i++;
                    f.truebody = ParseBody();
                    i--;
                    if (Current.val.ToString() == "else")
                    {
                        i++;
                        f.falsebody = ParseBody();
                    }
                    else
                    {
                        i++;
                    }
                    return(f);
                }
                else if (i < tokens.Length && Current.val.ToString() == "from")
                {
                    i++;
                    string inc = Current.val.ToString();
                    i++; //name
                    i++; //,
                    IEXPR to = ParseExpr(false);
                    i++; //expr
                    i++; //,
                    IEXPR by = ParseExpr(false);
                    i++; //by

                    ISTMT[] body = ParseBody();
                    Loop    l    = new Loop()
                    {
                        from = inc, to = to, by = by, body = body
                    };
                    return(l);
                }
                else if (i < tokens.Length && Current.val.ToString() == "while")
                {
                    i++;
                    IEXPR expr = ParseExpr(false);
                    i++;
                    ISTMT[] body = ParseBody();
                    While   w    = new While()
                    {
                        expr = expr, body = body
                    };
                    return(w);
                }
                else if (i < tokens.Length && Current.val.ToString() == "return")
                {
                    i++;
                    IEXPR v = ParseExpr(false);
                    i++;
                    Return r = new Return()
                    {
                        value = v
                    };
                    return(r);
                }
                else if (i < tokens.Length && Current.val.ToString() == "class")
                {
                    i++;
                    string name = Current.val.ToString();
                    i++;
                    DefClass d = new DefClass();
                    d.Name  = name;
                    isClass = true;
                    cref    = d;
                    cfs     = new List <DefineFunc>();
                    cvs     = new List <DefVar>();
                    ParseBody();
                    d.funcs = cfs.ToArray();
                    d.vars  = cvs.ToArray();
                    isClass = false;
                    return(d);
                }
                else if (i < tokens.Length && Current.val.ToString() == "public")
                {
                    i++;
                    string name = Current.val.ToString();
                    i++;
                    DefVar v = new DefVar()
                    {
                        access = "public", name = name
                    };
                    if (isClass)
                    {
                        cvs.Add(v);
                    }
                    return(v);
                }
                else if (i < tokens.Length && Current.val.ToString() == "private")
                {
                    i++;
                    string name = Current.val.ToString();
                    i++;
                    DefVar v = new DefVar()
                    {
                        access = "private", name = name
                    };
                    if (isClass)
                    {
                        cvs.Add(v);
                    }
                    return(v);
                }
                else if (i < tokens.Length && Current.val.ToString() == "\n")
                {
                    i++;
                    return(null);
                }
                else if (i < tokens.Length && Current.val.ToString() == "end")
                {
                    i++;
                    return(new End());
                }
                else if (i < tokens.Length && Current.val.ToString() == "else")
                {
                    i++;
                    return(new Else());
                }
                else
                {
                    throw new Exception("Unkown token");
                }
            }
            catch (IndexOutOfRangeException)
            {
                MessageBox.Show("Unexpected end of file", "Error");
                return(null);
            }
        }