gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
26452e802f4b3afb5c92775d9316b322f187af5a
[mjc2wsl.git] / src / com / quemaster / transformations / mjc2wsl / mjc2wsl.java
1 package com.quemaster.transformations.mjc2wsl;
2 /*
3 Copyright (C) 2014 Doni Pracner
5 This file is part of mjc2wsl.
7 mjc2wsl is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 mjc2wsl is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with mjc2wsl. If not, see <http://www.gnu.org/licenses/>.
19 */
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.PrintWriter;
25 import java.nio.file.FileSystems;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.util.Calendar;
29 import java.util.Properties;
31 import com.quemaster.transformations.TransMessages;
33 /**
34 * This program converts file from compiled MicroJava bytecode to WSL language
35 * which is a part of the FermaT Transformation system. MicroJava is a subset
36 * used in Compiler Construction courses by Hanspeter Moessenboeck, not
37 * "Java ME".
38 *
39 * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com
40 */
41 public class mjc2wsl{
42 //default version name, used if the file is not found
43 private static String versionN = "0.1.x";
45 private String versionFile = "/version.properties";
47 private TransMessages messages = new TransMessages();
49 private boolean genPauseAfterEachAddress=false,
50 genPrintForEachAddress = false,
51 genPrintEStackOnChange = false;
53 private boolean genPopPush=false;
55 private boolean genInlinePrint = false;
57 private boolean genLocalVars = true;
59 /** Constant used for marking a regular comment from the original file */
60 public static final char C_REG = ' ';
61 /**
62 * Constant used for marking when original code is inserted in the file,
63 * next to the translations
64 */
65 public static final char C_OC = '#';
66 /** Constant used for marking special messages from the translator */
67 public static final char C_SPEC = '&';
68 /** Constant used for marking error messages from the translator */
69 public static final char C_ERR = '!';
71 /** instruction code in MicroJava bytecode. */
72 public static final int
73 load = 1,
74 load_0 = 2,
75 load_1 = 3,
76 load_2 = 4,
77 load_3 = 5,
78 store = 6,
79 store_0 = 7,
80 store_1 = 8,
81 store_2 = 9,
82 store_3 = 10,
83 getstatic = 11,
84 putstatic = 12,
85 getfield = 13,
86 putfield = 14,
87 const_0 = 15,
88 const_1 = 16,
89 const_2 = 17,
90 const_3 = 18,
91 const_4 = 19,
92 const_5 = 20,
93 const_m1 = 21,
94 const_ = 22,
95 add = 23,
96 sub = 24,
97 mul = 25,
98 div = 26,
99 rem = 27,
100 neg = 28,
101 shl = 29,
102 shr = 30,
103 inc = 31,
104 new_ = 32,
105 newarray = 33,
106 aload = 34,
107 astore = 35,
108 baload = 36,
109 bastore = 37,
110 arraylength = 38,
111 pop = 39,
112 dup = 40,
113 dup2 = 41,
114 jmp = 42,
115 jeq = 43,
116 jne = 44,
117 jlt = 45,
118 jle = 46,
119 jgt = 47,
120 jge = 48,
121 call = 49,
122 return_ = 50,
123 enter = 51,
124 exit = 52,
125 read = 53,
126 print = 54,
127 bread = 55,
128 bprint = 56,
129 trap = 57;
131 private boolean originalInComments = false;
133 private Properties versionData;
135 private String getVersion() {
136 if (versionData == null) {
137 versionData = new Properties();
138 try {
139 versionData.load(getClass().getResourceAsStream(versionFile));
140 } catch (IOException e) {
141 e.printStackTrace();
144 String ver = versionData.getProperty("version");
145 if (ver != null)
146 return ver;
147 else
148 return versionN;
151 MicroJavaInput mjInput = new MicroJavaInput();
153 private PrintWriter out = null;
156 private void pr(int i){
157 out.print(i);
160 private void pr(char i){
161 out.print(i);
164 private void pr(String i){
165 out.print(i);
168 private void prl(String i){
169 out.println(i);
172 public String createStandardStart(){
173 return createStandardStart(10);
176 public String createStandardStart(int numWords){
177 StringBuilder ret = new StringBuilder(
178 "C:\" This file automatically converted from microjava bytecode\";\n"
179 +"C:\" with mjc2wsl v "+getVersion()+"\";\n");
181 ret.append("\nBEGIN");
182 ret.append("\nVAR <\n\t");
183 if (!genLocalVars){
184 ret.append("\n\ttempa := 0, tempb :=0, tempres := 0,");
186 ret.append("mjvm_locals := ARRAY(1,0),");
187 ret.append("\n\tmjvm_statics := ARRAY("+numWords+",0),");
188 ret.append("\n\tmjvm_arrays := < >,");
189 ret.append("\n\tmjvm_flag_jump := 0,");
190 ret.append("\n\tmjvm_objects := < >,");
191 ret.append("\n\tmjvm_estack := < >, mjvm_mstack := < > > :");
193 return ret.toString();
196 public String createAsciiString(){
197 StringBuilder ret = new StringBuilder("C:\"char array for ascii code conversions\";");
198 ret.append("\nascii := \"????????????????????????????????\"++\n");
199 ret.append("\" !\"++Quote++\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\";\n");
201 return ret.toString();
204 public String createStandardEnd(){
205 StringBuilder ret = new StringBuilder("SKIP\nENDVAR\n");
206 ret.append("\nWHERE\n");
208 ret.append("\nFUNCT CHR(num) ==:\n");
209 ret.append("\t@List_To_String(< num >)\n");
210 ret.append("END\n");
212 if (!genInlinePrint) {
213 ret.append("\nPROC Print_MJ(val, format VAR)==\n");
214 ret.append(createComment("print spacing", C_SPEC));
215 ret.append("\n\tIF format>1 THEN\n\t\tFOR i:=2 TO ");
216 ret.append("format STEP 1 DO PRINFLUSH(\" \") OD\n");
217 ret.append("\tFI;\n\tPRINFLUSH(val)\nEND\n");
219 ret.append("\nPROC Print_MJ_CHAR(val, format VAR)==\n");
220 ret.append(createComment("print spacing", C_SPEC));
221 ret.append("\n\tIF format>1 THEN\n\t\tFOR i:=2 TO ");
222 ret.append("format STEP 1 DO PRINFLUSH(\" \") OD\n");
223 ret.append("\tFI;\n\tPRINFLUSH(CHR(val))\n");
224 ret.append("END\n");
227 ret.append("\nEND");
228 return ret.toString();
231 private String createStartVar(String... vars){
232 if (genLocalVars) {
233 StringBuilder ret = new StringBuilder("VAR < ");
234 ret.append(vars[0] + " := 0");
235 for (int i = 1; i < vars.length; i++)
236 ret.append(", " + vars[i] + " := 0");
237 ret.append(" > : ");
239 return ret.toString();
241 return "";
244 private String createEndVar(){
245 if (genLocalVars)
246 return "ENDVAR;";
247 else
248 return "";
251 private String createLocal(int i) {
252 // arrays start at 1 in WSL, so we need an offset
253 return "mjvm_locals[" + (i + 1) + "]";
256 private String createStatic(int i) {
257 return "mjvm_statics[" + (i + 1) + "]";
260 private String createArray(int i) {
261 return "mjvm_arrays[" + i + "]";
264 private String createArray(String i) {
265 return "mjvm_arrays[" + i + "]";
268 private String createObject(String i) {
269 return "mjvm_objects[" + i + "]";
272 /**
273 * Creates a WSL comment with care to quote chars.
274 */
275 public static String createComment(String str){
276 return createComment(str, C_REG);
279 /**
280 * Creates a WSL comment with care to quote chars, of the
281 * given type. Types are given as char constants. They can be
282 * default comments, comments that contain the original code
283 * in them, or additional comments regarding the translation
284 * process.
285 */
286 public static String createComment(String str, char type) {
287 return "C:\"" + type + str.replace("\"", "''") + "\";";
290 // generalised stack operations
292 private String createToStack(String stack, String var){
293 if (genPopPush)
294 return "PUSH("+stack+"," + var + ");";
295 else
296 return stack + " := <" + var + " > ++ " + stack +";";
299 private String createFromStack(String stack, String var){
300 if (genPopPush)
301 return "POP("+ var + ", "+stack+");";
302 else
303 return var + ":= HEAD("+stack+"); "+stack+" := TAIL("+stack+");";
305 //Expression stack
307 private String createToEStack(int i) {
308 return createToEStack(i+"");
311 private String createToEStack(String i) {
312 String res = createToStack("mjvm_estack", i);
313 if (genPrintEStackOnChange)
314 res += "PRINT(\"eStack\",mjvm_estack);";
315 return res;
318 private String createFromEStack(String st) {
319 String res = createFromStack("mjvm_estack",st);
320 if (genPrintEStackOnChange)
321 res += "PRINT(\"eStack\",mjvm_estack);";
322 return res;
325 private String createPopEStack() {
326 String res = "mjvm_estack := TAIL(mjvm_estack);";
327 if (genPrintEStackOnChange)
328 res += "PRINT(\"eStack\",mjvm_estack);";
329 return res;
332 private String createTopTwoEStack() {
333 return createFromEStack("tempa") + "\n" + createFromEStack("tempb");
336 private String createTopEStack() {
337 return createFromEStack("tempa");
340 //Method stack
342 private String createToMStack(int i) {
343 return createToMStack(i+"");
346 private String createToMStack(String i) {
347 return createToStack("mjvm_mstack", i);
350 private String createFromMStack(String st) {
351 return createFromStack("mjvm_mstack", st);
354 public void convertStream(InputStream ins) throws Exception{
355 mjInput.setStream(ins);
356 //process start
357 mjInput.processHeader(this);
359 prl(createStandardStart(mjInput.getNumberOfWords(this)));
360 prl("SKIP;\n ACTIONS a" + (14 + mjInput.getMainAdr(this)) + " :");
361 int op = mjInput.get();
362 while (op >= 0) {
363 prl(" a" + mjInput.getCounter() + " ==");
364 if (originalInComments)
365 prl(createComment(mjInput.describeOpCode(op), C_OC));
366 if (genPrintForEachAddress) {
367 prl("PRINT(\"a" + mjInput.getCounter() + "\");");
368 if (genPauseAfterEachAddress)
369 prl("debug_disposable_string := @Read_Line(Standard_Input_Port);");
371 switch (op) {
372 case load: {
373 prl(createToEStack(createLocal(mjInput.get())));
374 break;
376 case load_0:
377 case load_1:
378 case load_2:
379 case load_3: {
380 prl(createStartVar("tempa"));
381 prl("tempa :="+createLocal(op - load_0)+";");
382 prl(createToEStack("tempa"));
383 prl(createEndVar());
384 break;
386 case store: {
387 prl(createFromEStack(createLocal(mjInput.get())));
388 break;
390 case store_0:
391 case store_1:
392 case store_2:
393 case store_3: {
394 prl(createStartVar("tempa"));
395 prl(createFromEStack("tempa"));
396 prl(createLocal(op - store_0)+" := tempa;");
397 prl(createEndVar());
398 break;
401 case getstatic: {
402 prl(createToEStack(createStatic(mjInput.get2())));
403 break;
405 case putstatic: {
406 prl(createFromEStack(createStatic(mjInput.get2())));
407 break;
410 case getfield: {
411 int f = mjInput.get2();
412 prl(createStartVar("tempa"));
413 prl(createTopEStack());
414 prl(createToEStack(createObject("tempa") + "[" + (f + 1) + "]"));
415 prl(createEndVar());
416 break;
418 case putfield: {
419 int f = mjInput.get2();
420 prl(createStartVar("tempa", "tempb"));
421 prl(createTopTwoEStack());
422 prl(createObject("tempb") + "[" + (f + 1) + "]:=tempa;");
423 prl(createEndVar());
424 break;
427 case const_: {
428 prl(createToEStack(mjInput.get4()));
429 break;
432 case const_m1: {
433 prl(createToEStack(-1));
434 break;
437 case const_0:
438 case const_1:
439 case const_2:
440 case const_3:
441 case const_4:
442 case const_5: {
443 prl(createToEStack(op - const_0));
444 break;
447 case add: {
448 prl(createStartVar("tempa", "tempb", "tempres"));
449 prl(createTopTwoEStack());
450 prl("tempres := tempb + tempa;");
451 prl(createToEStack("tempres"));
452 prl(createEndVar());
453 break;
455 case sub: {
456 prl(createStartVar("tempa", "tempb", "tempres"));
457 prl(createTopTwoEStack());
458 prl("tempres := tempb - tempa;");
459 prl(createToEStack("tempres"));
460 prl(createEndVar());
461 break;
463 case mul: {
464 prl(createStartVar("tempa", "tempb", "tempres"));
465 prl(createTopTwoEStack());
466 prl("tempres := tempb * tempa;");
467 prl(createToEStack("tempres"));
468 prl(createEndVar());
469 break;
471 case div: {
472 prl(createStartVar("tempa", "tempb", "tempres"));
473 prl(createTopTwoEStack());
474 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
475 prl("tempres := tempb DIV tempa;");
476 prl(createToEStack("tempres"));
477 prl(createEndVar());
478 break;
480 case rem: {
481 prl(createStartVar("tempa", "tempb", "tempres"));
482 prl(createTopTwoEStack());
483 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
484 prl("tempres := tempb MOD tempa;");
485 prl(createToEStack("tempres"));
486 prl(createEndVar());
487 break;
490 case neg: {
491 prl(createStartVar("tempa"));
492 prl(createTopEStack());
493 prl(createToEStack("-tempa"));
494 prl(createEndVar());
495 break;
498 case shl: {
499 prl(createStartVar("tempa", "tempb"));
500 prl(createTopTwoEStack());
501 prl("VAR <tempres :=tempb, i:=1 >:");
502 prl("\tFOR i:=1 TO tempa STEP 1 DO tempres := tempres * 2 OD;");
503 prl(createToEStack("tempres"));
504 prl("ENDVAR;");
505 prl(createEndVar());
506 break;
508 case shr: {
509 prl(createStartVar("tempa", "tempb"));
510 prl(createTopTwoEStack());
511 prl("VAR <tempres :=tempb, i:=1 >:");
512 prl("\tFOR i:=1 TO tempa STEP 1 DO tempres := tempres DIV 2 OD;");
513 prl(createToEStack("tempres"));
514 prl("ENDVAR;");
515 prl(createEndVar());
516 break;
519 case inc: {
520 int b1 = mjInput.get(), b2 = mjInput.get();
521 prl(createLocal(b1) + " := " + createLocal(b1) + " + " + b2 + ";");
522 break;
525 case new_: {
526 int size = mjInput.get2();
527 // TODO maybe objects and arrays should be in the same list?
528 prl("mjvm_objects := mjvm_objects ++ < ARRAY(" + size
529 + ",0) >;");
530 prl(createToEStack("LENGTH(mjvm_objects)"));
531 break;
533 case newarray: {
534 mjInput.get();// 0 - bytes, 1 - words; ignore for now
535 // TODO take into consideration 0/1
536 prl(createStartVar("tempa"));
537 prl(createTopEStack());
538 prl("mjvm_arrays := mjvm_arrays ++ < ARRAY(tempa,0) >;");
539 prl(createToEStack("LENGTH(mjvm_arrays)"));
540 prl(createEndVar());
541 break;
544 case aload:
545 case baload: {
546 prl(createStartVar("tempa", "tempb"));
547 prl(createTopTwoEStack());
548 prl(createToEStack(createArray("tempb") + "[tempa+1]"));
549 prl(createEndVar());
550 break;
552 case astore:
553 case bastore: {
554 prl(createStartVar("tempa", "tempb", "tempres"));
555 prl(createFromEStack("tempres"));
556 prl(createTopTwoEStack());
557 prl("mjvm_arrays[tempb][tempa+1]:=tempres;");
558 prl(createEndVar());
559 break;
561 case arraylength: {
562 prl(createStartVar("tempa", "tempb"));
563 prl(createTopEStack());
564 prl("tempb := LENGTH("+ createArray("tempa") + ");");
565 prl(createToEStack("tempb"));
566 prl(createEndVar());
567 break;
570 case dup: {
571 prl(createStartVar("tempa", "tempb"));
572 prl(createTopEStack());
573 prl(createToEStack("tempa"));
574 prl(createToEStack("tempa"));
575 prl(createEndVar());
576 break;
578 case dup2: {
579 prl(createStartVar("tempa", "tempb"));
580 prl(createTopTwoEStack());
581 prl(createToEStack("tempb"));
582 prl(createToEStack("tempa"));
583 prl(createToEStack("tempb"));
584 prl(createToEStack("tempa"));
585 prl(createEndVar());
586 break;
589 case pop: {
590 prl(createPopEStack());
591 break;
594 case jmp: {
595 prl("CALL a" + (mjInput.getCounter() + mjInput.get2()) + ";");
596 break;
599 case jeq:
600 case jne:
601 case jlt:
602 case jle:
603 case jgt:
604 case jge: {
605 prl(createStartVar("tempa", "tempb"));
606 prl(createTopTwoEStack());
607 prl("IF tempb " + mjInput.getRelationFor(op)
608 + " tempa THEN mjvm_flag_jump := 1"
609 + " ELSE mjvm_flag_jump := 0"
610 + " FI;");
611 prl(createEndVar());
612 prl("IF mjvm_flag_jump = 1 THEN CALL a"
613 + (mjInput.getCounter() + mjInput.get2())
614 + " ELSE CALL a" + (mjInput.getCounter() + 1)
615 + " FI;");
617 break;
620 case call: {
621 prl("CALL a" + (mjInput.getCounter() + mjInput.get2()) + ";");
622 break;
625 case return_: {
626 // we let the actions return
627 // there is nothing to clean up
628 prl("SKIP\n END\n b" + mjInput.getCounter() + " ==");
629 break;
631 case enter: {
632 int parameters = mjInput.get();
634 int locals = mjInput.get();
635 prl(createToMStack("mjvm_locals"));
636 prl("mjvm_locals := ARRAY(" + locals + ",0);");
637 for (int i = parameters - 1; i >= 0; i--)
638 prl(createFromEStack(createLocal(i)));
639 break;
641 case exit: {
642 prl(createFromMStack("mjvm_locals"));
643 break;
646 // read, print
647 case bread: {
648 // TODO make it a char for read
649 messages.message("char is read like a number", TransMessages.M_WAR);
650 prl(createComment("char is read like a number", C_SPEC));
652 case read: {
653 prl(createStartVar("tempa"));
654 prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
655 prl(createToEStack("tempa"));
656 prl(createEndVar());
657 break;
660 // the prints
661 case bprint: {
662 prl(createStartVar("tempa", "tempb"));
663 prl(createTopTwoEStack());
664 if (genInlinePrint){
665 prl(createComment("print spacing and transformation",C_SPEC));
666 prl("PRINFLUSH(SUBSTR(\" \", 0, MIN(10, MAX(0,tempa-1))), @List_To_String(< tempb >));");
667 } else
668 prl("Print_MJ_CHAR(tempb,tempa);");
669 prl(createEndVar());
670 break;
672 case print: {
673 // TODO printing numbers needs different lengths of spacing
674 prl(createStartVar("tempa", "tempb"));
676 prl(createTopTwoEStack());
677 if (genInlinePrint){
678 prl(createComment("print spacing",C_SPEC));
679 prl("PRINFLUSH(SUBSTR(\" \", 0, MIN(10, MAX(0, tempa-SLENGTH(@String(tempb))))), tempb);");
681 else
682 prl("Print_MJ(tempb,tempa);");
683 prl(createEndVar());
684 break;
687 case trap: {
688 prl("ERROR(\"Runtime error: trap(" + mjInput.get() + ")\");");
689 break;
692 default:
693 prl(createComment("unknown op error: " + op, C_ERR));
694 messages.message("unknown op error: " + op, TransMessages.M_ERR);
695 break;
698 boolean wasJump = mjInput.isJumpCode(op);
699 op = mjInput.get();
700 if (op >= 0)
701 if (wasJump)
702 prl("SKIP\n END");
703 else
704 prl("CALL a" + mjInput.getCounter() + "\n END");
706 prl("SKIP\n END\nENDACTIONS;\n");
707 pr(createStandardEnd());
710 public void convertFile(File f) {
711 try {
712 convertStream(new FileInputStream(f));
713 } catch (Exception ex) {
714 ex.printStackTrace();
718 public void printHelp() {
719 printVersion();
720 printUsage();
721 printHelpOutput();
722 printHelpHelp();
725 public void printLongHelp() {
726 printVersion();
727 printUsage();
728 System.out.println();
729 printHelpOutput();
730 System.out.println();
731 printHelpDirectives();
732 System.out.println();
733 printHelpGenerating();
734 System.out.println();
735 printHelpHelp();
738 public void printHelpOutput() {
739 System.out.println("Output options:");
740 System.out.println(" --screen print output to screen");
741 System.out.println(" -o --oc[+-] include original code in comments");
742 System.out.println(" -v verbose, print warning messages");
743 System.out.println(" -q quiet; don't print even the error messages");
744 System.out.println(" -d print detailed debug messages");
747 public void printHelpGenerating() {
748 System.out.println("Options for generating extra code for tracking code execution");
749 System.out.println(" --genEStackPrint generate print for all EStack changes");
750 System.out.println(" --genAddrPrint generate prints after every address of the original code ");
751 System.out.println(" --genAddrPause generate a pause after every address of the original code ");
752 System.out.println(" --genAddr short for --genAddrPrint and --genAddrPause");
753 System.out.println(" --genAll short for applying all code generation");
756 public void printHelpDirectives(){
757 System.out.println("Alternatives for code generation (* are the defaults):");
758 System.out.print(genPopPush?'*':' ');
759 System.out.println(" --genPopPush generate POP/PUSH instead of TAIL/HEAD");
760 System.out.print(!genPopPush?'*':' ');
761 System.out.println(" --genHeadTail generate TAIL/HEAD instead of POP/PUSH ");
762 System.out.println();
763 System.out.print(genInlinePrint?'*':' ');
764 System.out.println(" --genInlinePrint generate prints directly instead of procedure calls");
765 System.out.print(!genInlinePrint?'*':' ');
766 System.out.println(" --genProcedurePrint generate prints as custom procedure calls");
767 System.out.println();
768 System.out.print(genLocalVars?'*':' ');
769 System.out.println(" --genLocalVars generate local VAR block for temp variables");
770 System.out.print(!genLocalVars?'*':' ');
771 System.out.println(" --genGlobalVars do NOT generate local VAR block for temp variables");
774 public void printHelpHelp() {
775 System.out.println("Help and info options");
776 System.out.println(" -h basic help");
777 System.out.println(" --help print more detailed help");
778 System.out.println(" --version or -version print version and exit");
781 public void printUsage(){
782 System.out.println("usage:\n\t mjc2wsl {options} filename [outfile]");
785 public void printVersion() {
786 System.out.println("MicroJava bytecode to WSL converter. v " + getVersion()
787 + ", by Doni Pracner");
790 public String makeDefaultOutName(String inname){
791 String rez = inname;
792 if (inname.endsWith(".obj"))
793 rez = rez.substring(0, rez.length() - 4);
794 return rez + ".wsl";
797 public void run(String[] args) {
798 if (args.length == 0) {
799 printHelp();
800 } else {
801 int i = 0;
802 while (i < args.length && args[i].charAt(0) == '-') {
803 if (args[i].compareTo("-h") == 0) {
804 printHelp();
805 return;
806 } else if (args[i].compareTo("--help") == 0) {
807 printLongHelp();
808 return;
809 } else if (args[i].compareTo("--version") == 0
810 || args[i].compareTo("-version") == 0) {
811 printVersion();
812 return;
813 } else if (args[i].compareTo("-o") == 0
814 || args[i].startsWith("--oc")) {
815 if (args[i].length() == 2)
816 originalInComments = true;
817 else if (args[i].length() == 5)
818 originalInComments = args[i].charAt(4) == '+';
819 else
820 originalInComments = true;
821 } else if (args[i].compareTo("--screen") == 0) {
822 out = new PrintWriter(System.out);
823 } else if (args[i].compareTo("-d") == 0) {
824 messages.setPrintLevel(TransMessages.M_DEB);// print debug info
825 } else if (args[i].compareTo("-v") == 0) {
826 messages.setPrintLevel(TransMessages.M_WAR);// print warnings
827 } else if (args[i].compareTo("-q") == 0) {
828 messages.setPrintLevel(TransMessages.M_QUIET);// no printing
829 } else if (args[i].compareToIgnoreCase("--genEStackPrint") == 0) {
830 genPrintEStackOnChange = true;
831 } else if (args[i].compareToIgnoreCase("--genAddrPause") == 0) {
832 genPauseAfterEachAddress = true;
833 } else if (args[i].compareToIgnoreCase("--genAddrPrint") == 0) {
834 genPrintForEachAddress = true;
835 } else if (args[i].compareToIgnoreCase("--genAddr") == 0) {
836 genPrintForEachAddress = true;
837 genPauseAfterEachAddress = true;
838 } else if (args[i].compareToIgnoreCase("--genAll") == 0) {
839 genPrintEStackOnChange = true;
840 genPrintForEachAddress = true;
841 genPauseAfterEachAddress = true;
842 } else if (args[i].compareToIgnoreCase("--genPopPush") == 0) {
843 genPopPush = true;
844 } else if (args[i].compareToIgnoreCase("--genInlinePrint") == 0) {
845 genInlinePrint = true;
846 } else if (args[i].compareToIgnoreCase("--genHeadTail") == 0) {
847 genPopPush = false;
848 } else if (args[i].compareToIgnoreCase("--genProcedurePrint") == 0) {
849 genInlinePrint = false;
850 } else if (args[i].compareToIgnoreCase("--genLocalVars") == 0) {
851 genLocalVars = true;
852 } else if (args[i].compareToIgnoreCase("--genGlobalVars") == 0) {
853 genLocalVars = false;
855 i++;
858 if (i >= args.length) {
859 System.err.println("no filename supplied");
860 System.exit(2);
863 Path p = FileSystems.getDefault().getPath(args[i]);
864 if (!Files.exists(p)){
865 System.err.println("input file does not exist");
866 System.exit(1);
869 if (i + 1 < args.length) {
870 try {
871 out = new PrintWriter(args[i + 1]);
872 } catch (Exception e) {
873 System.err.println("error in opening out file:");
874 e.printStackTrace();
877 if (out == null) {
878 // if not set to screen, or a file, make a default filename
879 try {
880 out = new PrintWriter(makeDefaultOutName(args[i]));
881 } catch (Exception e) {
882 System.err.println("error in opening out file:");
883 e.printStackTrace();
886 Calendar now = Calendar.getInstance();
887 try {
888 convertStream(Files.newInputStream(p));
889 } catch (Exception e) {
890 // TODO Auto-generated catch block
891 e.printStackTrace();
893 long mili = Calendar.getInstance().getTimeInMillis()
894 - now.getTimeInMillis();
895 System.out.println("conversion time:" + mili + " ms");
896 messages.printMessageCounters();
897 out.close();
901 public static void main(String[] args) {
902 new mjc2wsl().run(args);
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner