~subsetpark/ec

ref: d48e38e3de4186d9f151e3c349917750349ad837 ec/main.janet -rw-r--r-- 2.5 KiB
d48e38e3 — Zach Smith Use a proto instead of a tagged value 2 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
(import /src/repl)

(use argparse)

(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 -f <file>   : load and evaluate <file> as an ec program.
      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}
   "file" {:kind :option
           :short "f"
           :help "The filename to evaluate"}])

(defn main
  [& _args]
  (when-let [res (argparse ;params)]
    (match res
      {"file" filename} (->>
                          filename
                          (slurp)
                          (string/split "\n")
                          (repl/handle-lines))
      {:default inputs} (->
                          inputs
                          (string/join " ")
                          (tuple)
                          (repl/handle-lines))
      _ (repl/repl))))