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)); }
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; }); }
public Push(VM vm, object value) : base(vm) { this.value = value; }
public Call(VM vm, Method callee) : base(vm) { this.callee = callee; }
public Stop(VM vm) : base(vm) { }
public PushLocal(VM vm, string name) : base(vm) { this.name= name; }
public Label(VM vm, string label) : base(vm) { this.label = label; }
public ApplyPrim(VM vm, int arity, Func<object[], object> primitive) : base(vm) { this.arity = arity; this.primitive = primitive; }
public Wait(VM vm) : base(vm) { }
public Ret(VM vm) : base(vm) { }
public CallNamed(VM vm, string calleeName) : base(vm) { this.calleeName = calleeName; }
public Jump(VM vm, string label) : base(vm) { this.label = label; }
// Just pop a value from the operand stack public Discard(VM vm) : base(vm) { }
public JumpIfNot(VM vm, string label) : base(vm) { this.label = label; }
public PopLocal(VM vm, string name) : base(vm) { this.name = name; }
public ApplyPrim(VM vm, int arity, Func <object[], object> primitive) : base(vm) { this.arity = arity; this.primitive = primitive; }
public Instruction(VM vm) { this.vm = vm; }
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); }); }
public Compiler(VM vm, BlockSpace blockSpace) { this.blockSpace = blockSpace; this.vm = vm; labelCount = varCount = 0; }