From e2f3ab756db056ee01b16afbe49108f7d4a0de07 Mon Sep 17 00:00:00 2001
From: Gabriel Tofvesson <gabto095@student.liu.se>
Date: Fri, 5 Apr 2019 21:34:49 +0200
Subject: [PATCH] Implement compilation from files

---
 compiler.kt | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/compiler.kt b/compiler.kt
index f54ee15..2890b69 100644
--- a/compiler.kt
+++ b/compiler.kt
@@ -271,8 +271,15 @@ fun parseInstructions(fileData: String): ShortArray {
         val commentIndex = lines[index].indexOf("#")
         if(commentIndex > 0)
             lines[index] = lines[index].substring(0, commentIndex)
-        val insn = parseInstruction(lines[index], unit)
-        if(insn != null) unit.registerInstruction(insn)
+        try{
+            val insn = parseInstruction(lines[index], unit)
+            if(insn != null) unit.registerInstruction(insn)
+        }catch(e: Exception){
+            print("An error occurred when compiling (line $index)")
+            val message = e.message
+            if(message != null) print(":\n\t$message")
+            println()
+        }
     }
     return unit.compile()
 }
@@ -291,7 +298,7 @@ fun main(args: Array<String>){
 		System.err.println("Given file doesn't exist!")
 		System.exit(-2)
 	}
-    for(insn in parseInstructions("LOAD GR0,0xFF"))
+    for(insn in parseInstructions(file.readText()))
         println(insn.toUHex())
 }