Exemple #1
0
        public override void Compile(CompiledStatement parent, string token, LinkedList <string> tokens, LinkedList <string> lines)
        {
            base.Compile(parent, token, tokens, lines);

            Delimiter.InlineToken = scriptToken;

            // Now check that there is another element after our keyword that we can use as the variable name
            Debug.Assert(tokens.Count > 0, "No value found for the right hand side of keyword: " + token);

            if (tokens.First.Value == FunctionKeyword.scriptToken)
            {
                // If we have a function after this, we do nothing - functions by default are added to the current local scope
                Delimiter.InlineToken = "";
                return;
            }

            // Get the next token that appears on the right hand side of this operator - this will be our variable name
            string rhsOfKeyword = CelesteCompiler.PopToken();

            Debug.Assert(!CelesteStack.CurrentScope.VariableExists(rhsOfKeyword), "Variable with the same name already exists in this scope");

            // Creates a new variable, but does not call Compile - Compile for variable assigns a reference from the stored variable in CelesteStack
            Variable variable = CelesteStack.CurrentScope.CreateLocalVariable <Variable>(rhsOfKeyword);

            // If we still have unparsed tokens, then add this variable to the tree
            // Otherwise, do not - we do not need to push an object onto the stack for no reason
            if (tokens.Count > 0)
            {
                // Add our variable underneath our parent - this keyword will do nothing at run time so do not add it to the tree
                parent.Add(variable);
            }
        }
        public override void Compile(CompiledStatement parent, string token, LinkedList <string> tokens, LinkedList <string> lines)
        {
            base.Compile(parent, token, tokens, lines);

            Debug.Assert(parent.ChildCount > 0, "No value found for the left hand side of operator: " + token);

            // Take the previous statement and add it as a sub statement of the newly created operator - we will check for validity in the Compile function
            parent.MoveChildAtIndex(parent.ChildCount - 1, this);

            // Add this operator to the tree
            parent.Add(this);

            // Now check that there is another element after our operator that we can act on
            Debug.Assert(tokens.Count > 0, "No value found for the right hand side of operator: " + token);

            // Get the next token that appears on the right hand side of this operator
            string rhsOfOperatorToken = CelesteCompiler.PopToken();

            // Parse the next token which we will act on
            if (CelesteCompiler.CompileToken(rhsOfOperatorToken, parent))
            {
                // Take the value that has been added to the root and add it under this operator instead
                parent.MoveChildAtIndex(parent.ChildCount - 1, this);
            }
            else
            {
                // Error message if we cannot parse the next token
                Debug.Fail("Could not compile token: " + token + " in operator " + token);
            }
        }
        /// <summary>
        /// Loads our file and sends it to the parser to compile into a statement tree
        /// </summary>
        public void Compile()
        {
            Compiling = true;

            if (CelesteStack.Scopes.Exists(x => x == ScriptScope))
            {
                CelesteStack.Scopes.Remove(ScriptScope);
            }

            ScriptScope = new Scope(ScriptPath);

            Tuple <bool, CompiledStatement> result = null;

            using (StreamReader scriptReader = File.OpenText(Cel.ScriptDirectoryPath + "\\" + ScriptPath))
            {
                result = CelesteCompiler.CompileScript(scriptReader);
            }

            Debug.Assert(result != null);
            if (result.Item1)
            {
                // If we have successfully parsed the script, we execute the compile tree
                ScriptExecutable = result.Item2;
            }
            else
            {
                Debug.Fail("Script " + ScriptPath + " had an error during compilation");
            }

            Compiling = false;
        }
Exemple #4
0
        public override void Compile(CompiledStatement parent, string token, LinkedList <string> tokens, LinkedList <string> lines)
        {
            base.Compile(parent, token, tokens, lines);

            // We know the token will at least start with the start delimiter
            if (token.Length > 1)
            {
                // In here if the start delimiter and another token are smushed together with no space
                // Split the start delimiter and the rest of the token and add the rest of the token to the start of our tokens list
                string rest = token.Remove(0, 1);
                tokens.AddFirst(rest);
            }

            // We keep parsing until we find a closing character for our list
            bool foundClosing = false;

            while (!foundClosing)
            {
                Debug.Assert(tokens.Count > 0 || lines.Count > 0, "List requires a closing " + endDelimiter);

                // Create a new set of tokens if we have run out for this line
                if (tokens.Count == 0)
                {
                    CelesteCompiler.TokenizeNextLine();
                }

                string nextToken = CelesteCompiler.PopToken();
                if (nextToken.EndsWith(endDelimiter) && nextToken.Length > 1)
                {
                    // Our token has the end delimiter squashed at the end of it, so we split the token in two and add the delimiter to the tokens list
                    // This will mean we still compile this token and finish our list the next iteration round
                    nextToken = nextToken.Remove(nextToken.Length - 1);
                    tokens.AddFirst(endDelimiter);
                }

                if (nextToken == endDelimiter)
                {
                    foundClosing = true;
                }
                else if (CelesteCompiler.CompileToken(nextToken, parent))
                {
                    // Take the value that has been created from compiling this token and add it to our list
                    // Then remove the compiled statement - we do not want objects in our list pushed onto the stack
                    CompiledStatement valueCreated = parent.ChildCompiledStatements[parent.ChildCount - 1];
                    parent.RemoveAt(parent.ChildCount - 1);

                    // The value we created MUST be a value
                    Debug.Assert(valueCreated is Value);
                    ListRef.Add((valueCreated as Value)._Value);
                }
                else
                {
                    // Error message if we cannot parse the next token
                    Debug.Fail("Error parsing token: " + token + " in list");
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Call compile when you wish to implement the function.
        /// Process the actual variable names that have been passed into the function using the token.
        /// Create a variable underneath this for each variable we are passing in.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="token"></param>
        /// <param name="tokens"></param>
        /// <param name="lines"></param>
        public override void Compile(CompiledStatement parent, string token, LinkedList <string> tokens, LinkedList <string> lines)
        {
            // We have invoked this function because we have parentheses
            if (tokens.Count > 0 && tokens.First.Value == OpenParenthesis.scriptToken)
            {
                // Group our inputs under this object so that we can store them underneath this function
                CompiledStatement thisCallsParams = new CompiledStatement();
                if (ParameterNames.Count > 0)
                {
                    // Only add our parameters if we have any.
                    // Otherwise this will just strip away the start and end parentheses
                    Add(thisCallsParams);
                }

                string openingParenthesis = CelesteCompiler.PopToken();
                Debug.Assert(openingParenthesis == OpenParenthesis.scriptToken, "No opening parenthesis found for function " + Name);

                string parameter = CelesteCompiler.PopToken();

                CompiledStatement tempContainer = new CompiledStatement();
                while (parameter != CloseParenthesis.scriptToken)
                {
                    Debug.Assert(CelesteCompiler.CompileToken(parameter, tempContainer),
                                 "Failed to compile input parameter " + parameter);
                    parameter = CelesteCompiler.PopToken();
                }

                // Add null references first for all of the parameters we are missing
                for (int i = tempContainer.ChildCount; i < ParameterNames.Count; i++)
                {
                    // This will push null onto the stack for every parameter we have not provided an input for
                    Reference refToNull = new Reference(null);
                    refToNull.Compile(thisCallsParams, "null", tokens, lines);
                }

                // Then add the actual parameters we have inputted
                for (int i = tempContainer.ChildCount - 1; i >= 0; i--)
                {
                    // Add our parameters in reverse order, so they get added to the stack in reverse order
                    tempContainer.MoveChildAtIndex(i, thisCallsParams);
                }

                // Add a reference to this function in our compile tree after we have added all of the inputs
                base.Compile(parent, token, tokens, lines);
            }
            else
            {
                // If we have no brackets, we are trying to compile the function as a reference rather than as a call (for use in equality for example)
                Reference funcRef = new Reference(this);
                funcRef.Compile(parent, Name, tokens, lines);
            }

            // Any local variable that is not set will be null
            // Any extra parameters will be added, but because we add them in reverse order, if they are not needed they will just be thrown away on the stack
        }
        /// <summary>
        /// Adds a not unary operator with the next token's compiled object to the parent
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="token"></param>
        /// <param name="tokens"></param>
        /// <param name="lines"></param>
        public override void Compile(CompiledStatement parent, string token, LinkedList <string> tokens, LinkedList <string> lines)
        {
            base.Compile(parent, token, tokens, lines);

            // Now check that there is an element after our keyword that we can use as the variable name
            Debug.Assert(tokens.Count > 0, "No value found for the right hand side of the 'not' keyword");

            string rhsToken = CelesteCompiler.PopToken();

            // Create a not operator and compile - this will do all the work of moving statements around in the parent
            // It will also add itself to the parent tree
            NotOperator notOperator = new NotOperator();

            notOperator.Compile(parent, NotOperator.scriptToken + rhsToken, tokens, lines);
        }
        /// <summary>
        /// Reads in the condition for the while loop and then the body to perform while the condition is true.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="token"></param>
        /// <param name="tokens"></param>
        /// <param name="lines"></param>
        public override void Compile(CompiledStatement parent, string token, LinkedList <string> tokens, LinkedList <string> lines)
        {
            base.Compile(parent, token, tokens, lines);

            KeyValuePair <CompiledStatement, CompiledStatement> whileCondBody = new KeyValuePair <CompiledStatement, CompiledStatement>(new CompiledStatement(), new CompiledStatement());

            ConditionsAndBodies.Add(whileCondBody);

            Debug.Assert(tokens.Count > 0, "Tokens required for while condition");
            while (tokens.Count > 0)
            {
                CelesteCompiler.CompileToken(CelesteCompiler.PopToken(), whileCondBody.Key);
            }

            // We keep parsing until we find the closing keyword for our while control
            bool foundClosing = false;

            while (!foundClosing)
            {
                Debug.Assert(tokens.Count > 0 || lines.Count > 0, "Function requires a closing " + endDelimiter);

                // Create a new set of tokens if we have run out for this line
                if (tokens.Count == 0)
                {
                    CelesteCompiler.TokenizeNextLine();
                }

                string nextToken = CelesteCompiler.PopToken();
                if (nextToken == endDelimiter)
                {
                    foundClosing = true;
                }
                else if (CelesteCompiler.CompileToken(nextToken, whileCondBody.Value))
                {
                }
                else
                {
                    // Error message if we cannot parse the next token
                    Debug.Fail("Operator invalid on token: " + token);
                }
            }

            // Close the scope that is automatically opened in our constructor
            CelesteStack.CurrentScope = FlowScope.ParentScope;
        }
        public override void Compile(CompiledStatement parent, string token, LinkedList <string> tokens, LinkedList <string> lines)
        {
            base.Compile(parent, token, tokens, lines);

            // Now check that there is another element after our keyword that we can use as the variable name
            Debug.Assert(tokens.Count > 0, "No value found for the right hand side of keyword: " + token);

            // Get the next token that appears on the right hand side of this operator - this will be our variable name
            string rhsOfKeyword = CelesteCompiler.PopToken();

            if (rhsOfKeyword == FunctionKeyword.scriptToken)
            {
                Debug.Assert(tokens.Count > 0, "Function name must exist");
                string functionName = tokens.First.Value;
                functionName = functionName.Remove(functionName.IndexOf(OpenParenthesis.scriptToken));

                if (CelesteCompiler.CompileToken(rhsOfKeyword, parent))
                {
                    // If we have compiled our global function, we need to remove it from the current scope it is added to by default and move it to the global scope
                    Debug.Assert(CelesteStack.CurrentScope.VariableExists(functionName));
                    Variable function = CelesteStack.CurrentScope.RemoveLocalVariable(functionName);
                    CelesteStack.GlobalScope.AddLocalVariable(function);
                }
                else
                {
                    Debug.Fail("Error parsing global function");
                }

                return;
            }

            Debug.Assert(!CelesteStack.GlobalScope.VariableExists(rhsOfKeyword), "Variable with the same name already exists in this scope");

            // Creates a new variable, but does not call Compile - Compile for variable assigns a reference from the stored variable in CelesteStack
            Variable variable = CelesteStack.GlobalScope.CreateLocalVariable <Variable>(rhsOfKeyword);

            // If we still have unparsed tokens, then add this variable to the tree
            // Otherwise, do not - we do not need to push an object onto the stack for no reason
            if (tokens.Count > 0)
            {
                // Add our variable underneath our parent - this keyword will do nothing at run time so do not add it to the tree
                parent.Add(variable);
            }
        }
Exemple #9
0
        public override void Compile(CompiledStatement parent, string token, LinkedList <string> tokens, LinkedList <string> lines)
        {
            base.Compile(parent, token, tokens, lines);

            parent.Add(this);

            while (tokens.Count > 0)
            {
                // Get the next token that appears on the right hand side of this operator
                // This will be the name of the local variables we wish to return
                string rhsOfKeyword = CelesteCompiler.PopToken();
                if (rhsOfKeyword.EndsWith(returnParameterDelimiter))
                {
                    // Return the parameter delimiter after each return parameter if it exists
                    rhsOfKeyword = rhsOfKeyword.Remove(rhsOfKeyword.Length - 1);
                }
                Debug.Assert(CelesteCompiler.CompileToken(rhsOfKeyword, this), "Error compiling return parameter");
            }
        }
Exemple #10
0
        public override void Compile(CompiledStatement parent, string token, LinkedList <string> tokens, LinkedList <string> lines)
        {
            base.Compile(parent, token, tokens, lines);

            // Unary operators act on other variables/values and so they must be included in the same token as another token (e.g. !var for example)
            // We remove the script token for the operator and split the other token out
            Debug.Assert(token.Length > 1);
            string rest = token.Remove(0, 1); // Removing wrong thing here

            // Add this operator to the tree
            parent.Add(this);

            // Parse the rest of the token which we will act on
            if (CelesteCompiler.CompileToken(rest, parent))
            {
                // Take the value that has been added to the root and add it under this operator instead
                parent.MoveChildAtIndex(parent.ChildCount - 1, this);
            }
            else
            {
                // Error message if we cannot parse the next token
                Debug.Fail("Could not compile token: " + token + " in operator " + token);
            }
        }
Exemple #11
0
        public override void Compile(CompiledStatement parent, string token, LinkedList <string> tokens, LinkedList <string> lines)
        {
            base.Compile(parent, token, tokens, lines);

            string fullString = "";

            // If our token is of the form "something" we have our full string
            if (token.EndsWith(endDelimiter))
            {
                // Remove the '"' from the start and the end
                fullString = token.Substring(1, token.Length - 2);
            }
            else
            {
                // Our full string is going to be split over many tokens - we need to keep adding tokens until we hit our second '"'
                // Remove the '"' from the first token
                fullString = token.Remove(0, 1);

                // Need more tokens left over to make our full string
                Debug.Assert(tokens.Count > 0, "No \" found to end the string: " + fullString);
                string currentToken;

                // We are going to keep removing elements, so need to check whether we still have tokens left to check
                while (tokens.Count > 0)
                {
                    currentToken = CelesteCompiler.PopToken();

                    if (!string.IsNullOrEmpty(currentToken))
                    {
                        // If the next token we are moving through contains '"' we are done finding our full string
                        if (currentToken.Contains(endDelimiter))
                        {
                            // Add the substring without the end string character to our full string
                            int index = currentToken.IndexOf(endDelimiter);
                            if (index > 0)
                            {
                                // Concatenate the contents of this token if it was more than just the end delimiter
                                fullString += " " + currentToken.Substring(0, index);
                            }

                            if (index < currentToken.Length - 1)
                            {
                                // The end delimiter was in the middle of the token and we have other stuff that should be popped back into the tokens list
                                // index + 1 because we don't want to add the end delimiter
                                tokens.AddFirst(currentToken.Substring(index + 1));
                            }

                            break;
                        }
                        else
                        {
                            fullString += " " + currentToken;
                        }
                    }

                    // We should only ever get here if we have tokens left
                    // If this assert triggers, it means that we have run out of tokens, but not found a close to our string
                    Debug.Assert(tokens.Count > 0, "No \" found to end the string: " + fullString);
                }
            }

            _Value = fullString;
        }
Exemple #12
0
        public override void Compile(CompiledStatement parent, string token, LinkedList <string> tokens, LinkedList <string> lines)
        {
            base.Compile(parent, token, tokens, lines);

            // We know the token will at least start with the start delimiter
            if (token.Length > 1)
            {
                // In here if the start delimiter and another token are smushed together with no space
                // Split the start delimiter and the rest of the token and add the rest of the token to the start of our tokens list
                string rest = token.Remove(0, 1);
                tokens.AddFirst(rest);
            }

            // We keep parsing until we find a closing character for our list
            bool foundClosing = false;

            while (!foundClosing)
            {
                Debug.Assert(tokens.Count > 0 || lines.Count > 0, "Table requires a closing " + endDelimiter);

                // Create a new set of tokens if we have run out for this line
                if (tokens.Count == 0)
                {
                    CelesteCompiler.TokenizeNextLine();
                }

                string nextToken = CelesteCompiler.PopToken();
                if (nextToken == AssignmentOperator.scriptToken)
                {
                    string valueToken = tokens.First.Value;
                    if (valueToken.EndsWith(endDelimiter) && valueToken.Length > 1)
                    {
                        valueToken         = valueToken.Remove(valueToken.Length - 1, 1);
                        tokens.First.Value = valueToken;
                        foundClosing       = true;
                    }
                }

                if (nextToken == endDelimiter)
                {
                    foundClosing = true;
                }
                else if (CelesteCompiler.CompileToken(nextToken, parent))
                {
                    CompiledStatement compiledStatement = parent.ChildCompiledStatements[parent.ChildCount - 1];

                    if (!gotKey)
                    {
                        // The statement we created for our key MUST be a value type
                        Debug.Assert(compiledStatement is Value, "Key in table must be a valid value type");
                        gotKey = true;
                    }
                    else if (!gotEquality)
                    {
                        // The statement we created after our key MUST be the equality operator
                        Debug.Assert(compiledStatement is AssignmentOperator, "Equality expected after key");
                        gotEquality = true;
                        gotValue    = compiledStatement.ChildCount == 2;
                    }

                    if (gotValue)
                    {
                        // We have an equals parented under the 'parent' parameter (along with this table), with the key and value parented under that
                        // We insert the key and value into our dictionary, discard the equals statement and adjust our flags
                        Debug.Assert(parent.ChildCount >= 2);
                        Debug.Assert(parent.ChildCompiledStatements[parent.ChildCount - 1].ChildCount == 2);
                        Debug.Assert(parent.ChildCompiledStatements[parent.ChildCount - 1] is AssignmentOperator);

                        AssignmentOperator equals = parent.ChildCompiledStatements[parent.ChildCount - 1] as AssignmentOperator;
                        Debug.Assert(equals.ChildCompiledStatements[0] is Value);
                        Debug.Assert(equals.ChildCompiledStatements[1] is Value);

                        DictionaryRef.Add((equals.ChildCompiledStatements[0] as Value)._Value, (equals.ChildCompiledStatements[1] as Value)._Value);
                        parent.ChildCompiledStatements.RemoveAt(parent.ChildCount - 1);

                        gotKey      = false;
                        gotEquality = false;
                        gotValue    = false;
                    }
                }
                else
                {
                    // Error message if we cannot parse the next token
                    Debug.Fail("Operator invalid on token: " + token);
                }
            }

            Debug.Assert(!gotKey && !gotEquality && !gotValue, "Incomplete key value pair in table");
        }
Exemple #13
0
        public override void Compile(CompiledStatement parent, string token, LinkedList <string> tokens, LinkedList <string> lines)
        {
            base.Compile(parent, token, tokens, lines);

            // Now check that there is another element after our keyword that we can use as the function name
            Debug.Assert(tokens.Count > 0 || lines.Count > 0, "No parameters or body found for the keyword: " + token);

            // The first token should be the function name
            string functionName = CelesteCompiler.PopToken();

            Debug.Assert(!CelesteStack.CurrentScope.VariableExists(functionName), "Variable with the same name already exists in this scope");

            // Get the next token - this should be an opening parenthesis
            string openingParenthesis = CelesteCompiler.PopToken();

            Debug.Assert(openingParenthesis == OpenParenthesis.scriptToken, "No opening parenthesis found for function " + functionName);

            // Creates a new function, but does not call Compile - Compile for function assigns a reference from the stored function in CelesteStack
            Function function = CelesteStack.CurrentScope.CreateLocalVariable <Function>(functionName);

            // Obtain the parameter names from the strings/tokens between the brackets
            List <string> paramNames = new List <string>();
            string        parameters = CelesteCompiler.PopToken();

            while (parameters != CloseParenthesis.scriptToken)
            {
                // Clean the parameter array of empty strings and remove any delimiter characters from parameter names
                List <string> parameterList = new List <string>(parameters.Split(Delimiter.scriptTokenChar));
                parameterList.RemoveAll(x => string.IsNullOrEmpty(x));
                parameterList = new List <string>(parameterList.Select(x => x = x.Replace(Delimiter.scriptToken, "")));

                // Add any parameters which are of this form 'param1,param2'
                paramNames.AddRange(parameterList);

                parameters = CelesteCompiler.PopToken();// This algorithm should completely take care of any mix of parameters separated with a space or not
            }

            function.SetParameters(paramNames.ToArray());

            // We keep parsing until we find the closing keyword for our function
            bool foundClosing = false;

            while (!foundClosing)
            {
                Debug.Assert(tokens.Count > 0 || lines.Count > 0, "Function requires a closing " + endDelimiter);

                // Create a new set of tokens if we have run out for this line
                if (tokens.Count == 0)
                {
                    CelesteCompiler.TokenizeNextLine();
                }

                string nextToken = CelesteCompiler.PopToken();
                if (nextToken == endDelimiter)
                {
                    foundClosing = true;
                }
                else if (CelesteCompiler.CompileToken(nextToken, function.FuncImpl))
                {
                }
                else
                {
                    // Error message if we cannot parse the next token
                    Debug.Fail("Operator invalid on token: " + token);
                }
            }

            // Do not add the function - it will be looked up to and called rather than pushed onto the stack (much like a variable)
            // Close the function's scope now - we have added all the appropriate variables to it
            // This scope is automatically opened in the Function constructor
            CelesteStack.CurrentScope = function.FunctionScope.ParentScope;
        }