Example #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            Graphics g = panel1.CreateGraphics();

            controller            = new BlockController(g, this.Font, panel1.Size, () => MakeTextBox());
            controller.Modified  += new ControllerModified(controller_Modified);
            document              = new Document("Kitsune");
            document.TitleChange += new TitleChangeEvent(document_TitleChange);
            document.SetTitle();
            RegisterMethods();
            controller.InitPalette(); // after methods are registered

            vm       = new VM.VM();
            compiler = new Compiler(vm, controller.GetBlockSpace());

            runForm = panel2;
            //runForm.Location = this.Location;
            //runForm.Size = this.Size;
            runGraphics = runForm.CreateGraphics();
            stage       = new Stage.Stage(runGraphics, this.Font, runForm.ClientSize);
            kitsune     = new Sprite(BitmapExtensions.LoadBmp("kitsune2.bmp"),
                                     runForm.ClientRectangle.Center().Offseted(-15, -15),
                                     true,
                                     -90);


            runForm.Paint       += new PaintEventHandler(runForm_Paint);
            runForm.DoubleClick += new EventHandler(runForm_DoubleClick);
            stage.AddSprite(kitsune);
            stage.RedrawAll();
            PrepareVM(vm, stage);
            PrepareCompiler(compiler);


            // I really should use a testing framework
            // testSplitFuncArgs();
            // testCBlockView(new Point(10, 10));
            // testReporterBlockView(new Point(250, 10));
            // testBlockStackView(new Point(450, 10));
        }
Example #2
0
        private void PrepareVM(VM.VM vm, Stage.Stage stage)
        {
            vm.RegisterPrimitve("fd", delegate(object[] args)
            {
                stage.Fwd(kitsune, (double)args[0]);
                return null;
            });

            vm.RegisterPrimitve("rt", delegate(object[] args)
            {
                stage.WithSprite(kitsune, (s) => s.rt((float)(double)args[0]));
                return null;
            });

            vm.RegisterPrimitve("lt", delegate(object[] args)
            {
                stage.WithSprite(kitsune, (s) => s.lt((float)(double)args[0]));
                return null;
            });

            vm.RegisterPrimitve("gotoxy", delegate(object[] args)
            {
                float x = (float) (double)args[0];
                float y = (float)(double)args[1];
                stage.WithSprite(kitsune, (s) => s.Move(new PointF(x,y)));
                return null;
            });

            vm.RegisterPrimitve("random", delegate(object[] args)
            {
                int low = (int)(double)args[0];
                int hi = (int)(double)args[1];
                double r = random.Next(low, hi);
                return r;
            });

            vm.RegisterPrimitve("+", delegate(object[] args)
            {
                double a = (double)args[0];
                double b = (double)args[1];
                return a+b;
            });

            vm.RegisterPrimitve("-", delegate(object[] args)
            {
                double a = (double)args[0];
                double b = (double)args[1];
                return a - b;
            });

            vm.RegisterPrimitve("*", delegate(object[] args)
            {
                double a = (double)args[0];
                double b = (double)args[1];
                return a * b;
            });

            vm.RegisterPrimitve("/", delegate(object[] args)
            {
                double a = (double)args[0];
                double b = (double)args[1];
                return a / b;
            });

            vm.RegisterPrimitve("<", delegate(object[] args)
            {
                object a = args[0];
                object b = args[1];
                if (a.GetType() != b.GetType())
                {
                    a = a.ToString();
                    b = b.ToString();
                }
                return (a as IComparable).CompareTo(b)< 0;
            });

            vm.RegisterPrimitve("=", delegate(object[] args)
            {
                object a = args[0];
                object b = args[1];
                if (a.GetType() != b.GetType())
                {
                    a = a.ToString();
                    b = b.ToString();
                }
                return a.Equals(b);
            });

            vm.RegisterPrimitve(">", delegate(object[] args)
            {
                object a = args[0];
                object b = args[1];
                if (a.GetType() != b.GetType())
                {
                    a = a.ToString();
                    b = b.ToString();
                }
                return (a as IComparable).CompareTo(b) > 0;
            });

            vm.RegisterPrimitve("and", delegate(object[] args)
            {
                bool a = (bool) args[0];
                bool b = (bool) args[1];

                return a && b;
            });

            vm.RegisterPrimitve("or", delegate(object[] args)
            {
                bool a = (bool)args[0];
                bool b = (bool)args[1];

                return a || b;
            });

            vm.RegisterPrimitve("not", delegate(object[] args)
            {
                bool a = (bool)args[0];

                return !a;
            });

            vm.RegisterPrimitve("say", delegate(object[] args)
            {
                stage.Say(kitsune, args[0].ToString());
                return null;
            });

            vm.RegisterPrimitve("sin", delegate(object[] args)
            {
                double angle = (double)args[0];
                angle = angle * Math.PI / 180.0;
                double result = Math.Sin(angle);
                return result;
            });

            vm.RegisterPrimitve("cos", delegate(object[] args)
            {
                double angle = (double)args[0];
                angle = angle * Math.PI / 180.0;
                double result = Math.Cos(angle);
                return result;
            });

            vm.RegisterPrimitve("tan", delegate(object[] args)
            {
                double angle = (double)args[0];
                angle = angle * Math.PI / 180.0;
                double result = Math.Tan(angle);
                return result;
            });

            vm.RegisterPrimitve("asin", delegate(object[] args)
            {
                double angle = (double)args[0];
                angle = angle * Math.PI / 180.0;
                double result = Math.Asin(angle);
                return result;
            });

            vm.RegisterPrimitve("acos", delegate(object[] args)
            {
                double angle = (double)args[0];
                angle = angle * Math.PI / 180.0;
                double result = Math.Acos(angle);
                return result;
            });

            vm.RegisterPrimitve("atan", delegate(object[] args)
            {
                double angle = (double)args[0];
                angle = angle * Math.PI / 180.0;
                double result = Math.Atan(angle);
                return result;
            });

            vm.RegisterPrimitve("sqrt", delegate(object[] args)
            {
                double angle = (double)args[0];
                angle = angle * Math.PI / 180.0;
                double result = Math.Sqrt(angle);
                return result;
            });

            vm.RegisterPrimitve("doNothing", delegate(object[] args)
            {
                return null;
            });
        }
Example #3
0
 public Push(VM vm, object value)
     : base(vm)
 {
     this.value = value;
 }
Example #4
0
 public Call(VM vm, Method callee)
     : base(vm)
 {
     this.callee = callee;
 }
Example #5
0
 public Stop(VM vm)
     : base(vm)
 {
 }
Example #6
0
 public PushLocal(VM vm, string name)
     : base(vm)
 {
     this.name= name;
 }
Example #7
0
 public Label(VM vm, string label)
     : base(vm)
 {
     this.label = label;
 }
Example #8
0
 public ApplyPrim(VM vm, int arity, Func<object[], object> primitive)
     : base(vm)
 {
     this.arity = arity;
     this.primitive = primitive;
 }
Example #9
0
 public Wait(VM vm)
     : base(vm)
 {
 }
Example #10
0
 public Ret(VM vm)
     : base(vm)
 {
 }
Example #11
0
 public CallNamed(VM vm, string calleeName)
     : base(vm)
 {
     this.calleeName = calleeName;
 }
Example #12
0
 public Call(VM vm, Method callee)
     : base(vm)
 {
     this.callee = callee;
 }
Example #13
0
 public Label(VM vm, string label)
     : base(vm)
 {
     this.label = label;
 }
Example #14
0
 public Jump(VM vm, string label)
     : base(vm)
 {
     this.label = label;
 }
Example #15
0
 public Stop(VM vm) : base(vm)
 {
 }
Example #16
0
        private void Form1_Load(object sender, EventArgs e)
        {
            Graphics g = panel1.CreateGraphics();

            controller = new BlockController(g, this.Font, panel1.Size, () => MakeTextBox());
            controller.Modified += new ControllerModified(controller_Modified);
            document = new Document("Kitsune");
            document.TitleChange += new TitleChangeEvent(document_TitleChange);
            document.SetTitle();
            RegisterMethods();
            controller.InitPalette(); // after methods are registered

            vm = new VM.VM();
            compiler = new Compiler(vm, controller.GetBlockSpace());

            runForm = panel2;
            //runForm.Location = this.Location;
            //runForm.Size = this.Size;
            runGraphics = runForm.CreateGraphics();
            stage = new Stage.Stage(runGraphics, this.Font, runForm.ClientSize);
            kitsune = new Sprite(BitmapExtensions.LoadBmp("kitsune2.bmp"),
                runForm.ClientRectangle.Center().Offseted(-15, -15),
                true,
                -90);

            runForm.Paint += new PaintEventHandler(runForm_Paint);
            runForm.DoubleClick += new EventHandler(runForm_DoubleClick);
            stage.AddSprite(kitsune);
            stage.RedrawAll();
            PrepareVM(vm, stage);
            PrepareCompiler(compiler);

            // I really should use a testing framework
            // testSplitFuncArgs();
            // testCBlockView(new Point(10, 10));
            // testReporterBlockView(new Point(250, 10));
            // testBlockStackView(new Point(450, 10));
        }
Example #17
0
 // Just pop a value from the operand stack
 public Discard(VM vm)
     : base(vm)
 {
 }
Example #18
0
 // Just pop a value from the operand stack
 public Discard(VM vm)
     : base(vm)
 {
 }
Example #19
0
 public JumpIfNot(VM vm, string label)
     : base(vm)
 {
     this.label = label;
 }
Example #20
0
 public PopLocal(VM vm, string name)
     : base(vm)
 {
     this.name = name;
 }
Example #21
0
 public Push(VM vm, object value)
     : base(vm)
 {
     this.value = value;
 }
Example #22
0
 public ApplyPrim(VM vm, int arity, Func <object[], object> primitive)
     : base(vm)
 {
     this.arity     = arity;
     this.primitive = primitive;
 }
Example #23
0
 public Ret(VM vm)
     : base(vm)
 {
 }
Example #24
0
 public Instruction(VM vm)
 {
     this.vm = vm;
 }
Example #25
0
 public Wait(VM vm)
     : base(vm)
 {
 }
Example #26
0
        private void PrepareVM(VM.VM vm, Stage.Stage stage)
        {
            vm.RegisterPrimitve("fd", delegate(object[] args)
            {
                stage.Fwd(kitsune, (double)args[0]);
                return(null);
            });

            vm.RegisterPrimitve("rt", delegate(object[] args)
            {
                stage.WithSprite(kitsune, (s) => s.rt((float)(double)args[0]));
                return(null);
            });

            vm.RegisterPrimitve("lt", delegate(object[] args)
            {
                stage.WithSprite(kitsune, (s) => s.lt((float)(double)args[0]));
                return(null);
            });

            vm.RegisterPrimitve("gotoxy", delegate(object[] args)
            {
                float x = (float)(double)args[0];
                float y = (float)(double)args[1];
                stage.WithSprite(kitsune, (s) => s.Move(new PointF(x, y)));
                return(null);
            });

            vm.RegisterPrimitve("random", delegate(object[] args)
            {
                int low  = (int)(double)args[0];
                int hi   = (int)(double)args[1];
                double r = random.Next(low, hi);
                return(r);
            });

            vm.RegisterPrimitve("+", delegate(object[] args)
            {
                double a = (double)args[0];
                double b = (double)args[1];
                return(a + b);
            });

            vm.RegisterPrimitve("-", delegate(object[] args)
            {
                double a = (double)args[0];
                double b = (double)args[1];
                return(a - b);
            });

            vm.RegisterPrimitve("*", delegate(object[] args)
            {
                double a = (double)args[0];
                double b = (double)args[1];
                return(a * b);
            });

            vm.RegisterPrimitve("/", delegate(object[] args)
            {
                double a = (double)args[0];
                double b = (double)args[1];
                return(a / b);
            });

            vm.RegisterPrimitve("<", delegate(object[] args)
            {
                object a = args[0];
                object b = args[1];
                if (a.GetType() != b.GetType())
                {
                    a = a.ToString();
                    b = b.ToString();
                }
                return((a as IComparable).CompareTo(b) < 0);
            });

            vm.RegisterPrimitve("=", delegate(object[] args)
            {
                object a = args[0];
                object b = args[1];
                if (a.GetType() != b.GetType())
                {
                    a = a.ToString();
                    b = b.ToString();
                }
                return(a.Equals(b));
            });

            vm.RegisterPrimitve(">", delegate(object[] args)
            {
                object a = args[0];
                object b = args[1];
                if (a.GetType() != b.GetType())
                {
                    a = a.ToString();
                    b = b.ToString();
                }
                return((a as IComparable).CompareTo(b) > 0);
            });

            vm.RegisterPrimitve("and", delegate(object[] args)
            {
                bool a = (bool)args[0];
                bool b = (bool)args[1];

                return(a && b);
            });

            vm.RegisterPrimitve("or", delegate(object[] args)
            {
                bool a = (bool)args[0];
                bool b = (bool)args[1];

                return(a || b);
            });

            vm.RegisterPrimitve("not", delegate(object[] args)
            {
                bool a = (bool)args[0];

                return(!a);
            });

            vm.RegisterPrimitve("say", delegate(object[] args)
            {
                stage.Say(kitsune, args[0].ToString());
                return(null);
            });

            vm.RegisterPrimitve("sin", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Sin(angle);
                return(result);
            });

            vm.RegisterPrimitve("cos", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Cos(angle);
                return(result);
            });

            vm.RegisterPrimitve("tan", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Tan(angle);
                return(result);
            });

            vm.RegisterPrimitve("asin", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Asin(angle);
                return(result);
            });

            vm.RegisterPrimitve("acos", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Acos(angle);
                return(result);
            });

            vm.RegisterPrimitve("atan", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Atan(angle);
                return(result);
            });


            vm.RegisterPrimitve("sqrt", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Sqrt(angle);
                return(result);
            });

            vm.RegisterPrimitve("doNothing", delegate(object[] args)
            {
                return(null);
            });
        }
Example #27
0
 public CallNamed(VM vm, string calleeName)
     : base(vm)
 {
     this.calleeName = calleeName;
 }
Example #28
0
 public Compiler(VM vm, BlockSpace blockSpace)
 {
     this.blockSpace = blockSpace;
     this.vm = vm;
     labelCount = varCount = 0;
 }