Beispiel #1
0
        internal static Symbol Declare(Node declareNode, Scope s, string name, TokenPos pos, SymbolUsage usage)
        {
            var pre = s.FindSymbol(name);

            if (pre != null)
            {
                throw new CompileException(string.Format("{0} redeclared, pre define: {1}", name, pre.DefinePos), pos);
            }

            Symbol sb = new Symbol();

            sb.Name      = name;
            sb.Decl      = declareNode;
            sb.DefinePos = pos;
            sb.Usage     = usage;


            s.Insert(sb);

            if (declareNode != null)
            {
                var ident = declareNode as Ident;

                if (ident == null)
                {
                    return(sb);
                }

                ident.Symbol = sb;
            }

            return(sb);
        }
Beispiel #2
0
        internal Scope OpenScope(ScopeType type, TokenPos pos)
        {
            var s = new Scope(_topScope, type, pos);

            _topScope = s;
            return(s);
        }
Beispiel #3
0
        public VarDeclareStmt(List <Ident> names, TokenPos varpos)
        {
            Names  = names;
            VarPos = varpos;

            BuildRelation();
        }
Beispiel #4
0
        public ReturnStmt(List <Expr> list, TokenPos retpos)
        {
            Results = list;
            RetPos  = retpos;

            BuildRelation();
        }
Beispiel #5
0
        public ExprStmt(List <Expr> x, TokenPos defpos)
        {
            X      = x;
            DefPos = defpos;

            BuildRelation();
        }
Beispiel #6
0
        public ImportStmt(BasicLit src, TokenPos pos)
        {
            Source    = src;
            ImportPos = pos;

            BuildRelation();
        }
Beispiel #7
0
 internal ValuePhoFunc(ObjectName name, TokenPos codepos, int regCount, Scope s)
     : base(name)
 {
     _regCount = regCount;
     _scope    = s;
     _defpos   = codepos;
 }
Beispiel #8
0
 public SelectorExpr(Expr x, Ident sel, TokenPos pos)
 {
     X        = x;
     Selector = sel;
     DotPos   = pos;
     BuildRelation();
 }
Beispiel #9
0
        public UnaryExpr(Expr x, TokenType t, TokenPos oppos)
        {
            X     = x;
            Op    = t;
            OpPos = oppos;

            BuildRelation();
        }
Beispiel #10
0
        public ArrayExpr(List <Expr> values, TokenPos lbracket, TokenPos rbracket)
        {
            Values      = values;
            LBracketPos = lbracket;
            RBracketPos = rbracket;

            BuildRelation();
        }
Beispiel #11
0
        public BlockStmt(List <Stmt> list, TokenPos lpos, TokenPos rpos)
        {
            Stmts     = list;
            LBracePos = lpos;
            RBracePos = rpos;

            BuildRelation();
        }
Beispiel #12
0
        public MapExpr(Dictionary <BasicLit, Expr> values, TokenPos lbrace, TokenPos rbrace)
        {
            Values    = values;
            LBracePos = lbrace;
            RBracePos = rbrace;

            BuildRelation();
        }
Beispiel #13
0
        public ParenExpr(Expr x, TokenPos lparen, TokenPos rparen)
        {
            X         = x;
            LParenPos = lparen;
            RParenPos = rparen;

            BuildRelation();
        }
Beispiel #14
0
        public NewExpr(Ident className, Ident pkgName, TokenPos newpos)
        {
            NewPos      = newpos;
            ClassName   = className;
            PackageName = pkgName;

            BuildRelation();
        }
Beispiel #15
0
        public IncDecStmt(Expr x, TokenType t, TokenPos oppos)
        {
            X     = x;
            Op    = t;
            OpPos = oppos;

            BuildRelation();
        }
        public ConstDeclareStmt(Ident name, Expr value, TokenPos defpos)
        {
            Name     = name;
            ConstPos = defpos;
            Value    = value;

            BuildRelation();
        }
Beispiel #17
0
        public AssignStmt(List <Expr> lhs, List <Expr> rhs, TokenPos assignPos, TokenType op)
        {
            LHS       = lhs;
            RHS       = rhs;
            Op        = op;
            AssignPos = assignPos;

            BuildRelation();
        }
Beispiel #18
0
        public WhileStmt(Expr con, TokenPos defpos, Scope s, BlockStmt body)
        {
            Condition = con;
            Pos       = defpos;
            ScopeInfo = s;
            Body      = body;

            BuildRelation();
        }
Beispiel #19
0
        public IfStmt(Expr con, BlockStmt body, BlockStmt elsebody, TokenPos ifpos)
        {
            Condition = con;
            Body      = body;
            ElseBody  = elsebody;
            IfPos     = ifpos;

            BuildRelation();
        }
Beispiel #20
0
        public BinaryExpr(Expr x, Expr y, TokenType t, TokenPos oppos)
        {
            X     = x;
            Y     = y;
            Op    = t;
            OpPos = oppos;

            BuildRelation();
        }
Beispiel #21
0
        public IndexExpr(Expr x, Expr index, TokenPos lpos, TokenPos rpos)
        {
            X         = x;
            Index     = index;
            LBrackPos = lpos;
            RBrackPos = rpos;

            BuildRelation();
        }
Beispiel #22
0
        public AssignStmt(Expr lhs, Expr rhs, TokenPos assignPos, TokenType op)
        {
            AssignPos = assignPos;
            Op        = op;
            LHS.Add(lhs);

            RHS.Add(rhs);

            BuildRelation();
        }
Beispiel #23
0
        public CallExpr(Expr f, List <Expr> args, Scope s, TokenPos lparen, TokenPos rparen)
        {
            Func   = f;
            Args   = args;
            S      = s;
            LParen = lparen;
            RParen = rparen;

            BuildRelation();
        }
Beispiel #24
0
        public ClassDeclare(Ident name, Ident parentName, Scope s, List <Ident> member, TokenPos classpos)
        {
            Name       = name;
            ParentName = parentName;
            ClassPos   = classpos;
            Member     = member;
            ScopeInfo  = s;

            BuildRelation();
        }
Beispiel #25
0
        public ForStmt(Stmt init, Expr con, Stmt post, TokenPos defpos, Scope s, BlockStmt body)
        {
            Condition = con;
            Init      = init;
            Post      = post;
            Pos       = defpos;
            ScopeInfo = s;
            Body      = body;

            BuildRelation();
        }
Beispiel #26
0
        public ForRangeStmt(Ident key, Ident value, Expr x, TokenPos inpos, TokenPos defpos, Scope s, BlockStmt body)
        {
            Key       = key;
            Value     = value;
            X         = x;
            InPos     = inpos;
            Pos       = defpos;
            ScopeInfo = s;
            Body      = body;

            BuildRelation();
        }
Beispiel #27
0
        public Scope(Scope outter, ScopeType type, TokenPos pos)
        {
            if (outter != null)
            {
                outter._child.Add(this);
            }

            _outter = outter;

            _type = type;

            _defpos = pos;
        }
Beispiel #28
0
        void ResolveSelectorElement(Expr x, Ident sel, TokenPos dotpos)
        {
            var xident = x as Ident;

            if (xident == null)
            {
                return;
            }

            if (xident.Symbol == null)
            {
                throw new CompileException(string.Format("{0} not defined", xident.Name), dotpos);
            }

            switch (xident.Symbol.Usage)
            {
            // 包.函数名
            case SymbolUsage.Package:
            {
                var pkg = Exe.GetPackageByName(xident.Name);
                if (pkg == null)
                {
                    throw new CompileException("package not found: " + xident.Name, dotpos);
                }

                // 包必须有一个顶级作用域
                if (pkg.TopScope == null)
                {
                    throw new CompileException("package should have a scope: " + xident.Name, dotpos);
                }


                ScopeMgr.Resolve(sel, pkg.TopScope);
            }
            break;

            // 实例.函数名
            case SymbolUsage.Variable:
            case SymbolUsage.Parameter:
            case SymbolUsage.SelfParameter:
            {
            }
            break;

            default:
                throw new CompileException("unknown usage", dotpos);
            }
        }
Beispiel #29
0
        internal Scope OpenClassScope(string name, TokenPos pos)
        {
            var exists = GetClassScope(name);

            if (exists != null)
            {
                // 被函数提前定义, 所以这里补下主定义位置
                exists.CodePos = pos;
                return(exists);
            }

            var s = OpenScope(ScopeType.Class, pos);

            s.ClassName = name;
            return(s);
        }
Beispiel #30
0
        internal string QuerySourceLine(TokenPos pos)
        {
            foreach (var p in _packages)
            {
                foreach (var f in p.FileList)
                {
                    if (f.Source.Name == pos.SourceName)
                    {
                        return(f.Source.GetLine(pos.Line));
                    }
                }
            }


            return(string.Empty);
        }