Skip to content

Commit

Permalink
Added string domain
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Oct 3, 2023
1 parent b055268 commit fae6072
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 19 deletions.
4 changes: 0 additions & 4 deletions src/Draco.Compiler/Internal/Binding/Binder_BoundPattern.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Draco.Compiler.Internal.BoundTree;
using Draco.Compiler.Internal.Diagnostics;
using Draco.Compiler.Internal.Solver;
Expand Down
4 changes: 0 additions & 4 deletions src/Draco.Compiler/Internal/Binding/Binder_UntypedPattern.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Draco.Compiler.Api.Syntax;
using Draco.Compiler.Internal.Diagnostics;
using Draco.Compiler.Internal.Solver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Draco.Compiler.Internal.Binding;
using Draco.Compiler.Internal.BoundTree;
using Draco.Compiler.Internal.Symbols;
using Draco.Compiler.Internal.Utilities;
Expand Down Expand Up @@ -114,7 +111,7 @@ public override void SubtractPattern(BoundPattern pattern)

public override string ToString()
{
if (this.IsEmpty) return $"[{this.minValue}; {this.maxValue}]";
if (this.IsEmpty) return "empty";

var parts = new List<string>();
if (this.subtracted[0].From != this.minValue) parts.Add($"[{this.minValue}; {this.subtracted[0].From})");
Expand Down
79 changes: 79 additions & 0 deletions src/Draco.Compiler/Internal/FlowAnalysis/Domain/StringDomain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Draco.Compiler.Internal.BoundTree;
using Draco.Compiler.Internal.Symbols;
using Draco.Compiler.Internal.Utilities;

namespace Draco.Compiler.Internal.FlowAnalysis.Domain;

internal sealed class StringDomain : ValueDomain
{
public override bool IsEmpty => this.allSubtracted;

private readonly TypeSymbol backingType;
private readonly HashSet<string> subtracted;
private bool allSubtracted;

private StringDomain(TypeSymbol backingType, HashSet<string> subtracted, bool allSubtracted)
{
this.backingType = backingType;
this.subtracted = subtracted;
this.allSubtracted = allSubtracted;
}

public StringDomain(TypeSymbol backingType)
: this(backingType, new(), false)
{
}

public override string ToString() => this.allSubtracted
? "empty"
: $"Universe \\ {{{string.Join(", ", this.subtracted.Select(StringUtils.Unescape))}}}";

public override ValueDomain Clone() =>
new StringDomain(this.backingType, this.subtracted.ToHashSet(), this.allSubtracted);

public override BoundPattern? SamplePattern()
{
if (this.allSubtracted) return null;

// Check some trivial ones
if (!this.subtracted.Contains(string.Empty)) return this.ToPattern(string.Empty);

// We use the same trick as proving that you can't list all possible reals
// We construct a string that differs in the first char from the first string,
// in the second char of the second string, ...

var result = new StringBuilder();
var i = 0;
foreach (var str in this.subtracted)
{
var ithChar = str.Length < i ? str[i] : '\0';
result.Append(ithChar == 'a' ? 'b' : 'a');
}

return this.ToPattern(result.ToString());
}

public override void SubtractPattern(BoundPattern pattern)
{
if (this.allSubtracted) return;

switch (pattern)
{
case BoundDiscardPattern:
this.subtracted.Clear();
this.allSubtracted = true;
break;
case BoundLiteralPattern litPattern when litPattern.Value is string str:
this.subtracted.Add(str);
break;
default:
throw new ArgumentException("illegal pattern for string domain", nameof(pattern));
}
}

private BoundPattern ToPattern(string text) => throw new NotImplementedException();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Draco.Compiler.Internal.BoundTree;
using Draco.Compiler.Internal.Symbols;
using Draco.Compiler.Internal.Symbols.Synthetized;
Expand Down
3 changes: 0 additions & 3 deletions src/Draco.Compiler/Internal/Utilities/BinarySearch.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Draco.Compiler.Internal.Utilities;

Expand Down

0 comments on commit fae6072

Please sign in to comment.