Skip to content

Commit

Permalink
Merge branch 'main' into gregli/regex-min
Browse files Browse the repository at this point in the history
  • Loading branch information
gregli-msft authored Mar 6, 2025
2 parents 14eac2d + c0b21b2 commit 587eff3
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
using System.Linq;
using System.Text;
using Microsoft.PowerFx.Core.Entities;
using Microsoft.PowerFx.Core.IR;
using Microsoft.PowerFx.Core.IR.Nodes;
using Microsoft.PowerFx.Core.IR.Symbols;
using Microsoft.PowerFx.Types;

namespace Microsoft.PowerFx.Tests
namespace Microsoft.PowerFx.Core.IR
{
// Pretty printer for tests.
// Stable - tests rely on this, so we need to fix the format
Expand Down
17 changes: 14 additions & 3 deletions src/libraries/Microsoft.PowerFx.Core/Types/DType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,11 @@ public DType Add(ref bool fError, DPath path, DName name, DType type)

Contracts.Assert(typeOuter.IsRecord || typeOuter.IsTable);

if (typeOuter.IsLazyType)
{
typeOuter = typeOuter.LazyTypeProvider.GetExpandedType(typeOuter.IsTable);
}

if (typeOuter.TypeTree.TryGetValue(name, out var typeCur))
{
fError = true;
Expand Down Expand Up @@ -1371,9 +1376,15 @@ public DType Add(DName name, DType type)
fullType = LazyTypeProvider.GetExpandedType(IsTable);
}

Contracts.Assert(!TypeTree.Contains(name));
var tree = TypeTree.SetItem(name, type);
var newType = new DType(Kind, tree, AssociatedDataSources, DisplayNameProvider);
Contracts.Assert(!fullType.TypeTree.Contains(name));

var tree = fullType.TypeTree.SetItem(name, type);
var newType = new DType(fullType.Kind, tree, AssociatedDataSources, fullType.DisplayNameProvider);

if (fullType.HasExpandInfo)
{
newType = CopyExpandInfo(newType, fullType);
}

return newType;
}
Expand Down
16 changes: 16 additions & 0 deletions src/libraries/Microsoft.PowerFx.Repl/PsuedoFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerFx.Core.IR;

#pragma warning disable CS0618 // Type or member is obsolete

Expand Down Expand Up @@ -47,6 +48,21 @@ await repl.Output.WriteLineAsync(irText, OutputKind.Repl, cancel)
public string Name => "IR";
}

/// <summary>
/// Print the compact IR() of an expression.
/// </summary>
public class CIRPseudoFunction : IPseudoFunction
{
public async Task ExecuteAsync(CheckResult checkResult, PowerFxREPL repl, CancellationToken cancel)
{
var irText = PrettyPrintIRVisitor.ToString(checkResult);
await repl.Output.WriteLineAsync(irText, OutputKind.Repl, cancel)
.ConfigureAwait(false);
}

public string Name => "CIR";
}

/// <summary>
/// Print the suggestions of an expression.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
using System.Text;
using System.Threading;
using Microsoft.PowerFx.Core.Entities;
using Microsoft.PowerFx.Core.IR;
using Microsoft.PowerFx.Core.Localization;
using Microsoft.PowerFx.Core.Types;
using Microsoft.PowerFx.Core.Types.Enums;
using Microsoft.PowerFx.Core.Utils;
using Microsoft.PowerFx.Syntax;
using Microsoft.PowerFx.Tests;
using Microsoft.PowerFx.Types;
using static Microsoft.PowerFx.Tests.BindingEngineTests;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ public void AddField()
Assert.Equal(3, _getter1CalledCount);

Assert.Equal("![Bar:s, Baz:b, Foo:n, Test:O]", type1.ToString());

// test Add overload without path
// using lazy record
var type2 = _lazyRecord1._type.Add(new DName("ThisRecord"), TestUtils.DT("![a: n]"));
Assert.Equal("r!", _lazyRecord1._type.ToString());
Assert.Equal("![Bar:s, Baz:b, Foo:n, ThisRecord:![a:n]]", type2.ToString());

// using lazy table
var type3 = _lazyTable2._type.Add(new DName("ThisRecord"), TestUtils.DT("![Value:O]"));
Assert.Equal("r*", _lazyTable2._type.ToString());
Assert.Equal(2, _getter2CalledCount);
Assert.Equal("*[Nested:r!, Qux:n, ThisRecord:![Value:O]]", type3.ToString());

// test Add overload with path and LazyType as field
// using lazy record
var type4 = _lazyRecord2._type.Add(ref fError, DPath.Root.Append(new DName("Nested")), new DName("New"), DType.Number);
Assert.Equal("![Nested:![Bar:s, Baz:b, Foo:n, New:n], Qux:n]", type4.ToString());

// using lazy table
var type5 = _lazyTable2._type.Add(ref fError, DPath.Root.Append(new DName("Nested")), new DName("NewCol"), TestUtils.DT("![a:n]"));
Assert.Equal("*[Nested:![Bar:s, Baz:b, Foo:n, NewCol:![a:n]], Qux:n]", type5.ToString());
}

[Fact]
Expand Down
15 changes: 15 additions & 0 deletions src/tests/Microsoft.PowerFx.Repl.Tests.Shared/ReplTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ public void TestIR()
Assert.Equal("AddDecimals:w(1:w, 2:w)", log);
}

[Fact]
public void TestCIR()
{
_repl.AddPseudoFunction(new CIRPseudoFunction());

// CIR() function is a meta-function that
// circumvents eval and dumps the compact IR.
_repl.HandleLine("CIR(1+2)");

Assert.Empty(_output.Get(OutputKind.Error));

var log = _output.Get(OutputKind.Repl);
Assert.Equal("AddDecimals(1,2)", log);
}

[Fact]
public void ExtraSymols()
{
Expand Down
1 change: 1 addition & 0 deletions src/tools/Repl/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public MyRepl()

this.EnableSampleUserObject();
this.AddPseudoFunction(new IRPseudoFunction());
this.AddPseudoFunction(new CIRPseudoFunction());
this.AddPseudoFunction(new SuggestionsPseudoFunction());

this.ParserOptions = new ParserOptions() { AllowsSideEffects = true, NumberIsFloat = _numberIsFloat, TextFirst = _textFirst };
Expand Down

0 comments on commit 587eff3

Please sign in to comment.