Example #1
0
 public string Visit(Identifier node)
 {
     if (globalSymbolAssLoad.ContainsKey(node.AnchorToken.Lexeme))
     {
         return(globalSymbolAssLoad[node.AnchorToken.Lexeme]);
     }
     if (localSymbolAssLoad.ContainsKey(node.AnchorToken.Lexeme))
     {
         return(localSymbolAssLoad[node.AnchorToken.Lexeme]);
     }
     if (functionParamTables[localscope].Contains(node.AnchorToken.Lexeme))
     {
         return("\tldarg " + "'" + node.AnchorToken.Lexeme + "'" + "\n");
     }
     else if (localSymbolTables.ContainsKey(node.AnchorToken.Lexeme))
     {
         return("\tldloc '" + node.AnchorToken.Lexeme + "'\n");
     }
     else if (globalSymbolTable.Contains(node.AnchorToken.Lexeme))
     {
         return("\tldsfld " + CILTypes[globalSymbolTable[node.AnchorToken.Lexeme]] + " 'ChimeraProgram'::'" + node.AnchorToken.Lexeme + "'\n");
     }
     else  //const table
     {
         return("\tldsfld " + CILTypes[globalConstTable[node.AnchorToken.Lexeme]] + " 'ChimeraProgram'::'" + node.AnchorToken.Lexeme + "'\n");
     }
 }
Example #2
0
        public Type Visit(ConstantDeclaration node)
        {
            var         constName = node.AnchorToken.Lexeme;
            var         type      = Visit((dynamic)node[0]);
            var         kind      = "CONST";
            dynamic     value     = node[0].AnchorToken.Lexeme;
            bool        isList    = false;
            SymbolTable symbolT   = symbolTable;

            if (inProcedure)
            {
                symbolT = LocalSTable;
            }

            if (type != Type.BOOL && type != Type.STRING && type != Type.INT && type != Type.VOID)
            {
                try
                {
                    //Extract value of list
                    List <dynamic> temp = new List <dynamic>();
                    foreach (var n in ((dynamic)node[0]))
                    {
                        Visit((dynamic)n);
                        temp.Add(n.AnchorToken.Lexeme);
                    }
                    value  = temp;
                    isList = true;
                }
                catch (ArgumentOutOfRangeException)
                {
                    throw new SemanticError("Constant list needs at least 1 value: " + node[0].AnchorToken.Category, node[0].AnchorToken);
                }
            }

            if (symbolT.Contains(constName))
            {
                throw new SemanticError("Duplicated constant: " + constName, node.AnchorToken);
            }

            else
            {
                symbolT[constName] = new Variables(type, kind, value, isList);
            }
            return(Type.VOID);
        }
Example #3
0
        public Type Visit(VariableDeclaration node)
        {
            var         category = node[0].AnchorToken.Category;
            var         isList   = false;
            var         kind     = "VAR";
            var         type     = typeMapper[category];
            dynamic     value    = null;
            SymbolTable sTable   = symbolTable;

            if (inProcedure)
            {
                sTable = LocalSTable;
            }

            if (isParam)
            {
                kind = "PARAM";
            }

            foreach (var n in node[1])
            {
                var temp = n.AnchorToken.Lexeme;
                if (type == Type.LISTVAR)
                {
                    isList = true;
                    type   = Visit((dynamic)node[0]);
                }

                if (sTable.Contains(temp))
                {
                    throw new SemanticError("Duplicated variable: " + temp, n.AnchorToken);
                }

                else
                {
                    sTable[temp] = new Variables(type, kind, value, isList);
                    if (isParam)
                    {
                        numParams++;
                        listOrderParams.Add(type);
                    }
                }
            }
            return(Type.VOID);
        }