-
Notifications
You must be signed in to change notification settings - Fork 1
/
redcode-mode.el
158 lines (132 loc) · 5.49 KB
/
redcode-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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
;;; redcode-mode.el --- major mode for editing Redcode files
;; Copyright (C) 2017 Fedor Ryabinin
;; Author: Fedor Ryabinin
;; 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 of the License, 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
(require 'cl-macs)
(defconst redcode-instructions
'("dat" "mov" "add" "sub" "mul" "div" "mod" "jmp" "jmz" "jmn"
"djn" "spl" "cmp" "seq" "sne" "slt" "ldp" "stp" "nop")
"List of Redcode instructions.")
(defconst redcode-pseudo-opcodes
'("org" "end" "for" "rof" "equ" "pin")
"List of MARS pseudo-OpCodes.")
(defconst redcode-instruction-modifiers
'("\\.a" "\\.b" "\\.ab" "\\.ba" "\\.f" "\\.x" "\\.i")
"List of Redcode instruction modifiers.")
(defconst redcode-addressing-modes
'("#" "\\$" "\\*" "@" "{" "}" "<" ">")
"List of Redcode addressing modes.")
(defconst redcode-constants
'("CORESIZE" "CURLINE" "MAXCYCLES" "MAXLENGTH" "MAXPROCESSES"
"MINDISTANCE" "PSPACESIZE" "VERSION" "WARRIORS")
"List of Redcode constants.")
(defvar redcode-mode-hook nil)
(defvar redcode-mode-map
(let ((map (make-keymap)))
(define-key map "\C-c;" 'comment-or-uncomment-region)
(define-key map "\C-j" 'newline-and-indent)
map)
"Keymap for Redcode mode.")
(defun redcode-font-lock-expr (xs)
(cl-loop for e in xs
collect (concat "\\<\\(?:" e "\\|" (upcase e) "\\)\\>")))
(defconst redcode-font-lock-instructions
(let (instrs)
(dolist (i redcode-instructions instrs)
(dolist (m redcode-instruction-modifiers instrs)
(setq instrs
(cons (concat "\\<\\(?1:" i "\\|" (upcase i)
"\\)\\(?2:" m "\\|" (upcase m) "\\)\\>")
instrs))))))
(defconst redcode-font-lock-addr-modes
(let (modes)
(dolist (i (append (redcode-font-lock-expr redcode-instructions)
redcode-font-lock-instructions
redcode-pseudo-opcodes)
modes)
(dolist (m redcode-addressing-modes modes)
(setq modes (cons (concat ",[[:space:]]*\\(?4:" m "\\)")
modes))
(setq modes (cons (concat i "[[:space:]]*\\(?4:" m "\\)")
modes))))))
(defconst redcode-font-lock-keywords
(append
(cl-loop for e in (redcode-font-lock-expr redcode-instructions)
collect (append (list e) font-lock-keyword-face))
(cl-loop for e in redcode-font-lock-instructions
collect
(append (list e) (list '(1 font-lock-keyword-face))
(list '(2 font-lock-type-face))))
(cl-loop for e in (redcode-font-lock-expr redcode-pseudo-opcodes)
collect (append (list e) font-lock-function-name-face))
(cl-loop for e in redcode-constants
collect (append (list (concat "\\<" e "\\>"))
font-lock-constant-face))
(cl-loop for e in redcode-font-lock-addr-modes
collect
(append (list e) (list '(4 font-lock-variable-name-face))))))
(defun redcode-indent-line ()
(interactive)
(let* ((localp (point))
(dist (condition-case nil
(save-excursion
(forward-line 0)
(skip-chars-forward "[:space:]")
(if (>= (point) localp) (setq localp nil))
(max (redcode-indentation) 0))
(error 0))))
(if localp (save-excursion (indent-line-to dist))
(indent-line-to dist))))
(defconst redcode-instructions-exp
(let (r)
(dolist (e (redcode-font-lock-expr redcode-instructions) r)
(if r (setq r (concat "\\(" e "\\)\\|" r))
(setq r (concat "\\(" e "\\)"))))
(dolist (e (redcode-font-lock-expr redcode-pseudo-opcodes) r)
(setq r (concat "\\(" e "\\)\\|" r)))))
(defun redcode-indentation ()
(if (looking-at redcode-instructions-exp)
(progn
(forward-line -1)
(if (looking-at "\\(\\sw\\|\\s_\\)*")
(progn
(skip-syntax-forward "\\sw\\s_")
(skip-chars-forward "[:space:]")
(if (looking-at redcode-instructions-exp)
(current-column) default-tab-width))
default-tab-width))
(if (looking-at ";")
(progn
(forward-line -1)
(if (looking-at "\\(\\S>\\)*\\s<")
(progn (skip-syntax-forward "^\\s<") (current-column)) 0))
0)))
(defvar redcode-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?_ "w" st)
(modify-syntax-entry ?$ "_" st)
(modify-syntax-entry ?: "_" st)
(modify-syntax-entry ?\; "<" st)
(modify-syntax-entry ?\n ">;" st)
st)
"Redcode syntax table.")
(define-derived-mode redcode-mode prog-mode "Redcode"
(setq-local comment-start ";")
(setq-local font-lock-defaults '(redcode-font-lock-keywords))
(setq-local indent-line-function #'redcode-indent-line)
(setq-local tab-always-indent t)
"Core War's Redcode major mode.")
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.red\\'" . redcode-mode))
(provide 'redcode-mode)
;;; redcode-mode.el ends here