public LegendaryCompiler() { Functions = new FunctionTable(); Functions["printi"] = 1; Functions["printc"] = 1; Functions["prints"] = 1; Functions["println"] = 1; Functions["readi"] = 0; Functions["reads"] = 0; Functions["new"] = 0; Functions["size"] = 0; Functions["add"] = 1; Functions["get"] = 0; Functions["set"] = 1; Variables = new SymbolTable(); isGlobal = true; Scope = "global"; }
public LegendarySemanticAnalyzer() { Functions = new FunctionTable(); Functions["printi"] = 1; Functions["printc"] = 1; Functions["prints"] = 1; Functions["println"] = 0; Functions["readi"] = 0; Functions["reads"] = 0; Functions["new"] = 1; Functions["size"] = 1; Functions["add"] = 2; Functions["get"] = 2; Functions["set"] = 3; Functions["sizeof"] = 1; Variables = new SymbolTable(); Scope = "global"; }
//----------------------------------------------------------- //<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); } } } }
//----------------------------------------------------------- public SemanticAnalyzer() { Global_Symbol_Table = new SymbolTable(); Global_Function_Table = new FunctionTable(); currentNamespaceTable = new SymbolTable(); firstPass = true; insideFunction = false; insideLoop = 0; /* System predefined functions*/ Global_Function_Table["add"] = 2; Global_Function_Table["get"] = 2; Global_Function_Table["new"] = 1; Global_Function_Table["printc"] = 1; Global_Function_Table["printi"] = 1; Global_Function_Table["println"] = 0; Global_Function_Table["prints"] = 1; Global_Function_Table["readi"] = 0; Global_Function_Table["reads"] = 0; Global_Function_Table["set"] = 3; Global_Function_Table["size"] = 1; }
public string Visit(FunDef node) { insideFunction = true; localVariables = new SymbolTable(); var temp1 = " "; var temp2 = " "; parameters = new FunctionTable(); var ind = 0; foreach (var n in node[0]) { parameters[n.AnchorToken.Lexeme] = ind; temp1 += "int32,"; temp2 += Line(Indent() + ".locals init (int32 '" + n.AnchorToken.Lexeme + "')"); localVariables.Add(n.AnchorToken.Lexeme); ind++; } var result = Line(Indent() + ".method public static int32 '" + node.AnchorToken.Lexeme + "'(" + temp1.Substring(0, temp1.Length - 1) + ") {"); indentCounter++; if (node.AnchorToken.Lexeme == "main") { result += Line(Indent() + ".entrypoint"); } result += temp2 + Visit((dynamic)node[1]) + Visit((dynamic)node[2]) + Line(Indent() + "ldc.i4.0") + Line(Indent() + "ret"); indentCounter--; insideFunction = false; return(result + Line(Indent() + "}") + Line(Indent())); //return VisitChildren(node); } //100%