package mtmc.os.shell.builtins import mtmc.emulator.MonTanaMiniComputer import mtmc.os.exec.Executable.Companion.load import mtmc.os.fs.FileSystem import mtmc.os.fs.Path import mtmc.os.shell.ShellCommand import mtmc.tokenizer.MTMCTokenizer class LoadCommand : ShellCommand() { @Throws(Exception::class) override fun exec(tokens: MTMCTokenizer, computer: MonTanaMiniComputer) { val fs = computer.fileSystem val program = tokens.collapseTokensAsString() require(!(program == null || program.isBlank())) { "missing or required argument 'src'" } val srcPath: Path = getDiskPath(program, fs) val exec = load(srcPath) computer.load(exec!!.code, exec.data, exec.graphics, exec.debugInfo) val source = tokens.source // set up an argument if given if (tokens.more()) { val firstArgToken = tokens.consume() val startChar: Int = firstArgToken.start val arg = source.substring(startChar) val strippedArg = arg.trim() if (!strippedArg.isEmpty()) { computer.setArg(strippedArg) } } } override val help: String get() = """ load - exec : path to an executable file """.trimIndent() companion object { fun getDiskPath(pathString: String, fs: FileSystem): Path { val path = Path.of("disk" + fs.resolve(pathString)) return path.toAbsolutePath() } } }