-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathad.nim
71 lines (57 loc) · 1.44 KB
/
ad.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import strutils, rdstdin, os
import src/obj, src/stack
const
prompt = "> "
doc = """
Simple Reverse Polish Calculator.
Usage:
ad [<exp>...]
If passed any arguments, ad will interpret
them as a series of commands. Otherwise it
will enter interactive mode, where you can
use it as a shell for running calculations.
"""
proc handleArgs(args: var seq[string]) =
## Arg handling and special cases.
# Handle quoted expressions
if args.len == 1 and ' ' in args[0]:
args = args[0].split(" ")
if "-h" in args or "--help" in args:
echo doc
quit()
if "-v" in args or "--version" in args:
echo "ad " & "0.7.4"
quit()
var mainStack: Stack = @[]
proc handleInput(input: string) =
## Handle a line of input in normal mode.
let tokens = input.split()
var oldStack = mainStack
try:
mainStack.ingestLine(tokens)
except ValueError:
mainStack = oldStack
echo getCurrentExceptionMsg()
proc main() =
# Parse arguments
var args = commandLineParams()
handleArgs(args)
defer: mainStack.displaySummary()
# Read input from command line or interactive mode.
if args.len > 0:
try:
mainStack.ingestLine(args)
except ValueError:
echo getCurrentExceptionMsg()
else:
var input: string
while true:
try:
input = readLineFromStdin(prompt)
except IOError:
break
input = input.strip()
if input.len > 0:
handleInput(input)
when isMainModule:
main()