-
Notifications
You must be signed in to change notification settings - Fork 3
/
init-nand2tetris.el
320 lines (278 loc) · 10.4 KB
/
init-nand2tetris.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
;; init-nand2tetris.el --- Several major modes for the nand2tetris projects. -*- lexical-binding: t -*-
;; Copyright (C) 2022 Deng Li
;; Author: Juan Cortez <[email protected]>
;; URL: https://github.com/Juan-Cortez3/dot_emacs_dot_d
;; This file is not part of GNU Emacs.
;;
;; This program 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 2, or
;; (at your option) any later version.
;;
;; This program 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;; Commentary:
;;
;; Perspective:
;; This file provides major modes for languages used in the course nand2tetris:
;;
;; * .hdl - Hardware description language
;; * .tst - Test script for simulators in the software suite
;; * .vm - The intermediate representation used by the VM translator
;; * .jack - The high level language
;;
;; Those languages are not meant for the real world but serve as tools to reveal
;; the computer engineering principles underlying modern computing systems
;;
;; Usage:
;; Currently, this file provides only basic functionalities:
;;
;; 1. Syntax highlighting
;; 2. Indentation
;; 3. Syntax table
;;
;; Just include this file in your init.el file: (require 'init-nand2tetris)
;;; Code:
;;
;; Major mode for hardware description language
;;
(defvar hdl-mode-hook nil)
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.hdl\\'" . hdl-mode))
;; Syntax highlighting
(defconst hdl-font-lock-keywords-builtin
(list
'("Nand\\|\
And[[:digit:]]*\\(Way\\)*[[:digit:]]*\\|\
Or[[:digit:]]*\\(Way\\)*[[:digit:]]*\\|\
Not[[:digit:]]*\\|\
Xor[[:digit:]]*\\(Way\\)*[[:digit:]]*\\|\
Mux[[:digit:]]*\\(Way\\)*[[:digit:]]*\\|\
DMux[[:digit:]]*\\(Way\\)*[[:digit:]]*\\|\
Add[[:digit:]]*\\|\
ALU\\|\
FullAdder\\|\
HalfAdder\\|\
Inc[[:digit:]]*\\|\
Bit\\|\
A*D*Register\\|\
RAM[[:digit:]]*K*\\|\
ROM[[:digit:]]*K*\\|\
PC\\|\
DFF\\|\
Screen\\|\
Keyboard\\|\
CPU\\|\
Memory" . font-lock-function-name-face))
"Highlighting expressions for builtin logic gates.")
(defconst hdl-font-lock-keywords-1
(append hdl-font-lock-keywords-builtin
(list
'("\\<CHIP\\|IN\\|OUT\\|PARTS\\|CLOCKED\\|BUILTIN\\>" . font-lock-keyword-face)
'("true\\|false" . font-lock-constant-face)))
"Basic highlighting expressions for HDL mode, including keywords and constant.")
(defconst hdl-font-lock-keywords-2
(append hdl-font-lock-keywords-1
(list
'("CHIP[[:blank:]]+\\(\\w*\\)[[:blank:]]+{" 1 font-lock-type-face)
'("=[[:blank:]]*\\(\\w+\\)[[:blank:]]*\\(,\\|\\;\\|)\\|\\[\\)" 1 font-lock-variable-name-face)
'("\\(\\[\\)[[:digit:]]+\\.*[[:digit:]]*\\(\\]\\)" 1 font-lock-type-face)
'("\\(\\[\\)[[:digit:]]+\\.*[[:digit:]]*\\(\\]\\)" 2 font-lock-type-face)))
"Highlighting expressions for variables.")
(defvar hdl-font-lock-keywords hdl-font-lock-keywords-2
"Defautl highlighting expressions for HDL mode.")
;; Intentation
(defun hdl-indent-line ()
"Indent current line as HDL code."
(interactive)
(beginning-of-line)
(if (bobp)
(indent-line-to 0)
(let ((not-indented t) cur-indent)
(if (looking-at ".*}")
(progn
(save-excursion
(forward-line -1)
(setq cur-indent (- (current-indentation) tab-width)))
(if (< cur-indent 0)
(setq cur-indent 0)))
(save-excursion
(while not-indented
(forward-line -1)
(if (looking-at ".*}")
(progn
(setq cur-indent (current-indentation))
(setq not-indented nil))
(if (looking-at ".*{")
(progn
(setq cur-indent (+ (current-indentation) tab-width))
(setq not-indented nil))
(if (bobp)
(setq not-indented nil)))))))
(if cur-indent
(indent-line-to cur-indent)
(indent-line-to 0)))))
;; The syntax table
;; A syntax table tells Emacs how it should treat various tokens in your text for various functions
(defvar hdl-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?_ "w" st)
(modify-syntax-entry ?/ ". 124b" st)
(modify-syntax-entry ?* ". 23" st)
(modify-syntax-entry ?\n "> b" st)
st)
"Syntax table for ‘hdl-mode’.")
;; The entry function
;; Create the function that will be called by Emacs when the mode is started
(define-derived-mode hdl-mode ()
"Major mode for editing Hardware Description Language files"
(interactive)
(kill-all-local-variables)
(set-syntax-table hdl-mode-syntax-table)
(set (make-local-variable 'font-lock-defaults) '(hdl-font-lock-keywords))
(set (make-local-variable 'indent-line-function) 'hdl-indent-line)
(setq major-mode 'hdl-mode)
(setq mode-name "HDL")
(run-hooks 'hdl-mode-hook))
;;
;; Major mode for test script language
;;
(defvar tst-mode-hook nil)
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.tst\\'" . tst-mode))
;; Syntax highlighting
(defconst tst-font-lock-keywords-1
(append hdl-font-lock-keywords-builtin
(list
'("\\<load\\|output-file\\|output-list\\|compare-to\\|!\\>" . font-lock-keyword-face)))
"Setup commands.")
(defconst tst-font-lock-keywords-2
(append tst-font-lock-keywords-1
(list
'("set\\|eval\\|output\\|tick\\|tock\\|repeat\\|while\\|echo\\|clear-echo\\|breakpoint\\|clear-breakpoints" . font-lock-function-name-face)))
"Simulation commands.")
(defconst tst-font-lock-keywords-3
(append tst-font-lock-keywords-2
(list
'("\\(%[[:alpha:]]\\)\\([[:digit:]]+\\)\\([[:blank:]]+\\|,\\|\\;\\|$\\|\\.\\)" 1 font-lock-variable-name-face)
'("[[:blank:]]+\\([[:digit:]]+\\)\\([[:blank:]]+\\|,\\|\\;\\)" 1 font-lock-constant-face)
))
"Others.")
(defvar tst-font-lock-keywords tst-font-lock-keywords-3
"Defautl highlighting expressions for TST mode.")
;; The entry function
;; Create the function that will be called by Emacs when the mode is started
(define-derived-mode tst-mode ()
"Major mode for editing Test Scripting Language files"
(interactive)
(kill-all-local-variables)
(set-syntax-table hdl-mode-syntax-table) ;; Use the same indentation function as hdl for tst
(set (make-local-variable 'font-lock-defaults) '(tst-font-lock-keywords))
(set (make-local-variable 'indent-line-function) 'hdl-indent-line) ;; Use the same syntax table function as hdl for tst
(setq major-mode 'tst-mode)
(setq mode-name "TST")
(run-hooks 'tst-mode-hook))
;;
;; Major mode for the intermediate representation used by the vm translator
;;
(defvar vm-mode-hook nil)
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.vm\\'" . vm-mode))
;; Syntax highlighting
(defconst vm-font-lock-keywords-1
(list
'("add\\|\
sub\\|\
neg\\|\
eq\\|\
gt\\|\
lt\\|\
and\\|\
or\\|\
not" . font-lock-function-name-face))
"Highlighting expressions for arithmetic and logical commands.")
(defconst vm-font-lock-keywords-2
(append vm-font-lock-keywords-1
(list
'("push\\|pop" . font-lock-keyword-face)
'("argument\\|local\\|static\\|constant\\|this\\|that\\|pointer\\|temp" . font-lock-constant-face)))
"Highlighting expressions for memory access commands and constants.")
(defconst vm-font-lock-keywords-3
(append vm-font-lock-keywords-2
(list
'("label\\|goto\\|if-goto\\|function\\|call\\|return" . font-lock-type-face)))
"Highlighting expressions for program flow and function calling commands.")
(defvar vm-font-lock-keywords vm-font-lock-keywords-3
"Defautl highlighting expressions for HDL mode.")
;; The entry function
(define-derived-mode vm-mode ()
"Major mode for editing .vm files"
(interactive)
(kill-all-local-variables)
(set-syntax-table hdl-mode-syntax-table)
(set (make-local-variable 'font-lock-defaults) '(vm-font-lock-keywords))
(set (make-local-variable 'indent-line-function) 'hdl-indent-line)
(setq major-mode 'vm-mode)
(setq mode-name "VM")
(run-hooks 'vm-mode-hook))
;;
;; Major mode for the high level language Jack
;;
(defvar jack-mode-hook nil)
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.jack\\'" . jack-mode))
;; Syntax highlighting
(defconst jack-font-lock-keywords-1
(list
'("class\\|\
constructor\\|\
method\\|\
function\\|\
var\\|\
static\\|\
field\\|\
let\\|\
do\\|\
if\\|\
else\\|\
while\\|\
return\\|\
this" . font-lock-keyword-face))
"Highlighting expressions for program components, variable declaration, statements, constant values, object reference")
(defconst jack-font-lock-keywords-2
(append jack-font-lock-keywords-1
(list
'("class[[:blank:]]+\\(\\w+\\)[[:blank:]]+{" 1 font-lock-function-name-face)
'("\\(\\w+\\)[[:blank:]]*=[[:blank:]]*" 1 font-lock-variable-name-face)
'("[^a-zA-Z0-9]\\(int\\|boolean\\|char\\|void\\)[^a-zA-Z0-9]" 1 font-lock-type-face)
'("true\\|false\\|null" . font-lock-constant-face)))
"Highlighting expressions for memory access commands and constants.")
(defconst jack-font-lock-keywords-3
(append jack-font-lock-keywords-2
(list
'("[^a-zA-Z0-9]\\(Math\\|String\\|Array\\|Output\\|Screen\\|Keyboard\\|Memory\\|Sys\\)[^a-zA-Z0-9]" 1 font-lock-function-name-face)
'("\\(constructor\\|method\\|function\\)[[:blank:]]+\\(\\w+\\)[[:blank:]]+\\(\\w+\\)[[:blank:]]*(" 3 font-lock-constant-face)))
"Highlighting expressions for Jack standard libraries.")
(defvar jack-font-lock-keywords jack-font-lock-keywords-3
"Defautl highlighting expressions for HDL mode.")
;; The entry function
(define-derived-mode jack-mode ()
"Major mode for editing .jack files"
(interactive)
(kill-all-local-variables)
(set-syntax-table hdl-mode-syntax-table) ;; use old hdl syntax table
(set (make-local-variable 'font-lock-defaults) '(jack-font-lock-keywords))
(set (make-local-variable 'indent-line-function) 'hdl-indent-line) ;; use old hdl indent rule
(setq major-mode 'jack-mode)
(setq mode-name "Jack")
(run-hooks 'jack-mode-hook))
(provide 'init-nand2tetris)
;;; init-nand2tetris.el ends here