Example #1
0
        public Node ParameterDeclaration()
        {
            var result = new ParameterDeclaration();

            var identifierList = new IdentifierList()
            {
                new Identifier()
                {
                    AnchorToken = Expect(TokenCategory.IDENTIFIER)
                }
            };

            result.Add(identifierList);

            while (CurrentToken == TokenCategory.COMA)
            {
                Expect(TokenCategory.COMA);
                identifierList.Add(new Identifier()
                {
                    AnchorToken = Expect(TokenCategory.IDENTIFIER)
                });
            }

            Expect(TokenCategory.COLON);
            result.Add(Type());
            Expect(TokenCategory.SEMICOLON);

            return(result);
        }
Example #2
0
        public Node ParamDeclaration()
        {
            var result = new ParamDeclaration();
            var idList = new IdentifierList();

            idList.Add(new Identifier()
            {
                AnchorToken = Expect(TokenCategory.IDENTIFIER)
            });
            while (CurrentToken != TokenCategory.TWOPOINTS)
            {
                Expect(TokenCategory.COMA);
                idList.Add(new Identifier()
                {
                    AnchorToken = Expect(TokenCategory.IDENTIFIER)
                });
            }
            result.Add(idList);
            Expect(TokenCategory.TWOPOINTS);
            result.Add(ChimeraType());
            Expect(TokenCategory.SEMICOL);
            return(result);
        }
Example #3
0
        Lexem Identifier(ref int row, string substring)
        {
            Idnt tempIdnt = IdentifierList.Find(x => x.Name == substring);

            if (tempIdnt != null)
            {
                LexemList.Add(new Lexem(row, tempIdnt.Name, 34, indexIdnt: tempIdnt.Index)); return(null);
            }
            else
            {
                if (LexemList.Count == 0)
                {
                    throw new Exception("Identifier can`t be first");
                }

                Lexem temp = LexemList.Last <Lexem>();
                if (temp.Code == 30)
                {
                    IdentifierList.Add(new Idnt(substring, IdentifierList.Count, "int"));
                }
                else if (temp.Code == 31)
                {
                    IdentifierList.Add(new Idnt(substring, IdentifierList.Count, "float"));
                }
                else if (temp.Code == 0)
                {
                    IdentifierList.Add(new Idnt(substring, IdentifierList.Count, "program"));
                }
                else
                {
                    throw new Exception($"Opps,I see a problem.Maybe you forgot about type 'int', 'float'or 'program' type in row {row} near '{substring}'");
                }
            }
            LexemList.Add(new Lexem(row, substring, 34, indexIdnt: IdentifierList.Count - 1));
            return(null);
        }
Example #4
0
        private void ParserLine(List <LexicalUnit> lexUnitInRowList)
        {
            int   tempInt;
            float tempFloat;

            foreach (var unit in lexUnitInRowList)
            {
                //if we have this lexem in reserved lexem
                tempInt = Check.IsReservedLexem(unit.Substring);
                if (tempInt >= 0)
                {
                    LexemList.Add(new Lexem(unit.Row, unit.Substring, tempInt));
                    continue;
                }

                //if this lexem is a constant
                if (float.TryParse(unit.Substring.Replace('.', ','), out tempFloat))
                {
                    if (int.TryParse(unit.Substring, out tempInt))
                    {
                        var const_ = ConstantList.Find(x => x._Const == tempInt);

                        if (const_ != null)
                        {
                            LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: const_.Index));
                        }
                        else
                        {
                            ConstantList.Add(new Const(tempInt, ConstantList.Count, "int"));
                            LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: ConstantList.Count - 1));
                        }
                    }
                    else
                    {
                        var const_ = ConstantList.Find(x => x._Const == tempInt);

                        if (const_ != null)
                        {
                            LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: const_.Index));
                        }
                        else
                        {
                            ConstantList.Add(new Const(tempInt, ConstantList.Count, "float"));
                            LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: ConstantList.Count - 1));
                        }
                    }
                    continue;
                }

                //if this lexem is a idnt;
                Idnt tempIdnt = IdentifierList.Find(x => x.Name == unit.Substring);
                if (tempIdnt != null)
                {
                    LexemList.Add(new Lexem(unit.Row, tempIdnt.Name, 35, indexIdnt: tempIdnt.Index));
                }
                else
                {
                    if (lexUnitInRowList.Exists(x => (x.Substring.Contains("float"))) &&
                        (lexUnitInRowList.Exists(x => x.Substring.Contains("="))))
                    {
                        IdentifierList.Add(new Idnt(unit.Substring, IdentifierList.Count, "float"));
                        LexemList.Add(new Lexem(unit.Row, unit.Substring, 35, indexIdnt: IdentifierList.Count - 1));
                    }


                    else if (lexUnitInRowList.Exists(x => (x.Substring.Contains("int"))) &&
                             (lexUnitInRowList.Exists(x => x.Substring.Contains("="))))
                    {
                        IdentifierList.Add(new Idnt(unit.Substring, IdentifierList.Count, "int"));
                        LexemList.Add(new Lexem(unit.Row, unit.Substring, 35, indexIdnt: IdentifierList.Count - 1));
                    }


                    else if ((lexUnitInRowList.Exists(x => x.Substring.Contains("program"))))
                    {
                        IdentifierList.Add(new Idnt(unit.Substring, IdentifierList.Count, "program"));
                        LexemList.Add(new Lexem(unit.Row, unit.Substring, 35, indexIdnt: IdentifierList.Count - 1));
                    }

                    else
                    {
                        throw new Exception($"not defined variable {unit.Substring} ");
                    }
                }
            }
        }