Blame view

circus/src/jcircus/JCircusControllerFrame.java 5.67 KB
8d0dc533f   Madiel de Souza Conserva Filho   first
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  /*
   * JCircusControllerFrame.java
   */
  
  package jcircus;
  
  import java.util.ArrayList;
  import java.util.List;
  import jcircus.exceptions.FailParsingException;
  import jcircus.exceptions.FailTranslationException;
  import jcircus.exceptions.FailTypeCheckingException;
  import jcircus.exceptions.JCircusException;
  import jcircus.exceptions.TranslationCancelledException;
  import jcircus.gui.JCircusFrame;
  import jcircus.translator.ProcInfoUpdater;
  import jcircus.translator.Translator2Java;
  import jcircus.util.Constants;
  import jcircus.util.MathToolkitConstants;
  import jcircus.util.ProcInfo;
  import jcircus.util.Error;
  import net.sourceforge.czt.base.ast.Term;
  import net.sourceforge.czt.circus.util.Factory;
  import net.sourceforge.czt.z.ast.Expr;
  import net.sourceforge.czt.z.ast.RefExpr;
  import net.sourceforge.czt.z.ast.Spec;
  import net.sourceforge.czt.typecheck.z.ErrorAnn;
  
  public class JCircusControllerFrame extends JCircusController {
  
      /* GUI */
      private JCircusFrame _jCircusFrame;
  
      /**
       * Constructor.
       */
      public JCircusControllerFrame() {
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  _jCircusFrame = new JCircusFrame(JCircusControllerFrame.this);
                  _jCircusFrame.showGui();
              }
          });
      }
      protected void translate2Java (String projectDir, String projectName, Spec spec, String compl, boolean useBarriers, boolean parallelism, boolean bench)
              throws FailTranslationException, TranslationCancelledException, JCircusException {
      	List <ProcInfo> procInfoList = new ArrayList <ProcInfo> ();
      	ProcInfoUpdater.initProcInfoAndCreateMainEnv (_jCircusFrame.getProcCreateMainEnv(), procInfoList, spec, parallelism, true);
      	_jCircusFrame.promptForMainProcessesPCME (procInfoList);
      	_translator = new Translator2Java(projectDir, projectName, spec, compl, useBarriers, parallelism, bench, _jCircusFrame, false);
      	_translator.translate(true);
  
      	// Prompt for the processes for each we want a main and 
          // prompt for the parameters, in case of parameterised processes
  
      	procInfoList = _translator.getProcInfoList();
          //_jCircusFrame.promptForMainProcesses(procInfoList);
          for (int i = 0; i < procInfoList.size(); i++) {
              ProcInfo procInfo = procInfoList.get(i);
  
              if (procInfo.getCreateMain() == true &&
                      procInfo.getParameters() != null &&
                      !procInfo.getParameters().isEmpty()) {
  
                  _jCircusFrame.promptForParameters(procInfo);
              }
          }
  
          // Create the source code files
          _translator.createSources();
      }
  
      protected void reportMessage (String message) {
          _jCircusFrame.reportMessage(message);
      }
      
      /**
       *
       */
      protected void reportParsingErrors(List<String> errors) {
  
          String message = parsingErrorMessage(errors);
          reportMessage(message);
      }
      
      /**
       *
       */
      protected void reportTypeCheckingErrors(List<ErrorAnn> errors) {
  
          String message = typeCheckingErrorMessage(errors);
          reportMessage(message);
      }
  
      /**
       *
       */
      protected void reportTranslationErrors(List<Error> errors) {
          
          String message = translationErrorMessage(errors);
          reportMessage(message);
      }
      
      /**
       * Receives the expression that was entered in the text field and the type
       * that it should correspond. This method is a gambi... It is okay,
       * because of the restrictions on the format of a declaration (the expression
       * must be always a RefExpr). This method performs >>fake<< parsing, typechecking
       * and translation to Java. Come back here in the future, when we will be dealing
       * with more kinds of types and expressions, and change the respective bits
       * of code for calls to the real parser, type checker and translator.
       *
       */
      public String codeForParameter(String expression, Expr typeExpr)
              throws FailParsingException, FailTypeCheckingException {
          String code = "";
  
          // For the moment, typeExpr will be always a reference. This will be
          // changed in the future. Probably this method will receive the actual
          // Type, instead of the type expression.
          assert(typeExpr instanceof RefExpr);
  
          String typeExprSt = ((RefExpr) typeExpr).getName().toString();
  
          if (typeExprSt.equals(MathToolkitConstants.NAT) ||
                  typeExprSt.equals(MathToolkitConstants.NUM) ||
                  typeExprSt.equals(MathToolkitConstants.ARITHMOS)) {
  
              // Number
              try {
                  // Simulate parsing/typeChecking here
                  int number = Integer.parseInt(expression);
                  code = "new " + Constants.CLS_CIRCNUM + "(" + number + ")";
              } catch (NumberFormatException nfe) {
                  // Error
                  List<String> errors = new Factory().list("Expression is not a number.");
                  throw new FailParsingException(errors);
              }
  
          } else {
  
              // Free type
              if (_translator.getEnvironment().isElementFreeType(typeExprSt, expression)) {
  
                  // Simulate parsing/typeChecking here
                  code = "new " + typeExprSt + "(" + typeExprSt + "." + expression + ")";
  
              } else {
  
                  // Error
                  List<String> errors = new Factory().list("Expression is not an element of free type.");
                  throw new FailParsingException(errors);
              }
          }
  
          return code;
      } 
  
      /**
       * Cancel the translation.
       */
      public void cancelTranslation () throws TranslationCancelledException {
          throw new TranslationCancelledException();
      }
  }