|
| 1 | +import arrow.core.Either |
| 2 | +import arrow.core.andThen |
| 3 | +import data.compiler.unwrap |
| 4 | +import data.getMessage |
| 5 | +import domain.Compiler |
| 6 | +import domain.Parser |
1 | 7 | import domain.Repl
|
| 8 | +import domain.Tokenizer |
| 9 | +import domain.VirtualMachine |
| 10 | +import kotlinx.cli.ArgParser |
| 11 | +import kotlinx.cli.ArgType |
| 12 | +import kotlinx.cli.default |
| 13 | +import kotlinx.cli.optional |
| 14 | +import util.loadFile |
| 15 | +import kotlin.system.exitProcess |
2 | 16 |
|
3 | 17 | fun main(args: Array<String>) {
|
4 |
| - Repl.createInstance().run(args.getOrNull(0), true) |
| 18 | + |
| 19 | + val parser = ArgParser("lispik") |
| 20 | + |
| 21 | + val noGlobalEnv by parser.option( |
| 22 | + type = ArgType.Boolean, |
| 23 | + shortName = "ng", |
| 24 | + fullName = "no-global", |
| 25 | + description = "Disables usage of the global environment for calling functions. Define keyword is not available in this mode and will throw an error", |
| 26 | + ).default(false) |
| 27 | + |
| 28 | + val evalOnly by parser.option( |
| 29 | + type = ArgType.Boolean, |
| 30 | + shortName = "e", |
| 31 | + fullName = "eval-only", |
| 32 | + description = "Evaluates the file on input, print results and exit. Disables REPL.", |
| 33 | + ).default(false) |
| 34 | + |
| 35 | + val compileOnly by parser.option( |
| 36 | + type = ArgType.Boolean, |
| 37 | + shortName = "c", |
| 38 | + fullName = "compile-only", |
| 39 | + description = "Just prints out compiled bytecode in human-readable form. Disables REPL.", |
| 40 | + ).default(false) |
| 41 | + |
| 42 | + val showByteCode by parser.option( |
| 43 | + type = ArgType.Boolean, |
| 44 | + shortName = "b", |
| 45 | + fullName = "show-bytecode", |
| 46 | + description = "Show compiled bytecode in the REPL mode", |
| 47 | + ).default(false) |
| 48 | + |
| 49 | + val debug by parser.option( |
| 50 | + type = ArgType.Boolean, |
| 51 | + shortName = "d", |
| 52 | + fullName = "debug", |
| 53 | + description = "Shows debug logs, lots of them", |
| 54 | + ).default(false) |
| 55 | + |
| 56 | + val filename by parser.argument( |
| 57 | + type = ArgType.String, |
| 58 | + description = "Load a library at startup and evaluates it.", |
| 59 | + ).optional() |
| 60 | + |
| 61 | + parser.parse(args) |
| 62 | + |
| 63 | + when { |
| 64 | + compileOnly -> compileOnly(filename, !noGlobalEnv) |
| 65 | + evalOnly -> evalOnly(filename, !noGlobalEnv) |
| 66 | + else -> |
| 67 | + Repl.createInstance().run( |
| 68 | + filename = filename, |
| 69 | + showByteCode = showByteCode, |
| 70 | + debug = debug, |
| 71 | + globalEnv = !noGlobalEnv, |
| 72 | + ) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +private fun compileOnly(filename: String?, globalEnabled: Boolean) { |
| 77 | + val source = filename?.let { |
| 78 | + when (val res = loadFile(filename)) { |
| 79 | + is Either.Left -> { |
| 80 | + println("Failed to read file '$filename'") |
| 81 | + exitProcess(1) |
| 82 | + } |
| 83 | + |
| 84 | + is Either.Right -> { |
| 85 | + res.value |
| 86 | + } |
| 87 | + } |
| 88 | + } ?: run { |
| 89 | + println("No file given, exiting") |
| 90 | + exitProcess(2) |
| 91 | + } |
| 92 | + |
| 93 | + val tokenizer = Tokenizer.from(source) |
| 94 | + val parser = Parser.from(tokenizer) |
| 95 | + parser.parseToAST().andThen { globalScope -> |
| 96 | + Compiler.from().compile(globalScope, globalEnabled) |
| 97 | + }.tap { |
| 98 | + println(it) |
| 99 | + }.tapInvalid { |
| 100 | + println(it.getMessage()) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +private fun evalOnly(filename: String?, globalEnabled: Boolean) { |
| 105 | + val source = filename?.let { |
| 106 | + when (val res = loadFile(filename)) { |
| 107 | + is Either.Left -> { |
| 108 | + println("Failed to read file '$filename'") |
| 109 | + exitProcess(1) |
| 110 | + } |
| 111 | + |
| 112 | + is Either.Right -> { |
| 113 | + res.value |
| 114 | + } |
| 115 | + } |
| 116 | + } ?: run { |
| 117 | + println("No file given, exiting") |
| 118 | + exitProcess(2) |
| 119 | + } |
| 120 | + |
| 121 | + val tokenizer = Tokenizer.from(source) |
| 122 | + val parser = Parser.from(tokenizer) |
| 123 | + parser.parseToAST().andThen { globalScope -> |
| 124 | + Compiler.from().compile(globalScope, globalEnabled) |
| 125 | + }.andThen { |
| 126 | + VirtualMachine.from().runCode(it, globalEnv = globalEnabled) |
| 127 | + } |
| 128 | + .tap { stack -> |
| 129 | + stack.forEach { |
| 130 | + println(it.unwrap()) |
| 131 | + } |
| 132 | + }.tapInvalid { |
| 133 | + println(it.getMessage()) |
| 134 | + } |
5 | 135 | }
|
0 commit comments