-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignatures.cs
112 lines (108 loc) · 4.82 KB
/
Signatures.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
using System.Collections.Generic;
using BabelFish.AST;
using enquanto.Model;
namespace enquanto
{
internal class Signatures
{
private readonly Dictionary<BinaryOperator, List<Signature<EnquantoType>>> binaryOperationSignatures;
public Signatures()
{
binaryOperationSignatures = new Dictionary<BinaryOperator, List<Signature<EnquantoType>>>
{
{
BinaryOperator.ADD, new List<Signature<EnquantoType>>
{
new Signature<EnquantoType>(EnquantoType.INT, EnquantoType.INT, EnquantoType.INT)
}
},
{
BinaryOperator.SUB, new List<Signature<EnquantoType>>
{
new Signature<EnquantoType>(EnquantoType.INT, EnquantoType.INT, EnquantoType.INT)
}
},
{
BinaryOperator.DIVIDE, new List<Signature<EnquantoType>>
{
new Signature<EnquantoType>(EnquantoType.INT, EnquantoType.INT, EnquantoType.INT)
}
},
{
BinaryOperator.MULTIPLY, new List<Signature<EnquantoType>>
{
new Signature<EnquantoType>(EnquantoType.INT, EnquantoType.INT, EnquantoType.INT)
}
},
{
BinaryOperator.AND, new List<Signature<EnquantoType>>
{
new Signature<EnquantoType>(EnquantoType.BOOL, EnquantoType.BOOL, EnquantoType.BOOL)
}
},
{
BinaryOperator.OR, new List<Signature<EnquantoType>>
{
new Signature<EnquantoType>(EnquantoType.BOOL, EnquantoType.BOOL, EnquantoType.BOOL)
}
},
{
BinaryOperator.LESSER, new List<Signature<EnquantoType>>
{
new Signature<EnquantoType>(EnquantoType.INT, EnquantoType.INT, EnquantoType.BOOL),
new Signature<EnquantoType>(EnquantoType.STRING, EnquantoType.STRING, EnquantoType.BOOL),
new Signature<EnquantoType>(EnquantoType.BOOL, EnquantoType.BOOL, EnquantoType.BOOL)
}
},
{
BinaryOperator.GREATER, new List<Signature<EnquantoType>>
{
new Signature<EnquantoType>(EnquantoType.INT, EnquantoType.INT, EnquantoType.BOOL),
new Signature<EnquantoType>(EnquantoType.STRING, EnquantoType.STRING, EnquantoType.BOOL),
new Signature<EnquantoType>(EnquantoType.BOOL, EnquantoType.BOOL, EnquantoType.BOOL)
}
},
{
BinaryOperator.EQUALS, new List<Signature<EnquantoType>>
{
new Signature<EnquantoType>(EnquantoType.INT, EnquantoType.INT, EnquantoType.BOOL),
new Signature<EnquantoType>(EnquantoType.STRING, EnquantoType.STRING, EnquantoType.BOOL),
new Signature<EnquantoType>(EnquantoType.BOOL, EnquantoType.BOOL, EnquantoType.BOOL)
}
},
{
BinaryOperator.DIFFERENT, new List<Signature<EnquantoType>>
{
new Signature<EnquantoType>(EnquantoType.INT, EnquantoType.INT, EnquantoType.BOOL),
new Signature<EnquantoType>(EnquantoType.STRING, EnquantoType.STRING, EnquantoType.BOOL),
new Signature<EnquantoType>(EnquantoType.BOOL, EnquantoType.BOOL, EnquantoType.BOOL)
}
},
{
BinaryOperator.CONCAT, new List<Signature<EnquantoType>>
{
new Signature<EnquantoType>(EnquantoType.ANY, EnquantoType.ANY, EnquantoType.STRING)
}
}
};
}
public EnquantoType CheckBinaryOperationTyping(BinaryOperator oper, EnquantoType left, EnquantoType right)
{
EnquantoType result;
if (binaryOperationSignatures.ContainsKey(oper))
{
var signatures = binaryOperationSignatures[oper];
var res = signatures.Find(sig => sig.Match(left, right));
if (res != null)
result = res.Result;
else
throw new SignatureException($"invalid operation {left} {oper} {right}");
}
else
{
result = left;
}
return result;
}
}
}