Files
sex/sex-mode.el
alex-eg 3a10ee099b implement sum and match
A sum is the tag enum, a payload struct per variant that carries
fields, the record holding both, and a constructor each. match is a
switch over the tag whose arms open a block and bind the payload as
locals, so the shape of the generated C is the shape a C programmer
would have written by hand.

What it is for is the refusal: a match that leaves a variant out is an
error naming the ones missing, and so are an arm the sum does not
have, a wrong number of binders, and the same arm written twice. C
cannot say any of that, which is why every switch over a tag in the
wild ends in a defensive default nobody can tell from a real case.

Only the sum is typedef'd. A payload struct would put shapeːcircle in
the ordinary namespace, where the constructor of that name already
lives; as a bare tag it collides with nothing. The sum's own typedef
is emitted ahead of the payload structs, since a variant may hold a
pointer back to the sum, and constructors are static even for a pub
sum, which is pasted into the importer whole.

The scrutinee is read once per binding, so one that is not a path of
names is held in a local first -- otherwise (match (f) ...) would call
f once for the tag and again for every field it binds.
2026-10-03 22:59:15 +03:00

112 lines
3.6 KiB
EmacsLisp

(require 'scheme)
(defgroup sex-mode nil
"Major mode for Sex code."
:prefix 'sex-
:group 'languages)
(define-abbrev-table 'sex-mode-abbrev-table ()
"Abbrev table for Sex mode.
It has `scheme-mode-abbrev-table' as its parent."
:parents (list scheme-mode-abbrev-table))
(defvar sex-mode-syntax-table
(let ((table (make-syntax-table lisp-data-mode-syntax-table)))
table))
(defvar sex-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map lisp-mode-shared-map)
map)
"Keymap for Sex mode.
All commands in `lisp-mode-shared-map' are inherited by this map.")
(defvar sex-mode-line-process "")
(defconst sex-font-lock-keywords
(eval-when-compile
(list
;; Declarations
(list (concat "("
(regexp-opt '("chicken-define"
"chicken-define-syntax"
"chicken-import"
"chicken-load"
"define"
"defmacro"
"extern"
"import"
"include"
"fn"
"pub"
"record"
"struct"
"sum"
"var"
"union")
'word)
"\\>"
"[[:space:]]*"
"\\([[:word:]]*\\)")
'(1 font-lock-keyword-face)
'(2 font-lock-function-name-face))
;; Keywords
(list (concat "("
(regexp-opt '(
"do"
"case"
"default"
"do"
"if"
"for"
"goto"
"return"
"switch"
"var"
"while")
'word)
"\\>")
'(1 'font-lock-builtin-face)))))
(defun sex-mode-set-variables ()
(set-syntax-table sex-mode-syntax-table)
(setq local-abbrev-table sex-mode-abbrev-table)
(setq mode-line-process '("" sex-mode-line-process))
(setq font-lock-defaults
'((sex-font-lock-keywords)
nil nil
(("+-*/.<>=!?$%_&:" . "w"))
nil
(font-lock-mark-block-function . mark-defun)))
(setq-local prettify-symbols-alist lisp-prettify-symbols-alist))
(put 'fn 'lisp-indent-function 'defun)
(put 'pub 'lisp-indent-function 'defun)
(put 'defmacro 'lisp-indent-function 'defun)
(put 'record 'lisp-indent-function 'defun)
(put 'sum 'lisp-indent-function 'defun)
(put 'match 'lisp-indent-function 1)
(put 'struct 'lisp-indent-function 'defun)
(put 'union 'lisp-indent-function 'defun)
(put 'var 'lisp-indent-function 0)
(put 'import 'lisp-indent-function 1)
(put 'switch 'lisp-indent-function 1)
(put 'case 'lisp-indent-function 1)
;;;###autoload
(define-derived-mode sex-mode lisp-data-mode "Sex"
"Major mode for editing Sex code.
Editing commands are similar to those of `lisp-mode'.
Commands:
Delete converts tabs to spaces as it moves back.
Blank lines separate paragraphs. Semicolons start comments.
\\{sex-mode-map}"
:group 'sex-mode
(sex-mode-set-variables))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.\\(sex\\|hsex\\)\\'" . sex-mode))
(provide 'sex-mode)