Beispiel #1
0
        public GSharpObject InvokeMethod(GSharpMethod method, StackFrame frame, GSharpObject self, GSharpObject[] arguments)
        {
            Stack.NewFrame(frame);
            int insCount = method.Body.Count;
            int i        = 0;

            foreach (string param in method.Parameters.Keys)
            {
                Stack.StoreLocal(method.Parameters[param], arguments[i++]);
            }

            StackFrame top = Stack.Top;

            while (top.InstructionPointer < insCount && !top.AbortExecution)
            {
                Instruction currInstr = method.Body[Stack.InstructionPointer++];
                ExecuteInstruction(currInstr);
            }

            if (top.AbortExecution)
            {
                return(null);
            }

            GSharpObject retVal = Stack.Pop();

            Stack.EndFrame();

            return(retVal);
        }
Beispiel #2
0
 public GSharpObject contains(VirtualMachine vm, GSharpObject self, GSharpObject[] arguments)
 {
     if (arguments.Length != 1)
     {
         throw new System.Exception("Invalid number of arguments to String.contains()");
     }
     return(new GSharpBool(Value.Contains(arguments[0].ToString())));
 }
Beispiel #3
0
 public StackFrame(int localCount, GSharpMethod method, GSharpObject self)
 {
     LocalCount = localCount;
     Method     = method;
     Module     = method.Module;
     Self       = self;
     locals     = new GSharpObject[localCount];
 }
Beispiel #4
0
        private GSharpObject add(VirtualMachine vm, GSharpObject self, GSharpObject[] arguments)
        {
            GSharpList list = self as GSharpList;

            foreach (GSharpObject obj in arguments)
            {
                list.Add(obj);
            }
            return(null);
        }
Beispiel #5
0
 public GSharpObject isLetters(VirtualMachine vm, GSharpObject self, GSharpObject[] arguments)
 {
     foreach (char ch in Value)
     {
         if (!char.IsLetter(ch))
         {
             return(GSharpBool.False);
         }
     }
     return(GSharpBool.True);
 }
Beispiel #6
0
 public GSharpObject isWhitespace(VirtualMachine vm, GSharpObject self, GSharpObject[] arguments)
 {
     foreach (char ch in Value)
     {
         if (!char.IsWhiteSpace(ch))
         {
             return(GSharpBool.False);
         }
     }
     return(GSharpBool.True);
 }
Beispiel #7
0
        public virtual GSharpObject PerformBinaryOperation(VirtualMachine vm, BinaryOperation binop, GSharpObject rval)
        {
            GSharpObject[] arguments = new GSharpObject[] { rval };

            switch (binop)
            {
            case BinaryOperation.Addition:
                return(GetAttribute("_add").Invoke(vm, arguments));

            case BinaryOperation.Subtraction:
                return(GetAttribute("_sub").Invoke(vm, arguments));

            case BinaryOperation.Multiplication:
                return(GetAttribute("_mul").Invoke(vm, arguments));

            case BinaryOperation.Division:
                return(GetAttribute("_div").Invoke(vm, arguments));

            case BinaryOperation.BooleanAnd:
                return(GetAttribute("_boolAnd").Invoke(vm, arguments));

            case BinaryOperation.BooleanOr:
                return(GetAttribute("_boolOr").Invoke(vm, arguments));

            case BinaryOperation.Modulus:
                return(GetAttribute("_mod").Invoke(vm, arguments));

            case BinaryOperation.Equals:
                return(GetAttribute("_equals").Invoke(vm, arguments));

            case BinaryOperation.NotEqualTo:
                return(GetAttribute("_notEquals").Invoke(vm, arguments));

            case BinaryOperation.LessThan:
                return(GetAttribute("_lessThan").Invoke(vm, arguments));

            case BinaryOperation.GreaterThan:
                return(GetAttribute("_greaterThan").Invoke(vm, arguments));

            case BinaryOperation.LesserOrEqual:
                return(GetAttribute("_lessOrEqual").Invoke(vm, arguments));

            case BinaryOperation.GreaterOrEqual:
                return(GetAttribute("_greaterOrEqual").Invoke(vm, arguments));

            default:
                return(null);
            }
        }
Beispiel #8
0
        public void SetAttribute(string name, GSharpObject value)
        {
            if (value is GSharpMethod)
            {
                if (((GSharpMethod)value).IsInstanceMethod)
                {
                    attributes[name] = new InstanceMethodCallback((GSharpMethod)value, this);
                    return;
                }
                else if (value is InstanceMethodCallback)
                {
                    attributes[name] = new InstanceMethodCallback(((InstanceMethodCallback)value).Method, this);
                    return;
                }
            }

            attributes[name] = value;
        }
Beispiel #9
0
 public void Push(GSharpObject obj)
 {
     stack.Push(obj);
 }
Beispiel #10
0
 public void StoreLocal(int index, GSharpObject obj)
 {
     locals[index] = obj;
 }
Beispiel #11
0
 public GSharpObject size(VirtualMachine vm, GSharpObject self, GSharpObject[] arguments)
 {
     return(new GSharpInteger(Value.Length));
 }
Beispiel #12
0
        public override GSharpObject PerformBinaryOperation(VirtualMachine vm, BinaryOperation binop, GSharpObject rval)
        {
            GSharpString strVal = rval as GSharpString;

            if (strVal == null)
            {
                strVal = (GSharpString)rval.GetAttribute("toString").Invoke(vm, new GSharpObject[] { });
            }

            switch (binop)
            {
            case BinaryOperation.Equals:
                return(new GSharpBool(strVal.Value == Value));

            case BinaryOperation.NotEqualTo:
                return(new GSharpBool(strVal.Value != Value));

            case BinaryOperation.Addition:
                return(new GSharpString(Value + strVal.Value));
            }

            return(null);
        }
Beispiel #13
0
        public override GSharpObject PerformBinaryOperation(VirtualMachine vm, BinaryOperation binop, GSharpObject rval)
        {
            GSharpBool boolVal = rval as GSharpBool;

            switch (binop)
            {
            case BinaryOperation.Equals:
                return(new GSharpBool(boolVal.Value == Value));

            case BinaryOperation.NotEqualTo:
                return(new GSharpBool(boolVal.Value != Value));

            case BinaryOperation.BooleanAnd:
                return(new GSharpBool(boolVal.Value && Value));

            case BinaryOperation.BooleanOr:
                return(new GSharpBool(boolVal.Value || Value));
            }

            return(null);
        }
Beispiel #14
0
        public override GSharpObject GetIndex(VirtualMachine vm, GSharpObject key)
        {
            GSharpInteger index = key as GSharpInteger;

            return(Objects[index.Value]);
        }
Beispiel #15
0
 public virtual void SetIndex(VirtualMachine vm, GSharpObject key, GSharpObject value)
 {
 }
Beispiel #16
0
        public override GSharpObject PerformBinaryOperation(VirtualMachine vm, BinaryOperation binop, GSharpObject rval)
        {
            GSharpInteger intVal = rval as GSharpInteger;

            if (intVal == null)
            {
                throw new System.Exception("Right value must be an integer.");
            }

            switch (binop)
            {
            case BinaryOperation.Addition:
                return(new GSharpInteger(Value + intVal.Value));

            case BinaryOperation.Subtraction:
                return(new GSharpInteger(Value - intVal.Value));

            case BinaryOperation.Multiplication:
                return(new GSharpInteger(Value * intVal.Value));

            case BinaryOperation.Division:
                return(new GSharpInteger(Value / intVal.Value));

            case BinaryOperation.Modulus:
                return(new GSharpInteger(Value % intVal.Value));

            case BinaryOperation.Equals:
                return(new GSharpBool(Value == intVal.Value));

            case BinaryOperation.NotEqualTo:
                return(new GSharpBool(Value != intVal.Value));

            case BinaryOperation.GreaterThan:
                return(new GSharpBool(Value > intVal.Value));

            case BinaryOperation.GreaterOrEqual:
                return(new GSharpBool(Value >= intVal.Value));

            case BinaryOperation.LessThan:
                return(new GSharpBool(Value < intVal.Value));

            case BinaryOperation.LesserOrEqual:
                return(new GSharpBool(Value <= intVal.Value));
            }

            return(null);
        }
Beispiel #17
0
 public void StoreLocal(int index, GSharpObject obj)
 {
     top.StoreLocal(index, obj);
 }
Beispiel #18
0
        private void ExecuteInstruction(Instruction ins)
        {
            switch (ins.OperationCode)
            {
            case OperationCode.Pop:
                Stack.Pop();
                break;

            case OperationCode.LoadTrue:
                Stack.Push(GSharpBool.True);
                break;

            case OperationCode.LoadFalse:
                Stack.Push(GSharpBool.False);
                break;

            case OperationCode.StoreLocal:
                Stack.StoreLocal(ins.Argument, Stack.Pop());
                break;

            case OperationCode.LoadLocal:
                Stack.Push(Stack.LoadLocal(ins.Argument));
                break;

            case OperationCode.StoreGlobal:
            {
                string name = ((GSharpName)Stack.CurrentModule.ConstantPool[ins.Argument]).Value;
                if (Stack.CurrentModule.HasAttribute(name))
                {
                    Stack.CurrentModule.SetAttribute(name, Stack.Pop());
                }
                else
                {
                    globalDictionary[name] = Stack.Pop();
                }
                break;
            }

            case OperationCode.LoadGlobal:
            {
                string name = ((GSharpName)Stack.CurrentModule.ConstantPool[ins.Argument]).Value;
                if (globalDictionary.ContainsKey(name))
                {
                    Stack.Push(globalDictionary[name]);
                }
                else
                {
                    Stack.Push(Stack.CurrentModule.GetAttribute(name));
                }
                break;
            }

            case OperationCode.StoreAttribute:
            {
                GSharpObject target    = Stack.Pop();
                GSharpObject value     = Stack.Pop();
                string       attribute = ((GSharpName)Stack.CurrentModule.ConstantPool[ins.Argument]).Value;
                target.SetAttribute(attribute, value);
                break;
            }

            case OperationCode.LoadAttribute:
            {
                GSharpObject target    = Stack.Pop();
                string       attribute = ((GSharpName)Stack.CurrentModule.ConstantPool[ins.Argument]).Value;
                if (target.HasAttribute(attribute))
                {
                    Stack.Push(target.GetAttribute(attribute));
                }
                else
                {
                    throw new System.Exception("Could not find attribute " + attribute + ".");
                }
                break;
            }

            case OperationCode.LoadConst:
                Stack.Push(Stack.CurrentModule.ConstantPool[ins.Argument]);
                break;

            case OperationCode.LoadNull:
                Stack.Push(null);
                break;

            case OperationCode.LoadIndex:
            {
                GSharpObject index  = Stack.Pop();
                GSharpObject target = Stack.Pop();
                Stack.Push(target.GetIndex(this, index));
                break;
            }

            case OperationCode.StoreIndex:
            {
                GSharpObject index  = Stack.Pop();
                GSharpObject target = Stack.Pop();
                GSharpObject value  = Stack.Pop();
                target.SetIndex(this, index, value);
                break;
            }

            case OperationCode.LoadThis:
                Stack.Push(Stack.Self);
                break;

            case OperationCode.BinaryOperation:
                Stack.Push(Stack.Pop().PerformBinaryOperation(this, (BinaryOperation)ins.Argument, Stack.Pop()));
                break;

            case OperationCode.InstanceOf:
            {
                GSharpObject o1 = Stack.Pop();
                GSharpObject o2 = Stack.Pop();
                Stack.Push(new GSharpBool(o1.Type == o2.Type));
                break;
            }

            case OperationCode.Invoke:
            {
                GSharpObject   target    = Stack.Pop();
                GSharpObject[] arguments = new GSharpObject[ins.Argument];
                for (int i = 1; i <= ins.Argument; i++)
                {
                    arguments[ins.Argument - i] = Stack.Pop();
                }
                Stack.Push(target.Invoke(this, arguments));
                break;
            }

            case OperationCode.Return:
                Stack.InstructionPointer = int.MaxValue;
                break;

            case OperationCode.JumpIfTrue:
                if (Stack.Pop().IsTrue())
                {
                    Stack.InstructionPointer = ins.Argument;
                }
                break;

            case OperationCode.JumpIfFalse:
                if (!Stack.Pop().IsTrue())
                {
                    Stack.InstructionPointer = ins.Argument;
                }
                break;

            case OperationCode.Jump:
                Stack.InstructionPointer = ins.Argument;
                break;
            }
        }
Beispiel #19
0
 private GSharpObject size(VirtualMachine vm, GSharpObject self, GSharpObject[] arguments)
 {
     return(new GSharpInteger(((GSharpList)self).Objects.Count));
 }
Beispiel #20
0
 public void Add(GSharpObject obj)
 {
     Objects.Add(obj);
 }
Beispiel #21
0
        public override void SetIndex(VirtualMachine vm, GSharpObject key, GSharpObject value)
        {
            GSharpInteger index = key as GSharpInteger;

            Objects[index.Value] = value;
        }
Beispiel #22
0
 public InstanceMethodCallback(GSharpMethod method, GSharpObject self) : base("Instance Method Callback")
 {
     Method    = method;
     this.self = self;
 }
Beispiel #23
0
 public virtual GSharpObject GetIndex(VirtualMachine vm, GSharpObject key)
 {
     return(null);
 }
Beispiel #24
0
        public override GSharpObject GetIndex(VirtualMachine vm, GSharpObject key)
        {
            GSharpInteger index = key as GSharpInteger;

            return(new GSharpString(Value[index.Value].ToString()));
        }
Beispiel #25
0
 public void NewFrame(int localCount, GSharpMethod method, GSharpObject self)
 {
     Frames++;
     top = new StackFrame(localCount, method, self);
     frames.Push(top);
 }
Beispiel #26
0
 private GSharpObject toChar(VirtualMachine vm, GSharpObject self, GSharpObject[] arguments)
 {
     return(new GSharpString(((char)Value).ToString()));
 }
Beispiel #27
0
 public int DefineConstant(GSharpObject obj)
 {
     constantPool.Add(obj);
     return(constantPool.Count - 1);
 }
Beispiel #28
0
 public GSharpObject toString(VirtualMachine vm, GSharpObject self, GSharpObject[] arguments)
 {
     return(new GSharpString(Value.ToString()));
 }
Beispiel #29
0
 public void Push(GSharpObject obj)
 {
     top.Push(obj);
 }