Exemplo n.º 1
0
 /// <summary>
 /// Creates a class inheriting from superClass.
 /// </summary>
 public ChimpyClass(string name, ChimpyClass superClass)
     : base("Class")
 {
     this.name = name;
     this.superClass = superClass;
     methods = new Dictionary<string, Method>();
     constants = new Dictionary<string, ChimpyObject>();
 }
Exemplo n.º 2
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;
            }
        }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
0
 /// <summary>
 /// Creates a new subclass of this class.
 /// </summary>
 /// <returns>
 /// The instance of subclass.
 /// </returns>
 /// <param name='name'>
 /// Name of subclass.
 /// </param>
 public ChimpyClass NewSubclass(string name)
 {
     ChimpyClass klass = new ChimpyClass(name,this);
     ChimpyRuntime.ObjectClass.SetConstant(name,klass);
     return klass;
 }
Exemplo n.º 5
0
 /// <summary>
 /// Determines whether this instance is subclass of the specified klass.
 /// </summary>
 /// <returns>
 /// <c>true</c> if this instance is subclass of the specified klass; otherwise, <c>false</c>.
 /// </returns>
 /// <param name='klass'>
 /// Class to check against
 /// </param>
 public bool IsSubclass(ChimpyClass klass)
 {
     if(klass == this) return true;
     if(klass.SuperClass == null) return false;
     if(klass.SuperClass == this) return true;
     return IsSubclass(klass.SuperClass);
 }
Exemplo n.º 6
0
 public ValueObject(ChimpyClass klass,Object value)
     : base(klass)
 {
     this.value=value;
 }
Exemplo n.º 7
0
 protected void SetRuntimeClass(string name)
 {
     runtimeClass = ChimpyRuntime.GetExceptionClass().Subclass(name);
 }
Exemplo n.º 8
0
 public ChimpyException(string message)
     : base(message)
 {
     this.runtimeClass = ChimpyRuntime.GetExceptionClass();
 }
Exemplo n.º 9
0
 public ChimpyException(ChimpyClass runtimeClass)
     : base()
 {
     this.runtimeClass = runtimeClass;
 }
Exemplo n.º 10
0
 public ChimpyException(ChimpyClass runtimeClass,string message)
     : base(message)
 {
     this.runtimeClass=runtimeClass;
 }
Exemplo n.º 11
0
 public ChimpyObject(ChimpyClass chimpyClass)
 {
     this._chimpyClass=chimpyClass;
     this._instanceVariables=new Dictionary<string,ChimpyObject>();
 }
Exemplo n.º 12
0
 public Context(ChimpyObject currentSelf, ChimpyClass currentClass)
     : this(currentSelf, currentClass, null)
 {
 }
Exemplo n.º 13
0
 public ExceptionHandler(ChimpyClass klass, string localName, IEvaluable handler)
 {
     this.localName=localName;
     this.klass=klass;
     this.handler=handler;
 }
Exemplo n.º 14
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);
        }