internal void AddFunctionHeader(string functionName, MethodAttributes methodAttributes, Type returnType, FunctionDefinition.Argument[] parameters) { var meth = typeBuilder.DefineMethod(functionName, methodAttributes, CallingConventions.Standard, returnType, parameters.Select(x => x.argType).ToArray()); for (int i = 0; i < parameters.Length; i++) { meth.DefineParameter(i + 1, ParameterAttributes.In, parameters[i].argName); } var function = new FunctionDefinition(meth, parameters.ToList()); functionTable.AddHeader(functionName, function); }
public FunctionDefinition this[string functionName] { get { if (functionTable.ContainsKey(functionName)) return functionTable[functionName]; if (functionName.Contains("."))//then it is a member call, and we should look in importedModules { int lastPeriod = functionName.LastIndexOf('.'); string className = functionName.Substring(0, lastPeriod); string funcName = functionName.Substring(lastPeriod + 1); FunctionDefinition def = new FunctionDefinition(importedModules[className].GetMethod(funcName)); return def; } throw new Exception($"Function {functionName} has not been declared"); } }
public CodeGenOld(ParseTreeNode stmt, string moduleName) { if (Path.GetFileName(moduleName) != moduleName) { throw new System.Exception("can only output into current directory!"); } AssemblyName name = new AssemblyName(Path.GetFileNameWithoutExtension(moduleName)); AssemblyBuilder asmb = System.AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Save); ModuleBuilder modb = asmb.DefineDynamicModule(moduleName); mainProgram = modb.DefineType("Program"); var mainArgs = new List<Tuple<string,Type>>(); var mainProgramDef = new FunctionDefinition() { methodDefinition=mainProgram.DefineMethod("Main", MethodAttributes.Static, typeof(void), System.Type.EmptyTypes), arguments=new List<FunctionDefinition.Argument>() }; functionTable.Add("Main",mainProgramDef); SymbolTable symbolTable = new SymbolTable(); // CodeGenerator var il = functionTable["Main"].methodDefinition.GetILGenerator(); // Go Compile! this.GenStmt(stmt, il, symbolTable); il.Emit(OpCodes.Ldstr, "Press any key to exit the program..."); il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new Type[] { typeof(string) })); il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("ReadKey", new Type[] { })); il.Emit(OpCodes.Ret); mainProgram.CreateType(); modb.CreateGlobalFunctions(); asmb.SetEntryPoint(functionTable["Main"].methodDefinition); asmb.Save(moduleName); foreach (var symbol in symbolTable.locals) { Console.WriteLine("{0}: {1}", symbol.Key, symbol.Value); } symbolTable = null; il = null; }
private void GenStmt(ParseTreeNode stmt, ILGenerator il, SymbolTable symbolTable) { if (stmt.Term.Name == "program") { if (stmt.ChildNodes.Count > 0) { this.GenStmt(stmt.ChildNodes[0].ChildNodes[0], il, symbolTable); this.GenStmt(stmt.ChildNodes[1], il, symbolTable); } } else if (stmt.Term.Name == "variableDeclaration") { Type localType; // declare a local if (stmt.ChildNodes[2].Term.Name == "typeSpecifier") { localType = this.TypeOfTypeDeclaration(stmt.ChildNodes[2].ChildNodes[1]); } else { localType = this.TypeOfExpr(stmt.ChildNodes[2].ChildNodes[1], symbolTable); //symbolTable.locals[stmt.ChildNodes[1].ChildNodes[0].Token.ValueString] = il.DeclareLocal(this.TypeOfExpr(stmt.ChildNodes[2].ChildNodes[1], symbolTable)); } Action <string> generateAssign = null; ParseTreeNode assign = stmt.ChildNodes.Where(x => x.Term.Name == "setEqual").SingleOrDefault(); // set the initial value if (assign != null) { generateAssign = new Action <string>(name => { this.GenExpr(assign.ChildNodes[1], symbolTable.locals[name].LocalType, il, symbolTable); symbolTable.Store(name, this.TypeOfExpr(assign.ChildNodes[1], symbolTable), il); }); } var variableIden = stmt.ChildNodes[1]; while (true) { string name = variableIden.ChildNodes[0].Token.ValueString; symbolTable.AddLocal(name, il.DeclareLocal(localType)); if (generateAssign != null) { generateAssign(name); } if (variableIden.ChildNodes.Count < 2) { break; } variableIden = variableIden.ChildNodes[1]; } } else if (stmt.Term.Name == "io") { if (stmt.ChildNodes[0].Token.ValueString == "put") { ParseTreeNode argItem = stmt.ChildNodes[1]; while (true) { this.GenExpr(argItem.ChildNodes[0], typeof(string), il, symbolTable); il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("Write", new System.Type[] { typeof(string) })); if (argItem.ChildNodes.Count > 1) { argItem = argItem.ChildNodes[1]; } else { break; } } il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new System.Type[] { })); } } else if (stmt.Term.Name == "assignment") { string ident = stmt.ChildNodes[0].Token.ValueString; this.GenExpr(stmt.ChildNodes[1].ChildNodes[1], this.TypeOfExpr(stmt.ChildNodes[1].ChildNodes[1], symbolTable), il, symbolTable); symbolTable.Store(ident, this.TypeOfExpr(stmt.ChildNodes[1].ChildNodes[1], symbolTable), il); //this.Store(ident, this.TypeOfExpr(stmt.ChildNodes[1].ChildNodes[1], symbolTable), ref il, symbolTable); } else if (stmt.Term.Name == "functionDefinition") { string functionName = stmt.ChildNodes[1].Token.ValueString; if (functionTable.ContainsKey(functionName)) { throw new Exception(functionName + " has already been defined"); } var parameterList = new List <FunctionDefinition.Argument>(); List <Type> types = new List <Type>(); SymbolTable localSymbols = new SymbolTable(); if (stmt.ChildNodes[2].ChildNodes.Count > 0) { var currParam = stmt.ChildNodes[2].ChildNodes[0]; while (true) { var parameterType = TypeOfExpr(currParam.ChildNodes[0].ChildNodes[1].ChildNodes[1], symbolTable); var paramIdentifier = currParam.ChildNodes[0].ChildNodes[0]; while (true) { var parameterName = paramIdentifier.ChildNodes[0].Token.ValueString; parameterList.Add(new FunctionDefinition.Argument() { argName = parameterName, argType = parameterType }); localSymbols.AddParameter(parameterName, parameterType); types.Add(parameterType); if (paramIdentifier.ChildNodes.Count == 1) { break; } paramIdentifier = paramIdentifier.ChildNodes[1]; } if (currParam.ChildNodes.Count == 1) { break; } currParam = currParam.ChildNodes[1]; } } var methodDeclaration = mainProgram.DefineMethod(functionName, MethodAttributes.Static, TypeOfExpr(stmt.ChildNodes[3].ChildNodes[1], symbolTable), types.ToArray()); var ilMeth = methodDeclaration.GetILGenerator(); //if (stmt.ChildNodes[2].ChildNodes.Count > 0) //{ // var currParam = stmt.ChildNodes[2].ChildNodes[0]; // while (true) // { // //localSymbols.parameters.Add(new Tuple<string, Type>(currParam.ChildNodes[0].ChildNodes[0].Token.ValueString, TypeOfExpr(currParam.ChildNodes[0].ChildNodes[1].ChildNodes[1], symbolTable))); // if (currParam.ChildNodes.Count == 1) // break; // currParam = currParam.ChildNodes[1]; // } //} GenStmt(stmt.ChildNodes[4], ilMeth, localSymbols); ilMeth.Emit(OpCodes.Ret); var methodDec = new FunctionDefinition() { methodDefinition = methodDeclaration, arguments = parameterList }; functionTable.Add(functionName, methodDec); } else if (stmt.Term.Name == "result") { GenExpr(stmt.ChildNodes[1], TypeOfExpr(stmt.ChildNodes[1], symbolTable), il, symbolTable); var result = il.DeclareLocal(TypeOfExpr(stmt.ChildNodes[1], symbolTable)); il.Emit(OpCodes.Stloc, result); il.Emit(OpCodes.Ldloc, result); il.Emit(OpCodes.Ret, result); } /* * else if (stmt is ReadInt) * { * this.il.Emit(Emit.OpCodes.Call, typeof(System.Console).GetMethod("ReadLine", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static, null, new System.Type[] { }, null)); * this.il.Emit(Emit.OpCodes.Call, typeof(int).GetMethod("Parse", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static, null, new System.Type[] { typeof(string) }, null)); * this.Store(((ReadInt)stmt).Ident, typeof(int)); * } * else if (stmt is ForLoop) * { * // example: * // for x = 0 to 100 do * // print "hello"; * // end; * * // x = 0 * ForLoop forLoop = (ForLoop)stmt; * Assign assign = new Assign(); * assign.Ident = forLoop.Ident; * assign.Expr = forLoop.From; * this.GenStmt(assign); * // jump to the test * Emit.Label test = this.il.DefineLabel(); * this.il.Emit(Emit.OpCodes.Br, test); * * // statements in the body of the for loop * Emit.Label body = this.il.DefineLabel(); * this.il.MarkLabel(body); * this.GenStmt(forLoop.Body); * * // to (increment the value of x) * this.il.Emit(Emit.OpCodes.Ldloc, this.symbolTable[forLoop.Ident]); * this.il.Emit(Emit.OpCodes.Ldc_I4, 1); * this.il.Emit(Emit.OpCodes.Add); * this.Store(forLoop.Ident, typeof(int)); * * // **test** does x equal 100? (do the test) * this.il.MarkLabel(test); * this.il.Emit(Emit.OpCodes.Ldloc, this.symbolTable[forLoop.Ident]); * this.GenExpr(forLoop.To, typeof(int)); * this.il.Emit(Emit.OpCodes.Blt, body); * }*/ else { throw new System.Exception("don't know how to gen a " + stmt.Term.Name); } }
public void AddHeader(string functionName, FunctionDefinition functionDefinition) { functionTable.Add(functionName, functionDefinition); }
public void AddHeader(string functionName, FunctionDefinition functionDefinition) => functionTable.Add(functionName, functionDefinition);
private void GenStmt(ParseTreeNode stmt, ILGenerator il, SymbolTable symbolTable) { if (stmt.Term.Name == "program") { if (stmt.ChildNodes.Count > 0) { this.GenStmt(stmt.ChildNodes[0].ChildNodes[0], il, symbolTable); this.GenStmt(stmt.ChildNodes[1], il, symbolTable); } } else if (stmt.Term.Name == "variableDeclaration") { Type localType; // declare a local if (stmt.ChildNodes[2].Term.Name == "typeSpecifier") { localType = this.TypeOfTypeDeclaration(stmt.ChildNodes[2].ChildNodes[1]); } else { localType = this.TypeOfExpr(stmt.ChildNodes[2].ChildNodes[1], symbolTable); //symbolTable.locals[stmt.ChildNodes[1].ChildNodes[0].Token.ValueString] = il.DeclareLocal(this.TypeOfExpr(stmt.ChildNodes[2].ChildNodes[1], symbolTable)); } Action<string> generateAssign = null; ParseTreeNode assign = stmt.ChildNodes.Where(x => x.Term.Name == "setEqual").SingleOrDefault(); // set the initial value if (assign != null) { generateAssign = new Action<string>(name => { this.GenExpr(assign.ChildNodes[1], symbolTable.locals[name].LocalType, il, symbolTable); symbolTable.Store(name, this.TypeOfExpr(assign.ChildNodes[1], symbolTable), il); }); } var variableIden = stmt.ChildNodes[1]; while (true) { string name = variableIden.ChildNodes[0].Token.ValueString; symbolTable.AddLocal(name,il.DeclareLocal(localType)); if (generateAssign != null) generateAssign(name); if (variableIden.ChildNodes.Count < 2) break; variableIden = variableIden.ChildNodes[1]; } } else if (stmt.Term.Name == "io") { if (stmt.ChildNodes[0].Token.ValueString == "put") { ParseTreeNode argItem = stmt.ChildNodes[1]; while (true) { this.GenExpr(argItem.ChildNodes[0], typeof(string), il, symbolTable); il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("Write", new System.Type[] { typeof(string) })); if (argItem.ChildNodes.Count > 1) argItem = argItem.ChildNodes[1]; else break; } il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new System.Type[] { })); } } else if (stmt.Term.Name == "assignment") { string ident = stmt.ChildNodes[0].Token.ValueString; this.GenExpr(stmt.ChildNodes[1].ChildNodes[1], this.TypeOfExpr(stmt.ChildNodes[1].ChildNodes[1], symbolTable), il, symbolTable); symbolTable.Store(ident, this.TypeOfExpr(stmt.ChildNodes[1].ChildNodes[1], symbolTable), il); //this.Store(ident, this.TypeOfExpr(stmt.ChildNodes[1].ChildNodes[1], symbolTable), ref il, symbolTable); } else if (stmt.Term.Name == "functionDefinition") { string functionName = stmt.ChildNodes[1].Token.ValueString; if (functionTable.ContainsKey(functionName)) { throw new Exception(functionName + " has already been defined"); } var parameterList = new List<FunctionDefinition.Argument>(); List<Type> types = new List<Type>(); SymbolTable localSymbols = new SymbolTable(); if (stmt.ChildNodes[2].ChildNodes.Count > 0) { var currParam = stmt.ChildNodes[2].ChildNodes[0]; while (true) { var parameterType = TypeOfExpr(currParam.ChildNodes[0].ChildNodes[1].ChildNodes[1], symbolTable); var paramIdentifier = currParam.ChildNodes[0].ChildNodes[0]; while (true) { var parameterName = paramIdentifier.ChildNodes[0].Token.ValueString; parameterList.Add(new FunctionDefinition.Argument() { argName = parameterName, argType = parameterType }); localSymbols.AddParameter(parameterName, parameterType); types.Add(parameterType); if (paramIdentifier.ChildNodes.Count == 1) break; paramIdentifier = paramIdentifier.ChildNodes[1]; } if (currParam.ChildNodes.Count == 1) break; currParam = currParam.ChildNodes[1]; } } var methodDeclaration = mainProgram.DefineMethod(functionName, MethodAttributes.Static, TypeOfExpr(stmt.ChildNodes[3].ChildNodes[1], symbolTable), types.ToArray()); var ilMeth = methodDeclaration.GetILGenerator(); //if (stmt.ChildNodes[2].ChildNodes.Count > 0) //{ // var currParam = stmt.ChildNodes[2].ChildNodes[0]; // while (true) // { // //localSymbols.parameters.Add(new Tuple<string, Type>(currParam.ChildNodes[0].ChildNodes[0].Token.ValueString, TypeOfExpr(currParam.ChildNodes[0].ChildNodes[1].ChildNodes[1], symbolTable))); // if (currParam.ChildNodes.Count == 1) // break; // currParam = currParam.ChildNodes[1]; // } //} GenStmt(stmt.ChildNodes[4], ilMeth, localSymbols); ilMeth.Emit(OpCodes.Ret); var methodDec = new FunctionDefinition() { methodDefinition = methodDeclaration, arguments = parameterList }; functionTable.Add(functionName, methodDec); } else if (stmt.Term.Name == "result") { GenExpr(stmt.ChildNodes[1], TypeOfExpr(stmt.ChildNodes[1], symbolTable), il, symbolTable); var result = il.DeclareLocal(TypeOfExpr(stmt.ChildNodes[1], symbolTable)); il.Emit(OpCodes.Stloc, result); il.Emit(OpCodes.Ldloc, result); il.Emit(OpCodes.Ret, result); } /* else if (stmt is ReadInt) { this.il.Emit(Emit.OpCodes.Call, typeof(System.Console).GetMethod("ReadLine", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static, null, new System.Type[] { }, null)); this.il.Emit(Emit.OpCodes.Call, typeof(int).GetMethod("Parse", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static, null, new System.Type[] { typeof(string) }, null)); this.Store(((ReadInt)stmt).Ident, typeof(int)); } else if (stmt is ForLoop) { // example: // for x = 0 to 100 do // print "hello"; // end; // x = 0 ForLoop forLoop = (ForLoop)stmt; Assign assign = new Assign(); assign.Ident = forLoop.Ident; assign.Expr = forLoop.From; this.GenStmt(assign); // jump to the test Emit.Label test = this.il.DefineLabel(); this.il.Emit(Emit.OpCodes.Br, test); // statements in the body of the for loop Emit.Label body = this.il.DefineLabel(); this.il.MarkLabel(body); this.GenStmt(forLoop.Body); // to (increment the value of x) this.il.Emit(Emit.OpCodes.Ldloc, this.symbolTable[forLoop.Ident]); this.il.Emit(Emit.OpCodes.Ldc_I4, 1); this.il.Emit(Emit.OpCodes.Add); this.Store(forLoop.Ident, typeof(int)); // **test** does x equal 100? (do the test) this.il.MarkLabel(test); this.il.Emit(Emit.OpCodes.Ldloc, this.symbolTable[forLoop.Ident]); this.GenExpr(forLoop.To, typeof(int)); this.il.Emit(Emit.OpCodes.Blt, body); }*/ else { throw new System.Exception("don't know how to gen a " + stmt.Term.Name); } }