Пример #1
0
        public override void Visit(StructDcel n)
        {
            SymReferencing sym = n.structType as SymReferencing;

            Code = Code.Remove(Code.Length - $"struct {sym.id} = ".Length);
            n.structType.accept(this);
            emit(" ");
            n.structId.accept(this);
            emit("{");
            if (n.declarings.Count > 0)
            {
                AST first = n.declarings[0];
                foreach (AST ast in n.declarings)
                {
                    if (first != ast)
                    {
                        emit(", ");
                    }
                    Assigning assigning = ast as Assigning;
                    assigning.child.accept(this);
                    //Code = Code.Remove(Code.Length - 2);
                }
            }
            emit("}");
        }
Пример #2
0
 public override void Visit(StructDcel n)
 {
     foreach (AST ast in n.declarings)
     {
         ast.accept(this);
     }
 }
Пример #3
0
        public override void Visit(StructDcel n)
        {
            string pastStructType = "";

            if (currentStructType != "")
            {
                pastStructType = currentStructType;
            }
            SymReferencing current = n.structId as SymReferencing;

            currentStructType = current.id;
            foreach (AST ast in n.declarings)
            {
                ast.accept(this);
            }
            currentStructType = pastStructType;
        }
Пример #4
0
 public override void Visit(StructDcel n)
 {
     emit("{");
     if (n.declarings.Count > 0)
     {
         AST first = n.declarings[0];
         foreach (AST ast in n.declarings)
         {
             if (first != ast)
             {
                 emit(", ");
             }
             ast.accept(this);
         }
     }
     emit("}\n");
 }
Пример #5
0
        public override void Visit(Assigning n)
        {
            n.child.accept(this);
            int            m       = -99;
            SymReferencing current = n.id as SymReferencing;

            if (currentStructType != "")
            {
                if (StructDic[currentStructType].Exists(x => x.Item1 == current.id))
                {
                    m = StructDic[currentStructType].Find(x => x.Item1 == current.id).Item2;
                }
                else
                {
                    Error($"{n.id} doesn't exist in {currentStructType}");
                }
            }
            else if (n.id is DotReferencing || n.id is ListReferencing)
            {
                n.id.accept(this);
                m = n.id.type;
            }
            else
            {
                m = AST.SymbolTable[GetKeyVal(current.id)];
            }
            int t = Generalize(n.child.type, m);

            n.child = Convert(n.child, m);
            n.type  = t;
            if (n.child is StructDcel)
            {
                StructDcel     assStructDcel = n.child as StructDcel;
                SymReferencing strucid       = assStructDcel.structId as SymReferencing;
                SymReferencing structype     = assStructDcel.structType as SymReferencing;
                StructDic.Add(strucid.id, StructDic[structype.id]);
            }
        }
Пример #6
0
        public void ASTTestMethod()
        {
            string  code    = "TestCode.txt";
            Scanner scanner = new Scanner(code);
            Parser  parser  = new Parser(scanner);

            parser.Parse();
            Prog actual = parser.ProgramAST;

            var structDef6 = new StructDef(new List <AST>()
            {
                new IntDcl("pinPower"), new FuncDecl(new VoidDcl("Power"), new List <SymDeclaring>()
                {
                    new IntDcl("value")
                }, new List <AST>()
                {
                    new Assigning(new SymReferencing("pinPower"), new SymReferencing("value"))
                }, null)
            });

            structDef6.structType = new SymReferencing("pin");

            var structDcel7 = new StructDcel(new SymReferencing("pin"), new List <AST>()
            {
                new Assigning(new SymReferencing("pinPower"), new IntConst("50"))
            });

            structDcel7.structId = new SymReferencing("led");

            Prog expected = new Prog(new List <AST>()
            {
                new Decl(new IntDcl("a"), new Assigning(new SymReferencing("a"), new IntConst("4"))),                                //int a = 4;
                new Decl(new FloatDcl("f"), new Assigning(new SymReferencing("f"), new FloatConst("2.5"))),                          //float f = 2.5;
                new Decl(new BooleanDcl("asd"), new Assigning(new SymReferencing("asd"), new BooleanConst("TRUE"))),                 //boolean asd = TRUE;
                new Decl(new StringDcl("text"), new Assigning(new SymReferencing("text"), new StringConst("\"th8is a 2 test !\""))), //string text = "th8is a 2 test !";
                new Decl(new ListDcl("testList", new ListDcl(new StringDcl())), new Assigning(new SymReferencing("testList"), new ListConst(new List <AST>()
                {
                    new ListConst(new List <AST>()
                    {
                        new SymReferencing("text")
                    }), new ListConst(new List <AST>()
                    {
                        new StringConst("\"list\"")
                    }), new ListConst(new List <AST>()
                    {
                        new StringConst("\"thing\"")
                    })
                }))),                                                                                                                                                                                                                                                                                                                                              //List<List<string>> testList = {{text}, {"list"}, {"thing"}};
                new Decl(new StringDcl("testlisty"), new Assigning(new SymReferencing("testlisty"), new ListReferencing(new SymReferencing("testList"), new List <AST>()
                {
                    new Expression("-", new IntConst("1"), new IntConst("1")), new IntConst("1")
                }))),                                                                                                                                                       //string testlisty = testList[1 - 1][OFF];
                new Decl(new StructDcl("pin"), new Assigning(new SymReferencing("pin"), structDef6)),
                new Decl(new StructDcl("led"), new Assigning(new SymReferencing("led"), structDcel7)),                                                                      //struct pin led{pinPower = 50};
                new Decl(new IntDcl("ledPin"), new Assigning(new SymReferencing("ledPin"), new DotReferencing(new SymReferencing("led"), new SymReferencing("pinPower")))), // int ledPin = led.pinNum;
                new ProgSetup(new List <AST>()
                {
                    new Assigning(new DotReferencing(new SymReferencing("led"), new SymReferencing("pinPower")), new IntConst("100")),
                    new Decl(new IntDcl("b"), new Assigning(new SymReferencing("b"), new Expression("+", new IntConst("4"), new Expression("+", new Expression("*", new IntConst("2"), new IntConst("5")), new Expression("%", new Expression("-", new IntConst("2"), new Expression("-", new Expression("/", new IntConst("1"), new IntConst("4")), new IntConst("5"))), new IntConst("5")))))),
                    new ForStmt(new Decl(new IntDcl("i"), new Assigning(new SymReferencing("i"), new IntConst("0"))), new LogiExpression("<=", new SymReferencing("i"), new IntConst("10")), new Assigning(new SymReferencing("i"), new Expression("+", new SymReferencing("i"), new IntConst("1"))), new List <AST>()
                    {
                        new Assigning(new SymReferencing("f"), new Expression("+", new SymReferencing("f"), new IntConst("3")))
                    })
                }),
                new ProgLoop(new List <AST>()
                {
                    new Decl(new IntDcl("q"), new Assigning(new SymReferencing("q"), new IntConst("1"))),
                    new Decl(new FloatDcl("b"), new Assigning(new SymReferencing("b"), new FloatConst("3"))),
                    new Assigning(new SymReferencing("a"), new Expression("+", new SymReferencing("a"), new FunctionStmt(new SymReferencing("test"), new List <AST>()
                    {
                        new SymReferencing("q"), new SymReferencing("b")
                    })))
                }),
                new FuncDecl(new IntDcl("test"), new List <SymDeclaring>()
                {
                    new IntDcl("numi"), new FloatDcl("numf")
                }, new List <AST>()
                {
                    new Decl(new IntDcl("ffs"), new Assigning(new SymReferencing("ffs"), new FloatConst("1337")))
                }, new Expression("+", new SymReferencing("ffs"), new SymReferencing("numi")))
            });

            Assert.IsTrue(parser.errors.count == 0, "Parser encountered errors");
            Assert.IsTrue(ObjectCompare(expected, actual), "Parsing faild");
        }
Пример #7
0
        public void AssigningUnitTest()
        {
            try {
                AST.SymbolTable =
                    new Dictionary <Tuple <string, string>, int>()
                {
                    { new Tuple <string, string>("1", "text"), AST.STRING },
                    { new Tuple <string, string>("1", "testList"), 664 },
                    { new Tuple <string, string>("1", "pin"), AST.STRUCT },
                    { new Tuple <string, string>("11", "pinPower"), AST.INTTYPE },
                    { new Tuple <string, string>("111", "value"), AST.INTTYPE }
                };
                Dictionary <string, List <Tuple <string, int> > > structDic =
                    new Dictionary <string, List <Tuple <string, int> > >()
                {
                    { "pin", new List <Tuple <string, int> >()
                      {
                          new Tuple <string, int>("pinPower", AST.INTTYPE),
                          new Tuple <string, int>("Power", AST.VOID)
                      } },
                    { "led", new List <Tuple <string, int> >()
                      {
                          new Tuple <string, int>("pinPower", AST.INTTYPE),
                          new Tuple <string, int>("Power", AST.VOID)
                      } }
                };
                InitiationChecker initiationChecker = new InitiationChecker(structDic);

                Assigning assigning = new Assigning(new SymReferencing("text"), new StringConst("4"));
                assigning.accept(initiationChecker);

                Dictionary <Tuple <string, string>, int>          actualSym    = initiationChecker.getInitiationTable;
                Dictionary <string, List <Tuple <string, int> > > actualStruct = initiationChecker.getStructDic;
                Dictionary <Tuple <string, string>, int>          expectedSym  =
                    new Dictionary <Tuple <string, string>, int>()
                {
                    { new Tuple <string, string>("1", "text"), 1 },
                    { new Tuple <string, string>("1", "testList"), 0 },
                    { new Tuple <string, string>("1", "pin"), 0 },
                    { new Tuple <string, string>("11", "pinPower"), 0 },
                    { new Tuple <string, string>("111", "value"), 0 }
                };
                Dictionary <string, List <Tuple <string, int> > > expectedStruct =
                    new Dictionary <string, List <Tuple <string, int> > >()
                {
                    { "pin", new List <Tuple <string, int> >()
                      {
                          new Tuple <string, int>("pinPower", 0),
                          new Tuple <string, int>("Power", 0)
                      } },
                    { "led", new List <Tuple <string, int> >()
                      {
                          new Tuple <string, int>("pinPower", 0),
                          new Tuple <string, int>("Power", 0)
                      } }
                };
                Assert.IsTrue(ObjectCompare(actualSym, expectedSym) && ObjectCompare(actualStruct, expectedStruct), "Simple InitiationCheck faild");

                assigning = new Assigning(new SymReferencing("testList"), new ListConst(new List <AST>()
                {
                    new ListConst(new List <AST>()
                    {
                        new SymReferencing("text")
                    }), new ListConst(new List <AST>()
                    {
                        new StringConst("\"list\"")
                    }), new ListConst(new List <AST>()
                    {
                        new StringConst("\"thing\"")
                    })
                }));
                assigning.accept(initiationChecker);

                actualSym    = initiationChecker.getInitiationTable;
                actualStruct = initiationChecker.getStructDic;
                expectedSym  =
                    new Dictionary <Tuple <string, string>, int>()
                {
                    { new Tuple <string, string>("1", "text"), 1 },
                    { new Tuple <string, string>("1", "testList"), 1 },
                    { new Tuple <string, string>("1", "pin"), 0 },
                    { new Tuple <string, string>("11", "pinPower"), 0 },
                    { new Tuple <string, string>("111", "value"), 0 }
                };
                expectedStruct =
                    new Dictionary <string, List <Tuple <string, int> > >()
                {
                    { "pin", new List <Tuple <string, int> >()
                      {
                          new Tuple <string, int>("pinPower", 0),
                          new Tuple <string, int>("Power", 0)
                      } },
                    { "led", new List <Tuple <string, int> >()
                      {
                          new Tuple <string, int>("pinPower", 0),
                          new Tuple <string, int>("Power", 0)
                      } }
                };
                Assert.IsTrue(ObjectCompare(actualSym, expectedSym) && ObjectCompare(actualStruct, expectedStruct), "List InitiationCheck faild");

                var structDef = new StructDef(new List <AST>()
                {
                    new IntDcl("pinPower"), new FuncDecl(new VoidDcl("Power"), new List <SymDeclaring>()
                    {
                        new IntDcl("value")
                    }, new List <AST>()
                    {
                        new Assigning(new SymReferencing("pinPower"), new SymReferencing("value"))
                    }, null)
                });
                structDef.structType = new SymReferencing("pin");
                assigning            = new Assigning(new SymReferencing("pin"), structDef);
                assigning.accept(initiationChecker);

                actualSym    = initiationChecker.getInitiationTable;
                actualStruct = initiationChecker.getStructDic;
                expectedSym  =
                    new Dictionary <Tuple <string, string>, int>()
                {
                    { new Tuple <string, string>("1", "text"), 1 },
                    { new Tuple <string, string>("1", "testList"), 1 },
                    { new Tuple <string, string>("1", "pin"), 1 },
                    { new Tuple <string, string>("11", "pinPower"), 1 },
                    { new Tuple <string, string>("111", "value"), 1 },
                    { new Tuple <string, string>("11", "Power"), 1 },
                    { new Tuple <string, string>("111", "pinPower"), 1 }
                };
                expectedStruct =
                    new Dictionary <string, List <Tuple <string, int> > >()
                {
                    { "led", new List <Tuple <string, int> >()
                      {
                          new Tuple <string, int>("pinPower", 0),
                          new Tuple <string, int>("Power", 0)
                      } }
                };
                Assert.IsTrue(ObjectCompare(actualSym, expectedSym) && ObjectCompare(actualStruct, expectedStruct), "StructDef InitiationCheck faild");

                var structDcel = new StructDcel(new SymReferencing("pin"), new List <AST>()
                {
                    new Assigning(new SymReferencing("pinPower"), new IntConst("50"))
                });
                structDcel.structId = new SymReferencing("led");
                assigning           = new Assigning(new SymReferencing("led"), structDcel);
                assigning.accept(initiationChecker);

                actualSym    = initiationChecker.getInitiationTable;
                actualStruct = initiationChecker.getStructDic;
                expectedSym  =
                    new Dictionary <Tuple <string, string>, int>()
                {
                    { new Tuple <string, string>("1", "text"), 1 },
                    { new Tuple <string, string>("1", "testList"), 1 },
                    { new Tuple <string, string>("1", "pin"), 1 },
                    { new Tuple <string, string>("11", "pinPower"), 1 },
                    { new Tuple <string, string>("111", "value"), 1 },
                    { new Tuple <string, string>("11", "Power"), 1 },
                    { new Tuple <string, string>("111", "pinPower"), 1 },
                    { new Tuple <string, string>("1", "led"), 1 }
                };
                expectedStruct =
                    new Dictionary <string, List <Tuple <string, int> > >()
                {
                    { "led", new List <Tuple <string, int> >()
                      {
                          new Tuple <string, int>("Power", 0),
                          new Tuple <string, int>("pinPower", 1)
                      } }
                };
                Assert.IsTrue(ObjectCompare(actualSym, expectedSym) && ObjectCompare(actualStruct, expectedStruct), "StructDcel InitiationCheck faild");
            } finally {
                AST.SymbolTable.Clear();
            }
        }