Exemplo n.º 1
0
        private bool CompileMethodBody(SamFile file, TokenReader tr, SamFileMethod method)
        {
            int level     = 0;
            int softLevel = 0;

            do
            {
                if (tr.Current == "{")
                {
                    level++;
                }
                else if (tr.Current == "}")
                {
                    level--;
                    if (level == 0)
                    {
                        return(true);
                    }
                    else if (level < 0)
                    {
                        return(false);
                    }
                }
                else
                {
                    string value = tr.Current;

                    if (value == ";")
                    {
                        method.Tokens.Add(new EndOfLineToken());
                        continue;
                    }

                    if (IsStringLiteral(value))
                    {
                        // maybe remove quotes.
                        method.Tokens.Add(new StringLiteralToken()
                        {
                            Value = value
                        });
                        continue;
                    }

                    // is a function
                    if (tr.CanMoveNext)
                    {
                        if (tr.Next == "(")
                        {
                            // this is a func call..
                            var callFunc = new CallFuncToken()
                            {
                                FuncName = value
                            };

                            method.Tokens.Add(callFunc);
                            softLevel++;

                            tr.MoveNext();

                            continue;
                        }
                        else if (tr.Next == "=")
                        {
                            // this is a set call..
                            continue;
                        }
                    }

                    if (tr.Current == ")")
                    {
                        method.Tokens.Add(new EndCallFuncToken());

                        softLevel--;
                        if (softLevel < 0)
                        {
                            return(false); // strange, why do we have a closing brace and no open one??
                        }
                    }
                    else if (tr.Current == "==")
                    {
                        method.Tokens.Add(new EqualToken());
                    }
                    else
                    {
                        // just add...

                        method.Tokens.Add(new LoadVariableToken()
                        {
                            Name = tr.Current
                        });
                    }

                    // code..
                }
            } while (tr.MoveNext());

            return(true);
        }