Advertisement
Guest User

Compiler for JASM

a guest
Nov 14th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. // sorry for the improper indenting, i copied this from my source
  2.  
  3.     private void write(String file, char[] Data) throws FileNotFoundException {
  4.         String filename = file;
  5.        
  6.         FileOutputStream fos = null;
  7.         DataOutputStream dos = null;
  8.  
  9.         try {
  10.            
  11.             fos = new FileOutputStream(filename);
  12.            
  13.             dos = new DataOutputStream(fos);
  14.              
  15.             for(char d : Data) {
  16.                 dos.writeChar(d);
  17.             }
  18.            
  19.         }
  20.         catch (FileNotFoundException fnfe) {
  21.             System.out.println("File not found" + fnfe);
  22.         }
  23.         catch (IOException ioe) {
  24.             System.out.println("Error while writing to file" + ioe);
  25.         }
  26.         finally {
  27.             try {
  28.                 if (dos != null) {
  29.                     dos.close();
  30.                 }
  31.                 if (fos != null) {
  32.                     fos.close();
  33.                 }
  34.             }
  35.             catch (Exception e) {
  36.                 System.out.println("Error while closing streams" + e);
  37.             }
  38.         }  
  39.     }
  40.    
  41.     private int get_lines(String file) throws IOException {
  42.        
  43.         InputStream is = new BufferedInputStream(new FileInputStream(file));
  44.         try {
  45.             byte[] c = new byte[1024];
  46.             int count = 0;
  47.             int readChars = 0;
  48.             boolean empty = true;
  49.             while ((readChars = is.read(c)) != -1) {
  50.                 empty = false;
  51.                 for (int i = 0; i < readChars; ++i) {
  52.                     if (c[0] == ';') {
  53.                         --count;
  54.                     }
  55.                     if (c[i] == '\n') {
  56.                         ++count;
  57.                     }
  58.                 }
  59.             }
  60.             return (count == 0 && !empty) ? 1 : count;
  61.         } finally {
  62.             is.close();
  63.         }
  64.     }
  65.    
  66.     private static String getChecksum(Serializable object) throws IOException, NoSuchAlgorithmException {
  67.         ByteArrayOutputStream baos = null;
  68.         ObjectOutputStream oos = null;
  69.         try {
  70.             baos = new ByteArrayOutputStream();
  71.             oos = new ObjectOutputStream(baos);
  72.             oos.writeObject(object);
  73.             MessageDigest md = MessageDigest.getInstance("MD5");
  74.             byte[] thedigest = md.digest(baos.toByteArray());
  75.             return DatatypeConverter.printHexBinary(thedigest);
  76.         } finally {
  77.             oos.close();
  78.             baos.close();
  79.         }
  80.     }
  81.    
  82.     /**
  83.      * Compiles JCPU compatible code.
  84.      * @param file File to compile.
  85.      * @throws FileNotFoundException Thrown when the file doesnt exist.
  86.      */
  87.     public void compile(String file, String toFile) throws FileNotFoundException {
  88.         try {
  89.             String argchk = "NO CHECKSUM DEFINED";
  90.             char[] data = new char[get_lines(file) + 1];
  91.             System.out.println("Compiling " + file + " with about " + (get_lines(file) + 1) + " lines.");
  92.             BufferedReader reader = new BufferedReader(new FileReader(file));
  93.             String line = reader.readLine();
  94.             int chr = 0;
  95.             while (line != null) {
  96.                 if(line.startsWith(";")) {
  97.                     System.out.println("New line");
  98.                 }
  99.                 if(line.startsWith("P,")) {
  100.                     String arg = line.substring(2);
  101.                     int x = Integer.parseInt(arg);
  102.                     if(x > 999) {
  103.                         x = 0;
  104.                     }
  105.                     data[chr] = (char) (0x4000 + x);
  106.                     System.out.println("Print command. Value: " + Integer.toHexString(0x4000 + x));
  107.                 }
  108.                 if(line.startsWith("CHK,")) {
  109.                     argchk = line.substring(4);
  110.                     System.out.println("Verifying checksum after compile.");
  111.                 }
  112.                 if(line.startsWith("A,")) {
  113.  
  114.                     String arg = line.substring(2);
  115.                     int x = Integer.parseInt(arg);
  116.                     if(x > 999) {
  117.                         x = 0;
  118.                     }
  119.                     data[chr] = (char) (0x1000 + x);
  120.                     System.out.println("Write to register A command. Value: " + Integer.toHexString(0x1000 + x));
  121.                 }
  122.                 if(line.startsWith("B,")) {
  123.  
  124.                     String arg = line.substring(2);
  125.                     int x = Integer.parseInt(arg);
  126.                     if(x > 999) {
  127.                         x = 0;
  128.                     }
  129.                     data[chr] = (char) (0x2000 + x);
  130.                     System.out.println("Write to register B command. Value: " + Integer.toHexString(0x2000 + x));
  131.                 }
  132.                 if(line.startsWith("T,")) {
  133.                     String arg = line.substring(2);
  134.                     int x = Integer.parseInt(arg);
  135.                     if(x > 999) {
  136.                         x = 0;
  137.                     }
  138.                     data[chr] = (char) (0x3000 + x);
  139.                     System.out.println("Write to register Temp command. Value: " + Integer.toHexString(0x3000 + x));
  140.                 }
  141.                 if(line.startsWith("J,")) {
  142.  
  143.                     String arg = line.substring(2);
  144.                     int x = Integer.parseInt(arg);
  145.                     if(x > 999) {
  146.                         x = 0;
  147.                     }
  148.                     data[chr] = (char) (0x5000 + x);
  149.                     System.out.println("Jump command. Value: " + Integer.toHexString(0x5000 + x));
  150.                 }
  151.                 if(line.startsWith("M,")) {
  152.                     String arg = line.substring(2);
  153.                     int x = Integer.parseInt(arg);
  154.                     if(x > 999) {
  155.                         x = 0;
  156.                     }
  157.                     data[chr] = (char) (0x6000 + x);
  158.                     System.out.println("Math command. Value: " + Integer.toHexString(0x6000 + x));
  159.                 }
  160.                 if(line.startsWith("NON")) {
  161.                     System.out.println("Writing a No Line");
  162.                     data[chr] = (char) (0x00EE);
  163.                 }
  164.                 if(line.startsWith("LIN")) {
  165.                     System.out.println("Writing an enable line");
  166.                     data[chr] = (char) (0x00ED);
  167.                 }
  168.                 if(line.startsWith("FJT")) {
  169.                     System.out.println("Writing an jump from location");
  170.                     data[chr] = (char) (0x00EF);
  171.                 }
  172.                 if(line.startsWith("CHC")) {
  173.                     System.out.println("Checksum verify command");
  174.                     data[chr] = (char) (0x00EC);
  175.                 }
  176.                 if(line.startsWith("END")) {
  177.                     System.out.println("Writing an end");
  178.                     data[chr] = (char) (0xF000);
  179.                 }
  180.                 if(line.startsWith("RGAB")) {
  181.                     System.out.println("Getting a compare");
  182.                     data[chr] = (char) (0x00EB);
  183.                 }
  184.                 if(line.startsWith("SKP")) {
  185.                     System.out.println("Skipping over next instruction");
  186.                     data[chr] = (char) (0x00EA);
  187.                 }
  188.                 chr = chr + 1;
  189.                 line = reader.readLine();
  190.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement