Skip to content

Commit b679128

Browse files
committed
Some for ss
1 parent 647499c commit b679128

File tree

11 files changed

+337
-9
lines changed

11 files changed

+337
-9
lines changed

global.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"sdk": {
3-
"version": "5.0",
4-
"rollForward": "latestMajor",
3+
"version": "5.0.0",
4+
"rollForward": "latestMinor",
55
"allowPrerelease": false
66
}
77
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Pidgin;
2+
using static Pidgin.Parser;
3+
namespace SimpleStateMachine.StructuralSearch.Sandbox
4+
{
5+
public static class Common
6+
{
7+
8+
}
9+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Collections.Immutable;
3+
using Pidgin;
4+
using Pidgin.Expression;
5+
using static Pidgin.Parser;
6+
using static Pidgin.Parser<char>;
7+
8+
namespace SimpleStateMachine.StructuralSearch.Sandbox
9+
{
10+
public static class ExprParser
11+
{
12+
private static Parser<char, T> Tok<T>(Parser<char, T> token)
13+
=> Try(token).Before(SkipWhitespaces);
14+
private static Parser<char, string> Tok(string token)
15+
=> Tok(String(token));
16+
17+
private static Parser<char, T> Parenthesised<T>(Parser<char, T> parser)
18+
=> parser.Between(Tok("("), Tok(")"));
19+
20+
private static Parser<char, Func<IExpr, IExpr, IExpr>> Binary(Parser<char, BinaryOperatorType> op)
21+
=> op.Select<Func<IExpr, IExpr, IExpr>>(type => (l, r) => new BinaryOp(type, l, r));
22+
private static Parser<char, Func<IExpr, IExpr>> Unary(Parser<char, UnaryOperatorType> op)
23+
=> op.Select<Func<IExpr, IExpr>>(type => o => new UnaryOp(type, o));
24+
25+
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Add
26+
= Binary(Tok("+").ThenReturn(BinaryOperatorType.Add));
27+
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Sub
28+
= Binary(Tok("-").ThenReturn(BinaryOperatorType.Sub));
29+
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Div
30+
= Binary(Tok("/").ThenReturn(BinaryOperatorType.Div));
31+
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Mul
32+
= Binary(Tok("*").ThenReturn(BinaryOperatorType.Mul));
33+
private static readonly Parser<char, Func<IExpr, IExpr>> Decrement
34+
= Unary(Tok("--").ThenReturn(UnaryOperatorType.Decrement));
35+
private static readonly Parser<char, Func<IExpr, IExpr>> Increment
36+
= Unary(Tok("++").ThenReturn(UnaryOperatorType.Increment));
37+
38+
private static readonly Parser<char, Func<IExpr, IExpr>> Minus
39+
= Unary(Tok("-").ThenReturn(UnaryOperatorType.Minus));
40+
private static readonly Parser<char, Func<IExpr, IExpr>> Plus
41+
= Unary(Tok("+").ThenReturn(UnaryOperatorType.Plus));
42+
43+
private static readonly Parser<char, IExpr> Identifier
44+
= Tok(Letter.Then(LetterOrDigit.ManyString(), (h, t) => h + t))
45+
.Select<IExpr>(name => new Identifier(name))
46+
.Labelled("identifier");
47+
48+
private static readonly Parser<char, IExpr> Literal
49+
= Tok(LongNum)
50+
.Select<IExpr>(value => new Literal(value))
51+
.Labelled("integer literal");
52+
53+
private static Parser<char, Func<IExpr, IExpr>> Call(Parser<char, IExpr> subExpr)
54+
=> Parenthesised(subExpr.Separated(Tok(",")))
55+
.Select<Func<IExpr, IExpr>>(args => method => new Call(method, args.ToImmutableArray()))
56+
.Labelled("function call");
57+
58+
private static readonly Parser<char, IExpr> Expr = ExpressionParser.Build<char, IExpr>(
59+
expr => (
60+
OneOf(
61+
Identifier,
62+
Literal,
63+
Parenthesised(expr).Labelled("parenthesised expression")
64+
),
65+
new[]
66+
{
67+
Operator.PostfixChainable(Call(expr)),
68+
Operator.Prefix(Decrement)
69+
.And(Operator.Prefix(Increment))
70+
.And(Operator.Prefix(Minus))
71+
.And(Operator.Prefix(Plus)),
72+
Operator.InfixL(Mul),
73+
Operator.InfixL(Div),
74+
Operator.InfixL(Add),
75+
Operator.InfixL(Sub)
76+
}
77+
)
78+
).Labelled("expression");
79+
80+
public static IExpr ParseOrThrow(string input)
81+
=> Expr.ParseOrThrow(input);
82+
83+
}
84+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using Pidgin;
3+
4+
namespace SimpleStateMachine.StructuralSearch.Sandbox.Extensions
5+
{
6+
public static class ParserExtensions
7+
{
8+
// public static Parser<TToken, TOut> WithResult<TToken, TOut>(this Parser<TToken, TOut> parser, Func<TToken, SourcePos, TOut> transformResult)
9+
// {
10+
// = Parser<TToken>.CurrentSourcePosDelta.Select<SourcePos>((Func<SourcePosDelta, SourcePos>) (d => new SourcePos(1, 1) + d));
11+
// parser.Select()
12+
// return this.Select<U>((Func<T, U>)(_ => result));
13+
// }
14+
}
15+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using System;
2+
using System.Collections.Immutable;
3+
using System.Linq;
4+
5+
namespace SimpleStateMachine.StructuralSearch.Sandbox
6+
{
7+
public interface IExpr
8+
{
9+
public double Invoke();
10+
}
11+
12+
public class Identifier : IExpr
13+
{
14+
public string Name { get; }
15+
16+
public Identifier(string name)
17+
{
18+
Name = name;
19+
}
20+
21+
public bool Equals(IExpr? other)
22+
=> other is Identifier i && this.Name == i.Name;
23+
24+
public double Invoke()
25+
{
26+
return 0;
27+
}
28+
}
29+
30+
public class Literal : IExpr
31+
{
32+
public double Value { get; }
33+
34+
public Literal(double value)
35+
{
36+
Value = value;
37+
}
38+
39+
public bool Equals(IExpr? other)
40+
=> other is Literal l && this.Value == l.Value;
41+
42+
public double Invoke()
43+
{
44+
return Value;
45+
}
46+
}
47+
48+
public class Call : IExpr
49+
{
50+
public IExpr Expr { get; }
51+
public ImmutableArray<IExpr> Arguments { get; }
52+
53+
public Call(IExpr expr, ImmutableArray<IExpr> arguments)
54+
{
55+
Expr = expr;
56+
Arguments = arguments;
57+
}
58+
59+
public bool Equals(IExpr? other)
60+
=> other is Call c
61+
&& ((Object)this.Expr).Equals(c.Expr)
62+
&& this.Arguments.SequenceEqual(c.Arguments);
63+
64+
public double Invoke()
65+
{
66+
throw new NotImplementedException();
67+
}
68+
}
69+
70+
public enum UnaryOperatorType
71+
{
72+
Increment,
73+
Decrement,
74+
Plus,
75+
Minus
76+
}
77+
public class UnaryOp : IExpr
78+
{
79+
public UnaryOperatorType Type { get; }
80+
public IExpr Expr { get; }
81+
82+
public UnaryOp(UnaryOperatorType type, IExpr expr)
83+
{
84+
Type = type;
85+
Expr = expr;
86+
}
87+
88+
public bool Equals(IExpr? other)
89+
=> other is UnaryOp u
90+
&& this.Type == u.Type
91+
&& ((Object)this.Expr).Equals(u.Expr);
92+
93+
public double Invoke()
94+
{
95+
return Type switch
96+
{
97+
UnaryOperatorType.Increment => Expr.Invoke() + 1,
98+
UnaryOperatorType.Decrement => Expr.Invoke() - 1,
99+
UnaryOperatorType.Plus => - Expr.Invoke(),
100+
UnaryOperatorType.Minus => + Expr.Invoke(),
101+
_ => throw new ArgumentOutOfRangeException()
102+
};
103+
}
104+
}
105+
106+
public enum BinaryOperatorType
107+
{
108+
Add, // +
109+
Sub, // -
110+
Mul, // *
111+
Div, // /
112+
}
113+
public class BinaryOp : IExpr
114+
{
115+
public BinaryOperatorType Type { get; }
116+
public IExpr Left { get; }
117+
public IExpr Right { get; }
118+
119+
public BinaryOp(BinaryOperatorType type, IExpr left, IExpr right)
120+
{
121+
Type = type;
122+
Left = left;
123+
Right = right;
124+
}
125+
126+
public bool Equals(IExpr? other)
127+
=> other is BinaryOp b
128+
&& this.Type == b.Type
129+
&& ((Object)this.Left).Equals(b.Left)
130+
&& ((Object)this.Right).Equals(b.Right);
131+
132+
public double Invoke()
133+
{
134+
return Type switch
135+
{
136+
BinaryOperatorType.Add => Left.Invoke() + Right.Invoke(),
137+
BinaryOperatorType.Mul => Left.Invoke() * Right.Invoke(),
138+
BinaryOperatorType.Div => Left.Invoke() / Right.Invoke(),
139+
BinaryOperatorType.Sub => Left.Invoke() - Right.Invoke(),
140+
};
141+
}
142+
}
143+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Pidgin;
2+
using Pidgin.Expression;
3+
using static Pidgin.Parser;
4+
5+
namespace SimpleStateMachine.StructuralSearch.Sandbox
6+
{
7+
8+
public static class PlaceholderParser
9+
{
10+
static string _placeholderSeparator = "$";
11+
static char _underScore = '_';
12+
13+
private static readonly Parser<char, string> Identifier
14+
= Letter.Then(LetterOrDigit.Or(Char(_underScore)).ManyString(), (h, t) => h + t);
15+
16+
private static Parser<char, T> PlaceholderSeparator<T>(Parser<char, T> parser)
17+
=> parser.Between(String(_placeholderSeparator), String(_placeholderSeparator));
18+
19+
public static Parser<char, string> Placeholder()
20+
=> PlaceholderSeparator(Identifier);
21+
22+
}
23+
}
Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,54 @@
1-
// See https://aka.ms/new-console-template for more information
2-
3-
using System;
1+
using System;
2+
using System.Collections.Generic;
3+
using Pidgin;
4+
using Pidgin.Expression;
5+
using static Pidgin.Parser;
6+
using static Pidgin.Parser<char>;
47

58
namespace SimpleStateMachine.StructuralSearch.Sandbox
69
{
7-
class Programm
10+
internal static class Program
811
{
12+
public class Test
13+
{
14+
15+
}
916
static void Main(string[] args)
1017
{
11-
Console.WriteLine("Hello, World!");
18+
Parser<char, double> r1 = Real;
19+
Parser<char, double> r2 = Real;
20+
Parser<char, IEnumerable<char>> r1r2 = Whitespaces.Then(Char('+')).Then(Whitespaces);
21+
Parser<char, double> rParser = Map((foo, _, bar) => bar + foo, r1, r1r2, r2);
22+
Parser<char, string> rParser2 = rParser.Select(x=>x.ToString());
23+
24+
25+
Parser<char, string> Var = Letter.Then(LetterOrDigit.ManyString(), (h, t) => h + t);
26+
Parser<char, string> sParser = Map((foo, _, bar) => foo + bar, Var, r1r2, Var);
27+
28+
29+
var js = String("js").ThenReturn(rParser2);
30+
var other = Whitespaces.ThenReturn(sParser);
31+
32+
var result = js.Or(other).Then(CurrentPos);
33+
var ds = r1r2.Select(x=>x);
34+
var tds = CurrentOffset;
35+
36+
var input = " + ";
37+
// var t2 = result.ParseOrThrow("55 + 25").WithResult(x => x);
38+
var dsdfsdf = ds.ParseOrThrow(input);
39+
var t2 = r1r2.ParseOrThrow(input);
40+
var t = result.ParseOrThrow("js foo + bar");
41+
42+
var t23 = PlaceholderParser.Placeholder().ParseOrThrow("$test$");
43+
44+
// Parser<char, IEnumerable<char>> parser3 = Whitespaces.Then(Char('+')).Then(Whitespaces);
45+
// Parser<char, double> sequencedParser = Map((foo, _,bar) => bar + foo, parser1, parser3, parser2);
46+
//
47+
// var or = String("")
48+
//
49+
//
50+
// var t = sequencedParser.ParseOrThrow("5+5");
51+
// var g = t.Invoke();
1252
}
1353
}
14-
15-
}
54+
}

src/SimpleStateMachine.StructuralSearch.Sandbox/SimpleStateMachine.StructuralSearch.Sandbox.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
<TargetFramework>net5.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<LangVersion>9</LangVersion>
89
</PropertyGroup>
910

11+
<ItemGroup>
12+
<PackageReference Include="Pidgin" Version="3.0.0" />
13+
</ItemGroup>
14+
1015
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace SimpleStateMachine.StructuralSearch.Sandbox
2+
{
3+
public static class StructuralSearch
4+
{
5+
6+
}
7+
}

src/SimpleStateMachine.StructuralSearch.Tests/SimpleStateMachine.StructuralSearch.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<Nullable>enable</Nullable>
66

77
<IsPackable>false</IsPackable>
8+
9+
<LangVersion>9</LangVersion>
810
</PropertyGroup>
911

1012
<ItemGroup>

0 commit comments

Comments
 (0)