EOS 2  1.1.0
Einfache Objektbasierte Sprache
IfElse.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.commands.Jump;
6 import de.lathanda.eos.vm.commands.JumpIfNot;
7 
8 import java.util.ArrayList;
9 
16 public class IfElse extends Node implements AlternativeUnit {
17 
18  Expression condition;
19  Sequence a;
20  Sequence b;
21 
22  public IfElse(Expression condition, Sequence a, Sequence b) {
23  this.condition = condition;
24  this.a = a;
25  this.b = b;
26  }
27 
29  return condition;
30  }
31 
32  @Override
33  public Sequence getThen() {
34  return a;
35  }
36 
37  @Override
38  public Sequence getElse() {
39  return b;
40  }
41 
42  @Override
43  public void compile(ArrayList<Command> ops, boolean autoWindow) throws Exception {
44  ArrayList<Command> aop = new ArrayList<>();
45  ArrayList<Command> bop = new ArrayList<>();
46  ArrayList<Command> cond = new ArrayList<>();
47 
48  cond.add(new DebugPoint(condition.getMarker()));
49  condition.compile(cond, autoWindow);
50 
51  a.compile(aop, autoWindow);
52  if (b != null) {
53  b.compile(bop, autoWindow);
54  }
55 
56  ops.addAll(cond);
57  ops.add(new JumpIfNot(aop.size() + 2));
58  ops.addAll(aop);
59  ops.add(new Jump(bop.size() + 1));
60  ops.addAll(bop);
61 
62  }
63 
64  @Override
65  public void resolveNamesAndTypes(Expression with, Environment env) {
66  a.resolveNamesAndTypes(with, env);
67  if (b != null) {
68  b.resolveNamesAndTypes(with, env);
69  }
70  condition.resolveNamesAndTypes(with, env);
71  if (!condition.getType().isBoolean()) {
72  env.addError(marker, "ConditionType", condition.getType());
73  }
74  }
75 
76  @Override
77  public String toString() {
78  return "if " + condition + " then\n" + a + "else\n" + b + "endif";
79  }
80 
81  @Override
82  public String getLabel() {
83  return createText("IfElse.Label", condition.getLabel());
84  }
85 }
void addError(Marker marker, String errorId, Object... data)
void compile(ArrayList< Command > ops, boolean autoWindow)
Definition: IfElse.java:43
void resolveNamesAndTypes(Expression with, Environment env)
Definition: IfElse.java:65
IfElse(Expression condition, Sequence a, Sequence b)
Definition: IfElse.java:22
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)
void resolveNamesAndTypes(Expression with, Environment env)
Definition: Sequence.java:67
void compile(ArrayList< Command > ops, boolean autoWindow)
Definition: Sequence.java:53
Impressum