Example #1
0
 public override void Visit(DotReferencing n)   //TODO add support for structs in structs
 {
     n.id.accept(this);
     if (n.id.type != AST.STRUCT)
     {
         Error($"{n.id.id} is not of type struct");
     }
     if (n.dotId is SymReferencing)
     {
         SymReferencing sym = n.dotId as SymReferencing;
         if (!StructDic[n.id.id].Exists(x => x.Item1 == sym.id))
         {
             Error($"{sym.id} doesn't exist in {n.id.id}");
         }
         n.dotId.type = StructDic[n.id.id].Find(x => x.Item1 == sym.id).Item2;
     }
     else if (n.dotId is DotReferencing)
     {
         DotReferencing dot = n.dotId as DotReferencing;
         if (!StructDic[n.id.id].Exists(x => x.Item1 == dot.id.id))
         {
             Error($"{dot.id.id} doesn't exist in {n.id.id}");
         }
         n.dotId.accept(this);
     }
     n.type = n.dotId.type;
 }
Example #2
0
 private string GetLastID(AST node)
 {
     if (node is SymReferencing)
     {
         SymReferencing sym = node as SymReferencing;
         return(sym.id);
     }
     else if (node is DotReferencing)
     {
         DotReferencing dot = node as DotReferencing;
         return(GetLastID(dot.dotId));
     }
     else
     {
         return("");
     }
 }
Example #3
0
        public override void Visit(Assigning n)
        {
            SymReferencing current = n.id as SymReferencing;

            if (currentStructType != "")
            {
                if (StructDic[currentStructType].Exists(x => x.Item1 == current.id))
                {
                    Tuple <string, int> tuple = StructDic[currentStructType].Find(x => x.Item1 == current.id);
                    StructDic[currentStructType].Remove(tuple);
                    StructDic[currentStructType].Add(new Tuple <string, int>(tuple.Item1, 1));
                }
                else
                {
                    error($"{n.id} doesn't exist in {currentStructType}");
                }
            }
            else if (n.id is DotReferencing)
            {
                DotReferencing dot   = n.id as DotReferencing;
                SymReferencing dotid = dot.dotId as SymReferencing;
                SymReferencing id    = dot.id as SymReferencing;
                if (StructDic[id.id].Exists(x => x.Item1 == dotid.id))
                {
                    Tuple <string, int> tuple = StructDic[id.id].Find(x => x.Item1 == dotid.id);
                    StructDic[id.id].Remove(tuple);
                    StructDic[id.id].Add(new Tuple <string, int>(tuple.Item1, 1));
                }
                else
                {
                    error($"{dotid.id} doesn't exist in {id.id}");
                }
            }
            else if (!(n.id is ListReferencing))
            {
                InitiationTable[GetKey(current.id)] = 1;
            }
            n.child.accept(this);
        }
Example #4
0
        public void DotReferencingUnitTest()
        {
            try {
                DotReferencing dotReferencing = new DotReferencing(new SymReferencing("led"), new SymReferencing("pinPower"));
                TypeChecker    typeChecker    = new TypeChecker();
                AST.SymbolTable =
                    new Dictionary <Tuple <string, string>, int>()
                {
                    { new Tuple <string, string>("1", "led"), AST.STRUCT }
                };
                typeChecker.StructDic.Add("led", new List <Tuple <string, int> >()
                {
                    new Tuple <string, int>("pinPower", AST.INTTYPE)
                });
                dotReferencing.accept(typeChecker);

                int actual   = dotReferencing.type;
                int expected = AST.INTTYPE;
                Assert.IsTrue(actual == expected, "DotReferencing faild");
            } finally {
                AST.SymbolTable.Clear();
            }
        }
Example #5
0
        public void DotReferencingUnitTest()
        {
            try {
                AST.SymbolTable = new Dictionary <Tuple <string, string>, int>();
                Dictionary <string, List <Tuple <string, int> > > structDic =
                    new Dictionary <string, List <Tuple <string, int> > >()
                {
                    { "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);

                var assign = new Assigning(new DotReferencing(new SymReferencing("led"), new SymReferencing("pinPower")), new IntConst("50"));
                assign.accept(initiationChecker);
                DotReferencing dotReferencing = new DotReferencing(new SymReferencing("led"), new SymReferencing("pinPower"));
                dotReferencing.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>();
                Dictionary <string, List <Tuple <string, int> > > 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), "DotReferencing faild");
            } finally {
                AST.SymbolTable.Clear();
            }
        }
Example #6
0
 public override void Visit(DotReferencing n)   //TODO add support for structs in structs
 {
     if (n.dotId is SymReferencing)
     {
         SymReferencing sym = n.dotId as SymReferencing;
         if (!StructDic[n.id.id].Exists(x => x.Item1 == sym.id))
         {
             error($"{sym.id} doesn't exist in {n.id.id}");
         }
         if (StructDic[n.id.id].Find(x => x.Item1 == sym.id).Item2 != 1)
         {
             error($"{sym.id} in {n.id.id} is not initiated with a value");
         }
     }
     else if (n.dotId is DotReferencing)
     {
         DotReferencing dot = n.dotId as DotReferencing;
         if (!StructDic[n.id.id].Exists(x => x.Item1 == dot.id.id))
         {
             error($"{dot.id.id} doesn't exist in {n.id.id}");
         }
         n.dotId.accept(this);
     }
 }
Example #7
0
 public override void Visit(DotReferencing n)
 {
     //throw new NotImplementedException();
 }
Example #8
0
 public override void Visit(DotReferencing n)
 {
     n.id.accept(this);
     emit($".");
     n.dotId.accept(this);
 }