gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
f5714aa692cb59df48c67ad996b09d2df75e48d8
[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,");
185 } else
186 ret.append("\n\tmjvm_flag_jump := 0,");
187 ret.append("mjvm_locals := ARRAY(1,0),");
188 ret.append("\n\tmjvm_statics := ARRAY("+numWords+",0),");
189 ret.append("\n\tmjvm_arrays := < >,");
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:=1 TO ");
216 ret.append("MAX(0, format-SLENGTH(@String(val))) 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("@Read_Line_Proc(VAR debug_disposable_string, 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 if (genLocalVars) {
606 prl(createStartVar("tempa", "tempb"));
607 prl(createTopTwoEStack());
608 prl("IF tempb " + mjInput.getRelationFor(op)
609 + " tempa THEN mjvm_flag_jump := 1"
610 + " ELSE mjvm_flag_jump := 0"
611 + " FI;");
612 prl(createEndVar());
613 prl("IF mjvm_flag_jump = 1 THEN CALL a"
614 + (mjInput.getCounter() + mjInput.get2())
615 + " ELSE CALL a" + (mjInput.getCounter() + 1)
616 + " FI;");
617 } else {
618 prl(createTopTwoEStack());
619 prl("IF tempb " + mjInput.getRelationFor(op)
620 + " tempa THEN CALL a"
621 + (mjInput.getCounter() + mjInput.get2())
622 + " ELSE CALL a" + (mjInput.getCounter() + 1)
623 + " FI;");
625 break;
628 case call: {
629 prl("CALL a" + (mjInput.getCounter() + mjInput.get2()) + ";");
630 break;
633 case return_: {
634 // we let the actions return
635 // there is nothing to clean up
636 prl("SKIP\n END\n b" + mjInput.getCounter() + " ==");
637 break;
639 case enter: {
640 int parameters = mjInput.get();
642 int locals = mjInput.get();
643 prl(createToMStack("mjvm_locals"));
644 prl("mjvm_locals := ARRAY(" + locals + ",0);");
645 for (int i = parameters - 1; i >= 0; i--)
646 prl(createFromEStack(createLocal(i)));
647 break;
649 case exit: {
650 prl(createFromMStack("mjvm_locals"));
651 break;
654 // read, print
655 case bread: {
656 // TODO maybe we'll need a bufer for multi chars!
657 prl(createStartVar("tempa"));
658 prl("@Read_Line_Proc(VAR tempa, Standard_Input_Port);");
659 prl("tempa := @String_To_List(tempa)[1];");
660 prl(createToEStack("tempa"));
661 prl(createEndVar());
662 break;
664 case read: {
665 prl(createStartVar("tempa"));
666 prl("@Read_Line_Proc(VAR tempa, Standard_Input_Port);");
667 prl("tempa := @String_To_Num(tempa);");
668 prl(createToEStack("tempa"));
669 prl(createEndVar());
670 break;
673 // the prints
674 case bprint: {
675 prl(createStartVar("tempa", "tempb"));
676 prl(createTopTwoEStack());
677 if (genInlinePrint){
678 prl(createComment("print spacing and transformation",C_SPEC));
679 prl("PRINFLUSH(@Format(tempa, @List_To_String(< tempb >)));");
680 } else
681 prl("Print_MJ_CHAR(tempb,tempa);");
682 prl(createEndVar());
683 break;
685 case print: {
686 prl(createStartVar("tempa", "tempb"));
688 prl(createTopTwoEStack());
689 if (genInlinePrint){
690 prl(createComment("print spacing",C_SPEC));
691 prl("PRINFLUSH(@Format(tempa,tempb));");
693 else
694 prl("Print_MJ(tempb,tempa);");
695 prl(createEndVar());
696 break;
699 case trap: {
700 prl("ERROR(\"Runtime error: trap(" + mjInput.get() + ")\");");
701 break;
704 default:
705 prl(createComment("unknown op error: " + op, C_ERR));
706 messages.message("unknown op error: " + op, TransMessages.M_ERR);
707 break;
710 boolean wasJump = mjInput.isJumpCode(op);
711 op = mjInput.get();
712 if (op >= 0)
713 if (wasJump)
714 prl("SKIP\n END");
715 else
716 prl("CALL a" + mjInput.getCounter() + "\n END");
718 prl("SKIP\n END\nENDACTIONS;\n");
719 pr(createStandardEnd());
722 public void convertFile(File f) {
723 try {
724 convertStream(new FileInputStream(f));
725 } catch (Exception ex) {
726 ex.printStackTrace();
730 public void printHelp() {
731 printVersion();
732 printUsage();
733 printHelpOutput();
734 printHelpHelp();
737 public void printLongHelp() {
738 printVersion();
739 printUsage();
740 System.out.println();
741 printHelpOutput();
742 System.out.println();
743 printHelpDirectives();
744 System.out.println();
745 printHelpGenerating();
746 System.out.println();
747 printHelpHelp();
750 public void printHelpOutput() {
751 System.out.println("Output options:");
752 System.out.println(" --screen print output to screen");
753 System.out.println(" -o --oc[+-] include original code in comments");
754 System.out.println(" -v verbose, print warning messages");
755 System.out.println(" -q quiet; don't print even the error messages");
756 System.out.println(" -d print detailed debug messages");
759 public void printHelpGenerating() {
760 System.out.println("Options for generating extra code for tracking code execution");
761 System.out.println(" --genEStackPrint generate print for all EStack changes");
762 System.out.println(" --genAddrPrint generate prints after every address of the original code ");
763 System.out.println(" --genAddrPause generate a pause after every address of the original code ");
764 System.out.println(" --genAddr short for --genAddrPrint and --genAddrPause");
765 System.out.println(" --genAll short for applying all code generation");
768 public void printHelpDirectives(){
769 System.out.println("Alternatives for code generation (* are the defaults):");
770 System.out.print(genPopPush?'*':' ');
771 System.out.println(" --genPopPush generate POP/PUSH instead of TAIL/HEAD");
772 System.out.print(!genPopPush?'*':' ');
773 System.out.println(" --genHeadTail generate TAIL/HEAD instead of POP/PUSH ");
774 System.out.println();
775 System.out.print(genInlinePrint?'*':' ');
776 System.out.println(" --genInlinePrint generate prints directly instead of procedure calls");
777 System.out.print(!genInlinePrint?'*':' ');
778 System.out.println(" --genProcedurePrint generate prints as custom procedure calls");
779 System.out.println();
780 System.out.print(genLocalVars?'*':' ');
781 System.out.println(" --genLocalVars generate local VAR block for temp variables");
782 System.out.print(!genLocalVars?'*':' ');
783 System.out.println(" --genGlobalVars do NOT generate local VAR block for temp variables");
786 public void printHelpHelp() {
787 System.out.println("Help and info options");
788 System.out.println(" -h basic help");
789 System.out.println(" --help print more detailed help");
790 System.out.println(" --version or -version print version and exit");
793 public void printUsage(){
794 System.out.println("usage:\n\t mjc2wsl {options} filename [outfile]");
797 public void printVersion() {
798 System.out.println("MicroJava bytecode to WSL converter. v " + getVersion()
799 + ", by Doni Pracner");
802 public String makeDefaultOutName(String inname){
803 String rez = inname;
804 if (inname.endsWith(".obj"))
805 rez = rez.substring(0, rez.length() - 4);
806 return rez + ".wsl";
809 public void run(String[] args) {
810 if (args.length == 0) {
811 printHelp();
812 } else {
813 int i = 0;
814 while (i < args.length && args[i].charAt(0) == '-') {
815 if (args[i].compareTo("-h") == 0) {
816 printHelp();
817 return;
818 } else if (args[i].compareTo("--help") == 0) {
819 printLongHelp();
820 return;
821 } else if (args[i].compareTo("--version") == 0
822 || args[i].compareTo("-version") == 0) {
823 printVersion();
824 return;
825 } else if (args[i].compareTo("-o") == 0
826 || args[i].startsWith("--oc")) {
827 if (args[i].length() == 2)
828 originalInComments = true;
829 else if (args[i].length() == 5)
830 originalInComments = args[i].charAt(4) == '+';
831 else
832 originalInComments = true;
833 } else if (args[i].compareTo("--screen") == 0) {
834 out = new PrintWriter(System.out);
835 } else if (args[i].compareTo("-d") == 0) {
836 messages.setPrintLevel(TransMessages.M_DEB);// print debug info
837 } else if (args[i].compareTo("-v") == 0) {
838 messages.setPrintLevel(TransMessages.M_WAR);// print warnings
839 } else if (args[i].compareTo("-q") == 0) {
840 messages.setPrintLevel(TransMessages.M_QUIET);// no printing
841 } else if (args[i].compareToIgnoreCase("--genEStackPrint") == 0) {
842 genPrintEStackOnChange = true;
843 } else if (args[i].compareToIgnoreCase("--genAddrPause") == 0) {
844 genPauseAfterEachAddress = true;
845 } else if (args[i].compareToIgnoreCase("--genAddrPrint") == 0) {
846 genPrintForEachAddress = true;
847 } else if (args[i].compareToIgnoreCase("--genAddr") == 0) {
848 genPrintForEachAddress = true;
849 genPauseAfterEachAddress = true;
850 } else if (args[i].compareToIgnoreCase("--genAll") == 0) {
851 genPrintEStackOnChange = true;
852 genPrintForEachAddress = true;
853 genPauseAfterEachAddress = true;
854 } else if (args[i].compareToIgnoreCase("--genPopPush") == 0) {
855 genPopPush = true;
856 } else if (args[i].compareToIgnoreCase("--genInlinePrint") == 0) {
857 genInlinePrint = true;
858 } else if (args[i].compareToIgnoreCase("--genHeadTail") == 0) {
859 genPopPush = false;
860 } else if (args[i].compareToIgnoreCase("--genProcedurePrint") == 0) {
861 genInlinePrint = false;
862 } else if (args[i].compareToIgnoreCase("--genLocalVars") == 0) {
863 genLocalVars = true;
864 } else if (args[i].compareToIgnoreCase("--genGlobalVars") == 0) {
865 genLocalVars = false;
866 } else {
867 System.err.println("unknown option: "+args[i]);
869 i++;
872 if (i >= args.length) {
873 System.err.println("no filename supplied");
874 System.exit(2);
877 Path p = FileSystems.getDefault().getPath(args[i]);
878 if (!Files.exists(p)){
879 System.err.println("input file does not exist");
880 System.exit(1);
883 if (i + 1 < args.length) {
884 try {
885 out = new PrintWriter(args[i + 1]);
886 } catch (Exception e) {
887 System.err.println("error in opening out file:");
888 e.printStackTrace();
891 if (out == null) {
892 // if not set to screen, or a file, make a default filename
893 try {
894 out = new PrintWriter(makeDefaultOutName(args[i]));
895 } catch (Exception e) {
896 System.err.println("error in opening out file:");
897 e.printStackTrace();
900 Calendar now = Calendar.getInstance();
901 try {
902 convertStream(Files.newInputStream(p));
903 } catch (Exception e) {
904 // TODO Auto-generated catch block
905 e.printStackTrace();
907 long mili = Calendar.getInstance().getTimeInMillis()
908 - now.getTimeInMillis();
909 System.out.println("conversion time:" + mili + " ms");
910 messages.printMessageCounters();
911 out.close();
915 public static void main(String[] args) {
916 new mjc2wsl().run(args);
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner