This commit is contained in:
zongor 2025-04-19 20:32:58 -04:00
parent 55e62a7975
commit 7dacca3f5d
1 changed files with 39 additions and 62 deletions

View File

@ -1,74 +1,51 @@
;;; ztl-mode.el --- Zongor's Transpiler Language (ZTL) mode for Emacs -*- lexical-binding: t; -*-
;;; Zongor's Transpiler Language (ZTL) mode for Emacs
;; Author: Zongor
;; Version: 0.0.1
(defconst ztl-mode-hook nil
"Hook for Zongor's Transpiler Language mode.")
(defvar ztl-constants
'("true" "false"))
(defconst ztl-file-extensions '(".ztl")
"List of file extensions for Zongor's Transpiler Language.")
(defvar ztl-keywords
'("fn" "to" "in" "is" "as" "use" "set" "if" "else" "for" "loop" "while" "push" "pop" "return" "const" "type" "this"
"eq" "ne" "mod" "not" "and" "or" "xor" "band" "bor" "bxor" "srl" "sll"))
(defconst ztl-keywords
'("fn" "to" "in" "is" "as" "use" "set" "if" "else" "for" "loop" "while"
"push" "pop" "return" "const" "type" "this" "do" "end" "then" "result"
"function" "eq" "ne" "mod" "not" "and" "or" "xor" "band" "bor" "bxor"
"srl" "sll")
"List of Zongor's Transpiler Language keywords.")
(defvar ztl-keywords2
'("char" "str" "f16" "f32" "f64" "f128"))
(defconst ztl-keywords-2
'("char" "str" "f16" "f32" "f64" "f128")
"List of Zongor's Transpiler Language secondary keywords.")
(defvar ztl-operators
'(";"))
(defconst ztl-literals
'("true" "false")
"List of Zongor's Transpiler Language literals.")
(defvar ztl-functions
'("fn" "to" "in" "is" "as" "use" "set" "if" "else" "for" "loop" "while" "push" "pop" "return" "const" "type" "this"
"eq" "ne" "mod" "not" "and" "or" "xor" "band" "bor" "bxor" "srl" "sll"))
;;;###autoload
(define-derived-mode ztl-mode prog-mode "ZTL"
"Major mode for editing Zongor's Transpiler Language (ZTL)."
(defvar ztl-font-lock-defaults
`((("\\<\\(!!.*?!!\\)\\>" . font-lock-comment-face) ; Block comments
("!!.*" . font-lock-comment-face) ; Line comments
("\\<\\(!.*\\)" . font-lock-comment-face) ; Line comments
("\\<\\([A-Z][%w_]*\\)\\>" . font-lock-keyword2-face) ; Keywords2
("\\<\\([viu][%d_]+\\)\\>" . font-lock-keyword2-face) ; Keywords2
("\\<\\([9][%w_]*\\)\\>" . font-lock-keyword2-face) ; Keywords2
("\\<\\([A-Z][%w_]*\\)\\>" . font-lock-keyword2-face) ; Keywords2
("\\<\\(-?%.?%d+f?\\)\\>" . font-lock-constant-face) ; Numbers
("\\<\\(fn\\)\\>\\s-*\\(\\([%a_][%w_]*\\)\\s*%(" . (1 font-lock-keyword-face) (2 font-lock-function-name-face)) ; Function definitions
(,(regexp-opt ztl-keywords 'words) . font-lock-keyword-face) ; Keywords
(,(regexp-opt ztl-keywords2 'words) . font-lock-builtin-face) ; Keywords2
(,(regexp-opt ztl-constants 'words) . font-lock-constant-face) ; Constants
(,(regexp-opt ztl-operators 'words) . font-lock-builtin-face) ; Operators
("\"\\([^\"]*\\)\"" . font-lock-string-face) ; Strings
("'\\([^']*\\)'" . font-lock-string-face) ; Strings
))
;; File extension
(setq auto-mode-alist
(cons (cons ztl-file-extensions 'ztl-mode) auto-mode-alist))
;; Comments
(define-derived-mode ztl-mode fundamental-mode "ZTL"
"Major mode for editing ZTL files."
(setq font-lock-defaults ztl-font-lock-defaults)
(setq comment-start "!")
(setq comment-end "")
(modify-syntax-entry ?! "<" ztl-mode-syntax-table)
(modify-syntax-entry ?\n ">" ztl-mode-syntax-table)
(modify-syntax-entry ?\" "\"" ztl-mode-syntax-table)
(modify-syntax-entry ?\' "\"" ztl-mode-syntax-table)
(modify-syntax-entry ?\\ "\\" ztl-mode-syntax-table))
;; Font Lock
(set (make-local-variable 'font-lock-defaults)
'((ztl-font-lock-keywords)))
;; Whitespace
(set (make-local-variable 'whitespace-line-column)
80)
(whitespace-mode 1))
(defconst ztl-font-lock-keywords
`(
;; Comments
("^!.*" . font-lock-comment-face)
("!!.*!!" . font-lock-comment-face)
;; Strings
("\\(\"\\([^\\\\\"]\\\\?.*?\\)\"\\|'\\([^\\\\']\\\\?.*?\\)'\\)" 1 font-lock-string-face)
;; Operators
(";" . font-lock-delimiter-face)
;; Keywords
,@(mapcar (lambda (kw) `(,kw . font-lock-keyword-face)) ztl-keywords)
;; Secondary keywords
,@(mapcar (lambda (kw) `(,kw . font-lock-keyword-face)) ztl-keywords-2)
;; Literals
,@(mapcar (lambda (lit) `(,lit . font-lock-builtin-face)) ztl-literals)
;; Function definitions
("\\<\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s*(\\)" 1 font-lock-function-name-face)))
;; Example usage in .emacs or init.el:
;; (add-to-list 'auto-mode-alist '("\\.ztl$" . ztl-mode))
(provide 'ztl-mode)
(provide 'ztl-mode)