public ChimpyObject Run(Context context, ChimpyException e) { if(localName != null) context.SetLocal(localName, e.GetRuntimeObject()); return handler.Eval(context); }
public override ChimpyObject Eval(Context context) { if(receiver == null && arguments == null && context.HasLocal(method)) { return context.GetLocal(method); } ChimpyObject evaledReceiver; if(receiver == null) { evaledReceiver = context.CurrentSelf; } else { evaledReceiver = receiver.Eval(context); } List<ChimpyObject> evaledArguments = new List<ChimpyObject>(); if(arguments != null) { foreach(Node arg in arguments) { evaledArguments.Add(arg.Eval(context)); } } return evaledReceiver.Call(method,evaledArguments.ToArray()); }
public override ChimpyObject Eval(Context context) { ChimpyObject receiverEvaled = receiver.Eval(context); if(receiverEvaled.IsTrue()) return argument.Eval(context); return receiverEvaled; }
public override ChimpyObject Eval(Context context) { while(condition.Eval(context).IsTrue()) { body.Eval(context); } return ChimpyRuntime.Nil; }
public override ChimpyObject Eval(Context context) { ChimpyObject lastEval = ChimpyRuntime.Nil; foreach(Node n in nodes) { lastEval = n.Eval(context); } return lastEval; }
public override ChimpyObject Eval(Context context) { if (condition.Eval(context).IsTrue()) { return ifBody.Eval(context); } else if (elseBody != null) { return elseBody.Eval(context); } return ChimpyRuntime.Nil; }
public override ChimpyObject Eval(Context context) { string [] parametersName; if(parameters == null) { parametersName = new string[0]; } else { parametersName = parameters.ToArray(); } context.CurrentClass.AddMethod(name,new InterpretedMethod(name,parametersName,body)); return ChimpyRuntime.Nil; }
public Context(ChimpyObject currentSelf, ChimpyClass currentClass, Context parent) { this.currentClass=currentClass; this.currentSelf=currentSelf; this.parent=parent; if(parent == null) { locals=new Dictionary<string, ChimpyObject>(); } else { locals=parent.locals; } }
public override ChimpyObject Call(ChimpyObject receiver, ChimpyObject [] arguments) { if(delegatedMethodCall != null) return delegatedMethodCall(receiver,arguments); Context context = new Context(receiver); if(parameters.Length != arguments.Length) throw new ArgumentError(name,parameters.Length,arguments.Length); for (int i=0;i<parameters.Length;i++) { context.SetLocal(parameters[i],arguments[i]); } return body.Eval(context); }
public override ChimpyObject Eval(Context context) { ChimpyClass klass; if(superName == null) { klass = new ChimpyClass(name); } else { ChimpyClass superClass = (ChimpyClass) context.CurrentClass.GetConstant(superName); klass = new ChimpyClass(name,superClass); } body.Eval(new Context(klass,klass)); context.CurrentClass.SetConstant(name,klass); return klass; }
public override ChimpyObject Eval(Context context) { Context tryContext = context.MakeChildContext(); try { return body.Eval(tryContext); } catch(ChimpyException exception) { foreach (CatchBlock block in catchBlocks) { ExceptionHandler handler = block.ToExceptionHandler(); if(handler.Handle(exception)) return handler.Run(tryContext,exception); } throw exception; } }
public override ChimpyObject Eval(Context context) { return context.CurrentSelf; }
public override ChimpyObject Eval(Context context) { return context.CurrentSelf.GetInstanceVariables(name); }
public static Context Run() { ChimpyClass objectClass = new ChimpyClass("Object"); ChimpyRuntime.ObjectClass = objectClass; ChimpyObject main = new ChimpyObject(); ChimpyRuntime.MainObject = main; ChimpyClass classClass = new ChimpyClass("Class"); objectClass.chimpyClass = classClass; classClass.chimpyClass = classClass; main.chimpyClass = objectClass; objectClass.SetConstant("Object",objectClass); objectClass.SetConstant("Class",classClass); ChimpyRuntime.Nil = objectClass.NewSubclass("NilClass").NewInstance(null); ChimpyRuntime.True = objectClass.NewSubclass("TrueClass").NewInstance(true); ChimpyRuntime.False = objectClass.NewSubclass("FalseClass").NewInstance(false); ChimpyClass stringClass = objectClass.NewSubclass("String"); ChimpyClass numberClass = objectClass.NewSubclass("Number"); ChimpyClass integerClass = objectClass.NewSubclass("Integer"); ChimpyClass floatClass = objectClass.NewSubclass("Float"); ChimpyClass exceptionClass = objectClass.NewSubclass("Exception"); exceptionClass.NewSubclass("IOException"); exceptionClass.NewSubclass("TypeError"); exceptionClass.NewSubclass("MethodNotFound"); exceptionClass.NewSubclass("ArgumentError"); exceptionClass.NewSubclass("FileNotFound"); objectClass.AddMethod("print",new InterpretedMethod(delegate (ChimpyObject receiver, ChimpyObject[] arguments){ foreach(ChimpyObject arg in arguments) { Console.WriteLine(arg.ToJavaObject()); } return ChimpyRuntime.Nil; })); objectClass.AddMethod("class",new InterpretedMethod(delegate (ChimpyObject receiver, ChimpyObject[] arguments){ return receiver.chimpyClass; })); objectClass.AddMethod("eval",new InterpretedMethod(delegate (ChimpyObject receiver, ChimpyObject[] arguments){ Context context= new Context(receiver); StringReader code = new StringReader(arguments[0].AsString()); return context.Eval(code); })); objectClass.AddMethod("require",new InterpretedMethod(delegate (ChimpyObject receiver, ChimpyObject[] arguments){ Context context = new Context(); string filename = arguments[0].AsString(); try { return context.Eval(File.OpenText(filename)); } catch (FileNotFoundException e) { throw new ChimpyException("FileNotFound","File not found "+filename); } })); classClass.AddMethod("new",new InterpretedMethod(delegate (ChimpyObject receiver, ChimpyObject[] arguments){ ChimpyClass self = (ChimpyClass) receiver; ChimpyObject instance = self.NewInstance(); if(self.HasMethod("initialize")) instance.Call("initialize",arguments); return instance; })); classClass.AddMethod("name",new InterpretedMethod(delegate (ChimpyObject receiver, ChimpyObject[] arguments){ ChimpyClass self = (ChimpyClass) receiver; return new ValueObject(self.Name); })); classClass.AddMethod("superclass",new InterpretedMethod(delegate (ChimpyObject receiver, ChimpyObject[] arguments){ ChimpyClass self = (ChimpyClass) receiver; return self.SuperClass; })); integerClass.AddMethod("+",new OperatorMethod<Int32>(delegate (Int32 receiver, Int32 argument){ return new ValueObject((Int32)(receiver+argument)); })); return new Context(main); }
public override ChimpyObject Eval(Context context) { ChimpyObject value = expression.Eval(context); context.CurrentClass.SetConstant(name, value); return value; }
public override ChimpyObject Eval(Context context) { ChimpyObject value = expression.Eval(context); context.SetLocal(name,value); return value; }
public abstract ChimpyObject Eval(Context context);
public override ChimpyObject Eval(Context context) { if(receiver.Eval(context).IsTrue()) return ChimpyRuntime.False; return ChimpyRuntime.True; }
public override ChimpyObject Eval(Context context) { return value; }
public override ChimpyObject Eval(Context context) { ChimpyObject value = expression.Eval(context); context.CurrentSelf.SetInstanceVariable(name,value); return value; }
public override ChimpyObject Eval(Context context) { return context.CurrentClass.GetConstant(name); }