public override void GenerateCode(Emit.ILGenerator ilg, ExecutionContext ec) { /* * Check for two things: * 1) Is it an argument? * 2) Is it a local variable (or global) */ var arg = ec.GetParameter(varName); if (arg != null) { //This is an argument - we need to store it in its index. ilg.Emit(Emit.OpCodes.Starg, arg.Position); return; } var varDec = ec.GetVariable(varName); if (varDec == null) { //The person wants this to be a local variable. It has not been declared, so we must do it for them. varDec = ilg.DeclareLocal(e.GetEvaluatedType(ec)); ec.AddVariable(varName, varDec); } e.Push(ilg, ec); ilg.Emit(Emit.OpCodes.Stloc, varDec); //Store the result of the expression. }
public override void Push(Emit.ILGenerator ilg, ExecutionContext ec) { /* * Check for three things: * 1) Is it an argument? * 2) Is it a local variable (or global) * 3) Is it a function call without parantheses? */ var arg = ec.GetParameter(name); if (arg != null) { //This is an argument - push its index onto the stack! ilg.Emit(Emit.OpCodes.Ldarg, arg.Position); return; } var varDec = ec.GetVariable(name); if (varDec != null) { //This is a local variable! Push it onto the stack. ilg.Emit(Emit.OpCodes.Ldloc, varDec); return; } var funcDec = Function.Get(name); if (funcDec != null) { if (funcDec.MethodInfo.ReturnType != typeof(void)) { ilg.Emit(Emit.OpCodes.Call, funcDec.MethodInfo); //Only functions without parameters can be called currently - changes to the grammar are needed to improve on this and will likely become their own type of expression return; } else { throw new Exception("Attempting to use a void type as an expression."); } } //At this point, we have an unrecognized identifier throw new Exception("Use of undeclared identifier in expression."); }
public override Type GetEvaluatedType(ExecutionContext ec) { var arg = ec.GetParameter(name); if (arg != null) { return arg.Type; } var varDec = ec.GetVariable(name); if (varDec != null) { return varDec.LocalType; } var funcDec = Function.Get(name); if (funcDec != null) { return funcDec.MethodInfo.ReturnType; } //At this point, we have an unrecognized identifier throw new Exception("Use of undeclared identifier in expression."); }