-
Notifications
You must be signed in to change notification settings - Fork 1
/
Syntax.cs
120 lines (109 loc) · 3.84 KB
/
Syntax.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Immutable;
using System.Linq;
using Sawmill;
namespace Amateurlog
{
record Program(ImmutableArray<TopLevel> Decls);
abstract record TopLevel;
record TypeDecl(
string Name,
ImmutableArray<string> Params,
ImmutableArray<TypeApplication> Constructors
) : TopLevel;
abstract partial record Type : IRewritable<Type>
{
public abstract int CountChildren();
public abstract void GetChildren(Span<Type> childrenReceiver);
public abstract Type SetChildren(ReadOnlySpan<Type> newChildren);
}
partial record TypeApplication(string Name, ImmutableArray<Type> Args) : Type
{
public override int CountChildren() => Args.Length;
public override void GetChildren(Span<Type> childrenReceiver)
{
for (var i = 0; i < Args.Length; i++)
{
childrenReceiver[i] = Args[i];
}
}
public override Type SetChildren(ReadOnlySpan<Type> newChildren)
=> new TypeApplication(Name, newChildren.ToArray().ToImmutableArray());
public Sig Sig { get; } = new Sig(Name, Args.Length);
}
partial record TypeVariable(string Name) : Type
{
public override int CountChildren() => 0;
public override void GetChildren(Span<Type> childrenReceiver) {}
public override Type SetChildren(ReadOnlySpan<Type> newChildren)
=> this;
}
record Rule(Functor Head, ImmutableArray<Functor> Body) : TopLevel
{
public override string ToString()
=> Head + (
Body.Length == 0
? ""
: " :- " + string.Join(", ", Body.Select(x => x.ToString()))
) + ".";
}
public record Sig(string Name, int ParamCount) : IComparable<Sig>
{
public int CompareTo(Sig? other)
{
if (other == null)
{
return 1;
}
switch (this.Name.CompareTo(other.Name))
{
case 0:
return this.ParamCount.CompareTo(other.ParamCount);
case var x:
return x;
}
}
}
abstract partial record Term : IRewritable<Term>
{
public abstract int CountChildren();
public abstract void GetChildren(Span<Term> childrenReceiver);
public abstract Term SetChildren(ReadOnlySpan<Term> newChildren);
public override string ToString()
=> this.Fold<Term, string>(
(childResults, x) =>
{
switch (x)
{
case Variable v:
return v.Name;
case Functor f:
return f.Atom + "(" + string.Join(", ", childResults.ToArray()) + ")";
default:
throw new ArgumentOutOfRangeException();
}
}
);
}
partial record Variable(string Name) : Term
{
public override int CountChildren() => 0;
public override void GetChildren(Span<Term> childrenReceiver) {}
public override Term SetChildren(ReadOnlySpan<Term> newChildren)
=> this;
}
partial record Functor(string Atom, ImmutableArray<Term> Args) : Term
{
public override int CountChildren() => Args.Length;
public override void GetChildren(Span<Term> childrenReceiver)
{
for (var i = 0; i < Args.Length; i++)
{
childrenReceiver[i] = Args[i];
}
}
public override Term SetChildren(ReadOnlySpan<Term> newChildren)
=> new Functor(Atom, newChildren.ToArray().ToImmutableArray());
public Sig Sig { get; } = new Sig(Atom, Args.Length);
}
}