diff options
Diffstat (limited to 'VM2M.java')
-rw-r--r-- | VM2M.java | 94 |
1 files changed, 92 insertions, 2 deletions
@@ -1,5 +1,14 @@ import java.io.*; import java.util.ArrayList; +import cs132.util.ProblemException; +import cs132.vapor.parser.VaporParser; +import cs132.vapor.ast.VaporProgram; +import cs132.vapor.ast.VBuiltIn.Op; +import cs132.vapor.ast.VDataSegment; +import cs132.vapor.ast.VFunction; +import cs132.vapor.ast.VInstr; + +import java.util.Arrays; import condense.*; import misc.*; @@ -8,7 +17,88 @@ public class VM2M { public static void main(String[] args) { - ArrayList<String> strProgram = SplitProgramInputStream.split(System.in); - CondenseVisitor cv = new CondenseVisitor(strProgram); + try { + byte[] bytes = readAllBytes(System.in); + InputStream is1 = new ByteArrayInputStream(bytes); + InputStream is2 = new ByteArrayInputStream(bytes); + + InputStream systemInCopy = createCopyOfSystemIn(); + ArrayList<String> strProg = SplitProgramInputStream.split(is1); + + VaporProgram prog = parseVapor(is2, System.out); + + MinimalLogger.info("Removing extraneous lines from the program representation..."); + for (String line : new ArrayList<String>(strProg)) { + // delete all lines not a function table! + if (!line.trim().startsWith("const") && !line.trim().startsWith(":")) + strProg.remove(line); + } + MinimalLogger.info(String.format("New program: %s", + strProg.toString())); + + CondenseVisitor cv = new CondenseVisitor(prog, strProg); + + System.out.println(String.join("\n", cv.getMIPS())); + + } catch (IOException e) { + System.out.println(e.toString()); + System.exit(1); + } } + + public static VaporProgram parseVapor(InputStream in, PrintStream err) throws IOException { + Op[] ops = { + Op.Add, Op.Sub, Op.MulS, Op.Eq, Op.Lt, Op.LtS, + Op.PrintIntS, Op.HeapAllocZ, Op.Error, + }; + boolean allowLocals = false; + String[] registers = { + "v0", "v1", + "a0", "a1", "a2", "a3", + "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", + "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", + "t8", + }; + boolean allowStack = true; + + VaporProgram program; + try { + program = VaporParser.run(new InputStreamReader(in), 1, 1, + java.util.Arrays.asList(ops), + allowLocals, registers, allowStack); + } catch (ProblemException ex) { + err.println(ex.getMessage()); + return null; + } + + return program; + } + + public static byte[] readAllBytes(InputStream inputStream) throws IOException { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + int nRead; + byte[] data = new byte[1024]; + while ((nRead = inputStream.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead); + } + buffer.flush(); + return buffer.toByteArray(); + } + + private static InputStream createCopyOfSystemIn() { + byte[] buffer = new byte[1024]; + int bytesRead; + ByteArrayInputStream bais = new ByteArrayInputStream(buffer); + + try { + bytesRead = System.in.read(buffer); + bais = new ByteArrayInputStream(buffer, 0, bytesRead); + } catch (IOException e) { + e.printStackTrace(); + } + + return bais; + } + + } |