Example #1
0
        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);
        }
Example #3
0
 public void SetConstant(string name, ChimpyObject value)
 {
     constants.Add(name,value);
 }
Example #4
0
 ///<summary>
 /// Call a method on the object
 /// </summary>
 //TODO: throws ChimpyException
 public ChimpyObject Call(string method,ChimpyObject []arguments)
 {
     return chimpyClass.Lookup(method).Call(this,arguments);
 }
Example #5
0
 public void SetInstanceVariable(string name, ChimpyObject value)
 {
     _instanceVariables.Add(name,value);
 }
Example #6
0
 public abstract ChimpyObject Call(ChimpyObject receiver, ChimpyObject [] arguments);
Example #7
0
 public Context(ChimpyObject currentSelf)
     : this(currentSelf,currentSelf.chimpyClass)
 {
 }
Example #8
0
 public Context(ChimpyObject currentSelf, ChimpyClass currentClass)
     : this(currentSelf, currentClass, null)
 {
 }
Example #9
0
 public void SetLocal(string name,ChimpyObject value)
 {
     locals.Add(name,value);
 }
Example #10
0
 public LiteralNode(ChimpyObject value)
 {
     this.value = value;
 }
Example #11
0
            public override ChimpyObject Call(ChimpyObject receiver, ChimpyObject[] arguments)
            {
                foreach(ChimpyObject arg in arguments)
                {
                    Console.WriteLine(arg.ToJavaObject());
                }

                return ChimpyRuntime.Nil;
            }
Example #12
0
        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);
        }