-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83b9ccf
commit af85c10
Showing
28 changed files
with
632 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace MiniZinc.Parser; | ||
|
||
public sealed class BoolDatum(bool value) : MiniZincDatum | ||
{ | ||
public bool Value => value; | ||
|
||
public static implicit operator bool(BoolDatum expr) => expr.Value; | ||
|
||
public override bool Equals(object? obj) | ||
{ | ||
if (obj is not bool other) | ||
return false; | ||
if (!value.Equals(other)) | ||
return false; | ||
return true; | ||
} | ||
|
||
public override string ToString() => Value.ToString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace MiniZinc.Parser; | ||
|
||
public sealed class BoolSet(List<bool> values) : SetDatum<bool>(values) { } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
namespace MiniZinc.Parser; | ||
|
||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
/// <summary> | ||
/// The values that that appear in MiniZinc data files or | ||
/// syntax. | ||
/// </summary> | ||
public abstract class MiniZincDatum | ||
{ | ||
public string Write(WriteOptions? options = null) | ||
{ | ||
var writer = new Writer(options); | ||
writer.WriteValue(this); | ||
var mzn = writer.ToString(); | ||
return mzn; | ||
} | ||
|
||
public static readonly MiniZincDatum Empty = new EmptyDatum(); | ||
|
||
public static readonly MiniZincDatum True = new BoolDatum(true); | ||
|
||
public static readonly MiniZincDatum False = new BoolDatum(false); | ||
|
||
public static MiniZincDatum Float(decimal f) => new FloatDatum(f); | ||
|
||
public static MiniZincDatum Int(int i) => new IntDatum(i); | ||
|
||
public static MiniZincDatum String(string s) => new StringDatum(s); | ||
|
||
public static MiniZincDatum FromJson(JsonNode? node) | ||
{ | ||
switch (node) | ||
{ | ||
case JsonArray array: | ||
return FromJson((JsonNode?)array); | ||
case JsonObject obj: | ||
return FromJson((JsonNode?)obj); | ||
case JsonValue val: | ||
return FromJson((JsonNode?)val); | ||
default: | ||
return Empty; | ||
} | ||
} | ||
|
||
public static MiniZincDatum FromJson(JsonObject obj) | ||
{ | ||
Dictionary<string, MiniZincDatum> dict = []; | ||
foreach (var (key, node) in obj) | ||
{ | ||
var value = FromJson(node); | ||
dict[key] = value; | ||
} | ||
|
||
var data = new RecordDatum(dict); | ||
return data; | ||
} | ||
|
||
public static MiniZincDatum FromJson(JsonArray array) | ||
{ | ||
List<MiniZincDatum> items = []; | ||
foreach (var node in array) | ||
{ | ||
var item = FromJson(node); | ||
items.Add(item); | ||
} | ||
var data = new DatumArray(items); | ||
return data; | ||
} | ||
|
||
public static MiniZincDatum FromJson(JsonValue node) => | ||
node.GetValueKind() switch | ||
{ | ||
JsonValueKind.Null => Empty, | ||
JsonValueKind.True => True, | ||
JsonValueKind.False => False, | ||
JsonValueKind.Number when node.TryGetValue<decimal>(out var dec) => Float(dec), | ||
JsonValueKind.Number when node.TryGetValue<int>(out var i) => Int(i), | ||
JsonValueKind.String => String(node.GetValue<string>()), | ||
_ => throw new ArgumentException($"Could not parse {node} as a datum") | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace MiniZinc.Parser; | ||
|
||
public sealed class EmptyDatum : MiniZincDatum | ||
{ | ||
public override bool Equals(object? obj) => obj is EmptyDatum; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace MiniZinc.Parser; | ||
|
||
public sealed class FloatDatum(decimal value) : MiniZincDatum | ||
{ | ||
public decimal Value => value; | ||
|
||
public static implicit operator decimal(FloatDatum expr) => expr.Value; | ||
|
||
public override bool Equals(object? obj) | ||
{ | ||
if (obj is not decimal other) | ||
return false; | ||
if (!value.Equals(other)) | ||
return false; | ||
return true; | ||
} | ||
|
||
public override string ToString() => Value.ToString(); | ||
} |
Oops, something went wrong.