|
| 1 | +/** |
| 2 | + * Farpy - A programming language |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Fernando (FernandoTheDev) |
| 5 | + * |
| 6 | + * This software is licensed under the MIT License. |
| 7 | + * See the LICENSE file in the project root for full license information. |
| 8 | + */ |
| 9 | +import { VERSION } from "../config.ts"; |
| 10 | +import { FarpyCompilerMain } from "../farpy.ts"; |
| 11 | + |
| 12 | +export async function repl(): Promise<void> { |
| 13 | + let code: string[] = []; |
| 14 | + let compiled = false; |
| 15 | + const history: string[] = []; |
| 16 | + |
| 17 | + const file = "repl.fp"; |
| 18 | + const binary = file.replace(".fp", ""); |
| 19 | + |
| 20 | + Deno.writeFileSync(file, new TextEncoder().encode("")); |
| 21 | + |
| 22 | + const helpMessage = `Farpy REPL Commands: |
| 23 | +. - Run the compiled binary |
| 24 | +; - Compile the current code |
| 25 | +clb - Clear code buffer |
| 26 | +cll - Clear last line of code |
| 27 | +swb - Show code buffer |
| 28 | +hist - Show command history |
| 29 | +help - Show this help message |
| 30 | +q/quit - Exit the REPL |
| 31 | +`; |
| 32 | + |
| 33 | + console.log(`Farpy ${VERSION} - REPL started. Type 'help' for commands.`); |
| 34 | + |
| 35 | + while (true) { |
| 36 | + const temp_code = prompt("farpy> "); |
| 37 | + |
| 38 | + if (!temp_code || temp_code.trim() === "") { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + if (!["hist", "swb"].includes(temp_code)) { |
| 43 | + history.push(temp_code); |
| 44 | + } |
| 45 | + |
| 46 | + if (temp_code === "help") { |
| 47 | + console.log(helpMessage); |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + if (temp_code === "hist") { |
| 52 | + if (history.length === 0) { |
| 53 | + console.log("No command history."); |
| 54 | + } else { |
| 55 | + console.log("Command history:"); |
| 56 | + history.forEach((cmd, i) => console.log(`${i + 1}: ${cmd}`)); |
| 57 | + } |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + if (temp_code === ".") { |
| 62 | + if (!compiled) { |
| 63 | + console.log("There is no compiled binary to run."); |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + try { |
| 68 | + const command = new Deno.Command(`./${binary}`); |
| 69 | + const { code: exitCode, stdout, stderr } = await command.output(); |
| 70 | + |
| 71 | + if (exitCode !== 0) { |
| 72 | + console.log(`Error: ${new TextDecoder().decode(stderr)}`); |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + console.log(new TextDecoder().decode(stdout)); |
| 77 | + } catch (error: any) { |
| 78 | + console.log(`Execution error: ${error.message}`); |
| 79 | + } |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + if (temp_code === ";") { |
| 84 | + if (code.length === 0) { |
| 85 | + console.log("No code to compile."); |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + try { |
| 90 | + Deno.writeTextFileSync(file, code.join("\n")); |
| 91 | + |
| 92 | + console.log("Starting compilation..."); |
| 93 | + const compiler = new FarpyCompilerMain([file, "--o", binary]); |
| 94 | + await compiler.run(); |
| 95 | + |
| 96 | + compiled = true; |
| 97 | + console.log("Code compiled successfully. Run with '.'"); |
| 98 | + } catch (error: any) { |
| 99 | + console.log(`Compilation error: ${error.message}`); |
| 100 | + } |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + if (temp_code === "clb") { |
| 105 | + code = []; |
| 106 | + console.log("Code buffer cleared."); |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + if (temp_code === "cll") { |
| 111 | + if (code.length > 0) { |
| 112 | + const removed = code.pop(); |
| 113 | + console.log(`Line removed: ${removed!.trim()}`); |
| 114 | + } else { |
| 115 | + console.log("Buffer is already empty."); |
| 116 | + } |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + if (temp_code === "swb") { |
| 121 | + if (code.length === 0) { |
| 122 | + console.log("Code buffer is empty."); |
| 123 | + } else { |
| 124 | + console.log("Current code buffer:"); |
| 125 | + console.log("----------------"); |
| 126 | + console.log(code.join("")); |
| 127 | + console.log("----------------"); |
| 128 | + } |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + if (temp_code === "q" || temp_code === "quit" || temp_code === "sair") { |
| 133 | + console.log("Exiting Farpy REPL. Bye bye!"); |
| 134 | + break; |
| 135 | + } |
| 136 | + |
| 137 | + code.push(temp_code.endsWith("\n") ? temp_code : `${temp_code}\n`); |
| 138 | + console.log("Line added to buffer."); |
| 139 | + } |
| 140 | + |
| 141 | + // Cleanup files when exiting |
| 142 | + try { |
| 143 | + if (Deno.statSync(file).isFile) { |
| 144 | + Deno.removeSync(file); |
| 145 | + } |
| 146 | + |
| 147 | + if (compiled && Deno.statSync(binary).isFile) { |
| 148 | + Deno.removeSync(binary); |
| 149 | + } |
| 150 | + } catch (_error) { |
| 151 | + // File might not exist, that's okay |
| 152 | + } |
| 153 | + |
| 154 | + console.log("Files cleaned up."); |
| 155 | +} |
0 commit comments