public EnumerableBehavior(IBehavior behavior, IBehavior superclass, Machine machine) : base(behavior, superclass, machine, typeof(IEnumerable)) { string dosource = @" do: aBlock | enumerator | enumerator := self !GetEnumerator. [enumerator !MoveNext] whileTrue: [ aBlock value: enumerator !Current ] "; string selectsource = @" select: aBlock | enumerator list | enumerator := self !GetEnumerator. list := @System.Collections.ArrayList !new. [enumerator !MoveNext] whileTrue: [ | item | item := enumerator !Current. (aBlock value: item) ifTrue: [ list add: item ] ]. ^list "; Parser parser = new Parser(dosource); this.DefineInstanceMethod(parser.CompileInstanceMethod(this)); parser = new Parser(selectsource); this.DefineInstanceMethod(parser.CompileInstanceMethod(this)); this.DefineInstanceMethod(new FunctionalMethod("includes:", this, this.IncludesMethod)); }
public BlockBehavior(IBehavior behavior, IBehavior superclass, Machine machine) : base(behavior, superclass, machine, typeof(Block)) { string assertcode = @" assert self value ifFalse: [@AjTalk.Exceptions.AssertError new raise]. ^true "; Parser parser = new Parser(assertcode); this.DefineInstanceMethod(parser.CompileInstanceMethod(this)); ////this.DefineInstanceMethod(new FunctionalMethod("assert", this, this.AssertMethod)); }
public BooleanBehavior(IBehavior behavior, IBehavior superclass, Machine machine) : base(behavior, superclass, machine, typeof(bool)) { string iffalseiftruesource = @" ifFalse: falseBlock ifTrue: trueBlock self ifFalse: [^falseBlock value]. ^trueBlock value. "; string iftrueiffalsesource = @" ifTrue: trueBlock ifFalse: falseBlock self ifFalse: [^falseBlock value]. ^trueBlock value. "; Parser parser = new Parser(iffalseiftruesource); this.DefineInstanceMethod(parser.CompileInstanceMethod(this)); parser = new Parser(iftrueiffalsesource); this.DefineInstanceMethod(parser.CompileInstanceMethod(this)); }
private static void ProcessLine(string [] words, StreamReader reader) { if (words[0].Length == 0) return; Console.WriteLine(words[0]); if (words[0] == "class") { if (words.Length > 2) cls = new BaseClass(words[1], (IClass) objects[words[2]], machine); else cls = new BaseClass(words[1], machine); objects[words[1]]=cls; } else if (words[0] == "variables") { for (int k = 1; k < words.Length; k++) cls.DefineInstanceVariable(words[k]); } else if (words[0] == "method") { string line; string body = ""; line = reader.ReadLine(); while (line != null && line != "") { body += line; body += " "; line = reader.ReadLine(); } Parser compiler = new Parser(body); compiler.CompileInstanceMethod(cls); } else Console.WriteLine("Unknown {0}", words[0]); }
public void CompileSuperNew() { Parser compiler = new Parser("new super new"); Block block = compiler.CompileInstanceMethod(null); Assert.IsNotNull(block); var decompiler = new BlockDecompiler(block); var steps = decompiler.Decompile(); Assert.IsNotNull(steps); Assert.AreEqual(2, steps.Count); Assert.AreEqual("GetSuper", steps[0]); Assert.AreEqual("Send new 0", steps[1]); }
public void CompileSetMethod() { Machine machine = new Machine(); IClass cls = machine.CreateClass("Rectangle"); cls.DefineInstanceVariable("x"); Parser compiler = new Parser("x: newX x := newX"); var method = compiler.CompileInstanceMethod(cls); Assert.IsNotNull(method); Assert.AreEqual("x:", method.Name); Assert.IsNotNull(method.ByteCodes); }
public void CompileMethodWithLocals() { Machine machine = new Machine(); IClass cls = machine.CreateClass("Rectangle"); cls.DefineInstanceVariable("x"); Parser compiler = new Parser("x | temp | temp := x. ^temp"); var method = compiler.CompileInstanceMethod(cls); Assert.IsNotNull(method); Assert.AreEqual("x", method.Name); Assert.IsNotNull(method.ByteCodes); }
public void CompileMethodWithHorizontalBarAsName() { Machine machine = new Machine(); IClass cls = machine.CreateClass("Rectangle"); Parser compiler = new Parser("| aBoolean ^aBoolean"); var method = compiler.CompileInstanceMethod(cls); Assert.IsNotNull(method); Assert.AreEqual("|", method.Name); Assert.IsNotNull(method.ByteCodes); }
internal static IClass CompileClass(string clsname, string[] varnames, string[] methods, string[] clsmethods) { Machine machine = new Machine(); IClass cls = machine.CreateClass(clsname); if (varnames != null) { foreach (string varname in varnames) { cls.DefineInstanceVariable(varname); } } if (methods != null) { foreach (string method in methods) { Parser compiler = new Parser(method); cls.DefineInstanceMethod(compiler.CompileInstanceMethod(cls)); } } if (clsmethods != null) { foreach (string method in clsmethods) { Parser compiler = new Parser(method); cls.DefineClassMethod(compiler.CompileClassMethod(cls)); } } return cls; }
static void Main(string[] args) { Machine machine = new Machine(); IClass cls = machine.CreateClass("Point"); cls.DefineInstanceVariable("x"); cls.DefineInstanceVariable("y"); IObject obj = (IObject) cls.NewObject(); Console.WriteLine(obj[0]); Console.WriteLine(obj[1]); obj[0]=0; obj[1]=1; Console.WriteLine(obj[0]); Console.WriteLine(obj[1]); Console.ReadLine(); Method mth = new Method(cls,"set:"); mth.CompileArgument("newValue"); mth.CompileGet("newValue"); mth.CompileSet("x"); mth.CompileGet("newValue"); mth.CompileSet("y"); mth.Execute(obj.Behavior,obj,new object[] {10}); cls.DefineInstanceMethod(mth); Console.WriteLine(obj[0]); Console.WriteLine(obj[1]); Console.ReadLine(); mth = new Method(cls,"x:y:"); mth.CompileArgument("newX"); mth.CompileArgument("newY"); mth.CompileGet("newX"); mth.CompileSet("x"); mth.CompileGet("newY"); mth.CompileSet("y"); mth.Execute(obj.Behavior,obj,new object[] {10,20}); cls.DefineInstanceMethod(mth); Console.WriteLine(obj[0]); Console.WriteLine(obj[1]); Console.ReadLine(); mth = new Method(cls,"x:"); mth.CompileArgument("newX"); mth.CompileGet("newX"); mth.CompileSet("x"); mth.Execute(obj.Behavior,obj,new object[] {10,20}); cls.DefineInstanceMethod(mth); mth = new Method(cls,"y:"); mth.CompileArgument("newY"); mth.CompileGet("newY"); mth.CompileSet("y"); mth.Execute(obj.Behavior,obj,new object[] {10,20}); cls.DefineInstanceMethod(mth); Parser compiler = new Parser("set2: newValue self x: newValue self y: newValue."); compiler.CompileInstanceMethod(cls); compiler = new Parser("x ^x."); compiler.CompileInstanceMethod(cls); compiler = new Parser("y ^y."); compiler.CompileInstanceMethod(cls); compiler = new Parser("x1 ^x+1."); compiler.CompileInstanceMethod(cls); compiler = new Parser("y1 ^y+1."); compiler.CompileInstanceMethod(cls); obj.SendMessage("set2:", new object[] { 10 }); Console.WriteLine(obj[0]); Console.WriteLine(obj[1]); Console.ReadLine(); obj.SendMessage("x:", new object[] { 30 }); Console.WriteLine(obj[0]); Console.WriteLine(obj[1]); Console.ReadLine(); obj.SendMessage("y:", new object[] { 20 }); Console.WriteLine(obj[0]); Console.WriteLine(obj[1]); Console.ReadLine(); Console.WriteLine(obj.SendMessage("x", null)); Console.WriteLine(obj.SendMessage("y", null)); Console.ReadLine(); }
private void CompileInstanceMethod(IBehavior cls, string text, Machine machine) { Parser parser = new Parser(text); cls.DefineInstanceMethod(parser.CompileInstanceMethod(cls)); }
public void EvaluateLocalInstanceInInnerBlock() { var cls = this.machine.CreateClass("MyClass", this.machine.UndefinedObjectClass, "x", string.Empty); this.machine.SetCurrentEnvironmentObject(cls.Name, cls); var result = this.Evaluate("myobj := MyClass basicNew"); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IObject)); var iobj = (IObject)result; iobj[0] = 10; Parser parser = new Parser("x ^[x] value"); cls.DefineInstanceMethod(parser.CompileInstanceMethod(cls)); Assert.AreEqual(10, this.Evaluate("myobj x")); }
public Method CompileInstanceMethod(string text, IBehavior cls) { Parser parser = new Parser(text); return parser.CompileInstanceMethod(cls); }