From b6d4238dcdc850b880cda0c6a0cee3e116ca4b81 Mon Sep 17 00:00:00 2001 From: Liliesh Date: Wed, 4 Mar 2026 18:32:24 +0100 Subject: [PATCH] Did some minor things --- .idea/misc.xml | 1 - src/Main.kt | 68 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..5af9c98 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/Main.kt b/src/Main.kt index 998653d..82db5f7 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -1,5 +1,65 @@ -//TIP To Run code, press or -// click the icon in the gutter. -fun main() { - println("Gurr Gurr"); +import java.io.BufferedReader +import java.io.File +import java.io.IOException +import java.io.InputStreamReader +import java.util.Scanner +import kotlin.jvm.Throws +import kotlin.system.exitProcess + +var hadError = false + +@Throws(IOException::class) +fun main(args: Array) { + if(args.size > 1){ + println("Usage: pigeon [script]") + exitProcess(64) + } else if (args.size == 1) { + runFile(args[0]) + } else { + runPrompt() + } +} + +@Throws(IOException::class) +fun runFile(path: String) { + val bytes = File(path).readBytes() + run(bytes.toString()) + + if (hadError){ + exitProcess(65) + } +} + +@Throws(IOException::class) +fun runPrompt() { + val input = InputStreamReader(System.`in`) + val reader = BufferedReader(input) + + while (true) { + print("> ") + val line = reader.readLine() + if (line == null) + break + + run(line) + hadError = false + } +} + +fun run(source: String) { + val scanner = Scanner(source) + val tokens = scanner.tokens() + + tokens.forEach { token -> + println("$token") + } +} + +fun error(line: Int, message: String) { + report(line, "", message) +} + +fun report(line: Int, where: String, message: String) { + System.err.println("[line $line] Error $where: $message") + hadError = true } \ No newline at end of file