-
Notifications
You must be signed in to change notification settings - Fork 2
/
lox-mode.el
96 lines (70 loc) · 2.77 KB
/
lox-mode.el
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
;;; lox-mode.el --- Major mode for the Lox programming language -*- lexical-binding: t -*-
;; Copyright (C) 2020 Timmy Jose<zoltan.jose@gmail.com>
;; Author: Timmy Jose <zoltan.jose@gmail.com>
;; Maintainer: Timmy Jose <zoltan.jose@gmail.com>
;; Version: 1.0
;; Keywords: languages, lox
;; Package-Requires: ((emacs "24.3"))
;; Homepage: https://github.com/timmyjose-projects/lox-mode
;; This file is not part of GNU Emacs.
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; A major mode for the Lox programming language (http://craftinginterpreters.com/the-lox-language.html).
;;; Code:
(defvar lox-mode-hook nil)
(defgroup lox-mode-group nil
"Lox mode configuration group."
:link '(url-link "https://www.craftinginterpreters.com/the-lox-language.html")
:group 'languages)
(defcustom lox-bin "clox"
"Path to the Lox executable."
:type 'string
:safe #'stringp
:group 'lox-mode-group)
;; Lox-specific
(defconst lox-keywords
'("class" "fun" "var" "for" "if" "else" "while" "return" "and" "or" "super" "this"))
(defconst lox-builtins
'("print"))
(defconst lox-constants
'("true" "false" "nil"))
(defvar lox-font-lock-definitions
(append
`((,(regexp-opt lox-keywords) . font-lock-keyword-face)
(,(regexp-opt lox-builtins) . font-lock-builtin-face)
(,(regexp-opt lox-constants) . font-lock-constant-face))))
;; supported commands
;;;###autoload
(defun lox-run ()
"Run the currently loaded script."
(interactive)
(compile (concat lox-bin " " (buffer-file-name))))
(defvar lox-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-r") 'lox-run)
map)
"Keymap for Lox mode.")
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.lox\\'" . lox-mode))
;; Derive the mode from c-mode for automatic syntax highlighting and
;; indentation. Once (if) a custom formatter is written for this mode, this
;; can probably derive from prog-mode instead.
;;;###autoload
(define-derived-mode lox-mode c-mode "Lox"
"A major mode for the Lox programming language.
\\{lox-mode-map}"
:group 'lox-mode-group
(setq-local comment-start "// ")
(setq-local comment-end "")
(setq font-lock-defaults '(lox-font-lock-definitions)))
(provide 'lox-mode)
;;; lox-mode.el ends here