EOS 2  1.1.0
Einfache Objektbasierte Sprache
RepeatTimes.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.baseparser;
2 
3 import de.lathanda.eos.vm.Command;
4 import de.lathanda.eos.vm.DebugPoint;
5 import de.lathanda.eos.vm.ReservedVariables;
6 import de.lathanda.eos.vm.commands.CreateVariable;
7 import de.lathanda.eos.vm.commands.GE;
8 import de.lathanda.eos.vm.commands.Jump;
9 import de.lathanda.eos.vm.commands.JumpIf;
10 import de.lathanda.eos.vm.commands.LoadConstant;
11 import de.lathanda.eos.vm.commands.LoadVariable;
12 import de.lathanda.eos.vm.commands.StoreVariable;
13 import de.lathanda.eos.vm.commands.SubtractI;
14 
15 import java.util.ArrayList;
16 
23 public class RepeatTimes extends Loop implements LoopTimesUnit {
24 
25  private final Expression timesE;
26  private String varName;
27  private int loopId;
28 
30  super(sequence, null);
31  this.timesE = times;
32  }
33 
34  @Override
35  public Expression getTimes() {
36  return timesE;
37  }
38 
39  @Override
40  public void compile(ArrayList<Command> ops, boolean autoWindow) throws Exception {
41  ArrayList<Command> body = new ArrayList<>();
42  sequence.compile(body, autoWindow);
43  ops.add(new DebugPoint(timesE.getMarker()));
44  ops.add(new CreateVariable(varName, Type.getInteger().getMType()));
45  timesE.compile(ops, autoWindow);
46 
47  ops.add(new StoreVariable(varName));
48  ops.add(new Jump(body.size() + 1));
49 
50  ops.addAll(body);
51 
52  ops.add(new DebugPoint(timesE.marker));
53  ops.add(new LoadVariable(varName));
54  ops.add(new LoadConstant(1));
55  ops.add(new SubtractI());
56  ops.add(new StoreVariable(varName));
57  ops.add(new LoadVariable(varName));
58  ops.add(new LoadConstant(0));
59  ops.add(new GE());
60  ops.add(new JumpIf(-(8 + body.size())));
61  }
62 
63  @Override
64  public void resolveNamesAndTypes(Expression with, Environment env) {
65  sequence.resolveNamesAndTypes(with, env);
66  if (timesE != null) {
67  timesE.resolveNamesAndTypes(with, env);
68  if (!timesE.getType().isNumber()) {
69  env.addError(marker, "RepeatTimesNumber", timesE.getType());
70  }
71  }
72  loopId = env.getUID();
73  varName = ReservedVariables.REPEAT_TIMES_INDEX + loopId;
74  }
75 
76  @Override
77  public String toString() {
78  return "repeat " + timesE + " times\n" + sequence + "endrepeat";
79  }
80 
81  @Override
82  public String getLabel() {
83  return createText("RepeatTimes.Label", timesE.getLabel());
84  }
85 
86  @Override
87  public boolean isPre() {
88  return true;
89  }
90 
91  @Override
92  public int getIndexId() {
93  return loopId;
94  }
95 }
void addError(Marker marker, String errorId, Object... data)
final Sequence sequence
Definition: Loop.java:10
abstract void compile(ArrayList< Command > ops, boolean autoWindow)
final String createText(String id, Object... args)
Definition: Node.java:34
abstract void resolveNamesAndTypes(Expression with, Environment env)
RepeatTimes(Expression times, Sequence sequence)
void compile(ArrayList< Command > ops, boolean autoWindow)
void resolveNamesAndTypes(Expression with, Environment env)
void resolveNamesAndTypes(Expression with, Environment env)
Definition: Sequence.java:67
void compile(ArrayList< Command > ops, boolean autoWindow)
Definition: Sequence.java:53
static SystemType getInteger()
Definition: Type.java:84
Impressum