Debian Codegraph @

ide/emacs/esml-gen.el

raw
1 ;;; esml-gen.el --- esml-gen.el -*- lexical-binding: t; -*-
2 ;; Copyright (C) 2005 Vesa Karvonen
3 ;;
4 ;; MLton is released under a HPND-style license.
5 ;; See the file MLton-LICENSE for details.
6
7 (require 'esml-util)
8
9 ;; Installation
10 ;; ============
11 ;;
12 ;; Push the path to this file (and `esml-util.el') to `load-path' and use
13 ;; (require 'esml-gen).
14 ;;
15 ;; Ideas for future development
16 ;; ============================
17 ;;
18 ;; - ?
19
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21
22 (defgroup esml-gen nil
23 "Code generation functions for Standard ML."
24 :group 'sml)
25
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; Prelude
28
29 ;; TBD: Consider moving these to another place if/when it makes sense.
30
31 (defun esml-extract-field-names (pattern-or-type)
32 (let ((fields nil))
33 (with-temp-buffer
34 (insert pattern-or-type)
35 (goto-char 0)
36 (skip-chars-forward " \t\n{},")
37 (while (not (eobp))
38 (let ((start (point)))
39 (if (cl-find (char-after) esml-sml-symbolic-chars)
40 (skip-chars-forward esml-sml-symbolic-chars)
41 (skip-chars-forward esml-sml-alphanumeric-chars))
42 (push (buffer-substring start (point)) fields))
43 (skip-chars-forward " \t\n")
44 (when (and (not (eobp))
45 (= ?\: (char-after)))
46 (let ((open-parens 0))
47 (while (not (or (eobp)
48 (and (zerop open-parens)
49 (= ?\, (char-after)))))
50 (cond ((or (= ?\( (char-after))
51 (= ?\{ (char-after)))
52 (setq open-parens (1+ open-parens)))
53 ((or (= ?\) (char-after))
54 (= ?\} (char-after)))
55 (setq open-parens (1- open-parens))))
56 (forward-char 1)
57 (skip-chars-forward "^,(){}"))))
58 (skip-chars-forward " \t\n{},")))
59 fields))
60
61 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
62 ;; Functional Record Update (see http://mlton.org/FunctionalRecordUpdate)
63
64 (defcustom esml-gen-fru-setter-template
65 '("fun set f =\nlet\nfun t2r (%1) = {%2}\nfun r2t {%3} = (%4)
66 in\nRecords.wrapSet (Tuples.set%n, t2r, t2r, r2t) f\nend\n"
67 "v%i, "
68 "%f = v%i, "
69 "%f = v%i, "
70 "v%i, ")
71 "Template for `esml-gen-fru-setter'. Indentation is automatic. The last
72 two characters of a pattern are deleted at the end."
73 :type '(list :tag "Template"
74 (string :tag "Code (`%1' = 1., `%2' = 2., ..., `%n' = n)")
75 (string :tag "1. pattern (`%i' = index, `%f' = field)")
76 (string :tag "2. pattern (`%i' = index, `%f' = field)")
77 (string :tag "3. pattern (`%i' = index, `%f' = field)")
78 (string :tag "4. pattern (`%i' = index, `%f' = field)"))
79 :group 'esml-gen)
80
81 (defun esml-gen-fru-setter (pattern-or-type)
82 "Generates a functional record update function. The parameter must be in
83 the format `[{]id[: ty][,] ...[,] id[}]' where `[]' marks optional parts."
84 (interactive "sSimple record pattern or type: ")
85 (let* ((fields (esml-extract-field-names pattern-or-type))
86 (n (length fields)))
87 (if (< n 2)
88 (error "%s" "Record must have at least two fields")
89 (let ((fields (sort fields 'string-lessp))
90 (start (point)))
91 (cl-labels ((format-fields
92 (fmt)
93 (with-temp-buffer
94 (loop
95 for f in fields
96 for i from 1 to n
97 do (insert
98 (let* ((result fmt)
99 (result (replace-regexp-in-string
100 "\\%f" f result t t))
101 (result (replace-regexp-in-string
102 "\\%i" (int-to-string i) result t t)))
103 result)))
104 (delete-char -2) ;; TBD
105 (buffer-string))))
106 (insert
107 (let* ((result (nth 0 esml-gen-fru-setter-template))
108 (result (replace-regexp-in-string
109 "%1" (format-fields (nth 1 esml-gen-fru-setter-template)) result t t))
110 (result (replace-regexp-in-string
111 "%2" (format-fields (nth 2 esml-gen-fru-setter-template)) result t t))
112 (result (replace-regexp-in-string
113 "%3" (format-fields (nth 3 esml-gen-fru-setter-template)) result t t))
114 (result (replace-regexp-in-string
115 "%4" (format-fields (nth 4 esml-gen-fru-setter-template)) result t t))
116 (result (replace-regexp-in-string
117 "%n" (int-to-string n) t t)))
118 result))
119 (indent-region start (point) nil))))))
120
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 ;; Functional Tuple Update (see http://mlton.org/FunctionalRecordUpdate)
123
124 (defcustom esml-gen-ftu-setters-template
125 '("fun set%n f v (%1) =\nlet\ndatatype (%2) t =\n%3
126 fun g h v =\n(%4)\nin\nf (%5) v\nend\n"
127 "v%i, "
128 "'v%i, "
129 " V%i of 'v%i |"
130 "case h v of V%i v%i => v%i | _ => v%i,\n"
131 "g V%i, ")
132 "Template for `esml-gen-ftu-setters'. Indentation is automatic. The last
133 two characters of a pattern are deleted at the end."
134 :type '(list :tag "Format"
135 (string :tag "Code (`%1' = 1., `%2' = 2., ..., `%n' = n)")
136 (string :tag "1. pattern (`%i' = index)")
137 (string :tag "2. pattern (`%i' = index)")
138 (string :tag "3. pattern (`%i' = index)")
139 (string :tag "4. pattern (`%i' = index)")
140 (string :tag "5. pattern (`%i' = index)"))
141 :group 'esml-gen)
142
143 (defun esml-gen-ftu-setters (n)
144 "Generates functional tuple update, or `set<N>', functions."
145 (interactive "nMaximum number of fields [2-100]: ")
146 (if (not (and (<= 2 n)
147 (<= n 100)))
148 (error "%s" "Number of fields must be between 2 and 100")
149 (cl-labels ((format-fields
150 (fmt n)
151 (with-temp-buffer
152 (loop for i from 1 to n
153 do (insert
154 (let* ((result fmt)
155 (result (replace-regexp-in-string
156 "%i" (int-to-string i) result t t)))
157 result)))
158 (delete-char -2) ;; TBD
159 (buffer-string))))
160 (let ((start (point)))
161 (loop for i from 2 to n do
162 (unless (= i 2)
163 (insert "\n"))
164 (insert
165 (let* ((result (nth 0 esml-gen-ftu-setters-template))
166 (result (replace-regexp-in-string
167 "%1" (format-fields (nth 1 esml-gen-ftu-setters-template) i) result t t))
168 (result (replace-regexp-in-string
169 "%2" (format-fields (nth 2 esml-gen-ftu-setters-template) i) result t t))
170 (result (replace-regexp-in-string
171 "%3" (format-fields (nth 3 esml-gen-ftu-setters-template) i) result t t))
172 (result (replace-regexp-in-string
173 "%4" (format-fields (nth 4 esml-gen-ftu-setters-template) i) result t t))
174 (result (replace-regexp-in-string
175 "%5" (format-fields (nth 5 esml-gen-ftu-setters-template) i) result t t))
176 (result (replace-regexp-in-string
177 "%n" (int-to-string i) result t t)))
178 result)))
179 (indent-region start (point) nil)))))
180
181 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
182
183 (provide 'esml-gen)