diff --git a/index.html b/index.html index 703ba17..6104b71 100644 --- a/index.html +++ b/index.html @@ -28,6 +28,23 @@ var res = Insect.repl(globalEnv)(line); globalEnv = res.newEnv; + // Handle shell commands + if (res.msgType == "command") { + if (res.msg == "clear") { + // Clear screen: + this.clear(); + return ""; + } else if (res.msg == "quit") { + // Treat as reset: + this.clear(); + globalEnv = Insect.initialEnvironment; + return ""; + } else { + return "Unknown command '" + res.msg + "'"; + } + + } + // Format the output var msg = res.msg; diff --git a/index.js b/index.js index d36d236..b6f7617 100755 --- a/index.js +++ b/index.js @@ -33,7 +33,9 @@ if (process.argv.length >= 4) { usage(); } else { var res = runInsect(arg); - console.log(res.msg); + if (res.msgType === "value") { + console.log(res.msg); + } process.exit(0); } } @@ -60,18 +62,29 @@ if (interactive) { var res = runInsect(line); if (res) { - // Format output - var msg = res.msg; - if (res.msgType === "value" || res.msgType === "value-set") { - msg = "\n " + colored("36", msg); - } else if (res.msgType == "error") { - msg = "\n " + colored("31", msg); - } else if (res.msgType == "info") { - msg = msg.replace(/`([^`\n]+)`/g, '\x1b[36m$1\x1b[0m'); - msg = msg.replace(/\*([^\*\n]+)\*/g, '\x1b[01m$1\x1b[0m'); + if (res.msgType == "command") { + if (res.msg == "quit") { + process.exit(0); + } else if (res.msg == "clear") { + process.stdout.write('\033[2J\033[0f'); + } else { + console.error("Unknown command '" + res.msg + "'"); + } + } + else { + // Format output + var msg = res.msg; + if (res.msgType === "value" || res.msgType === "value-set") { + msg = "\n " + colored("36", msg); + } else if (res.msgType == "error") { + msg = "\n " + colored("31", msg); + } else if (res.msgType == "info") { + msg = msg.replace(/`([^`\n]+)`/g, '\x1b[36m$1\x1b[0m'); + msg = msg.replace(/\*([^\*\n]+)\*/g, '\x1b[01m$1\x1b[0m'); + } + + console.log(msg + "\n"); } - - console.log(msg + "\n"); } rl.prompt(); @@ -85,12 +98,15 @@ if (interactive) { lineReader.eachLine(process.stdin, function(line) { var res = runInsect(line); if (res) { - // Only output values and halt on errors. Ignore other message types. + // Only output values and halt on errors. Ignore 'info' and 'value-set' + // message types. if (res.msgType === "value") { console.log(res.msg); } else if (res.msgType == "error") { console.error("Error: " + res.msg); process.exit(1); + } else if (res.msgType == "command" && res.msg == "quit") { + process.exit(0); } } }); diff --git a/package.json b/package.json index 0e67e42..1625a16 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "insect", - "version": "2.1.0", + "version": "2.2.0", "description": "REPL-style scientific calculator", "author": "David Peter ", "license": "MIT", diff --git a/src/Insect.purs b/src/Insect.purs index 5b58c3a..a758dd5 100644 --- a/src/Insect.purs +++ b/src/Insect.purs @@ -25,6 +25,7 @@ msgTypeToString Info = "info" msgTypeToString Error = "error" msgTypeToString Value = "value" msgTypeToString ValueSet = "value-set" +msgTypeToString Cmd = "command" -- | Run Insect, REPL-style. repl ∷ Environment → String → { msg ∷ String diff --git a/src/Insect/Interpreter.purs b/src/Insect/Interpreter.purs index b434c9d..343f035 100644 --- a/src/Insect/Interpreter.purs +++ b/src/Insect/Interpreter.purs @@ -33,7 +33,7 @@ data EvalError type Expect = Either EvalError -- | Output types for highlighting. -data MessageType = Value | ValueSet | Info | Error +data MessageType = Value | ValueSet | Info | Error | Cmd -- | The output type of the interpreter. data Message = Message MessageType String @@ -143,4 +143,5 @@ runInsect env (Command List) = runInsect env (Command Reset) = { msg: Message Info "Environment has been reset" , newEnv: initialEnvironment } -runInsect env (Command _) = { msg: Message Error "???", newEnv: env } +runInsect env (Command Quit) = { msg: Message Cmd "quit", newEnv: env} +runInsect env (Command Clear) = { msg: Message Cmd "clear", newEnv: env } diff --git a/src/Insect/Language.purs b/src/Insect/Language.purs index fba0431..f439459 100644 --- a/src/Insect/Language.purs +++ b/src/Insect/Language.purs @@ -79,6 +79,7 @@ data Command | Reset | List | Clear + | Quit derive instance eqCommand ∷ Eq Command derive instance genericCommand ∷ Generic Command diff --git a/src/Insect/Parser.purs b/src/Insect/Parser.purs index 921b40f..4d0061f 100644 --- a/src/Insect/Parser.purs +++ b/src/Insect/Parser.purs @@ -51,7 +51,8 @@ insectLanguage = LanguageDef , identLetter: identLetter , opStart: oneOf ['+', '-', '*', '·', '×', '/', '÷', '^', '=', '²', '³'] , opLetter: oneOf ['>', '*'] - , reservedNames: ["help", "?", "list", "ls", "reset", "clear"] + , reservedNames: ["help", "?", "list", "ls", "reset", "clear", "cls", "quit", + "exit"] , reservedOpNames: ["->", "+", "-", "*", "×", "/", "÷", "^", "**", "=", "²"] , caseSensitive: true } @@ -339,7 +340,8 @@ command = (reserved "help" <|> reserved "?") *> pure Help <|> (reserved "list" <|> reserved "ls") *> pure List <|> (reserved "reset") *> pure Reset - <|> (reserved "clear") *> pure Clear + <|> (reserved "clear" <|> reserved "cls") *> pure Clear + <|> (reserved "quit" <|> reserved "exit") *> pure Quit ) <* eof -- | Parse a variable assignment like `x = 3m*pi`