Пример #1
0
 /// <summary>
 /// Checks the function.
 /// </summary>
 /// <returns><c>true</c>, if function was checked, <c>false</c> otherwise.</returns>
 bool CheckFunction()
 {
     if (Now.Type == STokenType.Function)
     {
         Function_current = new Function {
             Name         = Now.Value,
             Instructions = new List <Instruction> (),
             Ret          = new Return(),
         };
         Now = Input [++Current];
         if (Now.Type == STokenType.Arguments)
         {
             Function_current.Arguments = parseArguments(Now);
             inFunction = true;
             Current++;
             return(true);
         }
     }
     return(false);
 }
Пример #2
0
        /// <summary>
        /// Parses the arguments.
        /// </summary>
        /// <returns>The arguments.</returns>
        /// <param name="args">Tokens.</param>
        Args parseArguments(SToken args)
        {
            Args n = new Args {
                Arguments = new List <Variable>()
            };
            bool toAdd = true;

            foreach (Token x in args.Instruction)
            {
                if (toAdd)
                {
                    toAdd = false;
                    if (x.Type == TokenType.Variable)
                    {
                        n.Arguments.Add(new Variable {
                            Name = x.Value
                        });
                    }
                    if (x.Type == TokenType.Number)
                    {
                        n.Arguments.Add(new Variable {
                            Name = x.Value, isNum = true, isConst = true
                        });
                    }
                    if (x.Type == TokenType.String)
                    {
                        n.Arguments.Add(new Variable {
                            Name = x.Value, isConst = true
                        });
                    }
                }
                else
                {
                    if (x.Type == TokenType.Operator && x.Value == ",")
                    {
                        toAdd = true;
                    }
                }
            }
            return(n);
        }
Пример #3
0
        /// <summary>
        /// Simplify this instance.
        /// </summary>
        void Simplify()
        {
            while (Current < Input.Count)
            {
                Token Now = Input[Current];
                if (Now.Type == TokenType.Parenthesis)
                {
                    if (Now.Value == "{")
                    {
                        STokens.Add(new SToken {
                            Location = Now.Location, Type = STokenType.Block, Value = "open"
                        });
                        Current++;
                        continue;
                    }
                    if (Now.Value == "}")
                    {
                        STokens.Add(new SToken {
                            Location = Now.Location, Type = STokenType.Block, Value = "close"
                        });
                        Current++;
                        continue;
                    }
                    if (Now.Value == "(")
                    {
                        SToken arg = new SToken
                        {
                            Location    = Now.Location,
                            Type        = STokenType.Arguments,
                            Instruction = new List <Token>()
                        };
                        while (!(Now.Value == ")"))
                        {
                            Now = Input[++Current];
                            if (Now.Value == ")")
                            {
                                break;
                            }
                            arg.Instruction.Add(Now);
                        }
                        Current++;
                        STokens.Add(arg);
                        continue;
                    }
                }
                if (Now.Type == TokenType.Identifier || Now.Type == TokenType.Operator || Now.Type == TokenType.Variable)
                {
                    if (Now.Value == "namespace")
                    {
                        if (Input[++Current].Type == TokenType.Identifier)
                        {
                            STokens.Add(new SToken {
                                Location = Now.Location, Type = STokenType.Namespace, Value = Input[Current].Value
                            });
                            Current++;
                            continue;
                        }
                    }

                    if (Now.Value == "return")
                    {
                        STokens.Add(new SToken {
                            Location    = Now.Location,
                            Type        = STokenType.Return,
                            Instruction = new List <Token> {
                                Input[Current + 1]
                            },
                            Value = "return"
                        });
                        Current += 2;
                        if (Input[Current].Type == TokenType.Break)
                        {
                            Current++;
                            continue;
                        }
                        Res.Errors.Add(new Error {
                            Code = 3, Value = "Unexpected Token in Return (" + Now.Type + ":" + Now.Value + ")", Location = Now.Location
                        });
                        break;
                    }

                    if (Now.Value == "function")
                    {
                        if (Input[++Current].Type == TokenType.Identifier)
                        {
                            STokens.Add(new SToken {
                                Location = Now.Location, Type = STokenType.Function, Value = Input[Current].Value
                            });
                            Current++;
                            continue;
                        }
                    }
                    if (Now.Type == TokenType.Variable || Now.Type == TokenType.Operator || Now.Type == TokenType.Identifier)
                    {
                        SToken instruc = new SToken
                        {
                            Location    = Now.Location,
                            Type        = STokenType.Instruction,
                            Value       = Now.Value,
                            Instruction = new List <Token>()
                        };
                        while (!(Now.Type == TokenType.Break || Now.Type == TokenType.Parenthesis))
                        {
                            instruc.Instruction.Add(Now);
                            Now = Input[++Current];
                        }
                        if (Now.Type == TokenType.Break)
                        {
                            Current++;
                        }
                        STokens.Add(instruc);
                        continue;
                    }
                }
                if (Now.Type == TokenType.PreProcessor)
                {
                    STokens.Add(new SToken {
                        Location = Now.Location, Type = STokenType.PreProcessor, Value = Now.Value
                    });
                    Current++;
                    continue;
                }
                if (Now.Type == TokenType.Break)
                {
                    Current++;
                    continue;
                }
                Res.Errors.Add(new Error {
                    Code = 2, Value = "Unexpected Token (" + Now.Type + ":" + Now.Value + ")", Location = Now.Location
                });
                break;
            }
        }
Пример #4
0
        /// <summary>
        /// Parse the instruction.
        /// </summary>
        bool Instruction()
        {
            if (Now.Type == STokenType.Block)
            {
                if (Now.Value == "open")
                {
                    if (inIBlock > 0)
                    {
                        return(false);
                    }
                    IBlock_current = new IBlock();
                    // Make sure that all of them are empty.
                    IBlock_current.Func          = new Execute();
                    IBlock_current.Func.Function = "_ !block";
                    IBlock_current.Op            = new Operator();
                    IBlock_current.Var           = new Variable();
                    inIBlock++;
                    Current++;
                    return(true);
                }
                if (Now.Value == "close")
                {
                    if (inIBlock < 0)
                    {
                        return(false);
                    }
                    Function_current.Instructions.Add(IBlock_current);
                    inIBlock--;
                    Current++;
                    return(true);
                }
            }
            if (Now.Type == STokenType.Arguments)
            {
                if (needArgs)
                {
                    Instruction_current.Func.Arguments = parseArguments(Now);
                    if (inIBlock == 0)
                    {
                        Function_current.Instructions.Add(Instruction_current);
                    }
                    else
                    {
                        IBlock_current.List.Add(Instruction_current);
                    }
                    needArgs = false;
                    Current++;
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            if (Now.Type == STokenType.Instruction)
            {
                needArgs = true;
                Token    Tfunc = new Token();
                Token    Top   = new Token();
                Variable Tvar  = new Variable();

                bool Lvar  = false;
                bool Lop   = false;
                bool Lfunc = false;

                foreach (Token x in Now.Instruction)
                {
                    if (x.Type == TokenType.Operator)
                    {
                        if (!Lop)
                        {
                            Top = x;
                            Lop = true;
                            continue;
                        }
                    }
                    if (x.Type == TokenType.Identifier)
                    {
                        if (!Lfunc)
                        {
                            Tfunc = x;
                            Lfunc = true;
                            continue;
                        }
                    }
                    if (x.Type == TokenType.Variable)
                    {
                        if (!Lvar)
                        {
                            Tvar = new Variable {
                                Name = x.Value
                            };
                            Lvar = true;
                            continue;
                        }
                    }
                    if (x.Type == TokenType.Number)
                    {
                        if (!Lvar)
                        {
                            Tvar = new Variable {
                                Name = x.Value, isNum = true, isConst = true
                            };
                            Lvar = true;
                            continue;
                        }
                    }
                    if (x.Type == TokenType.String)
                    {
                        if (!Lvar)
                        {
                            Tvar = new Variable {
                                Name = x.Value, isConst = true
                            };
                            Lvar = true;
                            continue;
                        }
                    }
                    Res.Errors.Add(new Error {
                        Code     = 5,
                        Value    = "Unexpected Token (" + Now.Type + ":" + Now.Value + ")",
                        Location = Now.Location
                    });
                    break;
                }
                string fNamespace = "";
                string fFunction  = "";

                string[] tc = Tfunc.Value?.Split('.');
                fFunction = tc [tc.Length - 1];
                for (int f = 0; f < tc.Length - 1; f++)
                {
                    fNamespace = tc[f] + ".";
                }
                if (fNamespace.Length > 1)
                {
                    fNamespace = fNamespace.Substring(0, fNamespace.Length - 1);
                }
                else
                {
                    fNamespace = Namespace_current.Name;
                }

                Execute func = new Execute()
                {
                    Function  = fFunction,
                    Namespace = fNamespace,
                    Arguments = new Args()
                    {
                        Arguments = new List <Variable>()
                    }
                };

                Operator op = new Operator()
                {
                    Type = Top.Value,
                };

                Instruction_current = new Instruction
                {
                    Func = func,
                    Op   = op,
                    Var  = Tvar,
                };
                Current++;
                return(true);
            }
            if (Now.Type == STokenType.Return)
            {
                Function_current.Ret = new Return {
                    Type  = Now.Instruction [0].Type,
                    Value = Now.Instruction [0].Value
                };
                while (!(Now.Type == STokenType.Block && Now.Value == "close"))
                {
                    Now = Input [++Current];
                }
                return(true);
            }
            return(false);
        }
Пример #5
0
 /// <summary>
 /// Parse this instance.
 /// </summary>
 void Parse()
 {
     while (Current < Input.Count)
     {
         Now = Input [Current];
         if (Now.Type == STokenType.PreProcessor)
         {
             PreProcessor.Add(Now.Value);
             Current++;
             continue;
         }
         if (Now.Type == STokenType.Block && Now.Value == "close" && (inIBlock == 0))
         {
             if (inFunctionBlock)
             {
                 inFunctionBlock = false;
                 inFunction      = false;
                 Current++;
                 Namespace_current.Functions.Add(Function_current);
                 continue;
             }
             else
             {
                 Namespaces.Add(Namespace_current);
                 inNamespace      = false;
                 inNamespaceBlock = false;
                 Current++;
                 continue;
             }
         }
         if (!inNamespace)
         {
             if (CheckNamespace())
             {
                 continue;
             }
         }
         else
         {
             if (!inNamespaceBlock)
             {
                 if (Now.Type == STokenType.Block && Now.Value == "open")
                 {
                     Now = Input [++Current];
                     inNamespaceBlock = true;
                     continue;
                 }
             }
             else
             {
                 if (!inFunction)
                 {
                     if (CheckFunction())
                     {
                         continue;
                     }
                 }
                 else
                 {
                     if (!inFunctionBlock)
                     {
                         if (Now.Type == STokenType.Block && Now.Value == "open")
                         {
                             Now             = Input [++Current];
                             inFunctionBlock = true;
                             continue;
                         }
                     }
                     else
                     {
                         if (Now.Type == STokenType.Arguments || Now.Type == STokenType.Block || Now.Type == STokenType.Instruction || Now.Type == STokenType.Return)
                         {
                             if (Instruction())
                             {
                                 continue;
                             }
                         }
                     }
                 }
             }
         }
         Res.Errors.Add(new Error {
             Code = 4, Value = "Unexpected Token (" + Now.Type + ":" + Now.Value + ")", Location = Now.Location
         });
         break;
     }
     Tree.Namespaces   = Namespaces;
     Tree.Preprocessor = PreProcessor;
 }