Created
April 6, 2022 10:44
-
-
Save jyuch/415b718d3a4c33fbb856ec91ac42b8f0 to your computer and use it in GitHub Desktop.
C#でDSLをでっち上げるアレ
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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Runtime.CompilerServices; | |
namespace ConsoleApp1 | |
{ | |
using IO = System.IO; | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
AsyncMain(); | |
} | |
static async void AsyncMain() | |
{ | |
var result = await "C:\\aaa" with | |
{ | |
_ = new _ | |
{ | |
await "bbb" with | |
{ | |
_ = new _ | |
{ | |
"ddd" | |
} | |
}, | |
await "ccc" with | |
{ | |
_ = new _ | |
{ | |
"eee", | |
"fff" | |
} | |
} | |
} | |
}; | |
Show(result); | |
} | |
static void Show(Path path) | |
{ | |
ShowImpl(path.Name, path._); | |
} | |
static void ShowImpl(string parent, IEnumerable<Path> paths) | |
{ | |
Console.WriteLine(parent); | |
foreach (var it in paths) | |
{ | |
var cur = IO::Path.Combine(parent, it.Name); | |
ShowImpl(cur, it._); | |
} | |
} | |
} | |
internal static class StringExtension | |
{ | |
internal static StringPathAwaiter GetAwaiter(this string @this) | |
{ | |
return new StringPathAwaiter(@this); | |
} | |
} | |
internal class StringPathAwaiter : INotifyCompletion | |
{ | |
private readonly string _path; | |
internal StringPathAwaiter(string path) | |
{ | |
_path = path; | |
} | |
public bool IsCompleted { get { return false; } } | |
public void OnCompleted(Action continuation) | |
{ | |
continuation(); | |
} | |
public Path GetResult() | |
{ | |
return new Path(_path); | |
} | |
} | |
internal record Path | |
{ | |
public string Name { get; } | |
internal Path(string value) | |
{ | |
Name = value; | |
} | |
public override string ToString() | |
{ | |
return Name; | |
} | |
public static Path operator /(Path x, string y) | |
{ | |
return new Path(IO::Path.Combine(x.Name, y)); | |
} | |
public IEnumerable<Path> _ { set; get; } = Enumerable.Empty<Path>(); | |
public static implicit operator Path(string path) | |
{ | |
return new Path(path); | |
} | |
} | |
internal class _ : IEnumerable<Path> | |
{ | |
private List<Path> _backing = new List<Path>(); | |
public void Add(Path path) | |
{ | |
_backing.Add(path); | |
} | |
public IEnumerator<Path> GetEnumerator() | |
{ | |
return _backing.GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
public static implicit operator List<Path>(_ x) | |
{ | |
return x._backing; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment