Ejemplo n.º 1
0
        public override void Visit(StructDef n)
        {
            n.type = AST.STRUCT;
            List <Tuple <string, int> > tuples = new List <Tuple <string, int> >();

            PlusScope();
            foreach (AST ast in n.declarings)
            {
                ast.accept(this);
                if (ast is SymDeclaring)
                {
                    SymDeclaring symDeclaring = ast as SymDeclaring;
                    tuples.Add(new Tuple <string, int>(symDeclaring.id, GetType(ast)));
                }
                else if (ast is FuncDecl)
                {
                    FuncDecl     funcDecl     = ast as FuncDecl;
                    SymDeclaring symDeclaring = funcDecl.declaring as SymDeclaring;
                    tuples.Add(new Tuple <string, int>(symDeclaring.id, GetType(funcDecl.declaring)));
                }
            }
            MinusScope();
            SymReferencing current = n.structType as SymReferencing;

            StructDic.Add(current.id, tuples);
        }
Ejemplo n.º 2
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("}");
        }
Ejemplo n.º 3
0
 public override void Visit(SymReferencing n)
 {
     if (!KeyValInit(n.id))
     {
         error($"{n.id} at {GetKey(n.id).Item1} is not initiated with a value");
     }
 }
Ejemplo n.º 4
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;
 }
Ejemplo n.º 5
0
        public override void Visit(FunctionStmt n)
        {
            SymReferencing current = n.id as SymReferencing;

            InitiationTable[FindKey(current.id)] = 1;
            foreach (AST ast in n.param_list)
            {
                ast.accept(this);
            }
        }
Ejemplo n.º 6
0
        public override void Visit(FunctionStmt n)
        {
            SymReferencing current = n.id as SymReferencing;

            n.type = AST.SymbolTable[GetKeyVal(current.id)];
            foreach (AST ast in n.param_list)
            {
                ast.accept(this);
            }
            funcCalls.Add(n);
        }
Ejemplo n.º 7
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("");
     }
 }
Ejemplo n.º 8
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;
        }
Ejemplo n.º 9
0
        public override void Visit(StructDef n)
        {
            plusScope();
            foreach (AST ast in n.declarings)
            {
                if (ast is SymDeclaring)
                {
                    SymDeclaring sym = ast as SymDeclaring;
                    InitiationTable[GetKey(sym.id)] = 1;
                }
                else
                {
                    ast.accept(this);
                }
            }
            SymReferencing current = n.structType as SymReferencing;

            StructDic.Remove(current.id);
            minusScope();
        }
Ejemplo n.º 10
0
 public override void Visit(Assigning n)
 {
     if (n.id is SymReferencing)
     {
         SymReferencing sym = n.id as SymReferencing;
         emit($"{sym.id} ");
     }
     else if (n.id is DotReferencing)
     {
         n.id.accept(this);
     }
     else
     {
         emit($"{n.id} ");
     }
     if (!(n.child is StructDef || n.child is StructDcel))
     {
         emit("= ");
     }
     n.child.accept(this);
     emit(";\n");
 }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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]);
            }
        }
Ejemplo n.º 13
0
        public override void Visit(ListReferencing n)
        {
            n.id.accept(this);
            int            inum = 0;
            string         type = n.id.type.ToString();
            SymReferencing sym  = n.id as SymReferencing;

            if (type.Length < n.index.Count)
            {
                Error($"too many index dereferences in {sym.id}");
            }
            foreach (AST item in n.index)
            {
                item.accept(this);
                if (item.type != AST.INTTYPE)
                {
                    Error($"{sym.id}'s {inum} index is not of type int");
                }
                inum++;
                type = type.Remove(0, 1);
            }
            n.type = int.Parse(type);
        }
Ejemplo n.º 14
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);
     }
 }
Ejemplo n.º 15
0
        private string FindId(AST node)
        {
            SymReferencing current = node as SymReferencing;

            return(current.id);
        }
Ejemplo n.º 16
0
 public override void Visit(SymReferencing n)
 {
     n.type = AST.SymbolTable[GetKeyVal(n.id)];
 }
Ejemplo n.º 17
0
 public override void Visit(SymReferencing n)
 {
     emit($"{n.id}");
 }
Ejemplo n.º 18
0
 public override void Visit(SymReferencing n)
 {
     //throw new NotImplementedException();
 }
Ejemplo n.º 19
0
 public abstract void visit(SymReferencing n);