Beispiel #1
0
        static void CALL_Execute(SIL_Action action)
        {
            SILFunction function = (SILFunction)action.ParsingArgs[0];
            ISILObject  value    = function.Value;

            action.ExecutionArgs = new object[] { value };
        }
Beispiel #2
0
        static ActionType WHILE_Parse(string functionspace, string unparsedArgs, ref object[] parsedArgs, ref int lineNumber)
        {
            //remove the WHILE word
            string parameters = unparsedArgs.Substring(6, unparsedArgs.Length - 6);

            ISILObject var = ExpressionParse(functionspace, parameters, new List <ISILObject>());

            SIL_Action         action;
            Queue <SIL_Action> actions_ = new Queue <SIL_Action>();

            do
            {
                lineNumber++;
                if (lineNumber >= sourceLines_.Length)
                {
                    throw new Exception("WEND expected!");
                }
                action = ParseNext(functionspace, ref lineNumber);
                if (action != null)
                {
                    actions_.Enqueue(action);
                }
            } while (action.ActionType != ActionType.WEND);
            parsedArgs    = new object[2];
            parsedArgs[0] = var;
            parsedArgs[1] = actions_;

            return(ActionType.WHILE);
        }
Beispiel #3
0
        static ActionType RETURN_Parse(string functionspace, string unparsedArgs, ref object[] parsedArgs, ref int lineNumber)
        {
            string     parameters  = unparsedArgs.Substring(7, unparsedArgs.Length - 7);
            ISILObject returnValue = ExpressionParse(functionspace, parameters, new List <ISILObject>());

            parsedArgs = new object[] { returnValue };
            return(ActionType.RETURN);
        }
Beispiel #4
0
        static ActionType READ_Parse(string functionspace, string unparsedArgs, ref object[] parsedArgs, ref int lineNumber)
        {
            string parameters = unparsedArgs.Substring(5, unparsedArgs.Length - 5).Trim();

            string[] split = parameters.Split(new char[] { ',' }, 2, StringSplitOptions.RemoveEmptyEntries);
            if (split.Length < 2)
            {
                throw new Exception("Syntax error!");
            }
            split[1] = split[1].Trim();
            if (split[1][0] != '\"' || split[1][split[1].Length - 1] != '\"')
            {
                throw new Exception("Missing \"!");
            }
            split[1] = split[1].Substring(1, split[1].Length - 2);
            ISILObject variable = Retrieve_SIL_Object(functionspace, split[0], null);

            parsedArgs = new object[] { variable, split[1] };
            return(ActionType.READ);
        }
Beispiel #5
0
        static ActionType IF_Parse(string functionspace, string unparsedArgs, ref object[] parsedArgs, ref int lineNumber)
        {
            //remove the IF word
            string parameters = unparsedArgs.Substring(3, unparsedArgs.Length - 3);

            //validate that expression ends in THEN
            string THENstring = unparsedArgs.Substring(unparsedArgs.Length - 5, 5);

            if (THENstring != " THEN")
            {
                throw new Exception("Missing THEN keyword!");
            }

            //get the conditional expression
            parameters = parameters.Substring(0, parameters.Length - 5);
            //parse this stuff into expression
            ISILObject         var = ExpressionParse(functionspace, parameters, new List <ISILObject>());
            SIL_Action         action;
            Queue <SIL_Action> actions_ = new Queue <SIL_Action>();

            do
            {
                lineNumber++;
                if (lineNumber >= sourceLines_.Length)
                {
                    throw new Exception("ENDIF expected!");
                }
                action = ParseNext(functionspace, ref lineNumber);
                if (action != null)
                {
                    actions_.Enqueue(action);
                }
            } while (action.ActionType != ActionType.ENDIF);
            parsedArgs    = new object[2];
            parsedArgs[0] = var;
            parsedArgs[1] = actions_;

            return(ActionType.IF);
        }
Beispiel #6
0
        static void PRINT_Execute(SIL_Action action)
        {
            //if there is arithmetic involved it needs to be executed here

            //At the moment no variables or arithmetic exists so we just build a single string to print
            string textToPrint = "";

            foreach (object arg in action.ParsingArgs)
            {
                if (arg.GetType() == typeof(string))
                {
                    textToPrint += (string)arg;
                }
                if (arg is ISILObject)
                {
                    ISILObject silObj = (ISILObject)arg;
                    int        n      = (SILInteger)silObj.Value;
                    textToPrint += n.ToString();
                }
            }
            action.ExecutionArgs = new string[] { textToPrint };
        }
Beispiel #7
0
        /// <summary>
        /// Parses LET function
        /// </summary>
        /// <param name="unparsedArgs"></param>
        /// <param name="parsedArgs"></param>
        /// <returns></returns>
        static ActionType LET_Parse(string functionspace, string unparsedArgs, ref object[] parsedArgs, ref int lineNumber)
        {
            //remove LET word
            string parameters = unparsedArgs.Substring(4, unparsedArgs.Length - 4);

            if (parameters.Length < 2)
            {
                throw new Exception("LET command must be followed by assignment operation!");
            }

            string[] args = parameters.Split('=');
            if (args.Length != 2)
            {
                throw new Exception("Syntax error!");
            }

            ISILObject varOrArrElement = Retrieve_SIL_Object(functionspace, args[0], null);
            ISILObject var             = ExpressionParse(functionspace, args[1], new List <ISILObject>());

            parsedArgs = new object[] { varOrArrElement, var };

            return(ActionType.LET);
        }
Beispiel #8
0
        public static ISILObject ExpressionParse(string functionspace, string unparsedArgs, List <ISILObject> equations)
        {
            unparsedArgs = unparsedArgs.Trim();

            //Check if it is not an equation but a variable, otherwise recurse until it is a single object
            if (!SIL_Math.IsEquation(unparsedArgs) && !unparsedArgs.Contains('['))
            {
                return(Retrieve_SIL_Object(functionspace, unparsedArgs, equations));
            }

            int leftIndex  = 0,
                rightIndex = 0;
            string deepestEquation;

            //parse arrays first
            if (unparsedArgs.Contains('['))
            {
                deepestEquation = FindDeepest(unparsedArgs, '[', ']', ref rightIndex, ref leftIndex);

                ISILObject arrIndex = ProcessEquation(functionspace, deepestEquation, equations);
                string     arrName  = "";
                leftIndex--;
                while (leftIndex > 0 ?
                       (!SIL_Math.IsOperator(unparsedArgs[leftIndex - 1]) &&
                        unparsedArgs[leftIndex - 1] != '[' &&
                        unparsedArgs[leftIndex - 1] != '(') : false)
                {
                    leftIndex--;
                    arrName += unparsedArgs[leftIndex];
                }
                arrName = Reverse(arrName).Trim();
                int    leftLength = leftIndex;
                string leftPart   = leftLength > 0 ? unparsedArgs.Substring(0, leftLength) : "";
                rightIndex = rightIndex == unparsedArgs.Length ? rightIndex : rightIndex + 1;
                int    rightLength = unparsedArgs.Length - rightIndex;
                string rightPart   = rightLength > 0 ? unparsedArgs.Substring(rightIndex, rightLength) : "";

                equations.Add(new SILArrayElement(functionspace + "." + arrName, arrIndex));
                unparsedArgs = leftPart +
                               "{" + (equations.Count - 1).ToString() + "}" +
                               rightPart;

                return(ExpressionParse(functionspace, unparsedArgs, equations));
            }
            deepestEquation = FindDeepest(unparsedArgs, '(', ')', ref rightIndex, ref leftIndex);

            if (leftIndex > 1)
            {
                if (char.IsLetterOrDigit(unparsedArgs[leftIndex - 2]))
                {
                    string functionName = "";
                    leftIndex--;
                    while (leftIndex > 0 ?
                           (!SIL_Math.IsOperator(unparsedArgs[leftIndex - 1]) &&
                            unparsedArgs[leftIndex - 1] != '[' &&
                            unparsedArgs[leftIndex - 1] != '(') : false)
                    {
                        leftIndex--;
                        functionName += unparsedArgs[leftIndex];
                    }
                    functionName = Reverse(functionName).Trim();

                    int    leftLength = leftIndex;
                    string leftPart   = leftLength > 0 ? unparsedArgs.Substring(0, leftLength) : "";
                    rightIndex = rightIndex == unparsedArgs.Length ? rightIndex : rightIndex + 1;
                    int    rightLength = unparsedArgs.Length - rightIndex;
                    string rightPart   = rightLength > 0 ? unparsedArgs.Substring(rightIndex, rightLength) : "";

                    string      function_s = unparsedArgs.Substring(leftLength, rightIndex - leftLength).Trim();
                    SILFunction function;
                    object[]    functionArgs = null;
                    int         linenumber   = 0;
                    CALL_Parse(functionspace, "CALL " + function_s, ref functionArgs, ref linenumber);
                    function = (SILFunction)functionArgs[0];

                    equations.Add(function);
                    unparsedArgs = leftPart +
                                   "{" + (equations.Count - 1).ToString() + "}" +
                                   rightPart;

                    return(ExpressionParse(functionspace, unparsedArgs, equations));
                }
            }

            if (!SIL_Math.IsEquation(deepestEquation))
            {
                unparsedArgs = unparsedArgs.Replace("(" + deepestEquation + ")", deepestEquation.Trim());
            }
            else
            {
                equations.Add((SILEquation)ProcessEquation(functionspace, deepestEquation, equations));
                int    leftLength = leftIndex > 0 ? leftIndex - 1 : 0;
                string leftPart   = leftLength > 0 ? unparsedArgs.Substring(0, leftLength) : "";
                rightIndex = rightIndex == unparsedArgs.Length ? rightIndex : rightIndex + 1;
                int    rightLength = unparsedArgs.Length - rightIndex;
                string rightPart   = rightLength > 0 ? unparsedArgs.Substring(rightIndex, rightLength) : "";
                unparsedArgs = leftPart +
                               "{" + (equations.Count - 1).ToString() + "}" +
                               rightPart;
            }
            return(ExpressionParse(functionspace, unparsedArgs, equations));
        }
Beispiel #9
0
        /// <summary>
        /// Parses PRINT command
        /// </summary>
        /// <param name="unparsedArgs">a line of everything including the command symbol</param>
        /// <param name="parsedArgs">ann array of parsed objects which can be strings, variables, or
        /// math operations</param>
        /// <returns>ActionType.PRINT</returns>
        static ActionType PRINT_Parse(string functionspace, string unparsedArgs, ref object[] parsedArgs, ref int lineNumber)
        {
            string[] parameters = unparsedArgs.Split(' ');

            if (parameters.Length < 2)
            {
                throw new Exception("PRINT command must be followed by message to display!");
            }
            bool          qouteOpen = false, plusSign = false;
            string        message = "", oneMessage = "";
            List <object> finalMessages = new List <object>();

            for (int i = 1; i < parameters.Length; i++)
            {
                message += parameters[i] + ((i < parameters.Length - 1) ? " " : "");
            }

            for (int i = 0; i < message.Length; i++)
            {
                //Process quoted messages
                if (message[i] == '"' && !qouteOpen)
                {
                    if (finalMessages.Count > 0 && !plusSign)
                    {
                        throw new Exception("+ expected!");
                    }
                    qouteOpen = true;
                    plusSign  = false;
                    continue;
                }
                if (message[i] == '"' && qouteOpen)
                {
                    qouteOpen = false;
                    finalMessages.Add(oneMessage);
                    oneMessage = "";
                    continue;
                }
                //check that quote is closed before message ends
                if (qouteOpen)
                {
                    if (i == message.Length - 1)
                    {
                        throw new Exception("\" Expected!");
                    }
                    oneMessage += message[i];
                    continue;
                }

                //process unquoted messages
                if (!qouteOpen)
                {
                    //process numner
                    if (Char.IsNumber(message[i]))
                    {
                        if (finalMessages.Count > 0 && !plusSign)
                        {
                            throw new Exception("+ expected!");
                        }
                        plusSign = false;
                        while (message[i] != ' ' && message[i] != '+')
                        {
                            if (!Char.IsNumber(message[i]))
                            {
                                throw new Exception("Incorrect argument, numeric character is expected!");
                            }
                            oneMessage += message[i];
                            i++;
                            if (i == message.Length)
                            {
                                break;
                            }
                        }
                        finalMessages.Add(oneMessage);
                        continue;
                    }
                    //process variable
                    if (Char.IsLetter(message[i]))
                    {
                        if (finalMessages.Count > 0 && !plusSign)
                        {
                            throw new Exception("+ expected!");
                        }
                        plusSign = false;
                        string varName = "";
                        while (message[i] != ' ' && message[i] != '+')
                        {
                            varName += message[i];
                            i++;
                            if (i == message.Length)
                            {
                                break;
                            }
                        }
                        ISILObject variable = Retrieve_SIL_Object(functionspace, varName, null);
                        finalMessages.Add(variable);
                        continue;
                    }
                    if (message[i] == ' ')
                    {
                        continue;
                    }
                    if (message[i] == '+')
                    {
                        plusSign = true;
                        continue;
                    }
                    throw new Exception("Unknown symbol!");
                }
            }
            parsedArgs = new object[finalMessages.Count];
            finalMessages.ToArray().CopyTo(parsedArgs, 0);
            return(ActionType.PRINT);
        }
Beispiel #10
0
        static void RETURN_Execute(SIL_Action action)
        {
            ISILObject returnValue = (ISILObject)action.ParsingArgs[0];

            action.ExecutionArgs = new object[] { returnValue.Value };
        }
Beispiel #11
0
        static void LET_Execute(SIL_Action action)
        {
            ISILObject varOrArrElement = (ISILObject)action.ParsingArgs[0];

            varOrArrElement.Value = ((ISILObject)action.ParsingArgs[1]).Value;
        }
Beispiel #12
0
 public SILArrayElement(string name, ISILObject arrayIndex)
 {
     name_       = name;
     arrayIndex_ = arrayIndex;
 }