示例#1
0
        public void AddData(string name, int data)
        {
            var sv = Locals[name];

            sv           = new ScopeVar(sv.Address, data);
            Locals[name] = sv;
        }
示例#2
0
        private void BuildName(DNamePattern node, Hints hints, CompilerContext ctx)
        {
            var err = GetTypeHandle(null, node.Name, out var handle, out var std);

            if (err == CompilerError.None)
            {
                cw.TypeCheck(new TypeHandle(handle, std));
            }
            else
            {
                ScopeVar sv    = default;
                var      found = hints.Has(Rebind)
                    ? TryGetVariable(node.Name, out sv)
                    : hints.Has(OpenMatch) ? false : TryGetLocalVariable(node.Name, out sv);
                var sva = sv.Address;

                if (!found)
                {
                    sva = AddVariable(node.Name, node, hints.Has(Const) ? VarFlags.Const : VarFlags.None);
                }
                else if ((sv.Data & VarFlags.Const) == VarFlags.Const)
                {
                    AddError(CompilerError.UnableAssignConstant, node.Location, node.Name);
                }

                cw.PopVar(sva);
                cw.Push(true);
            }
        }
示例#3
0
 public void PushVar(ScopeVar sv)
 {
     if ((sv.Data & VarFlags.External) == VarFlags.External)
         Emit(new Op(OpCode.Pushext, sv.Address));
     else if ((sv.Address & byte.MaxValue) == 0)
         Emit(new Op(OpCode.Pushloc, sv.Address >> 8));
     else
         Emit(new Op(OpCode.Pushvar, sv.Address));
 }
示例#4
0
        private bool TryGetLocalVariable(string name, out ScopeVar var)
        {
            var = default;

            if (currentScope.Locals.TryGetValue(name, out var sv))
            {
                var = new ScopeVar(0 | sv.Address << 8, sv.Data);
                return(true);
            }

            return(false);
        }
示例#5
0
        public int TryChangeVariable(string name)
        {
            var v = default(ScopeVar);

            if (Locals.TryGetValue(name, out v))
            {
                v            = new ScopeVar(v.Address, -1);
                Locals[name] = v;
                return(v.Address);
            }

            return(-1);
        }
示例#6
0
        private void Build(DMatch node, Hints hints, CompilerContext ctx)
        {
            ValidateMatch(node);
            StartScope(fun: false, node.Location);

            ctx = new CompilerContext(ctx)
            {
                MatchExit = cw.DefineLabel()
            };

            var sys  = AddVariable();
            var push = hints.Append(Push);

            if (node.Expression != null)
            {
                Build(node.Expression, push.Remove(Last), ctx);
            }

            cw.PopVar(sys);
            var sysVar = new ScopeVar(sys);

            foreach (var e in node.Entries)
            {
                BuildEntry(e, sysVar, push, ctx);
            }

            //It is some kind of a hack, but Expression can be null
            //only if this match is inside try/catch
            if (node.Expression != null)
            {
                cw.Fail(DyErrorCode.MatchFailed);
            }
            else
            {
                cw.PushVar(sysVar);
                cw.Fail();
            }

            cw.MarkLabel(ctx.MatchExit);
            cw.Nop();
            PopIf(hints);
            EndScope();
        }
示例#7
0
        private void BuildEntry(DMatchEntry node, ScopeVar sys, Hints hints, CompilerContext ctx)
        {
            StartScope(fun: false, node.Location);
            var skip = cw.DefineLabel();

            cw.PushVar(sys);
            BuildPattern(node.Pattern, hints.Remove(Last), ctx);
            cw.Brfalse(skip);

            if (node.Guard != null)
            {
                Build(node.Guard, hints.Remove(Last), ctx);
                cw.Brfalse(skip);
            }

            Build(node.Expression, hints, ctx);
            cw.Br(ctx.MatchExit);
            cw.MarkLabel(skip);
            cw.Nop();
            EndScope();
        }
示例#8
0
 private bool TryGetVariable(string name, out ScopeVar var)
 {
     var = default;
     var sv = GetVariable(name, currentScope, default, err: false);