-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionTyper.cs
112 lines (88 loc) · 3.82 KB
/
ExpressionTyper.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 BabelFish.AST;
using BabelFish.Compiler;
using enquanto.Model;
namespace enquanto
{
internal class ExpressionTyper
{
private readonly Signatures signatures;
public ExpressionTyper()
{
signatures = new Signatures();
}
public EnquantoType TypeExpression(IExpression<EnquantoType> expr, CompilerContext<EnquantoType> context)
{
switch (expr)
{
case BoolConstant @bool:
return TypeExpression(@bool, context);
case IntegerConstant @int:
return TypeExpression(@int, context);
case StringConstant @string:
return TypeExpression(@string, context);
case BinaryOperation binary:
return TypeExpression(binary, context);
case Neg neg:
return TypeExpression(neg, context);
case Not not:
return TypeExpression(not, context);
case Variable variable:
{
var varType = context.GetVariableType(variable.Name);
if (varType == EnquantoType.NONE)
{
throw new TypingException($" variable {variable.Name} {variable.Position} is not intialized");
}
variable.CompilerScope = context.CurrentScope;
return varType;
}
}
throw new SignatureException($"unknow expression type ({expr.GetType().Name})");
}
public EnquantoType TypeExpression(BoolConstant boolConst, CompilerContext<EnquantoType> context)
{
boolConst.CompilerScope = context.CurrentScope;
return EnquantoType.BOOL;
}
public EnquantoType TypeExpression(StringConstant stringConst, CompilerContext<EnquantoType> context)
{
stringConst.CompilerScope = context.CurrentScope;
return EnquantoType.STRING;
}
public EnquantoType TypeExpression(IntegerConstant intConst, CompilerContext<EnquantoType> context)
{
intConst.CompilerScope = context.CurrentScope;
return EnquantoType.INT;
}
public EnquantoType TypeExpression(BinaryOperation operation, CompilerContext<EnquantoType> context)
{
operation.CompilerScope = context.CurrentScope;
var left = TypeExpression(operation.Left, context);
operation.Left.Type = left;
var right = TypeExpression(operation.Right, context);
operation.Right.Type = right;
var resultType = signatures.CheckBinaryOperationTyping(operation.Operator, left, right);
return resultType;
}
public EnquantoType TypeExpression(Neg neg, CompilerContext<EnquantoType> context)
{
var positiveVal = TypeExpression(neg.Value, context);
neg.CompilerScope = context.CurrentScope;
if (positiveVal != EnquantoType.INT)
{
throw new SignatureException($"invalid operation type({positiveVal}) : {neg.Dump("")}");
}
return EnquantoType.INT;
}
public EnquantoType TypeExpression(Not not, CompilerContext<EnquantoType> context)
{
var positiveVal = TypeExpression(not.Value, context);
not.CompilerScope = context.CurrentScope;
if (positiveVal != EnquantoType.BOOL)
{
throw new SignatureException($"invalid operation type({positiveVal}) : {not.Dump("")}");
}
return EnquantoType.BOOL;
}
}
}