-
Notifications
You must be signed in to change notification settings - Fork 2
/
IndexAccess.cs
69 lines (58 loc) · 2.13 KB
/
IndexAccess.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
using Interpreter.Lib.BackEnd.Instructions;
using Interpreter.Lib.BackEnd.Values;
using Interpreter.Lib.IR.Ast.Nodes.Expressions.PrimaryExpressions;
using Interpreter.Lib.IR.CheckSemantics.Exceptions;
using Interpreter.Lib.IR.CheckSemantics.Types;
namespace Interpreter.Lib.IR.Ast.Nodes.Expressions.AccessExpressions
{
public class IndexAccess : AccessExpression
{
private readonly Expression _expression;
public IndexAccess(Expression expression, AccessExpression prev = null) : base(prev)
{
_expression = expression;
_expression.Parent = this;
}
public PrimaryExpression Expression => _expression as PrimaryExpression;
public override IEnumerator<AbstractSyntaxTreeNode> GetEnumerator()
{
yield return _expression;
if (HasNext())
{
yield return Next;
}
}
public override Type Check(Type prev)
{
if (prev is ArrayType arrayType)
{
var indexType = _expression.NodeCheck();
if (indexType.Equals(TypeUtils.JavaScriptTypes.Number))
{
var elemType = arrayType.Type;
return HasNext() ? Next.Check(elemType) : elemType;
}
throw new ArrayAccessException(Segment, indexType);
}
return null;
}
protected override string NodeRepresentation() => "[]";
public override List<Instruction> ToInstructions(int start, string temp)
{
if (HasNext())
{
if (_expression is PrimaryExpression prim)
{
var left = "_t" + start;
var nextInstructions = Next.ToInstructions(start + 1, left);
nextInstructions.Insert(0,
new Simple(left, (new Name(temp), prim.ToValue()), "[]", start)
);
return nextInstructions;
}
throw new NotImplementedException();
}
return new();
}
}
}