Skip to content

Commit

Permalink
Add proper 'clear' and 'quit' commands, fixes #49, closes #41
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Mar 22, 2017
1 parent ea21d5f commit f1c244a
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 18 deletions.
17 changes: 17 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
42 changes: 29 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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();
Expand All @@ -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);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "insect",
"version": "2.1.0",
"version": "2.2.0",
"description": "REPL-style scientific calculator",
"author": "David Peter <[email protected]>",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions src/Insect.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Insect/Interpreter.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
1 change: 1 addition & 0 deletions src/Insect/Language.purs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ data Command
| Reset
| List
| Clear
| Quit

derive instance eqCommandEq Command
derive instance genericCommandGeneric Command
Expand Down
6 changes: 4 additions & 2 deletions src/Insect/Parser.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit f1c244a

Please sign in to comment.