Browse files

Add a first cut at kwdefaults. Not sure this is the right way yet.

  • Loading branch information...
1 parent 5bc3e5e commit 997db461a6c4f53126e16893e6f633387bfa7369 @jdhardy committed Mar 24, 2013
View
24 Languages/IronPython/IronPython/Compiler/Ast/FunctionDefinition.cs
@@ -424,17 +424,22 @@ public FunctionDefinition(string name, Parameter[] parameters, Statement body, S
}
// emit defaults
- int defaultCount = 0;
+ int defaultCount = 0, kwDefaultCount = 0;
for (int i = _parameters.Length - 1; i >= 0; i--) {
var param = _parameters[i];
if (param.DefaultValue != null) {
compiler.Compile(AstUtils.Convert(param.DefaultValue, typeof(object)));
- defaultCount++;
+ if (param.Kind == ParameterKind.Normal) {
+ defaultCount++;
+ } else if (param.IsKeywordOnly) {
+ Debug.Assert(defaultCount == 0);
+ kwDefaultCount++;
+ }
}
}
- compiler.Instructions.Emit(new FunctionDefinitionInstruction(globalContext, this, defaultCount, globalName));
+ compiler.Instructions.Emit(new FunctionDefinitionInstruction(globalContext, this, defaultCount, kwDefaultCount, globalName));
}
private static void CompileAssignment(LightCompiler compiler, MSAst.Expression variable, Action<LightCompiler> compileValue) {
@@ -479,20 +484,27 @@ public FunctionDefinition(string name, Parameter[] parameters, Statement body, S
class FunctionDefinitionInstruction : Instruction {
private readonly FunctionDefinition _def;
- private readonly int _defaultCount;
+ private readonly int _defaultCount, _kwDefaultCount;
private readonly CodeContext _context;
private readonly PythonGlobal _name;
- public FunctionDefinitionInstruction(CodeContext context, FunctionDefinition/*!*/ definition, int defaultCount, PythonGlobal name) {
+ public FunctionDefinitionInstruction(CodeContext context, FunctionDefinition/*!*/ definition, int defaultCount, int kwDefaultCount, PythonGlobal name) {
Assert.NotNull(definition);
_context = context;
_defaultCount = defaultCount;
+ _kwDefaultCount = kwDefaultCount;
_def = definition;
_name = name;
}
public override int Run(InterpretedFrame frame) {
+ IDictionary<string, object> kwDefaults = new Dictionary<string, object>();
+ for (int i = 0; i < _defaultCount; i++) {
+ // TODO get the param name somehow
+ kwDefaults.Add(xxx, frame.Pop());
+ }
+
object[] defaults;
if (_defaultCount > 0) {
defaults = new object[_defaultCount];
@@ -512,7 +524,7 @@ class FunctionDefinitionInstruction : Instruction {
CodeContext context = (CodeContext)frame.Pop();
- frame.Push(PythonOps.MakeFunction(context, _def.FunctionCode, modName, defaults));
+ frame.Push(PythonOps.MakeFunction(context, _def.FunctionCode, modName, defaults, kwDefaults));
return +1;
}
View
5 Languages/IronPython/IronPython/Runtime/Operations/PythonOps.cs
@@ -3139,6 +3139,11 @@ public static partial class PythonOps {
}
[NoSideEffects]
+ public static object MakeFunction(CodeContext/*!*/ context, FunctionCode funcInfo, object modName, object[] defaults, IDictionary<string, object> kwDefaults) {
+ return new PythonFunction(context, funcInfo, modName, defaults, null);
+ }
+
+ [NoSideEffects]
public static object MakeFunctionDebug(CodeContext/*!*/ context, FunctionCode funcInfo, object modName, object[] defaults, Delegate target) {
funcInfo.SetDebugTarget(PythonContext.GetContext(context), target);
View
10 Languages/IronPython/IronPython/Runtime/PythonFunction.cs
@@ -62,6 +62,7 @@ public sealed partial class PythonFunction : PythonTypeSlot, IWeakReferenceable,
[ThreadStatic] private static int DepthSlow; // current depth stored in a real thread static with fast depth runs out
[MultiRuntimeAware]
private static int _CurrentId = 1; // The current ID for functions which are called in complex ways.
+private PythonDictionary _kwdefaults;
/// <summary>
/// Python ctor - maps to function.__new__
@@ -142,6 +143,15 @@ public sealed partial class PythonFunction : PythonTypeSlot, IWeakReferenceable,
}
}
+ public PythonDictionary __kwdefaults__ {
+ get {
+ return _kwdefaults;
+ }
+ set {
+ _kwdefaults = value;
+ }
+ }
+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public PythonTuple __closure__ {
get {

0 comments on commit 997db46

Please sign in to comment.