Пример #1
0
 //Comparisions
 public IDPacket COMP_OP(IDPacket OP1, IDPacket OP2, Token Operator)
 {
     if (Operator.Type == Type.GreaterThan)
     {
         return(COMP_GT(OP1, OP2));
     }
     else if (Operator.Type == Type.LessThan)
     {
         return(COMP_LT(OP1, OP2));
     }
     else if (Operator.Type == Type.Equals)
     {
         return(COMP_EQ(OP1, OP2));
     }
     else if (Operator.Type == Type.GreaterThanEqual)
     {
         return(LOGICAL_OR(COMP_GT(OP1, OP2), COMP_EQ(OP1, OP2)));
     }
     else if (Operator.Type == Type.LessThanEqual)
     {
         return(LOGICAL_OR(COMP_LT(OP1, OP2), COMP_EQ(OP1, OP2)));
     }
     else
     {
         Error("Operator not recognized");
         return(null);
     }
 }
Пример #2
0
    //methods for actual operations

    public void OUTPUT(IDPacket OP1)
    {
        if (OP1.Type == IdentityType.Number)
        {
            double d;
            TryGetNumber(OP1, out d);
            Output("" + d);
        }
        else if (OP1.Type == IdentityType.Text)
        {
            string t;
            TryGetText(OP1, out t);
            Output(t);
        }
        else if (OP1.Type == IdentityType.Boolean)
        {
            bool b;
            TryGetBoolean(OP1, out b);
            Output("" + b);
        }
        else
        {
            Output(String.Format("Data Packet Name {0} @ {1}, Type {2}", OP1.Name, OP1.Address, OP1.Type));
        }
    }
Пример #3
0
    public bool TryGetNumber(IDPacket packet, out double value)
    {
        ScopeFrame scope;

        FindNameScope(packet, out scope);
        return(scope.Primitives.Numbers.TryGetValue(packet.Name, out value));
    }
Пример #4
0
 //Arithmetic
 public IDPacket MATH_OP(IDPacket OP1, IDPacket OP2, Token Operator)
 {
     if (Operator.Type == Type.Plus)
     {
         return(MATH_PLUS(OP1, OP2));
     }
     else if (Operator.Type == Type.Minus)
     {
         return(MATH_MINUS(OP1, OP2));
     }
     else if (Operator.Type == Type.Asterisk)
     {
         return(MATH_MUL(OP1, OP2));
     }
     else if (Operator.Type == Type.Slash)
     {
         return(MATH_DIV(OP1, OP2));
     }
     else if (Operator.Type == Type.Percentage)
     {
         return(MATH_MOD(OP1, OP2));
     }
     else
     {
         Error("Operator not recognized");
         return(null);
     }
 }
Пример #5
0
    public bool TryGetText(IDPacket packet, out string value)
    {
        ScopeFrame scope;

        FindNameScope(packet, out scope);
        return(scope.Primitives.Text.TryGetValue(packet.Name, out value));
    }
Пример #6
0
    public bool TryGetBoolean(IDPacket packet, out Boolean value)
    {
        ScopeFrame scope;

        FindNameScope(packet, out scope);
        return(scope.Primitives.Booleans.TryGetValue(packet.Name, out value));
    }
Пример #7
0
    public bool TryGetGeneric(IDPacket packet, out GenericFrame value)
    {
        ScopeFrame scope;

        FindNameScope(packet, out scope);
        return(scope.Generics.TryGetValue(packet.Name, out value));
    }
Пример #8
0
    public bool TryGetFunction(IDPacket packet, out FunctionFrame value)
    {
        ScopeFrame scope;

        FindNameScope(packet, out scope);
        return(scope.Functions.TryGetValue(packet.Name, out value));
    }
Пример #9
0
    public void RemoveIdentity(IDPacket packet)
    {
        ScopeFrame scope;

        FindNameScope(packet, out scope);
        switch (packet.Type)
        {
        case IdentityType.Number:
            scope.Primitives.Numbers.Remove(packet.Name);
            break;

        case IdentityType.Boolean:
            scope.Primitives.Booleans.Remove(packet.Name);
            break;

        case IdentityType.Text:
            scope.Primitives.Text.Remove(packet.Name);
            break;

        case IdentityType.Structure:
            scope.Generics.Remove(packet.Name);
            break;

        case IdentityType.Function:
            scope.Functions.Remove(packet.Name);
            break;
        }
    }
Пример #10
0
 public IDPacket COMP_EQ(IDPacket OP1, IDPacket OP2)
 {
     IDPacket RES = null;
     bool VAL = false;
     if (OP1.Type == IdentityType.Number) {
         double op1, op2;
         TryGetNumber (OP1, out op1);
         TryGetNumber (OP2, out op2);
         Debug ("Operation(Number) ==, op1: " + op1 + ", " + op2);
         RES = IDPacket.CreateSystemPacket ("==OP", IdentityType.Boolean);
         VAL = op1 == op2;
     } else if (OP1.Type == IdentityType.Boolean) {
         double op1, op2;
         TryGetNumber (OP1, out op1);
         TryGetNumber (OP2, out op2);
         Debug ("Operation(Boolean) ==, op1: " + op1 + ", " + op2);
         RES = IDPacket.CreateSystemPacket ("==OP", IdentityType.Boolean);
         VAL = op1 == op2;
     } else if (OP1.Type == IdentityType.Text) {
         string op1, op2;
         TryGetText (OP1, out op1);
         TryGetText (OP2, out op2);
         Debug ("Operation(Text) ==, op1: " + op1 + ", " + op2);
         RES = IDPacket.CreateSystemPacket ("==OP", IdentityType.Boolean);
         VAL = op1 == op2;
     } else {
         Error ("Comparing incompatible types");
         return null;
     }
     PutBoolean (RES, VAL);
     return RES;
 }
Пример #11
0
 public IDPacket(IDPacket copy)
 {
     Name           = copy.Name;
     Type           = copy.Type;
     Address        = copy.Address;
     ArrayType      = copy.ArrayType;
     TypeDimensions = copy.TypeDimensions;
 }
Пример #12
0
    public ScopeFrame CreateFunctionScope(FunctionFrame ftype, string FunctionName, List <IDPacket> Bindings)
    {
        ScopeFrame fscope = CreateParseFunctionScope(ftype, FunctionName);

        //delete function frame inside scope
        fscope.Functions.Remove(FunctionName);

        PushScope(fscope);
        int BINDCNT = 0;

        foreach (FunctionParameter fp in ftype.Parameters)
        {
            IDPacket Binding = Bindings [BINDCNT];
            IDPacket Address = IDPacket.CreateIDPacket(this, fp.Name, Binding.Type);
            if (Binding.Type != fp.Type)
            {
                Error("Binding Error: Parameter type mismatch, expected: " + fp.Type + " got " + Binding.Type);
            }
            if (Binding.ArrayType != IdentityType.Unknown)
            {
                if (fp.TypeDimensions != null)
                {
                    if (Binding.ArrayType != fp.Type)
                    {
                        Error("Binding Error: Parameter type mismatch, expected: Array of " + fp.Type + " got Array of " + Binding.ArrayType);
                    }
                    int dcnt = 0;
                    foreach (int dim in fp.TypeDimensions)
                    {
                        if (dim == -1)
                        {
                            continue;
                        }
                        if (dim != Binding.TypeDimensions [dcnt++])
                        {
                            Error(String.Format("Binding Error: Dimension Mismatch, expecting {0} got {1}", fp.TypeDimensions,
                                                Binding.TypeDimensions));
                        }
                    }
                }
                else
                {
                    Error(String.Format("Binding Error: Expecting a Primitive {0} got Array of {1}", fp.Type, Binding.ArrayType));
                }
            }
            else if (fp.TypeDimensions != null)
            {
                Error(String.Format("Binding Error: Expecting an Array of {0} got Primitive {1}", fp.Type, Binding.ArrayType));
            }

            COPY(Address, Binding);
            Debug("After copy");
        }
        return(PopScopeNoSave());
    }
Пример #13
0
    public IDPacket LOGICAL_NOT(IDPacket OP1)
    {
        bool op1;

        TryGetBoolean(OP1, out op1);
        Debug("Operation OR, op1: " + op1);
        IDPacket RES = IDPacket.CreateSystemPacket("NOTOP", IdentityType.Boolean);

        PutBoolean(RES, !op1);
        return(RES);
    }
Пример #14
0
    IDPacket LOGICAL_OR(IDPacket OP1, IDPacket OP2)
    {
        bool op1, op2;

        TryGetBoolean(OP1, out op1);
        TryGetBoolean(OP2, out op2);
        Debug("Operation OR, op1: " + op1 + ", " + op2);
        IDPacket RES = IDPacket.CreateSystemPacket("OROP", IdentityType.Boolean);

        PutBoolean(RES, op1 || op2);
        return(RES);
    }
Пример #15
0
    //string
    public IDPacket STR_CONCAT(IDPacket OP1, IDPacket OP2)
    {
        string op1, op2;

        TryGetText(OP1, out op1);
        TryGetText(OP2, out op2);
        Debug("Operation +, op1: " + op1 + ", " + op2);
        IDPacket RES = IDPacket.CreateSystemPacket("CONCATOP", IdentityType.Text);

        PutText(RES, op1 + op2);
        return(RES);
    }
Пример #16
0
    IDPacket MATH_MOD(IDPacket OP1, IDPacket OP2)
    {
        double op1, op2;

        TryGetNumber(OP1, out op1);
        TryGetNumber(OP2, out op2);
        Debug("Operation %, op1: " + op1 + ", " + op2);
        IDPacket RES = IDPacket.CreateSystemPacket("%OP", IdentityType.Number);

        PutNumber(RES, op1 % op2);
        return(RES);
    }
Пример #17
0
    IDPacket COMP_LT(IDPacket OP1, IDPacket OP2)
    {
        double op1, op2;

        TryGetNumber(OP1, out op1);
        TryGetNumber(OP2, out op2);
        Debug("Operation <, op1: " + op1 + ", " + op2);
        IDPacket RES = IDPacket.CreateSystemPacket("<OP", IdentityType.Boolean);

        PutBoolean(RES, op1 < op2);
        return(RES);
    }
Пример #18
0
    public void PutBoolean(IDPacket packet, Boolean boolean)
    {
        ScopeFrame scope;

        FindNameScope(packet, out scope);
        bool trash;

        if (TryGetBoolean(packet, out trash))
        {
            scope.Primitives.Booleans [packet.Name] = boolean;
        }
        else
        {
            scope.Primitives.Booleans.Add(packet.Name, boolean);
        }
    }
Пример #19
0
    public void PutText(IDPacket packet, string text)
    {
        ScopeFrame scope;

        FindNameScope(packet, out scope);
        string trash;

        if (TryGetText(packet, out trash))
        {
            scope.Primitives.Text [packet.Name] = text;
        }
        else
        {
            scope.Primitives.Text.Add(packet.Name, text);
        }
    }
Пример #20
0
    public void PutGeneric(IDPacket packet, GenericFrame structure)
    {
        ScopeFrame scope;

        FindNameScope(packet, out scope);
        GenericFrame trash;

        if (TryGetGeneric(packet, out trash))
        {
            scope.Generics [packet.Name] = structure;
        }
        else
        {
            scope.Generics.Add(packet.Name, structure);
        }
    }
Пример #21
0
    public void PutFunction(IDPacket packet, FunctionFrame function)
    {
        ScopeFrame scope;

        FindNameScope(packet, out scope);
        FunctionFrame trash;

        if (TryGetFunction(packet, out trash))
        {
            scope.Functions [packet.Name] = function;
        }
        else
        {
            scope.Functions.Add(packet.Name, function);
        }
    }
Пример #22
0
    //Logical Operations

    public IDPacket LOGICAL_OP(IDPacket OP1, IDPacket OP2, Token Operator)
    {
        if (Operator.Type == Type.And)
        {
            return(LOGICAL_AND(OP1, OP2));
        }
        else if (Operator.Type == Type.Or)
        {
            return(LOGICAL_OR(OP1, OP2));
        }
        else
        {
            Error("Operator not recognized");
            return(null);
        }
    }
Пример #23
0
    public void COPY(IDPacket TO, IDPacket FROM)
    {
        if (TO.Type != FROM.Type)
        {
            Error("Attempting to copy non-matching types");
        }

        if (TO.Type == IdentityType.Number)
        {
            double d;
            TryGetNumber(FROM, out d);
            PutNumber(TO, d);
        }
        else if (TO.Type == IdentityType.Boolean)
        {
            bool b;
            TryGetBoolean(FROM, out b);
            PutBoolean(TO, b);
        }
        else if (TO.Type == IdentityType.Text)
        {
            string t;
            TryGetText(FROM, out t);
            PutText(TO, t);
        }
        else if (TO.Type == IdentityType.Structure)
        {
            GenericFrame sf;
            TryGetGeneric(FROM, out sf);
            PutGeneric(TO, sf);
        }
        else if (TO.Type == IdentityType.Function)
        {
            FunctionFrame ff;
            TryGetFunction(FROM, out ff);
            PutFunction(TO, ff);
        }
        else
        {
            Error("Unknown Idenitity Type");
        }
        Debug("Finished copy");
    }
Пример #24
0
    public void PutNumber(IDPacket packet, double number)
    {
        Debug(String.Format("Putting number into {0}{1}", packet.Address, packet.Name));
        ScopeFrame scope;

        FindNameScope(packet, out scope);
        Debug(String.Format("Found scope {0}, type {1}", scope.Name, scope.Type));
        double trash;

        if (TryGetNumber(packet, out trash))
        {
            scope.Primitives.Numbers [packet.Name] = number;
        }
        else
        {
            scope.Primitives.Numbers.Add(packet.Name, number);
        }
        Debug("Put number");
    }
Пример #25
0
    public IDPacket COMP_EQ(IDPacket OP1, IDPacket OP2)
    {
        IDPacket RES = null;
        bool     VAL = false;

        if (OP1.Type == IdentityType.Number)
        {
            double op1, op2;
            TryGetNumber(OP1, out op1);
            TryGetNumber(OP2, out op2);
            Debug("Operation(Number) ==, op1: " + op1 + ", " + op2);
            RES = IDPacket.CreateSystemPacket("==OP", IdentityType.Boolean);
            VAL = op1 == op2;
        }
        else if (OP1.Type == IdentityType.Boolean)
        {
            double op1, op2;
            TryGetNumber(OP1, out op1);
            TryGetNumber(OP2, out op2);
            Debug("Operation(Boolean) ==, op1: " + op1 + ", " + op2);
            RES = IDPacket.CreateSystemPacket("==OP", IdentityType.Boolean);
            VAL = op1 == op2;
        }
        else if (OP1.Type == IdentityType.Text)
        {
            string op1, op2;
            TryGetText(OP1, out op1);
            TryGetText(OP2, out op2);
            Debug("Operation(Text) ==, op1: " + op1 + ", " + op2);
            RES = IDPacket.CreateSystemPacket("==OP", IdentityType.Boolean);
            VAL = op1 == op2;
        }
        else
        {
            Error("Comparing incompatible types");
            return(null);
        }
        PutBoolean(RES, VAL);
        return(RES);
    }
Пример #26
0
 public IDPacket GET(string name)
 {
     if (current_scope.Primitives.Numbers.ContainsKey(name))
     {
         return(IDPacket.CreateIDPacket(this, name, IdentityType.Number));
     }
     else if (current_scope.Primitives.Booleans.ContainsKey(name))
     {
         return(IDPacket.CreateIDPacket(this, name, IdentityType.Boolean));
     }
     else if (current_scope.Primitives.Text.ContainsKey(name))
     {
         return(IDPacket.CreateIDPacket(this, name, IdentityType.Text));
     }
     else if (current_scope.Generics.ContainsKey(name))
     {
         IDPacket     StructPacket = IDPacket.CreateIDPacket(this, name, IdentityType.Structure);
         GenericFrame GF;
         ArrayFrame   AF = new ArrayFrame();
         TryGetGeneric(StructPacket, out GF);
         if (GF.GetType() == AF.GetType())
         {
             AF = (ArrayFrame)GF;
             StructPacket.ArrayType = AF.ResolvedType;
         }
         return(StructPacket);
     }
     else if (current_scope.Functions.ContainsKey(name))
     {
         return(IDPacket.CreateIDPacket(this, name, IdentityType.Function));
     }
     else
     {
         return(null);
     }
 }
Пример #27
0
 //methods for actual operations
 public void OUTPUT(IDPacket OP1)
 {
     if (OP1.Type == IdentityType.Number) {
         double d;
         TryGetNumber (OP1, out d);
         Output ("" + d);
     } else if (OP1.Type == IdentityType.Text) {
         string t;
         TryGetText (OP1, out t);
         Output (t);
     } else if (OP1.Type == IdentityType.Boolean) {
         bool b;
         TryGetBoolean (OP1, out b);
         Output ("" + b);
     } else {
         Output (String.Format("Data Packet Name {0} @ {1}, Type {2}", OP1.Name, OP1.Address, OP1.Type));
     }
 }
Пример #28
0
 public void PutBoolean(IDPacket packet, Boolean boolean)
 {
     ScopeFrame scope;
     FindNameScope (packet, out scope);
     bool trash;
     if (TryGetBoolean (packet, out trash))
         scope.Primitives.Booleans [packet.Name] = boolean;
     else
         scope.Primitives.Booleans.Add (packet.Name, boolean);
 }
Пример #29
0
 public bool TryGetGeneric(IDPacket packet, out GenericFrame value)
 {
     ScopeFrame scope;
     FindNameScope (packet, out scope);
     return scope.Generics.TryGetValue (packet.Name, out value);
 }
Пример #30
0
 //(text haystack, number at)
 public IDPacket EXTERNAL(string MethodName, IDPacket[] PARAMS)
 {
     IDPacket ret = null;
     MethodInfo minfo = this.GetType ().GetMethod (MethodName);
     try {
         int PCNT = 0;
         List<object> parameters = new List<object>();
         foreach(ParameterInfo pinfo in minfo.GetParameters()) {
             IDPacket PARAM = PARAMS[PCNT++];
             System.Type ptype = pinfo.ParameterType;
             if(ptype == typeof(int) || ptype == typeof(double) || ptype == typeof(short) || ptype == typeof(long))
             {
                 if(PARAM.Type == IdentityType.Number) {
                     double d;
                     TryGetNumber(PARAM, out d);
                     parameters.Add(d);
                 } else {
                     Error(String.Format("Type mismatch in external function call, expecting {0} in parameter {1} got {2}",
                         ptype, PCNT, PARAM.Type));
                 }
             } else if(ptype == typeof(string)) {
                 if(PARAM.Type == IdentityType.Number) {
                     string s;
                     TryGetText(PARAM, out s);
                     parameters.Add(s);
                 } else {
                     Error(String.Format("Type mismatch in external function call, expecting {0} in parameter {1} got {2}",
                         ptype, PCNT, PARAM.Type));
                 }
             } else if(ptype == typeof(bool)) {
                 if(PARAM.Type == IdentityType.Boolean) {
                     bool b;
                     TryGetBoolean(PARAM, out b);
                     parameters.Add(b);
                 } else {
                     Error(String.Format("Type mismatch in external function call, expecting {0} in parameter {1} got {2}",
                         ptype, PCNT, PARAM.Type));
                 }
             } else {
                 Error(String.Format("The external function {0} cannot be called from the YSInterpreter", minfo.Name));
             }
         }
         object returnValue = minfo.Invoke(minfo, parameters.ToArray());
         System.Type rtype = minfo.ReturnType;
         if(rtype == typeof(int) || rtype == typeof(double) || rtype == typeof(short) || rtype == typeof(long))
         {
             ret = IDPacket.CreateReturnPacket(IdentityType.Number);
             PutNumber(ret, (double) returnValue);
         } else if(rtype == typeof(string)) {
             ret = IDPacket.CreateReturnPacket(IdentityType.Text);
             PutText(ret, (string) returnValue);
         } else if(rtype == typeof(bool)) {
             ret = IDPacket.CreateReturnPacket(IdentityType.Boolean);
             PutBoolean(ret, (bool) returnValue);
         } else {
             Debug(String.Format("[!] The external function {0}'s returning value of type {1} cannot be used by YSInterpreter",
                 minfo.Name, minfo.ReturnType));
         }
     } catch(TargetInvocationException) {
         Error ("Failed to invoke the external method");
         return null;
     } catch(Exception) {
         Error ("Failed to invoke the external method");
         return null;
     }
     return ret;
 }
Пример #31
0
 public void PutNumber(IDPacket packet, double number)
 {
     Debug (String.Format("Putting number into {0}{1}", packet.Address, packet.Name));
     ScopeFrame scope;
     FindNameScope (packet, out scope);
     Debug (String.Format ("Found scope {0}, type {1}", scope.Name, scope.Type));
     double trash;
     if (TryGetNumber (packet, out trash))
         scope.Primitives.Numbers [packet.Name] = number;
     else
         scope.Primitives.Numbers.Add (packet.Name, number);
     Debug ("Put number");
 }
Пример #32
0
 public bool TryGetBoolean(IDPacket packet, out Boolean value)
 {
     ScopeFrame scope;
     FindNameScope (packet, out scope);
     return scope.Primitives.Booleans.TryGetValue (packet.Name, out value);
 }
Пример #33
0
 public void PutText(IDPacket packet, string text)
 {
     ScopeFrame scope;
     FindNameScope (packet, out scope);
     string trash;
     if (TryGetText (packet, out trash))
         scope.Primitives.Text [packet.Name] = text;
     else
         scope.Primitives.Text.Add (packet.Name, text);
 }
Пример #34
0
    /* Address format:
     * <type>:<name>
     * s:<name> - structure
     * f:<name> - function
     * l:<name> - loop
     * u - unknown
     *
     * [feature redacted] ..	go back into parent scope
     * / 	global scope
     */
    void FindNameScope(IDPacket packet, out ScopeFrame scope)
    {
        Debug("Attempting to find scope of " + packet.Address + ", name: " + packet.Name);
        if (packet.Address.Length == 0) {
            scope = TemporaryStorage;
            return;
        }
        string[] _path = packet.Address.Split ('/');
        List<string> pathList = new List<string>();
        foreach (string tok in _path)
            if (tok.Length > 0)
                pathList.Add (tok);

        string[] path = pathList.ToArray ();
        scope = Global_Scope;
        int PARENT_COUNTER = 0;
        ScopeFrame[] _SS = Scope_Stack.ToArray ();
        for (int i = 0; i < path.Length; i++) {
            string str = path [i];

            /* -- parent keyword feature has been removed
             *
            if (str.Equals (IDPacket.PARENT_TYPE)) {
                scope = _SS [_SS.Length - 1 - ++PARENT_COUNTER];
                continue;
            }
            */

            string[] addr_parts = str.Split (':');
            string addr_type = addr_parts [0], addr_name = addr_parts [1];

            if (!addr_type.Equals ("s")) {
                Debug ("Searching scope stack...");
                if (_SS [i].Name.Equals (addr_name)) {
                    Debug (String.Format ("Found {0} on top of {1}", addr_name, scope.Name));
                    scope = _SS [i];
                } else {
                    Debug (String.Format ("Didn't find {0} on top of {1}", addr_name, scope.Name));
                }
            } else {
                Debug ("Searching Generics...");
                GenericFrame schild;
                if (scope.Generics.TryGetValue (addr_name, out schild)) {
                    Debug ("[Generic] Child " + addr_name + " found");
                    scope = StructureFrame.Appropriate (addr_name, schild);
                } else {
                    Error (String.Format ("There is no structure named {0} in the scope", addr_name));
                }
            }
        }
        Debug ("Exiting Finder");
    }
Пример #35
0
 public void PutFunction(IDPacket packet, FunctionFrame function)
 {
     ScopeFrame scope;
     FindNameScope (packet, out scope);
     FunctionFrame trash;
     if (TryGetFunction (packet, out trash))
         scope.Functions [packet.Name] = function;
     else
         scope.Functions.Add (packet.Name, function);
 }
Пример #36
0
 IDPacket LOGICAL_OR(IDPacket OP1, IDPacket OP2)
 {
     bool op1, op2;
     TryGetBoolean (OP1, out op1);
     TryGetBoolean (OP2, out op2);
     Debug ("Operation OR, op1: " + op1 + ", " + op2);
     IDPacket RES = IDPacket.CreateSystemPacket ("OROP", IdentityType.Boolean);
     PutBoolean (RES, op1 || op2);
     return RES;
 }
Пример #37
0
 //string
 public IDPacket STR_CONCAT(IDPacket OP1, IDPacket OP2)
 {
     string op1, op2;
     TryGetText (OP1, out op1);
     TryGetText (OP2, out op2);
     Debug ("Operation +, op1: " + op1 + ", " + op2);
     IDPacket RES = IDPacket.CreateSystemPacket ("CONCATOP", IdentityType.Text);
     PutText (RES, op1 + op2);
     return RES;
 }
Пример #38
0
 IDPacket MATH_PLUS(IDPacket OP1, IDPacket OP2)
 {
     double op1, op2;
     TryGetNumber (OP1, out op1);
     TryGetNumber (OP2, out op2);
     Debug ("Operation +, op1: " + op1 + ", " + op2);
     IDPacket RES = IDPacket.CreateSystemPacket ("+OP", IdentityType.Number);
     PutNumber (RES, op1 + op2);
     return RES;
 }
Пример #39
0
 //Comparisions
 public IDPacket COMP_OP(IDPacket OP1, IDPacket OP2, Token Operator)
 {
     if (Operator.Type == Type.GreaterThan) {
         return COMP_GT (OP1, OP2);
     } else if (Operator.Type == Type.LessThan) {
         return COMP_LT (OP1, OP2);
     } else if (Operator.Type == Type.Equals) {
         return COMP_EQ (OP1, OP2);
     } else if (Operator.Type == Type.GreaterThanEqual) {
         return LOGICAL_OR(COMP_GT (OP1, OP2), COMP_EQ(OP1, OP2));
     } else if (Operator.Type == Type.LessThanEqual) {
         return LOGICAL_OR(COMP_LT (OP1, OP2), COMP_EQ(OP1, OP2));
     } else {
         Error ("Operator not recognized");
         return null;
     }
 }
Пример #40
0
    /* Address format:
     * <type>:<name>
     * s:<name> - structure
     * f:<name> - function
     * l:<name> - loop
     * u - unknown
     *
     * [feature redacted] ..	go back into parent scope
     * /    global scope
     */
    void FindNameScope(IDPacket packet, out ScopeFrame scope)
    {
        Debug("Attempting to find scope of " + packet.Address + ", name: " + packet.Name);
        if (packet.Address.Length == 0)
        {
            scope = TemporaryStorage;
            return;
        }
        string[]      _path    = packet.Address.Split('/');
        List <string> pathList = new List <string>();

        foreach (string tok in _path)
        {
            if (tok.Length > 0)
            {
                pathList.Add(tok);
            }
        }

        string[] path = pathList.ToArray();
        scope = Global_Scope;
        int PARENT_COUNTER = 0;

        ScopeFrame[] _SS = Scope_Stack.ToArray();
        for (int i = 0; i < path.Length; i++)
        {
            string str = path [i];

            /* -- parent keyword feature has been removed
             *
             * if (str.Equals (IDPacket.PARENT_TYPE)) {
             *      scope = _SS [_SS.Length - 1 - ++PARENT_COUNTER];
             *      continue;
             * }
             */

            string[] addr_parts = str.Split(':');
            string   addr_type = addr_parts [0], addr_name = addr_parts [1];

            if (!addr_type.Equals("s"))
            {
                Debug("Searching scope stack...");
                if (_SS [i].Name.Equals(addr_name))
                {
                    Debug(String.Format("Found {0} on top of {1}", addr_name, scope.Name));
                    scope = _SS [i];
                }
                else
                {
                    Debug(String.Format("Didn't find {0} on top of {1}", addr_name, scope.Name));
                }
            }
            else
            {
                Debug("Searching Generics...");
                GenericFrame schild;
                if (scope.Generics.TryGetValue(addr_name, out schild))
                {
                    Debug("[Generic] Child " + addr_name + " found");
                    scope = StructureFrame.Appropriate(addr_name, schild);
                }
                else
                {
                    Error(String.Format("There is no structure named {0} in the scope", addr_name));
                }
            }
        }
        Debug("Exiting Finder");
    }
Пример #41
0
 public bool TryGetFunction(IDPacket packet, out FunctionFrame value)
 {
     ScopeFrame scope;
     FindNameScope (packet, out scope);
     return scope.Functions.TryGetValue (packet.Name, out value);
 }
Пример #42
0
 IDPacket COMP_LT(IDPacket OP1, IDPacket OP2)
 {
     double op1, op2;
     TryGetNumber (OP1, out op1);
     TryGetNumber (OP2, out op2);
     Debug ("Operation <, op1: " + op1 + ", " + op2);
     IDPacket RES = IDPacket.CreateSystemPacket ("<OP", IdentityType.Boolean);
     PutBoolean (RES, op1 < op2);
     return RES;
 }
Пример #43
0
 //Arithmetic
 public IDPacket MATH_OP(IDPacket OP1, IDPacket OP2, Token Operator)
 {
     if (Operator.Type == Type.Plus) {
         return MATH_PLUS (OP1, OP2);
     } else if (Operator.Type == Type.Minus) {
         return MATH_MINUS (OP1, OP2);
     } else if (Operator.Type == Type.Asterisk) {
         return MATH_MUL (OP1, OP2);
     } else if (Operator.Type == Type.Slash) {
         return MATH_DIV (OP1, OP2);
     } else if (Operator.Type == Type.Percentage) {
         return MATH_MOD (OP1, OP2);
     } else {
         Error ("Operator not recognized");
         return null;
     }
 }
Пример #44
0
 public bool TryGetText(IDPacket packet, out string value)
 {
     ScopeFrame scope;
     FindNameScope (packet, out scope);
     return scope.Primitives.Text.TryGetValue (packet.Name, out value);
 }
Пример #45
0
 //Logical Operations
 public IDPacket LOGICAL_OP(IDPacket OP1, IDPacket OP2, Token Operator)
 {
     if (Operator.Type == Type.And) {
         return LOGICAL_AND (OP1, OP2);
     } else if (Operator.Type == Type.Or) {
         return LOGICAL_OR (OP1, OP2);
     } else {
         Error ("Operator not recognized");
         return null;
     }
 }
Пример #46
0
    //(text haystack, number at)
    public IDPacket EXTERNAL(string MethodName, IDPacket[] PARAMS)
    {
        IDPacket   ret   = null;
        MethodInfo minfo = this.GetType().GetMethod(MethodName);

        try {
            int           PCNT       = 0;
            List <object> parameters = new List <object>();
            foreach (ParameterInfo pinfo in minfo.GetParameters())
            {
                IDPacket    PARAM = PARAMS[PCNT++];
                System.Type ptype = pinfo.ParameterType;
                if (ptype == typeof(int) || ptype == typeof(double) || ptype == typeof(short) || ptype == typeof(long))
                {
                    if (PARAM.Type == IdentityType.Number)
                    {
                        double d;
                        TryGetNumber(PARAM, out d);
                        parameters.Add(d);
                    }
                    else
                    {
                        Error(String.Format("Type mismatch in external function call, expecting {0} in parameter {1} got {2}",
                                            ptype, PCNT, PARAM.Type));
                    }
                }
                else if (ptype == typeof(string))
                {
                    if (PARAM.Type == IdentityType.Number)
                    {
                        string s;
                        TryGetText(PARAM, out s);
                        parameters.Add(s);
                    }
                    else
                    {
                        Error(String.Format("Type mismatch in external function call, expecting {0} in parameter {1} got {2}",
                                            ptype, PCNT, PARAM.Type));
                    }
                }
                else if (ptype == typeof(bool))
                {
                    if (PARAM.Type == IdentityType.Boolean)
                    {
                        bool b;
                        TryGetBoolean(PARAM, out b);
                        parameters.Add(b);
                    }
                    else
                    {
                        Error(String.Format("Type mismatch in external function call, expecting {0} in parameter {1} got {2}",
                                            ptype, PCNT, PARAM.Type));
                    }
                }
                else
                {
                    Error(String.Format("The external function {0} cannot be called from the YSInterpreter", minfo.Name));
                }
            }
            object      returnValue = minfo.Invoke(minfo, parameters.ToArray());
            System.Type rtype       = minfo.ReturnType;
            if (rtype == typeof(int) || rtype == typeof(double) || rtype == typeof(short) || rtype == typeof(long))
            {
                ret = IDPacket.CreateReturnPacket(IdentityType.Number);
                PutNumber(ret, (double)returnValue);
            }
            else if (rtype == typeof(string))
            {
                ret = IDPacket.CreateReturnPacket(IdentityType.Text);
                PutText(ret, (string)returnValue);
            }
            else if (rtype == typeof(bool))
            {
                ret = IDPacket.CreateReturnPacket(IdentityType.Boolean);
                PutBoolean(ret, (bool)returnValue);
            }
            else
            {
                Debug(String.Format("[!] The external function {0}'s returning value of type {1} cannot be used by YSInterpreter",
                                    minfo.Name, minfo.ReturnType));
            }
        } catch (TargetInvocationException) {
            Error("Failed to invoke the external method");
            return(null);
        } catch (Exception) {
            Error("Failed to invoke the external method");
            return(null);
        }
        return(ret);
    }
Пример #47
0
 public IDPacket LOGICAL_NOT(IDPacket OP1)
 {
     bool op1;
     TryGetBoolean (OP1, out op1);
     Debug ("Operation OR, op1: " + op1);
     IDPacket RES = IDPacket.CreateSystemPacket ("NOTOP", IdentityType.Boolean);
     PutBoolean (RES, !op1);
     return RES;
 }
Пример #48
0
 public bool TryGetNumber(IDPacket packet, out double value)
 {
     ScopeFrame scope;
     FindNameScope (packet, out scope);
     return scope.Primitives.Numbers.TryGetValue (packet.Name, out value);
 }
Пример #49
0
 public void PutGeneric(IDPacket packet, GenericFrame structure)
 {
     ScopeFrame scope;
     FindNameScope (packet, out scope);
     GenericFrame trash;
     if (TryGetGeneric (packet, out trash))
         scope.Generics [packet.Name] = structure;
     else
         scope.Generics.Add (packet.Name, structure);
 }
Пример #50
0
 public IDPacket(IDPacket copy)
 {
     Name = copy.Name;
     Type = copy.Type;
     Address = copy.Address;
     ArrayType = copy.ArrayType;
     TypeDimensions = copy.TypeDimensions;
 }
Пример #51
0
    public void COPY(IDPacket TO, IDPacket FROM)
    {
        if (TO.Type != FROM.Type) {
            Error ("Attempting to copy non-matching types");
        }

        if (TO.Type == IdentityType.Number) {
            double d;
            TryGetNumber (FROM, out d);
            PutNumber (TO, d);
        } else if (TO.Type == IdentityType.Boolean) {
            bool b;
            TryGetBoolean (FROM, out b);
            PutBoolean (TO, b);
        } else if (TO.Type == IdentityType.Text) {
            string t;
            TryGetText (FROM, out t);
            PutText (TO, t);
        } else if (TO.Type == IdentityType.Structure) {
            GenericFrame sf;
            TryGetGeneric (FROM, out sf);
            PutGeneric (TO, sf);
        } else if (TO.Type == IdentityType.Function) {
            FunctionFrame ff;
            TryGetFunction (FROM, out ff);
            PutFunction (TO, ff);
        } else {
            Error ("Unknown Idenitity Type");
        }
        Debug ("Finished copy");
    }
Пример #52
0
 public void RemoveIdentity(IDPacket packet)
 {
     ScopeFrame scope;
     FindNameScope (packet, out scope);
     switch (packet.Type) {
     case IdentityType.Number:
         scope.Primitives.Numbers.Remove (packet.Name);
         break;
     case IdentityType.Boolean:
         scope.Primitives.Booleans.Remove (packet.Name);
         break;
     case IdentityType.Text:
         scope.Primitives.Text.Remove (packet.Name);
         break;
     case IdentityType.Structure:
         scope.Generics.Remove (packet.Name);
         break;
     case IdentityType.Function:
         scope.Functions.Remove (packet.Name);
         break;
     }
 }