Beispiel #1
0
        int AddConstant(float constant, string type)
        {
            Const tempConst = ConstantList.Find(x => x._Const == constant);

            if (tempConst == null)
            {
                ConstantList.Add(new Const(constant, ConstantList.Count, type));
                return(ConstantList.Count - 1);
            }
            else
            {
                return(tempConst.Index);
            }
        }
Beispiel #2
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} ");
                    }
                }
            }
        }