예제 #1
0
파일: LayeState.cs 프로젝트: LayeLang/Laye
        internal LayeObject Execute(StackFrame frame, LayeClosure closure, LayeObject ths = null, params LayeObject[] args)
        {
            if (frame == null)
                frame = stack.PushFrame(closure, ths, args);
            else stack.PushFrame(frame);

        reentry:
            var kit = closure.kit;

            var outers = closure.outers;
            var nested = closure.proto.nested;
            var strings = closure.proto.strings;
            var numericConsts = closure.proto.numericConsts;

            var code = closure.proto.code;
            var codeLen = (uint)code.Length;

            var paramc = closure.proto.numParams;
            var argc = args.Length;
            var vargs = closure.proto.hasVargs;

            var openOuters = frame.openOuters;

            uint insn;
            OpCode op;

            for (; frame.ip < codeLen && !frame.Aborted && !frame.yielded; frame.ip++)
            {
                op = (OpCode)((insn = code[frame.ip]) & Insn.MAX_OP);
#if DEBUG_STACK
                Console.WriteLine(frame.ip + " " + op);
                frame.PrintLocals(this);
                frame.PrintStack(this);
#endif
                switch (op)
                {
                    default: RaiseException(string.Format("Unhandled op code {0}.", op)); return NULL;

                    case OpCode.NOP: continue;
                    case OpCode.POP: frame.Pop(); continue;
                    case OpCode.DUP: frame.Dup(); continue;

                    case OpCode.CLOSE: CloseOuters(openOuters, Insn.GET_C(insn)); continue;

                        // These all do n - 1 because frame.ip++ happens anyway. I hate it, but oh well.
                    case OpCode.JUMP: frame.ip = Insn.GET_C(insn) - 1; continue;
                    case OpCode.JUMPEQ: if (frame.SwapPop().Equals(frame.Pop())) frame.ip = Insn.GET_C(insn) - 1; continue;
                    case OpCode.JUMPNEQ: if (!frame.SwapPop().Equals(frame.Pop())) frame.ip = Insn.GET_C(insn) - 1; continue;
                    case OpCode.JUMPT: if (frame.Pop().ToBool(this)) frame.ip = Insn.GET_C(insn) - 1; continue;
                    case OpCode.JUMPF: if (!frame.Pop().ToBool(this)) frame.ip = Insn.GET_C(insn) - 1; continue;

                    case OpCode.LLOAD: frame.Push(frame[Insn.GET_C(insn)]); continue;
                    case OpCode.LSTORE: frame[Insn.GET_C(insn)] = frame.Top; continue;
                    case OpCode.OLOAD: frame.Push(outers[Insn.GET_C(insn)].Value); continue;
                    case OpCode.OSTORE: outers[Insn.GET_C(insn)].Set(this, frame.Top); continue;
                    case OpCode.KLOAD: frame.Push(kit[this, strings[Insn.GET_C(insn)]]); continue;
                    case OpCode.KSTORE: kit[this, strings[Insn.GET_C(insn)]] = frame.Top; continue;
                    case OpCode.GLOAD: frame.Push(kit.GetGlobal(this, strings[Insn.GET_C(insn)])); continue;
                    case OpCode.GSTORE: kit.SetGlobal(this, strings[Insn.GET_C(insn)], frame.Top); continue;
                    case OpCode.ILOAD: DoILoad(frame, insn); continue;
                    case OpCode.ISTORE: DoIStore(frame, insn); continue;
                    case OpCode.OILOAD: frame.Push(frame.Pop().OperatorIndexGet(this, strings[Insn.GET_C(insn)])); continue;
                    case OpCode.OISTORE: frame.SwapPop().OperatorIndexSet(this, strings[Insn.GET_C(insn)], frame.Top); continue;
                    case OpCode.FLOAD: frame.Push(frame.Pop()[this, strings[Insn.GET_C(insn)]]); continue;
                    case OpCode.FSTORE: frame.SwapPop()[this, strings[Insn.GET_C(insn)]] = frame.Top; continue;
                    case OpCode.LOAD: DoLoad(frame, kit, strings[Insn.GET_C(insn)]); continue;
                    case OpCode.STORE: DoStore(frame, kit, strings[Insn.GET_C(insn)]); continue;

                    case OpCode.NULL: frame.Push(NULL); continue;
                    case OpCode.TRUE: frame.Push(TRUE); continue;
                    case OpCode.FALSE: frame.Push(FALSE); continue;
                    case OpCode.ENDL: frame.Push(ENDL); continue;
                    case OpCode.NCONST: frame.Push(numericConsts[Insn.GET_C(insn)]); continue;
                    case OpCode.SCONST: frame.Push(Insn.GET_B(insn) == 0 ? new LayeString(strings[Insn.GET_A(insn)]) as LayeObject : LayeSymbol.getUnsafe(strings[Insn.GET_A(insn)])); continue;

                    case OpCode.ICONSTM1: frame.Push(ICONSTM1); continue;
                    case OpCode.ICONST0: frame.Push(ICONST0); continue;
                    case OpCode.ICONST1: frame.Push(ICONST1); continue;
                    case OpCode.ICONST2: frame.Push(ICONST2); continue;
                    case OpCode.ICONST3: frame.Push(ICONST3); continue;
                    case OpCode.ICONST4: frame.Push(ICONST4); continue;
                    case OpCode.ICONST5: frame.Push(ICONST5); continue;

                    case OpCode.FCONSTM1: frame.Push(FCONSTM1); continue;
                    case OpCode.FCONST0: frame.Push(FCONST0); continue;
                    case OpCode.FCONST1: frame.Push(FCONST1); continue;
                    case OpCode.FCONST2: frame.Push(FCONST2); continue;

                    case OpCode.LIST: frame.Push(new LayeList(frame.PopCount((int)Insn.GET_C(insn)))); continue;
                    case OpCode.TUPLE: frame.Push(new LayeTuple(frame.PopCount((int)Insn.GET_C(insn)))); continue;

                    case OpCode.CLOSURE: frame.Push(BuildClosure(nested[Insn.GET_A(insn)], openOuters, null, frame.PopCount((int)Insn.GET_B(insn)))); continue;
                    case OpCode.GENERATOR: frame.Push(new LayeClosureGeneratorSpawner(frame.Pop() as LayeClosure)); continue;

                    case OpCode.INVOKE: DoInvoke(frame, insn); continue;
                    case OpCode.MINVOKE: DoMethodInvoke(frame, strings[Insn.GET_B(insn)], insn); continue;
                    case OpCode.TINVOKE: DoThisInvoke(frame, ths, strings[Insn.GET_B(insn)], insn); continue;
                    case OpCode.TAILINVOKE:
                        var saveArgs = frame.PopCount((int)Insn.GET_C(insn));
                        EndCall(frame, openOuters);
                        frame.Reset();
                        frame.SetArgs(saveArgs);
                        goto reentry;

                    case OpCode.YIELD: frame.yielded = true; continue;
                    case OpCode.RES: DoRes(frame); continue;

                    case OpCode.KIT: frame.Push(kit); continue;
                    case OpCode.THIS: frame.Push(ths); continue;
                    case OpCode.SELF: frame.Push(closure); continue;
                    case OpCode.STATIC: frame.Push(closure.definedType); continue;

                    case OpCode.PREFIX: frame.Push(frame.Pop().Prefix(this, strings[Insn.GET_C(insn)])); continue;
                    case OpCode.INFIX: frame.Push(frame.SwapPop().Infix(this, strings[Insn.GET_C(insn)], frame.Pop())); continue;
                    case OpCode.AS: frame.Push(frame.SwapPop().As(this, frame.Pop())); continue;

                    case OpCode.NOT: frame.Push(frame.Pop().ToBool(this) ? FALSE : TRUE); continue;
                    case OpCode.AND: if (frame.Top.ToBool(this)) frame.Pop(); else frame.ip = Insn.GET_C(insn) - 1; continue;
                    case OpCode.OR: if (frame.Top.ToBool(this)) frame.ip = Insn.GET_C(insn) - 1; else frame.Pop(); continue;
                    case OpCode.XOR: frame.Push((frame.Pop().ToBool(this) != frame.Pop().ToBool(this)) ? TRUE : FALSE); continue;

                    case OpCode.COMPIS: frame.Push((LayeBool)ReferenceEquals(frame.Pop(), frame.Pop())); continue;
                    case OpCode.COMPNOTIS: frame.Push((LayeBool)!ReferenceEquals(frame.Pop(), frame.Pop())); continue;
                    case OpCode.COMPTYPEOF: frame.Push((LayeBool)frame.SwapPop().TypeOf(this, frame.Pop())); continue;
                    case OpCode.COMPNOTTYPEOF: frame.Push((LayeBool)!frame.SwapPop().TypeOf(this, frame.Pop())); continue;
                    case OpCode.TYPEOF: frame.Push(frame.Pop().TypeDef); continue;

                    case OpCode.THROW: RaiseException(frame.Pop()); continue;
                    case OpCode.STOREEX: frame[Insn.GET_C(insn)] = lastException; continue;
                    case OpCode.BEGINEXH: var c = Insn.GET_C(insn); while (frame.stackPointer >= c) frame.Pop(); continue;
                    case OpCode.PUSHEXH: frame.activeExceptionHandlers++; exceptionHandlers.Push(new ExceptionHandler(stack.FrameCount, Insn.GET_C(insn))); continue;
                    case OpCode.POPEXH: frame.activeExceptionHandlers--; exceptionHandlers.Pop(); continue;

                    case OpCode.ITERPREP: DoIterPrep(frame, Insn.GET_A(insn), Insn.GET_B(insn) != 0); continue;
                    case OpCode.ITERLOOP: DoIterLoop(frame, Insn.GET_A(insn), Insn.GET_B(insn)); continue;
                    case OpCode.EACHPREP: DoEachPrep(frame, Insn.GET_A(insn)); continue;
                    case OpCode.EACHLOOP: DoEachLoop(frame, Insn.GET_A(insn), Insn.GET_B(insn)); continue;
                    case OpCode.IEACHPREP: DoIEachPrep(frame, Insn.GET_A(insn)); continue;
                    case OpCode.IEACHLOOP: DoIEachLoop(frame, Insn.GET_A(insn), Insn.GET_B(insn)); continue;
                } // end switch
            } // end for

            EndCall(frame, openOuters);

#if DEBUG_STACK
            frame.PrintStack(this);
#endif
            if (frame.Aborted)
                return NULL;
            stack.PopFrame();

            return frame.HasValue() ? frame.Top : NULL;
        }
예제 #2
0
파일: LayeState.cs 프로젝트: LayeLang/Laye
 internal StackFrame NewFrame(LayeClosure closure, LayeObject ths, LayeObject[] args)
 {
     return new StackFrame(Top, closure, ths, args);
 }
예제 #3
0
파일: LayeState.cs 프로젝트: LayeLang/Laye
        internal LayeClosure BuildClosure(FunctionPrototype proto, OuterValue[] openOuters, LayeTypeDef definedType, LayeObject[] defaults)
        {
            var top = stack.Top;
            var closure = new LayeClosure(top.closure.kit, proto, definedType, defaults);
            var protoOuters = proto.outers;

            for (var i = 0; i < protoOuters.Length; i++)
                if (protoOuters[i].type == OuterValueType.LOCAL)
                    closure.outers[i] = FindOuterValue(top.Locals, protoOuters[i].location, openOuters);
                else closure.outers[i] = top.closure.outers[protoOuters[i].location];

            return closure;
        }
예제 #4
0
파일: LayeState.cs 프로젝트: LayeLang/Laye
 internal StackFrame PushFrame(LayeClosure closure, LayeObject ths, LayeObject[] args)
 {
     FrameCount++;
     return Top = new StackFrame(Top, closure, ths, args);
 }
예제 #5
0
파일: LayeState.cs 프로젝트: LayeLang/Laye
        internal StackFrame(StackFrame previous, LayeClosure closure, LayeObject ths, LayeObject[] args)
        {
            this.previous = previous;
            this.closure = closure;
            this.ths = ths;
            locals = new LayeObject[closure.proto.maxLocalCount];
            stack = new LayeObject[closure.proto.maxStackCount];

            openOuters = closure.proto.nested.Length != 0 ? new OuterValue[closure.proto.maxStackCount] : null;
            SetArgs(args);
        }
예제 #6
0
파일: LayeKit.cs 프로젝트: LayeLang/Laye
 internal LayeKit(string fileLocation, FunctionPrototype proto)
     : base(TYPE)
 {
     this.fileLocation = fileLocation;
     body = new LayeClosure(this, proto);
 }
예제 #7
0
파일: LayeKit.cs 프로젝트: LayeLang/Laye
 public LayeKit()
     : base(TYPE)
 {
     body = null;
 }
예제 #8
0
파일: LayeASM.cs 프로젝트: LayeLang/Laye
        public void Run()
        {
            var builder = new FunctionBuilder();
            builder.fileName = Path.GetFileName(filePath);

            var lines = File.ReadAllLines(filePath);
            for (uint lineNum = 0; lineNum < lines.Length; lineNum++)
            {
                var line = lines[lineNum].Trim();
                if (line.Length == 0 || line.StartsWith(";"))
                    continue;

                // make sure the builder knows what line we're on, woo.
                builder.currentLineNumber = lineNum + 1;

                var command = (line.Contains(" ") ? line.Substring(0, line.IndexOf(' ')) : line).ToLower();
                var rest = line.Substring(command.Length).Trim();

                if (command.StartsWith("."))
                {
                    switch (command.Substring(1))
                    {
                        case "fn":
                            var paramNames = rest.Split(' ');
                            builder = new FunctionBuilder(builder);
                            builder.currentLineNumber = lineNum + 1;
                            for (int i = 0; i < paramNames.Length; i++)
                                builder.AddParameter(paramNames[i]);
                            continue;
                        case "endfn":
                            var proto = builder.Build();
                            builder = builder.parent;
                            builder.OpClosure(proto);
                            continue;
                        default: Console.WriteLine(command); throw new ArgumentException();
                    }
                }
                else
                {
                    switch (command)
                    {
                        case "nop": CheckEmpty(rest); builder.OpNop(); continue;
                        case "pop": CheckEmpty(rest); builder.OpPop(); continue;
                        case "dup": CheckEmpty(rest); builder.OpDup(); continue;

                        case "jump": builder.OpJump((uint)(builder.InsnCount + int.Parse(rest) + 1)); continue;
                        case "jumpeq": builder.OpJumpEq((uint)(builder.InsnCount + int.Parse(rest) + 1)); continue;
                        case "jumpneq": builder.OpJumpNeq((uint)(builder.InsnCount + int.Parse(rest) + 1)); continue;
                        case "jumpt": builder.OpJumpT((uint)(builder.InsnCount + int.Parse(rest) + 1)); continue;
                        case "jumpf": builder.OpJumpF((uint)(builder.InsnCount + int.Parse(rest) + 1)); continue;

                        case "lload":
                        {
                            CheckSymbol(rest);
                            uint location;
                            if (builder.GetLocalLocation(rest, out location))
                                builder.OpLLoad(location);
                            else throw new ArgumentException();
                        } continue;
                        case "lstore":
                        {
                            CheckSymbol(rest);
                            uint location;
                            if (builder.GetLocalLocation(rest, out location))
                                builder.OpLStore(location);
                            else throw new ArgumentException();
                        } continue;
                        case "oload":
                        {
                            CheckSymbol(rest);
                            uint location;
                            if (builder.GetOuterLocation(rest, out location))
                                builder.OpOLoad(location);
                            else throw new ArgumentException();
                        } continue;
                        case "ostore":
                        {
                            CheckSymbol(rest);
                            uint location;
                            if (builder.GetOuterLocation(rest, out location))
                                builder.OpOStore(location);
                            else throw new ArgumentException();
                        } continue;
                        case "kload": CheckSymbol(rest); builder.OpKLoad(rest); continue;
                        case "kstore": CheckSymbol(rest); builder.OpKStore(rest); continue;
                        case "gload": CheckSymbol(rest); builder.OpGLoad(rest); continue;
                        case "gstore": CheckSymbol(rest); builder.OpGStore(rest); continue;
                        case "iload": CheckEmpty(rest); builder.OpILoad(uint.Parse(rest)); continue;
                        case "istore": CheckEmpty(rest); builder.OpIStore(uint.Parse(rest)); continue;
                        case "fload": CheckSymbol(rest); builder.OpFLoad(rest); continue;
                        case "fstore": CheckSymbol(rest); builder.OpFStore(rest); continue;
                        case "load": CheckSymbol(rest); builder.OpLoad(rest); continue;
                        case "store": CheckSymbol(rest); builder.OpStore(rest); continue;

                        case "null": CheckEmpty(rest); builder.OpNull(); continue;
                        case "bconst": builder.OpBConst(bool.Parse(rest)); continue;

                        case "iconst": builder.OpIConst(Laye.ConvertToInt(rest)); continue;
                        case "fconst": builder.OpFConst(Laye.ConvertToFloat(rest)); continue;

                        case "invoke": builder.OpInvoke(uint.Parse(rest)); continue;

                        case "prefix": CheckOperator(rest); builder.OpPrefix(rest); continue;
                        case "infix": CheckOperator(rest); builder.OpInfix(rest); continue;

                        case "compis": CheckEmpty(rest); builder.OpCompIs(); continue;
                        case "compnotis": CheckEmpty(rest); builder.OpCompNotIs(); continue;
                        case "comptypeof": CheckEmpty(rest); builder.OpCompTypeOf(); continue;
                        case "compnottypeof": CheckEmpty(rest); builder.OpCompNotTypeOf(); continue;
                        case "typeof": CheckEmpty(rest); builder.OpTypeOf(); continue;

                        default: Console.WriteLine(command); throw new ArgumentException();
                    }
                }
            }

            var kit = new LayeKit();
            var closure = new LayeClosure(kit, builder.Build());
            var lstate = new LayeState();
            /*
            lstate["println"] = (LayeCallback)((state, args) =>
            {
                var sbuilder = new StringBuilder();
                for (int i = 0; i < args.Length; i++)
                {
                    if (i > 0)
                        sbuilder.Append(" ");
                    sbuilder.Append(args[i].ToString());
                }
                Console.WriteLine(sbuilder);
                return LayeObject.NULL;
            }); */
            closure.Invoke(lstate);
        }