update
This commit is contained in:
		
							parent
							
								
									55e62a7975
								
							
						
					
					
						commit
						290692570f
					
				
							
								
								
									
										98
									
								
								ztl-mode.el
								
								
								
								
							
							
						
						
									
										98
									
								
								ztl-mode.el
								
								
								
								
							| 
						 | 
					@ -1,74 +1,58 @@
 | 
				
			||||||
;;; ztl-mode.el --- Zongor's Transpiler Language (ZTL) mode for Emacs                     -*- lexical-binding: t; -*-
 | 
					;;; ztl-mode.el --- Major mode for ZTL programs
 | 
				
			||||||
;; Author: Zongor
 | 
					 | 
				
			||||||
;; Version: 0.0.1
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
(defconst ztl-mode-hook nil
 | 
					 | 
				
			||||||
  "Hook for Zongor's Transpiler Language mode.")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
(defconst ztl-file-extensions '(".ztl")
 | 
					 | 
				
			||||||
  "List of file extensions for Zongor's Transpiler Language.")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
(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.")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
(defconst ztl-keywords-2
 | 
					 | 
				
			||||||
  '("char" "str" "f16" "f32" "f64" "f128")
 | 
					 | 
				
			||||||
  "List of Zongor's Transpiler Language secondary keywords.")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
(defconst ztl-literals
 | 
					 | 
				
			||||||
  '("true" "false")
 | 
					 | 
				
			||||||
  "List of Zongor's Transpiler Language literals.")
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
;;;###autoload
 | 
					;;;###autoload
 | 
				
			||||||
(define-derived-mode ztl-mode prog-mode "ZTL"
 | 
					(add-to-list 'auto-mode-alist '("\\.ztl\\'" . ztl-mode))
 | 
				
			||||||
  "Major mode for editing Zongor's Transpiler Language (ZTL)."
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ;; File extension
 | 
					(defconst ztl-mode-syntax-table
 | 
				
			||||||
  (setq auto-mode-alist
 | 
					  (let ((syn-table (make-syntax-table)))
 | 
				
			||||||
        (cons (cons ztl-file-extensions 'ztl-mode) auto-mode-alist))
 | 
					    (modify-syntax-entry ?! "< b" syn-table) ; Line comment (extend to end of line)
 | 
				
			||||||
 | 
					    (modify-syntax-entry ?\[ "(]" syn-table)
 | 
				
			||||||
 | 
					    (modify-syntax-entry ?\] ")[" syn-table)
 | 
				
			||||||
 | 
					    (modify-syntax-entry ?\" "\"" syn-table)
 | 
				
			||||||
 | 
					    syn-table))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ;; Comments
 | 
					(defvar ztl-keywords
 | 
				
			||||||
  (setq comment-start "!")
 | 
					  '("fn" "to" "in" "is" "as" "use" "set" "if" "else" "for" "loop"
 | 
				
			||||||
  (setq comment-end "")
 | 
					    "while" "push" "pop" "return" "const" "type" "this" "eq" "ne"
 | 
				
			||||||
 | 
					    "mod" "not" "and" "or" "xor" "band" "bor" "bxor" "srl" "sll"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ;; Font Lock
 | 
					(defvar ztl-types
 | 
				
			||||||
  (set (make-local-variable 'font-lock-defaults)
 | 
					  '("char" "str" "i8" "i16" "i32" "i64" "i128" "f8" "f16" "f32"
 | 
				
			||||||
       '((ztl-font-lock-keywords)))
 | 
					    "f64" "f128" "9p" "9pmsg"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ;; Whitespace
 | 
					(defvar ztl-font-lock-keywords
 | 
				
			||||||
  (set (make-local-variable 'whitespace-line-column)
 | 
					 | 
				
			||||||
       80)
 | 
					 | 
				
			||||||
  (whitespace-mode 1))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
(defconst ztl-font-lock-keywords
 | 
					 | 
				
			||||||
  `(
 | 
					  `(
 | 
				
			||||||
    ;; Comments
 | 
					    ;; Comments
 | 
				
			||||||
    ("^!.*" . font-lock-comment-face)
 | 
					    ("!.*$" 0 font-lock-comment-face) 
 | 
				
			||||||
    ("!!.*!!" . font-lock-comment-face)
 | 
					    ("!!\\(.*?\\)!!" 0 font-lock-comment-face)
 | 
				
			||||||
 | 
					 | 
				
			||||||
    ;; Strings
 | 
					 | 
				
			||||||
    ("\\(\"\\([^\\\\\"]\\\\?.*?\\)\"\\|'\\([^\\\\']\\\\?.*?\\)'\\)" 1 font-lock-string-face)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ;; Operators
 | 
					 | 
				
			||||||
    (";" . font-lock-delimiter-face)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ;; Keywords
 | 
					    ;; Keywords
 | 
				
			||||||
    ,@(mapcar (lambda (kw) `(,kw . font-lock-keyword-face)) ztl-keywords)
 | 
					    (,(concat "\\<" (regexp-opt ztl-keywords) "\\>") 0 font-lock-keyword-face)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ;; Secondary keywords
 | 
					    ;; Types
 | 
				
			||||||
    ,@(mapcar (lambda (kw) `(,kw . font-lock-keyword-face)) ztl-keywords-2)
 | 
					    (,(concat "\\<" (regexp-opt ztl-types) "\\>") 0 font-lock-type-face)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ;; Literals
 | 
					    ;; Constants
 | 
				
			||||||
    ,@(mapcar (lambda (lit) `(,lit . font-lock-builtin-face)) ztl-literals)
 | 
					    ("\\_<\\(true\\|false\\)\\>" 1 font-lock-constant-face)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ;; Structs (PascalCased)
 | 
				
			||||||
 | 
					    ("\\_<\\([A-Z][a-zA-Z0-9_]+\\)\\>" 1 font-lock-type-face)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ;; Function definitions
 | 
					    ;; Function definitions
 | 
				
			||||||
    ("\\<\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s*(\\)" 1 font-lock-function-name-face)))
 | 
					    ("\\_<fn\\>\\s-+\\(\\w+\\)\\s-*(" (1 font-lock-function-name-face))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
;; Example usage in .emacs or init.el:
 | 
					    ;; Function calls
 | 
				
			||||||
;; (add-to-list 'auto-mode-alist '("\\.ztl$" . ztl-mode))
 | 
					    ("\\_<\\(\\w+\\)\\s-*(\\>" (1 font-lock-function-name-face))
 | 
				
			||||||
 | 
					))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(define-derived-mode ztl-mode prog-mode "ZTL"
 | 
				
			||||||
 | 
					  "Major mode for ZTL programming."
 | 
				
			||||||
 | 
					  :syntax-table ztl-mode-syntax-table
 | 
				
			||||||
 | 
					  (setq font-lock-multiline t)
 | 
				
			||||||
 | 
					  (setq-local comment-start "!! ")
 | 
				
			||||||
 | 
					  (setq-local comment-end "!!")
 | 
				
			||||||
 | 
					  (setq-local comment-start-skip "!!+\\s-*")
 | 
				
			||||||
 | 
					  (set (make-local-variable 'font-lock-defaults)
 | 
				
			||||||
 | 
					       '(ztl-font-lock-keywords)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(provide 'ztl-mode)
 | 
					(provide 'ztl-mode)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue