Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coalton mode #1093

Closed
wants to merge 13 commits into from
Prev Previous commit
Next Next commit
initial indentation rule
- indent all sublists by 2 characters
- add example .coalton file
jbouwman committed Apr 23, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 7cbd89a6a58d6b2e98d83899a4f3c5945eeb5828
47 changes: 35 additions & 12 deletions coalton-mode.el
Original file line number Diff line number Diff line change
@@ -30,6 +30,32 @@
(defvar coalton--debug t
"Enable debugging.")

;; Fontification

(defun coalton--font-lock-settings ()
"Return settings for `treesit-font-lock-settings'."
(treesit-font-lock-rules
:feature 'symbol
:language 'coalton
'((symbol) @font-lock-builtin-face) ; what's the right choice?

:feature 'number
:language 'coalton
'((number) @font-lock-number-face)

:feature 'comment
:language 'coalton
'((comment) @font-lock-comment-face)))

;; Indentation

(defun coalton--indent-rules ()
"Return rules for `treesit-simple-indent-rules'."
`((coalton
((parent-is "list") parent 2))))

;; Mode initialization

(defun coalton--load-grammar ()
"Install grammar."
(let ((grammars '((coalton "https://github.com/jbouwman/tree-sitter-coalton.git" "main"))))
@@ -39,22 +65,19 @@
(let ((treesit-language-source-alist grammars))
(treesit-install-language-grammar language)))))))

(defun coalton--font-lock-settings ()
"Get settings for `treesit-font-lock-settings'."
(treesit-font-lock-rules
:feature 'comment
:language 'coalton
'((comment) @font-lock-comment-face)))

(defun coalton-mode-variables ()
"Init buffer-local vars."
"Initialize buffer-local vars."
(setq-local treesit-font-lock-settings
(coalton--font-lock-settings))
(setq-local treesit-font-lock-feature-list
'((comment)
()
()
())))
;; Amount of decoration, from least to most, cumulative,
;; controlled by `treesit-font-lock-level'.
'((comment) ; 1
() ; 2
(number symbol) ; 3
())) ; 4
(setq-local treesit-simple-indent-rules
(coalton--indent-rules)))

;;;###autoload
(define-derived-mode coalton-mode prog-mode "Coalton"
21 changes: 10 additions & 11 deletions examples/small-coalton-programs/src/diff.coalton
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
(package diff
(import coalton-prelude))
(package diff-example)

;;;; This program computes symbolic derivatives of simple
;;;; expressions. It uses a new Symbol data type for demonstration
;;;; purposes.
;; This program computes symbolic derivatives of simple
;; expressions. It uses a new Symbol data type for demonstration
;; purposes.

(define-type Symbol
(Symbol String))
@@ -15,7 +14,7 @@
(define-instance (Eq Symbol)
(define (== a b)
(== (symbol-name a) (symbol-name b))))

(define-type Expr
(EConst Integer)
(EVar Symbol)
@@ -26,11 +25,11 @@
(define (diff x f)
(match f
((EConst _)
(EConst 0))
(EConst 0))
((EVar s)
(if (== s x) (EConst 1) (EConst 0)))
(if (== s x) (EConst 1) (EConst 0)))
((E+ a b)
(E+ (diff x a) (diff x b)))
(E+ (diff x a) (diff x b)))
((E* a b)
(E+ (E* (diff x a) b)
(E* a (diff x b))))))
(E+ (E* (diff x a) b)
(E* a (diff x b))))))