home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / sys / runtime / SmallEiffelRuntime.java < prev    next >
Text File  |  1999-06-05  |  8KB  |  312 lines

  1. /*
  2. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  3. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  4. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  5. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  6. -- this header is kept unaltered, and a notification of the changes is added.
  7. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  8. -- another product.
  9. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  10. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  11. --                       http://www.loria.fr/SmallEiffel
  12. --
  13. */
  14.  
  15. import java.io.*;
  16.  
  17. /**
  18.  * The SmallEiffelRuntime class implements some external "SmallEiffel" 
  19.  * features. This class must be present to execute the Java byte-code
  20.  * generated by command `compile_to_jvm'.
  21.  * Do not forget to add this class in your CLASSPATH system
  22.  * environment variable.
  23.  * You can also move this class file in an appropriate directory.
  24.  */
  25. public final class SmallEiffelRuntime {
  26.  
  27.     /** 
  28.      * To implement Eiffel GENERAL.se_getenv
  29.      */
  30.     public static String se_getenv (Object bytes) {
  31.     String name = bytesToString(bytes);
  32.     return System.getProperty(name);
  33.     }
  34.  
  35.     /** 
  36.      * To implement Eiffel GENERAL.print_run_time_stack
  37.      */
  38.     public static void print_run_time_stack () {
  39.     Throwable stack = new Throwable();
  40.     System.err.println("Printing the JVM stack:");
  41.     stack.printStackTrace();
  42.     }
  43.  
  44.     /** 
  45.      * To implement Eiffel GENERAL.die_with_code
  46.      */
  47.     public static void die_with_code (int code) {
  48.     System.exit(code);
  49.     }
  50.  
  51.     /** 
  52.      * To implement Eiffel GENERAL.se_system
  53.      */
  54.     public static void se_system (Object system_command_line) {
  55.     String scl = bytesToString(system_command_line);
  56.     Process process;
  57.     
  58.     try {
  59.         process = Runtime.getRuntime().exec(scl);
  60.  
  61.         process.waitFor();
  62.         }
  63.     catch (Exception e) {
  64.         System.err.print("SmallEiffelRuntime: se_system(\"");
  65.         System.err.print(scl);
  66.         System.err.println("\") failed.");
  67.         print_run_time_stack();
  68.         System.err.println("Try to continue execution.");
  69.     }
  70.     }
  71.  
  72.     /**
  73.      * To implement Eiffel FILE_TOOLS.se_remove
  74.      */
  75.     public static void se_remove (Object bytes) {
  76.     String name = bytesToString(bytes);
  77.     File file = new File(name);
  78.     file.delete();
  79.     }
  80.  
  81.     /**
  82.      * To implement the Eiffel FILE_TOOLS.se_rename
  83.      */
  84.     public static void se_rename (Object old_path, Object new_path) {
  85.     String oldPath = bytesToString(old_path);
  86.     String newPath = bytesToString(new_path);
  87.     File oldFile = new File(oldPath);
  88.     File newFile = new File(newPath);
  89.  
  90.     oldFile.renameTo(newFile);
  91.     }
  92.  
  93.     /**
  94.      * To implement the Eiffel STD_FILE_READ.sfr_open
  95.      */
  96.     public static Object sfr_open (Object bytes) {
  97.     String name = bytesToString(bytes);
  98.     File file = new File(name);
  99.  
  100.     try {
  101.         return new FileInputStream (file);
  102.     } catch (FileNotFoundException e) {
  103.         return null;
  104.     }
  105.     }
  106.  
  107.     /**
  108.      * To implement the Eiffel STD_FILE_WRITE.sfw_open
  109.      */
  110.     public static Object sfw_open (Object bytes) {
  111.     String name = bytesToString(bytes);
  112.     File file = new File(name);
  113.  
  114.     try {
  115.         return new FileOutputStream (file);
  116.     } catch (IOException e) {
  117.         return null;
  118.     }
  119.     }
  120.  
  121.     /**
  122.      * To implement the Eiffel STRING.se_string2double
  123.      */
  124.     public static double se_string2double (Object number_view) {
  125.     String number = bytesToString(number_view);
  126.     
  127.     return Double.valueOf(number).doubleValue();
  128.     }
  129.  
  130.     /**
  131.      * Convert <code>bytes</code> as a  <code>String </code>.
  132.      * Assume <code>bytes</code> is a C like string (ie. with a
  133.      * null character is the end).
  134.      */
  135.     public static String bytesToString (Object bytes) {
  136.     int i = 0;
  137.     byte b [] = ((byte[])bytes);
  138.     while ( b[i] != 0 ) {
  139.         i++;
  140.     }
  141.     return (new String(b,0,i));
  142.     }
  143.  
  144.     public static int internal_exception_number = 3;
  145.  
  146.     public static void runtime_error(int line,
  147.                      int column,
  148.                      String path,
  149.                      String type,
  150.                      String message) {
  151.         System.err.println("Run-time Eiffel error.");
  152.     if (message != null) {
  153.         System.err.println(message);
  154.     }
  155.     if (type != null) {
  156.         System.err.print("Eiffel type: \"");
  157.         System.err.print(type);
  158.         System.err.println("\".");
  159.     }
  160.     if (line != 0) {
  161.         System.err.print("Error occurs at line ");
  162.         System.err.print(line);
  163.         System.err.print(" column ");
  164.         System.err.print(column);
  165.         System.err.print(" in \"");
  166.         System.err.print(path);
  167.         System.err.println("\".");
  168.     }
  169.     print_run_time_stack();
  170.     System.exit(1);
  171.     }
  172.  
  173.     public static void runtime_error_bad_target(Object target,
  174.                         int line,
  175.                         int column,
  176.                         String path,
  177.                         String type,
  178.                         String message) {
  179.     
  180.     if (target == null) {
  181.         System.err.println("Void (null) target for a call.");
  182.     }
  183.     else {
  184.         System.err.println("Bad target for a call.");
  185.         System.err.print("Target is \"");
  186.         System.err.print(target.toString());
  187.         System.err.println("\"");
  188.     }
  189.     runtime_error(line,column,path,type,message);
  190.     }
  191.  
  192.     public static int runtime_error_inspect(int inspect_value,
  193.                         int line,
  194.                         int column,
  195.                         String path) {
  196.     System.err.print("Bad inspect value = ");
  197.     System.err.println(inspect_value);
  198.     runtime_error(line,column,path,null,
  199.               "Invalid inspect (nothing selected).");
  200.     return inspect_value;
  201.     }
  202.  
  203.     public static int runtime_check_loop_variant(int loop_counter,
  204.                          int prev_variant,
  205.                          int next_variant,
  206.                          int line,
  207.                          int column,
  208.                          String path) {
  209.     if (next_variant < 0) {
  210.         bad_loop_variant(loop_counter,
  211.                  prev_variant,
  212.                  next_variant,
  213.                  line,column,path);
  214.         return 0;
  215.     }
  216.     else if (loop_counter == 0) {
  217.         return next_variant;
  218.     }
  219.     else if (next_variant < prev_variant) {
  220.         return next_variant;
  221.     }
  222.     else {
  223.         bad_loop_variant(loop_counter,
  224.                  prev_variant,
  225.                  next_variant,
  226.                  line,column,path);
  227.         return 0;
  228.     }
  229.     }
  230.  
  231.     private static void bad_loop_variant(int loop_counter,
  232.                     int prev_variant,
  233.                     int next_variant,
  234.                     int line,
  235.                     int column,
  236.                     String path) {
  237.     System.err.print("Loop body counter = ");
  238.     System.err.print(loop_counter);
  239.     System.err.println(" (done)");
  240.     if (loop_counter > 0) {
  241.         System.err.print("Previous variant = ");
  242.         System.err.println(prev_variant);
  243.     }
  244.     System.err.print("Current variant = ");
  245.     System.err.print(next_variant);
  246.     if (next_variant < 0) {
  247.         System.err.println(" (Bad Negative value)");
  248.     }
  249.     else {
  250.         System.err.println(" (must be less than previous)");
  251.     }
  252.     runtime_error(line,column,path,null,"Loop variant violation.");
  253.     }
  254.     
  255.     /**
  256.      * For -trace flag (this is a new undocumented stuff).
  257.      */
  258.     public static void se_trace(Object target,
  259.                 int line,
  260.                 int column,
  261.                 String path) {
  262.     se_trace_show(line,column,path);
  263.     }
  264.  
  265.     public static void se_trace(boolean target,
  266.                 int line,
  267.                 int column,
  268.                 String path) {
  269.     if (target) {
  270.         System.out.print("Current = true ");
  271.     }
  272.     else {
  273.         System.out.print("Current = false ");
  274.     }
  275.     se_trace_show(line,column,path);
  276.     }
  277.  
  278.     public static void se_trace(byte target,
  279.                 int line,
  280.                 int column,
  281.                 String path) {
  282.     System.out.print("Current = ");
  283.     System.out.print(target);
  284.     System.out.print(" ");
  285.     se_trace_show(line,column,path);
  286.     }
  287.  
  288.     public static void se_trace(int target,
  289.                 int line,
  290.                 int column,
  291.                 String path) {
  292.     System.out.print("Current = ");
  293.     System.out.print(target);
  294.     System.out.print(" ");
  295.     se_trace_show(line,column,path);
  296.     }
  297.  
  298.     public static void se_trace_show(int line,
  299.                      int column,
  300.                      String path) {
  301.     System.out.print(path);
  302.     System.out.print(" l=");
  303.     System.out.print(line);
  304.     System.out.print(" c=");
  305.     System.out.print(column);
  306.     System.out.println();
  307.     System.out.flush();
  308.     }
  309.  
  310. }
  311.  
  312.