~subsetpark/ec

ref: 603ec2239a258dcb283f48501d47444ca156c546 ec/main.janet -rw-r--r-- 4.2 KiB
603ec223 — Zach Smith Add some documentation 11 months ago
                                                                                
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
(import /src/calc)
(import /src/print)
(import /src/parser)

(use argparse)

(defn handle-signal
  [input]
  (let [str (string input)]
    (when (= str "") (os/exit 0))
    str))

(defn- display
  [data]
  (print (print/p data))
  "")

(defn- display-all
  [stack]
  (let [inner (map print/p stack)]
    (print (string/join inner " ")))
  "")

(defn- display-help
  []
  (each [k v] (pairs parser/dictionary)
    (printf "%s: %s" k (string (v :type))))
  "")

(defn- describe-all
  [q]
  (defn- describe
    [elem]
    (printf "%s\n\n%s: %s"
            (print/p elem)
            (string (elem :type))
            (elem :doc)))
  (each item (q :data)
    (describe item))
  "")

(defn- handle-special
  [s special]
  (case (freeze (string/trim special))
    "." (display (calc/peek s))
    "p" (display (calc/pop s))
    "s" (display-all (s :data))
    "?" (describe-all (calc/pop s))
    "??" (display-help)))

(defn handle-commands
  [s input]
  (each token input
    (match token
      @[:special patt] (handle-special s patt)
      _ (calc/push s token))))

(def- max-width 40)
(def delimiters-peg (peg/compile
                     ~{:main (+ :parens :brackets)
                       :parens (* (look -1 ")") (not "...") (to "("))
                       :brackets (* (look -1 "]") (not "...") (to "["))}))

(defn- truncate
  [peek]
  (var buf (buffer peek))
  (var last-length (length buf))
  
  (reverse! buf)
  
  (while (> (length buf) 40)
    (set buf (tracev (peg/replace delimiters-peg "..." buf)))
    (if (= (length buf) last-length) (break))
    (set last-length (length buf)))
  
  (string (reverse! buf)))

(defn- prompt
  [s]
  (->> s
       (calc/peek)
       (print/p)
       (truncate)
       (string/format "<%s> $ ")))

(defn repl
  []
  (def s (:new calc/Stack))
  (while true
    (def bak (array/slice (s :data)))
    (try (->> (getline (prompt s) @"" parser/dictionary)
              (handle-signal)
              (parser/parse)
              (handle-commands s))

      ([err fib]
        (eprint err)
        (if (os/getenv "EC_TRACEBACK")
          (propagate err fib)
          (put s :data bak))))))

(defn handle-line
  [line]
  (let [s (:new calc/Stack)]
    (->> line
         (parser/parse)
         (handle-commands s))
    (display-all (s :data))))

(def params
  [```
   A desk calculator with vectors and quotations.

   USAGE:
      ec <expression>: evaluate <expression> as a sequence of
                       ec commands and print the resulting stack.
      ec             : enter interactive (repl) mode.
   
      ec operates as a RPN desk calculator with quotation
      and vector math functions. Ordinarily, tokens are
      evaluated from left to right as they're pushed onto
      the stack. eg.,
   
          3 4 +
   
      will push 3, then 4, then apply the operation +, which
      will pop two elements from the stack, add them, and
      push their sum.
   
      To quote a sequence of tokens, wrap it in
      parentheses. eg.,
   
          (3 4 +)
   
      will push a single quotation containing just those
      three tokens, which can be applied by subsequent
      commands.
   
      To create a vector, enclose a sequence of numbers or
      other vectors in square brackets. eg.,
   
          [3 4]
   
      will create a 1-dimensional vector and push it on the
      stack.
   
      Vectors can be added to other vectors or individual
      numbers. eg.,
    
          [3 4] 2 +
   
      will add 2 to each element of [3 4], resulting in
      [5 6].
   
          [3 4] [2 1] +
   
      will add matching elements of vectors, resulting in
      [5 5].
   
      Vectors can be of arbitrary dimension, but must
      contain homogeneous data. eg.,
   
          [[2 1] [0 0]]
   
      is a valid vector (of shape [2 2]), but
   
          [[2] [0 0]]
   
      is not.
   
      For a full dictionary listing, enter the special
      command:
   
        ??
   
      To get a description of any word, quote it and use the
      special command. eg.,
   
          (!) ?
   
      Will print a description of the `apply` adverb.
   ```
   :default {:kind :accumulate}])

(defn main
  [& _args]
  (let [res (argparse ;params)]
    (when res
      (match (res :default)
        nil (repl)
        inputs (handle-line (string/join inputs " "))))))