]> code.delx.au - gnu-emacs-elpa/blob - packages/shen-mode/shen-mode.el
45d4262334c989503980f9b573704ffc79f9f2b2
[gnu-emacs-elpa] / packages / shen-mode / shen-mode.el
1 ;;; shen-mode.el --- A major mode for editing shen source code
2
3 ;; Copyright (C) 2011 Free Software Foundation, Inc.
4
5 ;; Author: Eric Schulte <schulte.eric@gmail.com>
6 ;; Version: 0.1
7 ;; Keywords: languages, shen
8 ;; Description: A major mode for editing shen source code
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; A minor mode for editing shen source code. Shen is a modern lisp
28 ;; dialect with support for functional and declarative programming,
29 ;; pattern matching and a very powerful type system. See the
30 ;; following for more information on Shen. http://www.shenlanguage.org
31
32 ;;; Code:
33 (require 'lisp-mode)
34 (require 'imenu)
35
36 (defcustom shen-mode-hook '(turn-on-eldoc-mode)
37 "Normal hook run when entering `shen-mode'."
38 :type 'hook
39 :group 'shen)
40
41 (defvar shen-mode-map
42 (let ((map (make-sparse-keymap)))
43 (set-keymap-parent map lisp-mode-shared-map)
44 map)
45 "Currently just inherits from `lisp-mode-shared-map'.")
46
47 (defconst shen-functions
48 '((* "number --> number --> number" "Number multiplication.")
49 (+ "number --> number --> number" "Number addition.")
50 (- "number --> number --> number" "Number subtraction.")
51 (/ "number --> number --> number" "Number division.")
52 (/. "_" "Abstraction builder, receives a variable and an expression; does the job of --> in the lambda calculus.")
53 (< "number --> number --> boolean" "Less than.")
54 (<-vector nil nil)
55 (<= "number --> number --> boolean" "Less than or equal to.")
56 (<e> nil nil)
57 (= "A --> A --> boolean" "Equal to.")
58 (== "A --> B --> boolean" "Equal to.")
59 (> "number --> number --> boolean" "Greater than.")
60 (>= "number --> number --> boolean" "Greater than or equal to.")
61 (@p "_" "Takes two inputs and forms an ordered pair.")
62 (@s "_" "Takes two or more inputs and forms a string.")
63 (@v "_" "Takes two or more inputs and forms a vector.")
64 (abort nil "throw a simple error")
65 (adjoin nil "add arg1 to list arg2 if not already a member")
66 (and "boolean --> boolean --> boolean" "Boolean and.")
67 (append "(list A) --> (list A) --> (list A)" "Appends two lists into one list.")
68 (apply "(A --> B) --> (A --> B)" "Applies a function to an input.")
69 (arity nil nil)
70 (assoc nil nil)
71 (assoc-type "symbol --> variable --> symbol" "Associates a Qi type (first input) with Lisp type (second input)..")
72 (average nil "return the average of two numbers")
73 (bind nil nil)
74 (boolean\? "A --> boolean" "Recognisor for booleans.")
75 (bound\? nil "check is a symbol is bound")
76 (byte->string nil "return the string represented by bytes")
77 (call nil nil)
78 (cd "string --> string" "Changes the home directory. (cd \"My Programs\") will cause (load \"hello_world.txt\") to load MyPrograms/hello_world.txt. (cd \"\") is the default.")
79 (character\? "A --> boolean" "Recognisor for characters.")
80 (compile nil nil)
81 (complex\? "A --> boolean" "Recognisor for complex numbers.")
82 (concat "symbol --> symbol --> symbol" "Concatenates two symbols.")
83 (congruent\? "A --> A --> boolean" "Retrns true if objects are identical or else if they are strings or characters which are identical differing at most in case or numbers of equal value (e.g. 1 and 1.0) or tuples composed of congruent elements.")
84 (cons "_" "A special form that takes an object e of type A and a list l of type (list A) and produces a list of type (list A) by adding e to the front of l.")
85 (cons\? "--> boolean" "Returns true iff the input is a non-empty list.")
86 (core nil nil)
87 (cut nil nil)
88 (debug "A --> string" "The input is ignored and debugging is returned; but all terminal output is echoed to the file debug.txt until the undebug function is executed.")
89 (declare "_" "Takes a function name f and a type t expressed as a list and gives f the type t.")
90 (define "_" "Define a function, takes a name, an optional type and a pattern matching body.")
91 (delete-file "string --> string" "The file named in the string is deleted and the string returned.")
92 (destroy "_" "Receives the name of a function and removes it and its type from the environment.")
93 (difference "(list A) --> (list A) --> (list A)" "Subtracts the elements of the second list from the first")
94 (do "_" "A special form: receives n well-typed expressions and evaluates each one, returning the normal form of the last one.")
95 (dump "string --> string" "Dumps all user-generated Lisp from the file f denoted by the argument into a file f.lsp.")
96 (echo "string --> string" "Echoes all terminal input/output to a file named by string (which is either appended to if it exists or created if not) until the command (echo \"\") is received which switches echo off.")
97 (element\? "A -> (list A) --> boolean" "Returns true iff the first input is an element in the second.")
98 (empty\? "--> boolean" "Returns true iff the input is [].")
99 (error "_" "A special form: takes a string followed by n (n --> 0) expressions. Prints error string.")
100 (eval "_" "Evaluates the input.")
101 (explode "A --> (list character)" "Explodes an object to a list of characters.")
102 (fail nil nil)
103 (fix "(A --> A) --> (A --> A)" "Applies a function to generate a fixpoint.")
104 (float\? "A --> boolean" "Recognisor for floating point numbers.")
105 (floor nil nil)
106 (format nil "takes a stream, a format string and args, formats and prints to the stream")
107 (freeze "A --> (lazy A)" "Returns a frozen version of its input.")
108 (fst "(A * B) --> A" "Returns the first element of a tuple.")
109 (fwhen nil nil)
110 (gensym "_" "Generates a fresh symbol or variable from a string..")
111 (get nil "gets property arg2 from object arg1")
112 (get-array "(array A) --> (list number) --> A --> A" "3-place function that takes an array of elements of type A, an index to that array as a list of natural numbers and an expression E of type A. If an object is stored at the index, then it is returned, otherwise the normal form of E is returned.")
113 (get-prop "_" "3-place function that takes a symbol S, a pointer P (which can be a string, symbol or number), and an expression E of any kind and returns the value pointed by P from S (if one exists) or the normal form of E otherwise.")
114 (hash nil "hash an object")
115 (hdv nil nil)
116 (head "(list A) --> A" "Returns the first element of a list.")
117 (identical nil nil)
118 (if "boolean --> A --> A" "takes a boolean b and two expressions x and y and evaluates x if b evaluates to true and evaluates y if b evaluates to false.")
119 (if-with-checking "string --> (list A)" "If type checking is enabled, raises the string as an error otherwise returns the empty list..")
120 (if-without-checking "string --> (list A)" "If type checking is disabled, raises the string as an error otherwise returns the empty list.")
121 (include "(list symbol) --> (list symbol)" "Includes the datatype theories or synonyms for use in type checking.")
122 (include-all-but "(list symbol) --> (list symbol)" "Includes all loaded datatype theories and synonyms for use in type checking apart from those entered.")
123 (inferences "A --> number" "The input is ignored. Returns the number of logical inferences executed since the last call to the top level.")
124 (input "_" "0-place function. Takes a user input i and returns the normal form of i.")
125 (input+ "_" "Special form. Takes inputs of the form : <expr>. Where d(<expr>) is the type denoted by the choice of expression (e.g. \"number\" denotes the type number). Takes a user input i and returns the normal form of i given i is of the type d(<expr>).")
126 (integer\? "A --> boolean" "Recognisor for integers.")
127 (interror nil nil)
128 (intersection "(list A) --> (list A) --> (list A)" "Computes the intersection of two lists.")
129 (intmake-string nil nil)
130 (intoutput nil nil)
131 (lambda "_" "Lambda operator from lambda calculus.")
132 (length "(list A) --> integer" "Returns the number of elements in a list.")
133 (let nil nil)
134 (limit nil nil)
135 (lineread "_" "Top level reader of read-evaluate-print loop. Reads elements into a list. lineread terminates with carriage return when brackets are balanced. ^ aborts lineread.")
136 (list "A .. A --> (list A)" "A special form. Assembles n (n --> 0) inputs into a list.")
137 (load "string --> symbol" "Takes a file name and loads the file, returning loaded as a symbol.")
138 (macroexpand nil nil)
139 (make-string "string A1 - An --> string" "A special form: takes a string followed by n (n --> 0) well-typed expressions; assembles and returns a string.")
140 (map "(A --> B) --> (list A) --> (list B)" "The first input is applied to each member of the second input and the results consed into one list..")
141 (mapcan "(A --> (list B)) --> (list A) --> (list B)" "The first input is applied to each member of the second input and the results appended into one list.")
142 (maxinferences "number --> number" "Returns the input and as a side-effect, sets a global variable to a number that limits the maximum number of inferences that can be expended on attempting to typecheck a program. The default is 1,000,000.")
143 (mod nil "arg1 mod arg2")
144 (newsym "symbol --> symbol" "Generates a fresh symbol from a symbol.")
145 (newvar "variable --> variable" "Generates a fresh variable from a variable")
146 (nl nil nil)
147 (not "boolean --> boolean" "Boolean not.")
148 (nth "number --> (list A) --> A" "Gets the nth element of a list numbered from 1.")
149 (number\? "A --> boolean" "Recognisor for numbers.")
150 (occurences "A --> B --> number" "Returns the number of times the first argument occurs in the second.")
151 (occurrences nil "returns the number of occurrences of arg1 in arg2")
152 (occurs-check "symbol --> boolean" "Receives either + or - and enables/disables occur checking in Prolog, datatype definitions and rule closures. The default is +.")
153 (opaque "symbol --> symbol" "Applied to a Lisp macro makes it opaque to Qi.")
154 (or "boolean --> (boolean --> boolean)" "Boolean or.")
155 (output "string A1 - An --> string" "A special form: takes a string followed by n (n --> 0) well-typed expressions; prints a message to the screen and returns an object of type string (the string \"done\").")
156 (preclude "(list symbol) --> (list symbol)" "Removes the mentioned datatype theories and synonyms from use in type checking.")
157 (preclude-all-but "(list symbol) --> (list symbol)" "Removes all the datatype theories and synonyms from use in type checking apart from the ones given.")
158 (print "A --> A" "Takes an object and prints it, returning it as a result.")
159 (profile "(A --> B) --> (A --> B)" "Takes a function represented by a function name and inserts profiling code returning the function as an output.")
160 (profile-results "A --> symbol" "The input is ignored. Returns a list of profiled functions and their timings since profile-results was last used.")
161 (ps "_" "Receives a symbol denoting a Qi function and prints the Lisp source code associated with the function.")
162 (put nil "puts value of arg3 as property arg2 in object arg1")
163 (put-array "(array A) --> (list number) --> A --> A" "3-place function that takes an array of elements of type A, an index to that array as a list of natural numbers and an expression E of type A. The normal form of E is stored at that index and then returned.")
164 (put-prop "_" "3-place function that takes a symbol S, a pointer P (a string symbol or number), and an expression E. The pointer P is set to point from S to the normal form of E which is then returned.")
165 (quit "_" "0-place function that exits Qi.")
166 (random "number --> number" "Given a positive number n, generates a random number between 0 and n-1.")
167 (rational\? "A --> boolean" "Recognisor for rational numbers.")
168 (read nil nil)
169 (read-char "A --> character" "The input is discarded and the character typed by the user is returned.")
170 (read-chars-as-stringlist "(list character) --> (character --> boolean) --> (list string)" "Returns a list of strings whose components are taken from the character list. The second input acts as a tokeniser. Thus (read-chars-as-stringlist [#\\H #\\i #\\Space #\\P #\\a #\\t] (/. X (= X #\\Space))) will produce [\"Hi\" \"Pat\"].")
171 (read-file "string --> (list unit)" "Returns the contents of an ASCII file designated by a string. Returns a list of units, where unit is an unspecified type.")
172 (read-file-as-charlist "string --> (list character)" "Returns the list of characters from the contents of an ASCII file designated by a string.")
173 (read-file-as-string nil nil)
174 (real\? "A --> boolean" "Recognisor for real numbers.")
175 (remove "A --> (list A) --> (list A)" "Removes all occurrences of an element from a list.")
176 (return nil nil)
177 (reverse "(list A)--> ?(list A)" "Reverses a list.")
178 (round "number--> ?number" "Rounds a number.")
179 (save "_" "0 place function. Saves a Qi image.")
180 (snd "(A * B) --> B" "Returns the second element of a tuple.")
181 (specialise "symbol --> symbol" "Receives the name of a function and turns it into a special form. Special forms are not curried during evaluation or compilation.")
182 (speed "number --> number" "Receives a value 0 to 3 and sets the performance of the generated Lisp code, returning its input. 0 is the lowest setting.")
183 (spy "symbol --> boolean" "Receives either + or - and respectively enables/disables tracing the operation of T*.")
184 (sqrt "number --> number" "Returns the square root of a number.")
185 (step "symbol --> boolean" "Receives either + or - and enables/disables stepping in the trace.")
186 (stinput nil nil)
187 (string\? "A --> boolean" "Recognisor for strings.")
188 (strong-warning "symbol --> boolean" "Takes + or -; if + then warnings are treated as error messages.")
189 (subst nil nil)
190 (sugar "symbol --> (A --> B) --> number --> (A --> B)" "Receives either in or out as first argument, a function f and an integer greater than 0 and returns f as a result. The function f is placed on the sugaring list at a position determined by the number.")
191 (sugar-list "symbol --> (list symbol)" "Receives either in or out as first argument, and returns the list of sugar functions.")
192 (sum nil "sum a list of numbers")
193 (symbol\? "A --> boolean" "Recognisor for symbols.")
194 (systemf nil nil)
195 (tail "(list A) --> (list A)" "Returns all but the first element of a non-empty list.")
196 (tc "symbol --> boolean" "Receives either + or - and respectively enables/disables static typing.")
197 (tc\? nil "return true if type checking")
198 (thaw "(lazy A) --> A" "Receives a frozen input and evaluates it to get the unthawed result..")
199 (time "A --> A" "Prints the run time for the evaluation of its input and returns its normal form.")
200 (tlv nil nil)
201 (track "symbol --> symbol" "Tracks the I/O behaviour of a function.")
202 (transparent "symbol --> symbol" "Applied to a Lisp macro makes it transparent to Qi.")
203 (tuple\? "A --> boolean" "Recognisor for tuples.")
204 (type "_" "Returns a type for its input (if any) or false if the input has no type.")
205 (unassoc-type "symbol --> symbol" "Removes any associations with the Qi type in the type association table.")
206 (undebug "A --> string" "The input is ignored, undebugging is returned and all terminal output is closed to the file debug.txt.")
207 (unify nil nil)
208 (unify! nil nil)
209 (union "(list A) --> (list A) --> (list A)" "Forms the union of two lists.")
210 (unprofile "(A --> B) --> (A --> B)" "Unprofiles a function.")
211 (unspecialise "symbol --> symbol" "Receives the name of a function and deletes its special form status.")
212 (unsugar "symbol --> (A --> B) --> (A --> B)" "Receives either out or in and the name of a function and removes its status as a sugar function.")
213 (untrack "symbol --> symbol" "Untracks a function.")
214 (value "_" "Applied to a symbol, returns the global value assigned to it.")
215 (variable\? "A --> boolean" "Applied to a variable, returns true.")
216 (vector nil nil)
217 (vector-> nil nil)
218 (vector\? nil nil)
219 (version "string --> string" "Changes the version string displayed on startup.")
220 (warn "string --> string" "Prints the string as a warning and returns \"done\". See strong-warning")
221 (write-to-file "string --> A --> string" "Writes the second input into a file named in the first input. If the file does not exist, it is created, else it is overwritten. If the second input is a string then it is written to the file without the enclosing quotes. The first input is returned.")
222 (y-or-n\? "string --> boolean" "Prints the string as a question and returns true for y and false for n."))
223 "Shen functions taken largely from the Qi documentation by Dr. Mark Tarver.")
224
225 \f
226 ;;; Fontification
227 (defconst shen-font-lock-keywords
228 (eval-when-compile
229 `(;; definitions
230 (,(concat "(\\("
231 (regexp-opt
232 '("define" "defmacro" "defprolog" "/." "synonyms" "defcc"))
233 "\\)\\>"
234 "[ \t]*(?"
235 "\\(\\sw+\\)?")
236 (1 font-lock-keyword-face)
237 (2 font-lock-function-name-face nil t))
238 ("(\\(lambda\\)\\>[ \t]*(?\\(\\sw+\\)?"
239 (1 font-lock-keyword-face)
240 (2 font-lock-variable-name-face nil t))
241 ;; data types
242 ("(\\(datatype\\)\\>[ \t]*(?\\(\\sw+\\)?"
243 (1 font-lock-keyword-face)
244 (2 font-lock-type-face nil t))
245 ;; variables
246 ("\\<\\([A-Z]\\w*\\)\\>" . font-lock-variable-name-face)
247 ;; control structures
248 (,(concat
249 "("
250 (regexp-opt
251 (append
252 '("let" "=" "eval-without-reader-macros" "freeze" "type") ; generic
253 '("if" "and" "or" "cond")) t) ; boolean
254 "\\>") . 1)
255 ;; errors
256 ("(\\(error\\)\\>" 1 font-lock-warning-face)
257 ;; built-in
258 (,(concat
259 "("
260 (regexp-opt
261 (mapcar
262 (lambda (it) (format "%s" it))
263 (append
264 '(intern function) ; symbols
265 '(pos tlstr cn str string?) ; strings
266 '(set value) ; assignment
267 '(cons hd tl cons?) ; lists
268 '(absvector address-> <-address absvector?) ; vector
269 '(pr read-byte open close) ; stream
270 '(get-time) ; time
271 '(+ - * / > < >= <= number?) ; arithmetic
272 '(fst snd tupple?) ; tuple
273 '(@s @v @p)
274 '(put get) ; property lists
275 '(simple-error trap-error error-to-string) ; error
276 ;; predicates
277 (mapcar
278 (lambda (it) (format "%s?" it))
279 '(boolean character complex congruent cons element empty float
280 integer number provable rational solved string symbol
281 tuple variable))
282 ;; misc functions
283 (mapcar #'car shen-functions)))
284 t)
285 "\\>")
286 1 font-lock-builtin-face)
287 ;; external global variables
288 (,(concat
289 (regexp-opt
290 (mapcar
291 (lambda (cnst) (format "*%s*" cnst))
292 '("language" "implementation" "port" "porters"
293 "stinput" "home-directory" "version"
294 "maximum-print-sequence-size" "printer" "macros")) t)
295 "\\>")
296 1 font-lock-builtin-face)))
297 "Default expressions to highlight in Shen mode.")
298
299 (defvar shen-mode-syntax-table
300 (let ((table (make-syntax-table)))
301 (dolist (pair '((?@ . "w")
302 (?_ . "w")
303 (?- . "w")
304 (?+ . "w")
305 (?? . "w")
306 (?! . "w")
307 (?< . "w")
308 (?> . "w")
309 (?/ . "w")
310 ;; comment delimiters
311 (?\\ . ". 14")
312 (?* . ". 23")))
313 (modify-syntax-entry (car pair) (cdr pair) table))
314 table)
315 "Syntax table to use in shen-mode.")
316
317 \f
318 ;;; Indentation
319 ;; Copied from qi-mode, which in turn is from scheme-mode and from lisp-mode
320 (defun shen-indent-function (indent-point state)
321 (let ((normal-indent (current-column)))
322 (goto-char (1+ (elt state 1)))
323 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
324 (if (and (elt state 2)
325 (not (looking-at "\\sw\\|\\s_")))
326 ;; car of form doesn't seem to be a symbol
327 (progn
328 (if (not (> (save-excursion (forward-line 1) (point))
329 calculate-lisp-indent-last-sexp))
330 (progn (goto-char calculate-lisp-indent-last-sexp)
331 (beginning-of-line)
332 (parse-partial-sexp (point)
333 calculate-lisp-indent-last-sexp 0 t)))
334 ;; Indent under the list or under the first sexp on the same
335 ;; line as calculate-lisp-indent-last-sexp. Note that first
336 ;; thing on that line has to be complete sexp since we are
337 ;; inside the innermost containing sexp.
338 (backward-prefix-chars)
339 (current-column))
340 (let ((function (buffer-substring (point)
341 (progn (forward-sexp 1) (point))))
342 method)
343 (setq method (or (get (intern-soft function) 'shen-indent-function)
344 (get (intern-soft function) 'shen-indent-hook)))
345 (cond ((or (eq method 'defun)
346 (and (null method)
347 (> (length function) 3)
348 (string-match "\\`def" function)))
349 (lisp-indent-defform state indent-point))
350 ((integerp method)
351 (lisp-indent-specform method state
352 indent-point normal-indent))
353 (method
354 (funcall method state indent-point normal-indent)))))))
355
356 (defun shen-let-indent (state indent-point normal-indent)
357 (let ((edge (- (current-column) 2)))
358 (goto-char indent-point) (skip-chars-forward " \t")
359 (if (looking-at "[-a-zA-Z0-9+*/?!@$%^&_:~]")
360 ;; deeper indent because we're still defining local variables
361 (lisp-indent-specform 5 state indent-point normal-indent)
362 ;; shallow indent because we're in the body
363 edge)))
364
365 (defun shen-package-indent (state indent-point normal-indent)
366 (- (current-column) 8))
367
368 (put 'let 'shen-indent-function 'shen-let-indent)
369 (put 'lambda 'shen-indent-function 1)
370 (put 'package 'shen-indent-function 'shen-package-indent)
371 (put 'datatype 'shen-indent-function 1)
372
373 \f
374 ;;; Function documentation
375 (defun shen-current-function ()
376 (ignore-errors
377 (save-excursion
378 (backward-up-list)
379 (forward-char 1)
380 (thing-at-point 'word))))
381
382 (defun shen-mode-eldoc ()
383 (let ((func (assoc (intern (or (shen-current-function) "")) shen-functions)))
384 (when func
385 (format "%s%s:%s"
386 (propertize (symbol-name (car func))
387 'face 'font-lock-function-name-face)
388 (if (cadr func) (concat "[" (cadr func) "]") "")
389 (if (caddr func) (concat " " (caddr func)) "")))))
390
391 (defvar shen-imenu-generic-expression
392 '(("Functions" "^\\s-*(\\(define\\)" 1)))
393
394 \f
395 ;;; Major mode definition
396 ;; apparently some versions of Emacs don't have `prog-mode' defined
397 (unless (fboundp 'prog-mode)
398 (defalias 'prog-mode 'fundamental-mode))
399
400 (define-derived-mode shen-mode prog-mode "shen"
401 "Major mode for editing Shen code."
402 :syntax-table shen-mode-syntax-table
403 ;; set a variety of local variables
404 ((lambda (local-vars)
405 (dolist (pair local-vars)
406 (set (make-local-variable (car pair)) (cdr pair))))
407 `((adaptive-fill-mode . nil)
408 (fill-paragraph-function . lisp-fill-paragraph)
409 (indent-line-function . lisp-indent-line)
410 (lisp-indent-function . shen-indent-function)
411 (parse-sexp-ignore-comments . t)
412 (comment-start . "\\* ")
413 (comment-end . " *\\")
414 (comment-add . 0)
415 (comment-column . 32)
416 (parse-sexp-ignore-comments . t)
417 (comment-use-global-state . nil)
418 (comment-multi-line . t)
419 (eldoc-documentation-function . shen-mode-eldoc)
420 (imenu-case-fold-search . t)
421 (imenu-generic-expression . ,shen-imenu-generic-expression)
422 (mode-name . "Shen")
423 (font-lock-defaults . (shen-font-lock-keywords)))))
424
425 (add-to-list 'auto-mode-alist '("\\.shen\\'" . shen-mode))
426
427 (provide 'shen-mode)
428 ;;; shen-mode.el ends here