Skip to content

Commit b5e1b61

Browse files
committed
Use file scoped namespaces
1 parent 1b0cd25 commit b5e1b61

File tree

90 files changed

+2219
-2309
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+2219
-2309
lines changed
Lines changed: 109 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,141 @@
11
using System.Collections.Immutable;
22

3-
namespace SimpleStateMachine.StructuralSearch.Sandbox
3+
namespace SimpleStateMachine.StructuralSearch.Sandbox;
4+
5+
public interface IExpr
46
{
5-
public interface IExpr
6-
{
7-
public double Invoke();
8-
}
7+
public double Invoke();
8+
}
99

10-
public class Identifier : IExpr
11-
{
12-
public string Name { get; }
10+
public class Identifier : IExpr
11+
{
12+
public string Name { get; }
1313

14-
public Identifier(string name)
15-
{
16-
Name = name;
17-
}
14+
public Identifier(string name)
15+
{
16+
Name = name;
17+
}
1818

19-
public bool Equals(IExpr? other)
20-
=> other is Identifier i && this.Name == i.Name;
19+
public bool Equals(IExpr? other)
20+
=> other is Identifier i && this.Name == i.Name;
2121

22-
public double Invoke()
23-
{
24-
return 0;
25-
}
22+
public double Invoke()
23+
{
24+
return 0;
2625
}
26+
}
2727

28-
public class Literal : IExpr
29-
{
30-
public double Value { get; }
28+
public class Literal : IExpr
29+
{
30+
public double Value { get; }
3131

32-
public Literal(double value)
33-
{
34-
Value = value;
35-
}
32+
public Literal(double value)
33+
{
34+
Value = value;
35+
}
3636

37-
public bool Equals(IExpr? other)
38-
=> other is Literal l && this.Value == l.Value;
37+
public bool Equals(IExpr? other)
38+
=> other is Literal l && this.Value == l.Value;
3939

40-
public double Invoke()
41-
{
42-
return Value;
43-
}
40+
public double Invoke()
41+
{
42+
return Value;
4443
}
44+
}
4545

46-
public class Call : IExpr
47-
{
48-
public IExpr Expr { get; }
49-
public ImmutableArray<IExpr> Arguments { get; }
46+
public class Call : IExpr
47+
{
48+
public IExpr Expr { get; }
49+
public ImmutableArray<IExpr> Arguments { get; }
5050

51-
public Call(IExpr expr, ImmutableArray<IExpr> arguments)
52-
{
53-
Expr = expr;
54-
Arguments = arguments;
55-
}
51+
public Call(IExpr expr, ImmutableArray<IExpr> arguments)
52+
{
53+
Expr = expr;
54+
Arguments = arguments;
55+
}
5656

57-
public bool Equals(IExpr? other)
58-
=> other is Call c
59-
&& ((Object)this.Expr).Equals(c.Expr)
60-
&& this.Arguments.SequenceEqual(c.Arguments);
57+
public bool Equals(IExpr? other)
58+
=> other is Call c
59+
&& ((Object)this.Expr).Equals(c.Expr)
60+
&& this.Arguments.SequenceEqual(c.Arguments);
6161

62-
public double Invoke()
63-
{
64-
throw new NotImplementedException();
65-
}
62+
public double Invoke()
63+
{
64+
throw new NotImplementedException();
6665
}
66+
}
6767

68-
public enum UnaryOperatorType
68+
public enum UnaryOperatorType
69+
{
70+
Increment,
71+
Decrement,
72+
Plus,
73+
Minus
74+
}
75+
public class UnaryOp : IExpr
76+
{
77+
public UnaryOperatorType Type { get; }
78+
public IExpr Expr { get; }
79+
80+
public UnaryOp(UnaryOperatorType type, IExpr expr)
6981
{
70-
Increment,
71-
Decrement,
72-
Plus,
73-
Minus
82+
Type = type;
83+
Expr = expr;
7484
}
75-
public class UnaryOp : IExpr
76-
{
77-
public UnaryOperatorType Type { get; }
78-
public IExpr Expr { get; }
7985

80-
public UnaryOp(UnaryOperatorType type, IExpr expr)
81-
{
82-
Type = type;
83-
Expr = expr;
84-
}
86+
public bool Equals(IExpr? other)
87+
=> other is UnaryOp u
88+
&& this.Type == u.Type
89+
&& ((Object)this.Expr).Equals(u.Expr);
8590

86-
public bool Equals(IExpr? other)
87-
=> other is UnaryOp u
88-
&& this.Type == u.Type
89-
&& ((Object)this.Expr).Equals(u.Expr);
90-
91-
public double Invoke()
91+
public double Invoke()
92+
{
93+
return Type switch
9294
{
93-
return Type switch
94-
{
95-
UnaryOperatorType.Increment => Expr.Invoke() + 1,
96-
UnaryOperatorType.Decrement => Expr.Invoke() - 1,
97-
UnaryOperatorType.Plus => - Expr.Invoke(),
98-
UnaryOperatorType.Minus => + Expr.Invoke(),
99-
_ => throw new ArgumentOutOfRangeException(nameof(Type), Type, null)
100-
};
101-
}
95+
UnaryOperatorType.Increment => Expr.Invoke() + 1,
96+
UnaryOperatorType.Decrement => Expr.Invoke() - 1,
97+
UnaryOperatorType.Plus => - Expr.Invoke(),
98+
UnaryOperatorType.Minus => + Expr.Invoke(),
99+
_ => throw new ArgumentOutOfRangeException(nameof(Type), Type, null)
100+
};
102101
}
102+
}
103+
104+
public enum BinaryOperatorType
105+
{
106+
Add, // +
107+
Sub, // -
108+
Mul, // *
109+
Div, // /
110+
}
111+
public class BinaryOp : IExpr
112+
{
113+
public BinaryOperatorType Type { get; }
114+
public IExpr Left { get; }
115+
public IExpr Right { get; }
103116

104-
public enum BinaryOperatorType
117+
public BinaryOp(BinaryOperatorType type, IExpr left, IExpr right)
105118
{
106-
Add, // +
107-
Sub, // -
108-
Mul, // *
109-
Div, // /
119+
Type = type;
120+
Left = left;
121+
Right = right;
110122
}
111-
public class BinaryOp : IExpr
112-
{
113-
public BinaryOperatorType Type { get; }
114-
public IExpr Left { get; }
115-
public IExpr Right { get; }
116123

117-
public BinaryOp(BinaryOperatorType type, IExpr left, IExpr right)
118-
{
119-
Type = type;
120-
Left = left;
121-
Right = right;
122-
}
123-
124-
public bool Equals(IExpr? other)
125-
=> other is BinaryOp b
126-
&& this.Type == b.Type
127-
&& ((Object)this.Left).Equals(b.Left)
128-
&& ((Object)this.Right).Equals(b.Right);
129-
130-
public double Invoke()
124+
public bool Equals(IExpr? other)
125+
=> other is BinaryOp b
126+
&& this.Type == b.Type
127+
&& ((Object)this.Left).Equals(b.Left)
128+
&& ((Object)this.Right).Equals(b.Right);
129+
130+
public double Invoke()
131+
{
132+
return Type switch
131133
{
132-
return Type switch
133-
{
134-
BinaryOperatorType.Add => Left.Invoke() + Right.Invoke(),
135-
BinaryOperatorType.Mul => Left.Invoke() * Right.Invoke(),
136-
BinaryOperatorType.Div => Left.Invoke() / Right.Invoke(),
137-
BinaryOperatorType.Sub => Left.Invoke() - Right.Invoke(),
138-
_ => throw new ArgumentOutOfRangeException(nameof(Type), Type, null)
139-
};
140-
}
134+
BinaryOperatorType.Add => Left.Invoke() + Right.Invoke(),
135+
BinaryOperatorType.Mul => Left.Invoke() * Right.Invoke(),
136+
BinaryOperatorType.Div => Left.Invoke() / Right.Invoke(),
137+
BinaryOperatorType.Sub => Left.Invoke() - Right.Invoke(),
138+
_ => throw new ArgumentOutOfRangeException(nameof(Type), Type, null)
139+
};
141140
}
142141
}

src/SimpleStateMachine.StructuralSearch.Sandbox/ExprParser.cs

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,73 @@
22
using Pidgin.Expression;
33
using static Pidgin.Parser;
44

5-
namespace SimpleStateMachine.StructuralSearch.Sandbox
5+
namespace SimpleStateMachine.StructuralSearch.Sandbox;
6+
7+
public static class ExprParser
68
{
7-
public static class ExprParser
8-
{
9-
private static Parser<char, T> Tok<T>(Parser<char, T> token)
10-
=> Try(token).Before(SkipWhitespaces);
11-
private static Parser<char, string> Tok(string token)
12-
=> Tok(String(token));
9+
private static Parser<char, T> Tok<T>(Parser<char, T> token)
10+
=> Try(token).Before(SkipWhitespaces);
11+
private static Parser<char, string> Tok(string token)
12+
=> Tok(String(token));
1313

14-
private static Parser<char, T> Parenthesised<T>(Parser<char, T> parser)
15-
=> parser.Between(Tok("("), Tok(")"));
14+
private static Parser<char, T> Parenthesised<T>(Parser<char, T> parser)
15+
=> parser.Between(Tok("("), Tok(")"));
1616

17-
private static Parser<char, Func<IExpr, IExpr, IExpr>> Binary(Parser<char, BinaryOperatorType> op)
18-
=> op.Select<Func<IExpr, IExpr, IExpr>>(type => (l, r) => new BinaryOp(type, l, r));
19-
private static Parser<char, Func<IExpr, IExpr>> Unary(Parser<char, UnaryOperatorType> op)
20-
=> op.Select<Func<IExpr, IExpr>>(type => o => new UnaryOp(type, o));
17+
private static Parser<char, Func<IExpr, IExpr, IExpr>> Binary(Parser<char, BinaryOperatorType> op)
18+
=> op.Select<Func<IExpr, IExpr, IExpr>>(type => (l, r) => new BinaryOp(type, l, r));
19+
private static Parser<char, Func<IExpr, IExpr>> Unary(Parser<char, UnaryOperatorType> op)
20+
=> op.Select<Func<IExpr, IExpr>>(type => o => new UnaryOp(type, o));
2121

22-
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Add
23-
= Binary(Tok("+").ThenReturn(BinaryOperatorType.Add));
24-
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Sub
25-
= Binary(Tok("-").ThenReturn(BinaryOperatorType.Sub));
26-
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Div
27-
= Binary(Tok("/").ThenReturn(BinaryOperatorType.Div));
28-
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Mul
29-
= Binary(Tok("*").ThenReturn(BinaryOperatorType.Mul));
30-
private static readonly Parser<char, Func<IExpr, IExpr>> Decrement
31-
= Unary(Tok("--").ThenReturn(UnaryOperatorType.Decrement));
32-
private static readonly Parser<char, Func<IExpr, IExpr>> Increment
33-
= Unary(Tok("++").ThenReturn(UnaryOperatorType.Increment));
22+
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Add
23+
= Binary(Tok("+").ThenReturn(BinaryOperatorType.Add));
24+
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Sub
25+
= Binary(Tok("-").ThenReturn(BinaryOperatorType.Sub));
26+
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Div
27+
= Binary(Tok("/").ThenReturn(BinaryOperatorType.Div));
28+
private static readonly Parser<char, Func<IExpr, IExpr, IExpr>> Mul
29+
= Binary(Tok("*").ThenReturn(BinaryOperatorType.Mul));
30+
private static readonly Parser<char, Func<IExpr, IExpr>> Decrement
31+
= Unary(Tok("--").ThenReturn(UnaryOperatorType.Decrement));
32+
private static readonly Parser<char, Func<IExpr, IExpr>> Increment
33+
= Unary(Tok("++").ThenReturn(UnaryOperatorType.Increment));
3434

35-
private static readonly Parser<char, Func<IExpr, IExpr>> Minus
36-
= Unary(Tok("-").ThenReturn(UnaryOperatorType.Minus));
37-
private static readonly Parser<char, Func<IExpr, IExpr>> Plus
38-
= Unary(Tok("+").ThenReturn(UnaryOperatorType.Plus));
35+
private static readonly Parser<char, Func<IExpr, IExpr>> Minus
36+
= Unary(Tok("-").ThenReturn(UnaryOperatorType.Minus));
37+
private static readonly Parser<char, Func<IExpr, IExpr>> Plus
38+
= Unary(Tok("+").ThenReturn(UnaryOperatorType.Plus));
3939

40-
private static readonly Parser<char, IExpr> Identifier
41-
= Tok(Letter.Then(LetterOrDigit.ManyString(), (h, t) => h + t))
42-
.Select<IExpr>(name => new Identifier(name))
43-
.Labelled("identifier");
40+
private static readonly Parser<char, IExpr> Identifier
41+
= Tok(Letter.Then(LetterOrDigit.ManyString(), (h, t) => h + t))
42+
.Select<IExpr>(name => new Identifier(name))
43+
.Labelled("identifier");
4444

45-
private static readonly Parser<char, IExpr> Literal
46-
= Tok(LongNum)
47-
.Select<IExpr>(value => new Literal(value))
48-
.Labelled("integer literal");
45+
private static readonly Parser<char, IExpr> Literal
46+
= Tok(LongNum)
47+
.Select<IExpr>(value => new Literal(value))
48+
.Labelled("integer literal");
4949

50-
private static readonly Parser<char, IExpr> Expr = ExpressionParser.Build<char, IExpr>(
51-
expr => (
52-
OneOf(
53-
Identifier,
54-
Literal,
55-
Parenthesised(expr).Labelled("parenthesised expression")
56-
),
57-
new[]
58-
{
59-
Operator.Prefix(Decrement)
60-
.And(Operator.Prefix(Increment))
61-
.And(Operator.Prefix(Minus))
62-
.And(Operator.Prefix(Plus)),
63-
Operator.InfixL(Mul),
64-
Operator.InfixL(Div),
65-
Operator.InfixL(Add),
66-
Operator.InfixL(Sub)
67-
}
68-
)
69-
).Labelled("expression");
50+
private static readonly Parser<char, IExpr> Expr = ExpressionParser.Build<char, IExpr>(
51+
expr => (
52+
OneOf(
53+
Identifier,
54+
Literal,
55+
Parenthesised(expr).Labelled("parenthesised expression")
56+
),
57+
new[]
58+
{
59+
Operator.Prefix(Decrement)
60+
.And(Operator.Prefix(Increment))
61+
.And(Operator.Prefix(Minus))
62+
.And(Operator.Prefix(Plus)),
63+
Operator.InfixL(Mul),
64+
Operator.InfixL(Div),
65+
Operator.InfixL(Add),
66+
Operator.InfixL(Sub)
67+
}
68+
)
69+
).Labelled("expression");
7070

71-
public static IExpr ParseOrThrow(string input)
72-
=> Expr.ParseOrThrow(input);
71+
public static IExpr ParseOrThrow(string input)
72+
=> Expr.ParseOrThrow(input);
7373

74-
}
7574
}

0 commit comments

Comments
 (0)