EOS 2  1.1.0
Einfache Objektbasierte Sprache
BackgroundCompiler.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.common.gui;
2 
3 import java.util.LinkedList;
4 
5 import de.lathanda.eos.baseparser.AbstractProgram;
6 import de.lathanda.eos.baseparser.CompilerListener;
7 import de.lathanda.eos.baseparser.Program;
8 import de.lathanda.eos.baseparser.Source;
9 import de.lathanda.eos.baseparser.exceptions.TranslationException;
10 import de.lathanda.eos.config.Language;
11 import de.lathanda.eos.interpreter.parser.de.EosParserFactory;
12 import de.lathanda.eos.vm.ErrorInformation;
13 
14 
23 public class BackgroundCompiler implements Runnable {
24  private AbstractProgram program;
25  private final Source source;
26  private LinkedList<ErrorInformation> errors;
27  private final CompilerMulticaster cmc;
28 
29  public BackgroundCompiler(Source source) {
30  this.cmc = new CompilerMulticaster();
31  this.source = source;
32  errors = new LinkedList<>();
33  }
34 
35  @Override
36  public void run() {
37  while (true) {
38  update();
39  }
40  }
41 
42  private void update() {
43  try {
44  String src = source.getSourceCode();
45  errors = new LinkedList<>();
46  program = new Program(
47  src,
48  new EosParserFactory(),
51  );
52  program.parse(source.getPath());
53  program.compile();
54  } catch (TranslationException te) {
55  errors.add(te.getErrorInformation());
56  }
57 
58  if (program != null) {
59  errors.addAll(program.getErrors());
60  }
61  cmc.fireCompileComplete();
62  }
63 
64  protected class CompilerMulticaster {
65 
66  private final LinkedList<CompilerListener> compilerListener;
67 
68  protected CompilerMulticaster() {
69  compilerListener = new LinkedList<>();
70  }
71 
72  void add(CompilerListener cl) {
73  compilerListener.add(cl);
74  }
75 
76  void remove(CompilerListener cl) {
77  compilerListener.remove(cl);
78  }
79 
80  void fireCompileComplete() {
81  compilerListener.forEach((cl) -> {
82  cl.compileComplete(errors, program);
83  });
84  }
85  }
86 
88  cmc.add(cl);
89  if (program != null) {
90  // if a program is already available distribute it
91  cl.compileComplete(errors, program);
92  }
93  }
94 
96  cmc.remove(cl);
97  }
98 }
LinkedList< ErrorInformation > getErrors()
void compileComplete(LinkedList< ErrorInformation > errors, AbstractProgram program)
Impressum