Пример #1
0
        public override void Compile(Kernel k)
        {
            k.EmitPush("1u");
            k.Emit(Opcode.ALLOC).SetDebug(File, Line, Column, DebugType.Define, this.VariableName);
            k.CurrentScope.MemorySpace += 1;

            Symbol symbol = new Symbol()
                {
                    Name = this.VariableName,
                    SMode = Symbol.Mode.Intern,
                    SType = Symbol.Type.Variable,
                    Id = k.CurrentScope.RequestId()
                };

            k.RegisterSymbol(symbol);

            if (this.Value == null)
            {
                k.Emit(Opcode.PUSHNIL).Comment = "clear memory for variable";
            }
            else
            {
                this.Value.Compile(k);
            }

            k.EmitPush(symbol.Id.ToString() + "u");
            k.Emit(Opcode.STLO).SetDebug(File, Line, Column, DebugType.Set, this.VariableName);
        }
Пример #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, "");
        }
Пример #3
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);
            }
        }
Пример #4
0
 public override void Compile(Kernel k)
 {
     if(this.Value)
     {
         k.Emit(Opcode.PUSHT).SetDebug(File, Line, Column, DebugType.Value, "true");
     }
     else
     {
         k.Emit(Opcode.PUSHF).SetDebug(File, Line, Column, DebugType.Value, "false");
     }
 }
Пример #5
0
        public override void Compile(Kernel k)
        {
            k.CurrentScope.PushMemory(k);

            Scope scope = k.PushScope();
            scope.Name = "for" + scope.Parent.RequestLabelId();

            if (this.Init != null)
                this.Init.Compile(k);

            string forLabel = "sl_fl_" + k.GetScopeName();
            string endLabel = "sl_flh_" + k.GetScopeName();

            Symbol breakSymbol = new Symbol()
                {
                    Name = "+break",
                    AsmName = endLabel
                };
            k.RegisterSymbol(breakSymbol);

            k.Emit(Opcode.LABEL, forLabel).Comment = "for loop";
            k.Emit(Opcode.NOOP).SetDebug(File, Line, Column, DebugType.ForLoop, "");

            this.Check.Compile(k);

            k.Emit(Opcode.NOT);
            k.Emit(Opcode.GOTOF, '"' + endLabel + '"');

            scope.PushMemory(k);

            Scope innerScope = k.PushScope();
            innerScope.Name = "in";

            if (this.Body != null)
                this.Body.Compile(k);

            k.EmitPush(innerScope.MemorySpace.ToString() + "u");
            k.Emit(Opcode.DEALLOC);

            k.PopScope();
            scope.PopMemory(k);

            if (this.Increment != null)
                this.Increment.Compile(k);

            k.Emit(Opcode.GOTO, '"' + forLabel + '"');
            k.Emit(Opcode.LABEL, endLabel).Comment = "end for";

            k.PopScope();

            k.CurrentScope.PopMemory(k);
        }
Пример #6
0
        public override void Compile(Kernel k)
        {
            int op = 0;
            for (int i = 0; i < this.Values.Count; i++)
            {
                this.Values[i].Compile(k);

                if(i > 0)
                {
                    k.Emit(Opcode.SWAP);
                    k.Emit(this.Ops[op]);
                    op++;
                }
            }
        }
Пример #7
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, "");
        }
Пример #8
0
 public override void Compile(Kernel k)
 {
     if(this.Value == null)
     {
         k.Emit(Opcode.PUSHNIL).SetDebug(File, Line, Column, DebugType.Value, "nil");
     }
     else
     {
         k.EmitPush(this.Value).SetDebug(File, Line, Column, DebugType.Value, this.Value);
     }
 }
Пример #9
0
        public override void Compile(Kernel k)
        {
            k.CurrentScope.PushMemory(k);

            Scope ifScope = k.PushScope();
            ifScope.Name = "if" + ifScope.Parent.RequestLabelId();

            string trueLabel = "sl_if_" + k.GetScopeName();
            string falseLabel = "sl_fe_" + k.GetScopeName();
            string endLabel = "sl_fh_" + k.GetScopeName();

            this.Check.Compile(k);

            k.Emit(Opcode.GOTOF, '"' + trueLabel + '"').SetDebug(File, Line, Column, DebugType.Branch, "");
            k.Emit(Opcode.GOTO, '"' + falseLabel + '"');

            k.Emit(Opcode.LABEL, trueLabel);

            k.CurrentScope.PushMemory(k);

            Scope trueScope = k.PushScope();
            trueScope.Name = "true";

            if (this.BranchTrue != null)
                this.BranchTrue.Compile(k);

            k.PopScope();

            k.CurrentScope.PopMemory(k);

            if(this.BranchFalse == null)
            {
                k.Emit(Opcode.LABEL, falseLabel).Comment = "no else block, used as end label";
            }
            else if(this.BranchFalse != null)
            {
                k.Emit(Opcode.GOTO, '"' + endLabel + '"');

                k.Emit(Opcode.LABEL, falseLabel);

                k.CurrentScope.PushMemory(k);

                Scope falseScope = k.PushScope();
                falseScope.Name = "false";

                this.BranchFalse.Compile(k);

                k.PopScope();

                k.CurrentScope.PopMemory(k);

                k.Emit(Opcode.LABEL, endLabel).Comment = "end if";
            }

            k.PopScope();

            k.CurrentScope.PopMemory(k);
        }
Пример #10
0
        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);
            }
        }
Пример #11
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";
            }
        }
Пример #12
0
 public override void Compile(Kernel k)
 {
     k.Emit(this.Operation).SetDebug(File, Line, Column, DebugType.Raw, "");
 }
Пример #13
0
        public override void Compile(Kernel k)
        {
            this.Value.Compile(k);

            k.Emit(Opcode.NOT).SetDebug(File, Line, Column, DebugType.Operation, "not");
        }
Пример #14
0
 public override void Compile(Kernel k)
 {
     k.Emit(Opcode.DBRK).SetDebug(File, Line, Column, DebugType.Debug, "");
 }
Пример #15
0
        protected void CompileLibrary(Kernel k)
        {
            for (int i = this.Arguments.Count - 1; i >= 0; i--)
            {
                this.Arguments[i].Compile(k);
            }

            uint returnId = k.CurrentScope.RequestLabelId();

            k.Emit(Opcode.PLABL, "\"sl_r_" + k.GetScopeName() + "_" + returnId.ToString() + "\"");
            var rvn = new RetrieveVariableNode(-1, -1)
                {
                    VariableName = this.Function
                };
            rvn.PrePass(k);
            rvn.PreCompile(k);
            rvn.Compile(k);
            k.CurrentScope.PushMemory(k);
            k.Emit(Opcode.JUMP).SetDebug(File, Line, Column, DebugType.LCall, this.Function);

            k.Emit(Opcode.LABEL, "sl_r_" + k.GetScopeName() + "_" + returnId.ToString()).Comment = "return point from " + this.Function;
            k.CurrentScope.PopMemory(k);
        }
Пример #16
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);
        }
Пример #17
0
        protected void CompileExtern(Kernel k)
        {
            for(int i = this.Arguments.Count - 1; i >= 0; i--)
            {
                this.Arguments[i].Compile(k);
            }

            k.EmitPush('"' + this.Function + '"');
            k.Emit(Opcode.ECALL).SetDebug(File, Line, Column, DebugType.ECall, this.Function);
        }
Пример #18
0
 public override void Compile(Kernel k)
 {
     k.Emit(Opcode.PUSHNIL).SetDebug(File, Line, Column, DebugType.Value, "nil");
 }
Пример #19
0
 public override void Compile(Kernel k)
 {
     k.Emit(Opcode.NOOP).SetDebug(File, Line, Column, DebugType.Raw, "user");
     k.Emit(new Operation() { Raw = this.Assembly });
     k.Emit(Opcode.NOOP).Comment = "end user section";
 }
Пример #20
0
 public override void Compile(Kernel k)
 {
     k.Emit(Opcode.HALT).SetDebug(File, Line, Column, DebugType.Raw, "halt");
 }
Пример #21
0
        public override void Compile(Kernel k)
        {
            Symbol symbol = new Symbol()
                {
                    Name = this.Function,
                    SMode = Symbol.Mode.Intern,
                    SType = Symbol.Type.Function,
                    Args = (uint)this.Arguments.Count
                };

            k.RegisterSymbol(symbol);

            Scope scope = k.PushScope();
            scope.Name = this.Function;
            scope.MemorySpace += (uint)this.Arguments.Count + 1;

            symbol.AsmName = string.Format("sl_f_{0}", k.GetScopeName());

            switch(k.CurrentImportMode)
            {
                case ImportMode.Library:
                    k.PopScope();
                    symbol.SMode = Symbol.Mode.Library;
                    symbol.Id = k.CurrentScope.RequestId();
                    k.CurrentScope.MemorySpace++;
                    k.AddImport(symbol);
                    return;

                case ImportMode.Export:
                    k.AddExport(symbol);
                    break;
            }

            k.Emit(Opcode.LABEL, symbol.AsmName).Comment = "function " + this.Function; // function label
            k.Emit(Opcode.NOOP).SetDebug(File, Line, Column, DebugType.Function, this.Function);

            k.EmitPush(scope.MemorySpace.ToString() + "u").Comment = "allocate function parameter memory"; // allocate memory space for arguments and return location
            k.Emit(Opcode.ALLOC);

            Symbol returnSymbol = new Symbol()
                {
                    Name = "+return",
                    SMode = Symbol.Mode.Intern,
                    SType = Symbol.Type.Variable,
                    Id = k.CurrentScope.RequestId()
                };
            k.RegisterSymbol(returnSymbol);

            k.EmitPush(returnSymbol.Id.ToString() + "u").Comment = "store return location"; // store the return location
            k.Emit(Opcode.STLO);

            foreach(string arg in this.Arguments)
            {
                Symbol argSymbol = new Symbol()
                    {
                        Name = arg,
                        SMode = Symbol.Mode.Intern,
                        SType = Symbol.Type.Variable,
                        Id = k.CurrentScope.RequestId()
                    };
                k.RegisterSymbol(argSymbol);

                k.EmitPush(argSymbol.Id.ToString() + "u").Comment = "store argument " + arg; // store the argument
                k.Emit(Opcode.STLO);
            }

            k.Emit(Opcode.NOOP).Comment = "function body";

            if (this.Body != null)
                this.Body.Compile(k);

            new ReturnNode(-1, -1).Compile(k);

            k.PopScope();

            k.Emit(Opcode.NOOP).Comment = "end of function";
        }