//----------------------------------------------------------- //<id>// public void Visit(Identifier node, int i) { var variableName = node.AnchorToken.Lexeme; if (i == 1) { if (GlobalVarsTable.Contains(variableName)) { throw new SemanticError( "Duplicated var: " + variableName, node.AnchorToken); } else { GlobalVarsTable[variableName] = "Global Var"; } } else if (i == 2) { if (FunctionTable.Contains(variableName)) { throw new SemanticError( "Duplicated function: " + variableName, node.AnchorToken); } else { currentFunc = variableName; FunctionTable[variableName] = new GFuncStruct("u", 0, new FunContainer()); } } }
//----------------------------------------------------------- void VisitChildren(Node node, int i) { int counter = 0; foreach (var n in node) { if (i == 3) { counter++; } Visit((dynamic)n, i); } if (i == 3) { GFuncStruct oneStruct = FunctionTable[currentFunc]; oneStruct.arity = counter; FunctionTable[currentFunc] = oneStruct; } }
//----------------------------------------------------------- public SemanticAnalyzer() { GlobalVarsTable = new SymbolTable(); FunctionTable = new FuncTable(); //For second run, the dictionary of particular Funcs FunMethods = new FuncMethods(); //Fill in the non-user defined functions FunctionTable["printi"] = new GFuncStruct("p", 1, new FunContainer()); FunctionTable["printc"] = new GFuncStruct("p", 1, new FunContainer()); FunctionTable["prints"] = new GFuncStruct("p", 1, new FunContainer()); FunctionTable["println"] = new GFuncStruct("p", 0, new FunContainer()); FunctionTable["readi"] = new GFuncStruct("p", 0, new FunContainer()); FunctionTable["reads"] = new GFuncStruct("p", 0, new FunContainer()); FunctionTable["new"] = new GFuncStruct("p", 1, new FunContainer()); FunctionTable["size"] = new GFuncStruct("p", 1, new FunContainer()); FunctionTable["add"] = new GFuncStruct("p", 2, new FunContainer()); FunctionTable["get"] = new GFuncStruct("p", 2, new FunContainer()); FunctionTable["set"] = new GFuncStruct("p", 3, new FunContainer()); }
//----------------------------------------------------------- //<funcall>// public void Visit(FunCall node, char i) { var variableName = node.AnchorToken.Lexeme; var fctemp = FunMethods[currentFunc]; if (!FunMethods.Contains(variableName) && !FunctionTable.Contains(variableName)) { throw new SemanticError( "Func Not declared: " + variableName, node.AnchorToken); } else { if (i.Equals('c')) { parameterCounter2 = 0; VisitChildren(node, 'q'); GFuncStruct temp = FunctionTable[variableName]; //Console.WriteLine(variableName); if (parameterCounter2 != temp.arity) { throw new SemanticError( "Incorrect parameters arity in function call: " + variableName + ", expected:" + temp.arity + ", actual:" + parameterCounter2, node.AnchorToken); } } else { parameterCounter = 0; VisitChildren(node, 'c'); GFuncStruct temp = FunctionTable[variableName]; if (parameterCounter != temp.arity) { throw new SemanticError( "Incorrect parameters arity in function call: " + variableName + ", expected:" + temp.arity + ", actual:" + parameterCounter, node.AnchorToken); } } } }