Example #1
0
        public override void Compile(Kernel k)
        {
            Symbol symbol = k.Lookup(this.VariableName);

            if(this.Value == null)
            {
                k.Emit(Opcode.PUSHNIL).Comment = "set variable to nil";
            }
            else
            {
                this.Value.Compile(k);
            }

            if (symbol.SScope == k.CurrentScope)
            {
                k.EmitPush(symbol.Id.ToString() + "u").Comment = "store into variable " + this.VariableName;
                k.Emit(Opcode.STLO).SetDebug(File, Line, Column, DebugType.Set, this.VariableName);
            }
            else
            {
                uint mem = k.CurrentScope.WalkMemoryBack(symbol.SScope);
                mem -= symbol.Id;

                k.EmitPush(mem.ToString() + "u").Comment = "store into variable " + this.VariableName;
                k.Emit(Opcode.STNLO).SetDebug(File, Line, Column, DebugType.Set, this.VariableName);
            }
        }
Example #2
0
        public override void Compile(Kernel k)
        {
            Symbol returnSymbol = k.Lookup("+return");

            if (this.Value != null)
                this.Value.Compile(k);
            else
                k.Emit(Opcode.PUSHNIL);

            var rvn = new RetrieveVariableNode(-1, -1) {VariableName = "+return"};
            rvn.PrePass(k);
            rvn.PreCompile(k);
            rvn.Compile(k);

            uint mem = 0;
            Scope current = k.CurrentScope;
            while(current != returnSymbol.SScope)
            {
                mem += current.MemorySpace;
                current.PopMemory(k, false);

                current = current.Parent;
            }

            mem += current.MemorySpace;

            current.PopMemory(k, false);

            k.EmitPush(mem + "u").Comment = "deallocate function memory";
            k.Emit(Opcode.DEALLOC);

            k.Emit(Opcode.JUMP).SetDebug(File, Line, Column, DebugType.Return, "");
        }
Example #3
0
        public override void Compile(Kernel k)
        {
            Symbol breakSymbol = k.Lookup("+break");
            uint mem = 0;
            Scope current = k.CurrentScope;
            while (current != breakSymbol.SScope)
            {
                mem += current.MemorySpace;
                current.PopMemory(k, false);

                current = current.Parent;
            }

            current.PopMemory(k, false);

            k.EmitPush(mem + "u").Comment = "deallocate scope memory";
            k.Emit(Opcode.DEALLOC);

            k.Emit(Opcode.GOTO, "\"" + breakSymbol.AsmName + "\"").SetDebug(File, Line, Column, DebugType.Break, "");
        }
Example #4
0
        public override void Compile(Kernel k)
        {
            Symbol symbol = k.Lookup(this.Function);

            switch(symbol.SType)
            {
                case Symbol.Type.Function:
                    break;

                default:
                    throw new InvalidOperationException(string.Format("Tried to compile a function call on a non-function symbol \"{0}\"", this.Function));
            }

            if (symbol.Args != this.Arguments.Count)
                throw new ArgumentOutOfRangeException(string.Format("Tried to pass {0} arguments to {1} (requires: {2})", this.Arguments.Count, symbol.Name, symbol.Args));

            switch(symbol.SMode)
            {
                default:
                    throw new InvalidOperationException(string.Format("Unknown symbol mode {0} on symbol \"{1}\"", symbol.SMode.ToString(), this.Function));

                case Symbol.Mode.Extern:
                    this.CompileExtern(k);
                    break;

                case Symbol.Mode.Intern:
                    this.CompileIntern(k);
                    break;

                case Symbol.Mode.Library:
                    this.CompileLibrary(k);
                    break;
            }

            if(!this.UseReturn)
            {
                k.Emit(Opcode.POP).Comment = "remove unused return value";
            }
        }
        public override void Compile(Kernel k)
        {
            Symbol symbol = k.Lookup(this.VariableName);

            if (symbol.SType != Symbol.Type.Variable)
            {
                throw new CompileException(String.Format("Symbol {0} is not a variable", this.VariableName));
            }

            if (symbol.SScope == k.CurrentScope)
            {
                k.EmitPush(symbol.Id.ToString() + "u").Comment = "retrieve variable " + this.VariableName;
                k.Emit(Opcode.LDLO).SetDebug(File, Line, Column, DebugType.Retrieve, this.VariableName);
            }
            else
            {
                uint mem = k.CurrentScope.WalkMemoryBack(symbol.SScope);
                mem -= symbol.Id;

                k.EmitPush(mem.ToString() + "u").Comment = "retrieve variable " + this.VariableName;
                k.Emit(Opcode.LDNLO).SetDebug(File, Line, Column, DebugType.Retrieve, this.VariableName);
            }
        }
Example #6
0
        protected void CompileIntern(Kernel k)
        {
            for(int i = this.Arguments.Count - 1; i >= 0; i--)
            {
                this.Arguments[i].Compile(k);
            }

            uint returnId = k.CurrentScope.RequestLabelId();

            k.CurrentScope.PushMemory(k);
            k.Emit(Opcode.PLABL, "\"sl_r_" + k.GetScopeName() + "_" + returnId.ToString() + "\"");
            k.Emit(Opcode.GOTO, '"' + k.Lookup(this.Function).AsmName + '"').SetDebug(File, Line, Column, DebugType.Call, this.Function);

            k.Emit(Opcode.LABEL, "sl_r_" + k.GetScopeName() + "_" + returnId.ToString()).Comment = "return point from " + this.Function;
            k.CurrentScope.PopMemory(k);
        }