Ejemplo n.º 1
0
        private static void ParseField(Lexer.Lexer lexer, out Exp k, out Exp v)
        {
            k = null;
            v = null;

            if (lexer.LookAhead() == ETokenType.SepLBracket)
            {
                lexer.NextToken(out _, out _, out _);
                k = ParseExp(lexer);
                lexer.NextTokenOfKind(ETokenType.SepRBracket, out _, out _);
                lexer.NextTokenOfKind(ETokenType.OpAssign, out _, out _);
                v = ParseExp(lexer);
                return;
            }

            var exp = ParseExp(lexer);

            if (exp is NameExp nameExp)
            {
                if (lexer.LookAhead() == ETokenType.OpAssign)
                {
                    lexer.NextToken(out _, out _, out _);
                    k = new StringExp {
                        Line = nameExp.Line, Str = nameExp.Name
                    };
                    v = ParseExp(lexer);
                    return;
                }
            }

            v = exp;
        }
Ejemplo n.º 2
0
        private static Stat ParseIfStat(Lexer.Lexer lexer)
        {
            var exps   = new List <Exp>();
            var blocks = new List <Block>();

            lexer.NextTokenOfKind(ETokenType.KwIf, out _, out _);
            exps.Add(ParseExp(lexer));

            lexer.NextTokenOfKind(ETokenType.KwThen, out _, out _);
            blocks.Add(ParseBlock(lexer));

            while (lexer.LookAhead() == ETokenType.KwElseIf)
            {
                lexer.NextToken(out _, out _, out _);
                exps.Add(ParseExp(lexer));
                lexer.NextTokenOfKind(ETokenType.KwThen, out _, out _);
                blocks.Add(ParseBlock(lexer));
            }

            if (lexer.LookAhead() == ETokenType.KwElse)
            {
                lexer.NextToken(out _, out _, out _);
                exps.Add(new TrueExp {
                    Line = lexer.Line
                });
                blocks.Add(ParseBlock(lexer));
            }

            lexer.NextTokenOfKind(ETokenType.KwEnd, out _, out _);
            return(new IfStat {
                Exps = exps, Blocks = blocks
            });
        }
Ejemplo n.º 3
0
        private static List<Exp> ParseRetExps(Lexer.Lexer lexer)
        {
            if (lexer.LookAhead() != ETokenType.KwReturn)
                return null;

            lexer.NextToken(out _, out _, out _);

            var ret = new List<Exp>();

            switch (lexer.LookAhead())
            {
                case ETokenType.Eof:
                case ETokenType.KwEnd:
                case ETokenType.KwElse:
                case ETokenType.KwElseIf:
                case ETokenType.KwUntil:
                    return ret;

                case ETokenType.SepSemi:
                    lexer.NextToken(out _, out _, out _);
                    return ret;

                default:
                    var exps = ParseExpList(lexer);
                    if (lexer.LookAhead() == ETokenType.SepSemi)
                        lexer.NextToken(out _, out _, out _);
                    return exps;
            }
        }
Ejemplo n.º 4
0
        private static Exp FinishPrefixExp(Lexer.Lexer lexer, Exp exp)
        {
            while (true)
            {
                switch (lexer.LookAhead())
                {
                case ETokenType.SepLBracket:
                {
                    lexer.NextToken(out _, out _, out _);
                    var keyExp = ParseExp(lexer);
                    lexer.NextTokenOfKind(ETokenType.SepRBracket, out _, out _);
                    exp = new TableAccessExp
                    {
                        LastLine  = lexer.Line,
                        PrefixExp = exp,
                        KeyExp    = keyExp
                    };
                    break;
                }

                case ETokenType.SepDot:
                {
                    lexer.NextToken(out _, out _, out _);
                    lexer.NextIdentifier(out var line, out var name);
                    var keyExp = new StringExp
                    {
                        Line = line,
                        Str  = name
                    };
                    exp = new TableAccessExp
                    {
                        LastLine  = line,
                        PrefixExp = exp,
                        KeyExp    = keyExp
                    };
                    break;
                }

                case ETokenType.SepColon:
                case ETokenType.SepLParen:
                case ETokenType.SepLCurly:
                case ETokenType.String:
                {
                    exp = FinishFuncCallExp(lexer, exp);
                    break;
                }

                default:
                    return(exp);
                }
            }
        }
Ejemplo n.º 5
0
        private static Exp ParseExp5(Lexer.Lexer lexer)
        {
            var exp = ParseExp4(lexer);

            if (lexer.LookAhead() != ETokenType.OpConcat)
            {
                return(exp);
            }

            var line = 0;
            var exps = new List <Exp> {
                exp
            };

            while (lexer.LookAhead() == ETokenType.OpConcat)
            {
                lexer.NextToken(out line, out _, out _);
                exps.Add(ParseExp4(lexer));
            }

            return(new ConcatExp
            {
                Line = line,
                Exps = exps
            });
        }
Ejemplo n.º 6
0
        private static void ParseFieldList(Lexer.Lexer lexer, out List <Exp> ks, out List <Exp> vs)
        {
            ks = null;
            vs = null;

            if (lexer.LookAhead() != ETokenType.SepRCurly)
            {
                ParseField(lexer, out var k, out var v);

                ks = new List <Exp> {
                    k
                };
                vs = new List <Exp> {
                    v
                };

                while (IsFieldSep(lexer.LookAhead()))
                {
                    lexer.NextToken(out _, out _, out _);
                    if (lexer.LookAhead() != ETokenType.SepRCurly)
                    {
                        ParseField(lexer, out k, out v);
                        ks.Add(k);
                        vs.Add(v);
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public void Init()
        {
            var token = lexer.NextToken();

            Console.WriteLine("First token");
            Console.WriteLine(token.TokenType);
            Console.WriteLine(token.Value);
        }
Ejemplo n.º 8
0
        private static void ParseFuncName(Lexer.Lexer lexer, out Exp exp, out bool hasColon)
        {
            hasColon = false;
            lexer.NextIdentifier(out var line, out var name);
            exp = new NameExp {
                Line = line, Name = name
            };

            while (lexer.LookAhead() == ETokenType.SepDot)
            {
                lexer.NextToken(out _, out _, out _);
                lexer.NextIdentifier(out line, out name);
                var idx = new StringExp {
                    Line = line, Str = name
                };
                exp = new TableAccessExp
                {
                    LastLine  = line,
                    KeyExp    = idx,
                    PrefixExp = exp,
                };
            }

            if (lexer.LookAhead() == ETokenType.SepColon)
            {
                lexer.NextToken(out _, out _, out _);
                lexer.NextIdentifier(out line, out name);
                var idx = new StringExp {
                    Line = line, Str = name
                };
                exp = new TableAccessExp
                {
                    LastLine  = line,
                    PrefixExp = exp,
                    KeyExp    = idx,
                };
                hasColon = true;
            }
        }
Ejemplo n.º 9
0
        private static List <Exp> ParseExpList(Lexer.Lexer lexer)
        {
            var exps = new List <Exp> {
                ParseExp(lexer)
            };

            while (lexer.LookAhead() == ETokenType.SepComma)
            {
                lexer.NextToken(out _, out _, out _);
                exps.Add(ParseExp(lexer));
            }

            return(exps);
        }
Ejemplo n.º 10
0
        private static void ParseParList(Lexer.Lexer lexer, out List <string> parList, out bool isVararg)
        {
            parList  = new List <string>();
            isVararg = false;

            switch (lexer.LookAhead())
            {
            case ETokenType.SepRParen:
                return;

            case ETokenType.Vararg:
            {
                lexer.NextToken(out _, out _, out _);
                isVararg = true;
                return;
            }
            }

            lexer.NextIdentifier(out _, out var name);
            parList.Add(name);

            while (lexer.LookAhead() == ETokenType.SepComma)
            {
                lexer.NextToken(out _, out _, out _);
                if (lexer.LookAhead() == ETokenType.Identifier)
                {
                    lexer.NextIdentifier(out _, out name);
                    parList.Add(name);
                }
                else
                {
                    lexer.NextTokenOfKind(ETokenType.Vararg, out _, out _);
                    isVararg = true;
                    break;
                }
            }
        }
Ejemplo n.º 11
0
        private static List <string> FinishNameList(Lexer.Lexer lexer, string name)
        {
            var names = new List <string> {
                name
            };

            while (lexer.LookAhead() == ETokenType.SepComma)
            {
                lexer.NextToken(out _, out _, out _);
                lexer.NextIdentifier(out _, out var tokenName);
                names.Add(tokenName);
            }

            return(names);
        }
Ejemplo n.º 12
0
        private static List <Exp> FinishVarList(Lexer.Lexer lexer, Exp prefixExp)
        {
            var vars = new List <Exp> {
                CheckVar(lexer, prefixExp)
            };

            while (lexer.LookAhead() == ETokenType.SepComma)
            {
                lexer.NextToken(out _, out _, out _);
                var exp = ParsePrefixExp(lexer);
                vars.Add(CheckVar(lexer, exp));
            }

            return(vars);
        }
Ejemplo n.º 13
0
        private static StringExp ParseNameExp(Lexer.Lexer lexer)
        {
            if (lexer.LookAhead() == ETokenType.SepColon)
            {
                lexer.NextToken(out _, out _, out _);

                lexer.NextIdentifier(out var line, out var name);
                return(new StringExp
                {
                    Line = line,
                    Str = name,
                });
            }

            return(null);
        }
Ejemplo n.º 14
0
        private static List <Exp> ParseArgs(Lexer.Lexer lexer)
        {
            List <Exp> ret = null;

            switch (lexer.LookAhead())
            {
            case ETokenType.SepLParen:
            {
                lexer.NextToken(out _, out _, out _);
                if (lexer.LookAhead() != ETokenType.SepRParen)
                {
                    ret = ParseExpList(lexer);
                }

                lexer.NextTokenOfKind(ETokenType.SepRParen, out _, out _);
                break;
            }

            case ETokenType.SepLCurly:
            {
                ret = new List <Exp> {
                    ParseTableConstructorExp(lexer)
                };
                break;
            }

            default:
            {
                lexer.NextTokenOfKind(ETokenType.String, out var line, out var str);
                ret = new List <Exp>
                {
                    new StringExp
                    {
                        Line = line,
                        Str  = str
                    }
                };
                break;
            }
            }

            return(ret);
        }
Ejemplo n.º 15
0
        private static Stat FinishLocalVarDeclStat(Lexer.Lexer lexer)
        {
            lexer.NextIdentifier(out _, out var name0);
            var        nameList = FinishNameList(lexer, name0);
            List <Exp> expList  = null;

            if (lexer.LookAhead() == ETokenType.OpAssign)
            {
                lexer.NextToken(out _, out _, out _);
                expList = ParseExpList(lexer);
            }

            var lastLine = lexer.Line;

            return(new LocalVarDeclStat
            {
                ExpList = expList,
                LastLine = lastLine,
                NameList = nameList
            });
        }
Ejemplo n.º 16
0
        public static void Start()
        {
            while (true)
            {
                Console.Write(prompt);
                string input = Console.ReadLine();

                if (input == string.Empty)
                {
                    return;
                }

                var l = new Lexer.Lexer(input);

                Token token;

                do
                {
                    token = l.NextToken();
                    Console.WriteLine(token);
                } while (token.Type.Equals(TokenType.EOF) == false);
            }
        }
Ejemplo n.º 17
0
        private static Stat FinishForNumStat(Lexer.Lexer lexer, int lineOfToken, string name)
        {
            lexer.NextTokenOfKind(ETokenType.OpAssign, out _, out _);
            var initExp = ParseExp(lexer);

            lexer.NextTokenOfKind(ETokenType.SepComma, out _, out _);
            var limitExp = ParseExp(lexer);

            Exp stepExp;

            if (lexer.LookAhead() == ETokenType.SepComma)
            {
                lexer.NextToken(out _, out _, out _);
                stepExp = ParseExp(lexer);
            }
            else
            {
                stepExp = new IntegerExp {
                    Line = lexer.Line, Val = 1
                };
            }

            lexer.NextTokenOfKind(ETokenType.KwDo, out var lineOfDo, out _);
            var block = ParseBlock(lexer);

            lexer.NextTokenOfKind(ETokenType.KwEnd, out _, out _);
            return(new ForNumStat
            {
                LineOfFor = lineOfToken,
                LineOfDo = lineOfDo,
                Block = block,
                InitExp = initExp,
                LimitExp = limitExp,
                StepExp = stepExp,
                VarName = name
            });
        }
Ejemplo n.º 18
0
 public void ConsumeNextToken()
 {
     lookahead[lookaheadIndex] = input.NextToken();
     lookaheadIndex            = (lookaheadIndex + 1) % lookaheadCount;
 }
Ejemplo n.º 19
0
 private void NextToken()
 {
     _currentToken = _peekToken;
     _peekToken    = _lexer.NextToken();
 }
Ejemplo n.º 20
0
        private static Exp ParseExp0(Lexer.Lexer lexer)
        {
            switch (lexer.LookAhead())
            {
            case ETokenType.Vararg:
            {
                lexer.NextToken(out var line, out _, out _);
                return(new VarargExp {
                        Line = line
                    });
            }

            case ETokenType.KwNil:
            {
                lexer.NextToken(out var line, out _, out _);
                return(new NilExp {
                        Line = line
                    });
            }

            case ETokenType.KwTrue:
            {
                lexer.NextToken(out var line, out _, out _);
                return(new TrueExp {
                        Line = line
                    });
            }

            case ETokenType.KwFalse:
            {
                lexer.NextToken(out var line, out _, out _);
                return(new FalseExp {
                        Line = line
                    });
            }

            case ETokenType.String:
            {
                lexer.NextToken(out var line, out _, out var token);
                return(new StringExp {
                        Line = line, Str = token
                    });
            }

            case ETokenType.Number:
            {
                return(ParseNumberExp(lexer));
            }

            case ETokenType.SepLCurly:
            {
                return(ParseTableConstructorExp(lexer));
            }

            case ETokenType.KwFunction:
            {
                lexer.NextToken(out _, out _, out _);
                return(ParseFuncDefExp(lexer));
            }

            default:
                return(ParsePrefixExp(lexer));
            }
        }