Skip to content

Commit

Permalink
Merge branch 'master' of http://github.com/ironruby/ironruby
Browse files Browse the repository at this point in the history
  • Loading branch information
Shri Borde committed Mar 20, 2009
2 parents 8cf29c0 + be5ccb7 commit d522059
Show file tree
Hide file tree
Showing 162 changed files with 8,654 additions and 1,389 deletions.
14 changes: 7 additions & 7 deletions Merlin/Main/Languages/IronPython/IronPython.Modules/IterTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public class dropwhile : IterBase {
PythonContext pc = PythonContext.GetContext(_context);

while (MoveNextHelper(iter)) {
if (!Converter.ConvertToBoolean(pc.Call(predicate, iter.Current))) {
if (!Converter.ConvertToBoolean(pc.CallSplat(predicate, iter.Current))) {
yield return iter.Current;
break;
}
Expand Down Expand Up @@ -246,7 +246,7 @@ public class groupby : IterBase {
private object GetKey(object val) {
if (_key == null) return val;

return PythonContext.GetContext(_context).Call(_key, val);
return PythonContext.GetContext(_context).CallSplat(_key, val);
}
}

Expand All @@ -271,7 +271,7 @@ public class ifilter : IterBase {
private bool ShouldYield(object predicate, object current) {
if (predicate == null) return PythonOps.IsTrue(current);

return Converter.ConvertToBoolean(PythonContext.GetContext(_context).Call(predicate, current));
return Converter.ConvertToBoolean(PythonContext.GetContext(_context).CallSplat(predicate, current));
}
}

Expand All @@ -296,7 +296,7 @@ public class ifilterfalse : IterBase {
if (predicate == null) return !PythonOps.IsTrue(current);

return !Converter.ConvertToBoolean(
PythonContext.GetContext(_context).Call(predicate, current)
PythonContext.GetContext(_context).CallSplat(predicate, current)
);
}
}
Expand Down Expand Up @@ -332,7 +332,7 @@ public class imap : IEnumerator {
if (_function == null) {
return PythonTuple.MakeTuple(args);
} else {
return PythonContext.GetContext(_context).Call(_function, args);
return PythonContext.GetContext(_context).CallSplat(_function, args);
}
}
}
Expand Down Expand Up @@ -550,7 +550,7 @@ public class starmap : IterBase {
for (int i = 0; i < objargs.Length; i++) {
objargs[i] = args[i];
}
yield return pc.Call(function, objargs);
yield return pc.CallSplat(function, objargs);
}
}
}
Expand All @@ -568,7 +568,7 @@ public class takewhile : IterBase {
private IEnumerator<object> Yielder(object predicate, IEnumerator iter) {
while (MoveNextHelper(iter)) {
if(!Converter.ConvertToBoolean(
PythonContext.GetContext(_context).Call(predicate, iter.Current)
PythonContext.GetContext(_context).CallSplat(predicate, iter.Current)
)) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public sealed partial class weakcallableproxy :

[SpecialName]
public object Call(CodeContext/*!*/ context, params object[] args) {
return PythonContext.GetContext(context).Call(GetObject(), args);
return PythonContext.GetContext(context).CallSplat(GetObject(), args);
}

[SpecialName]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ public Pickler(CodeContext/*!*/ context, object file, object protocol, object bi

private void Save(CodeContext/*!*/ context, object obj) {
if (_persist_id != null) {
string res = Converter.ConvertToString(PythonContext.GetContext(context).Call(_persist_id, obj));
string res = Converter.ConvertToString(PythonContext.GetContext(context).CallSplat(_persist_id, obj));
if (res != null) {
SavePersId(context, res);
return;
Expand Down Expand Up @@ -1531,7 +1531,7 @@ public Unpickler(CodeContext/*!*/ context, object file)
private void LoadBinPersid(CodeContext/*!*/ context) {
if (_pers_loader == null) throw CannotUnpickle(context, "cannot unpickle binary persistent ID w/o persistent_load");

_stack.append(PythonContext.GetContext(context).Call(_pers_loader, _stack.pop()));
_stack.append(PythonContext.GetContext(context).CallSplat(_pers_loader, _stack.pop()));
}

private void LoadBinPut(CodeContext/*!*/ context) {
Expand Down Expand Up @@ -1761,7 +1761,7 @@ public Unpickler(CodeContext/*!*/ context, object file)
if (_pers_loader == null) {
throw CannotUnpickle(context, "A load persistent ID instruction is present but no persistent_load function is available");
}
_stack.append(PythonContext.GetContext(context).Call(_pers_loader, ReadLineNoNewline(context)));
_stack.append(PythonContext.GetContext(context).CallSplat(_pers_loader, ReadLineNoNewline(context)));
}

private void LoadPop(CodeContext/*!*/ context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ public class itemgetter {
return concat(context, a, b);
}

public static object contains(CodeContext/*!*/ context, object a, object b) {
return PythonContext.GetContext(context).Operation(PythonOperationKind.Contains, b, a);
public static bool contains(CodeContext/*!*/ context, object a, object b) {
return PythonContext.GetContext(context).Contains(b, a);
}

public static object __contains__(CodeContext/*!*/ context, object a, object b) {
public static bool __contains__(CodeContext/*!*/ context, object a, object b) {
return contains(context, a, b);
}

Expand Down
4 changes: 2 additions & 2 deletions Merlin/Main/Languages/IronPython/IronPython.Modules/socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,7 @@ class PythonUserSocketStream : Stream {
foreach (string s in _data) {
res.Append(s);
}
DefaultContext.DefaultPythonContext.Call(PythonOps.GetBoundAttr(DefaultContext.Default, _userSocket, SymbolTable.StringToId("sendall")), res.ToString());
DefaultContext.DefaultPythonContext.CallSplat(PythonOps.GetBoundAttr(DefaultContext.Default, _userSocket, SymbolTable.StringToId("sendall")), res.ToString());
_data.Clear();
}
}
Expand All @@ -1869,7 +1869,7 @@ class PythonUserSocketStream : Stream {
}

public override int Read(byte[] buffer, int offset, int count) {
object received = DefaultContext.DefaultPythonContext.Call(PythonOps.GetBoundAttr(DefaultContext.Default, _userSocket, SymbolTable.StringToId("recv")), count);
object received = DefaultContext.DefaultPythonContext.CallSplat(PythonOps.GetBoundAttr(DefaultContext.Default, _userSocket, SymbolTable.StringToId("recv")), count);
string data = Converter.ConvertToString(received);

return PythonAsciiEncoding.Instance.GetBytes(data, 0, data.Length, buffer, offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -386,33 +387,39 @@ internal AstGenerator(CompilationMode mode, CompilerContext/*!*/ context, Source
// Do we need conversion?
if (!CanAssign(type, expression.Type)) {
// Add conversion step to the AST
/*ActionExpression ae = expression as ActionExpression;
if (ae != null) {
DynamicExpression ae = expression as DynamicExpression;
ReducableDynamicExpression rde = expression as ReducableDynamicExpression;

if ((ae != null && ae.Binder is PythonBinaryOperationBinder) ||
(rde != null && rde.Binder is PythonBinaryOperationBinder)) {
// create a combo site which does the conversion
ParameterMappingInfo[] infos = new ParameterMappingInfo[ae.Arguments.Count];
PythonBinaryOperationBinder binder;
MSAst.Expression[] args;
if (ae != null) {
binder = (PythonBinaryOperationBinder)ae.Binder;
args = ArrayUtils.ToArray(ae.Arguments);
} else {
binder = (PythonBinaryOperationBinder)rde.Binder;
args = rde.Args;
}

ParameterMappingInfo[] infos = new ParameterMappingInfo[args.Length];
for (int i = 0; i<infos.Length; i++) {
infos[i] = ParameterMappingInfo.Parameter(i);
}

expression = Ast.Dynamic(
new ComboBinder(
new BinderMappingInfo(
(MetaAction)ae.BindingInfo,
infos
),
new BinderMappingInfo(
new ConversionBinder(
BinderState,
type,
ConversionResultKind.ExplicitCast
),
ParameterMappingInfo.Action(0)
expression = Globals.Dynamic(
BinderState.BinaryOperationRetType(
binder,
BinderState.Convert(
type,
ConversionResultKind.ExplicitCast
)
),
type,
ae.Arguments
args
);
} else*/ {
} else {
expression = Convert(
type,
ConversionResultKind.ExplicitCast,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ out tmp
),
type
);
} else if (op == PythonOperator.In) {
return AstUtils.Convert(
ag.Operation(
typeof(bool),
PythonOperationKind.Contains,
left,
right
),
type
);
}

PythonOperationKind action = PythonOperatorToAction(op);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public class GeneratorExpression : Expression {
internal override MSAst.Expression Transform(AstGenerator ag, Type type) {
MSAst.Expression func = _function.TransformToFunctionExpression(ag);

Debug.Assert(func.Type == typeof(PythonFunction));
// Generator expressions always return functions. We could do even better here when all PythonFunction's are in the same class.

return ag.Invoke(
typeof(object),
new CallSignature(1),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Microsoft Public License. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Microsoft Public License, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Microsoft Public License.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/

using System;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Dynamic;

namespace IronPython.Compiler {
/// <summary>
/// Provides a wrapper around "dynamic" expressions which we've opened coded (for optimized code generation).
///
/// This lets us recognize both normal Dynamic and our own Dynamic expressions and apply the combo binder on them.
/// </summary>
class ReducableDynamicExpression : Expression {
private readonly Expression/*!*/ _reduction;
private readonly DynamicMetaObjectBinder/*!*/ _binder;
private readonly Expression/*!*/[] _args;

public ReducableDynamicExpression(Expression/*!*/ reduction, DynamicMetaObjectBinder/*!*/ binder, Expression/*!*/[]/*!*/ args) {
_reduction = reduction;
_binder = binder;
_args = args;
}

public DynamicMetaObjectBinder/*!*/ Binder {
get {
return _binder;
}
}

public Expression/*!*/[]/*!*/ Args {
get {
return _args;
}
}

public override bool CanReduce {
get {
return true;
}
}

protected override ExpressionType NodeTypeImpl() {
return ExpressionType.Extension;
}

protected override Type TypeImpl() {
return _reduction.Type;
}

public override Expression Reduce() {
return _reduction;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,20 @@ class StaticGlobalAllocator : GlobalAllocator {
MSAst.Expression siteField = CreateFieldBuilderExpression(fi);
_sites.Add(new SiteInfo(binder, delegateType, fi, siteField));

return Ast.Call(
Ast.Field(
siteField,
siteType.GetField("Target")
return new ReducableDynamicExpression(
Ast.Call(
Ast.Field(
siteField,
siteType.GetField("Target")
),
delegateType.GetMethod("Invoke"),
ArrayUtils.Insert(
siteField,
args
)
),
delegateType.GetMethod("Invoke"),
ArrayUtils.Insert(
siteField,
args
)
binder,
args
);
}

Expand Down Expand Up @@ -281,6 +285,10 @@ class StaticGlobalAllocator : GlobalAllocator {
public override MSAst.Expression/*!*/ GetConstant(object value) {
// if we can emit the value and we won't be continiously boxing/unboxing
// then don't bother caching the value in a static field.

// TODO: Sometimes we don't want to pre-box the values, such as if it's an int
// going to a call site which can be strongly typed. We need to coordinate
// more with whoever consumes the values.
if (CompilerHelpers.CanEmitConstant(value, CompilerHelpers.GetType(value)) &&
!CompilerHelpers.GetType(value).IsValueType) {
return Ast.Constant(value);
Expand Down
35 changes: 32 additions & 3 deletions Merlin/Main/Languages/IronPython/IronPython/Compiler/Tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,12 @@ internal Tokenizer(ErrorSink errorSink, PythonCompilerOptions options, bool verb
if (start == '0') {
if (NextChar('x') || NextChar('X')) {
return ReadHexNumber();
} else if (_python26 && (NextChar('b') || NextChar('B'))) {
return ReadBinaryNumber();
} else if (_python26) {
if (NextChar('b') || NextChar('B')) {
return ReadBinaryNumber();
} else if (NextChar('o') || NextChar('O')) {
return ReadOctalNumber();
}
}
b = 8;
}
Expand Down Expand Up @@ -756,6 +760,31 @@ internal Tokenizer(ErrorSink errorSink, PythonCompilerOptions options, bool verb
}
}

private Token ReadOctalNumber() {
while (true) {
int ch = NextChar();

switch (ch) {
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
break;

case 'l': case 'L':
_buffer.MarkSingleLineTokenEnd();

// TODO: parse in place
return new ConstantValueToken(LiteralParser.ParseBigInteger(_buffer.GetTokenSubstring(2, _buffer.TokenLength - 2), 8));

default:
_buffer.Back();
_buffer.MarkSingleLineTokenEnd();

// TODO: parse in place
return new ConstantValueToken(ParseInteger(_buffer.GetTokenSubstring(2), 8));
}
}
}

private Token ReadHexNumber() {
while (true) {
int ch = NextChar();
Expand All @@ -778,7 +807,7 @@ internal Tokenizer(ErrorSink errorSink, PythonCompilerOptions options, bool verb
_buffer.MarkSingleLineTokenEnd();

// TODO: parse in place
return new ConstantValueToken(ParseInteger(_buffer.GetTokenSubstring(2, _buffer.TokenLength - 2), 16));
return new ConstantValueToken(ParseInteger(_buffer.GetTokenSubstring(2), 16));
}
}
}
Expand Down

0 comments on commit d522059

Please sign in to comment.