From 30b3c516745666777e174bbcd8d7e1ebcd07c42a Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 10 Oct 2026 12:17:09 +0000 Subject: [PATCH] Step 16: editor support, and a stress test of native threads editor/vim: ftdetect (4 spaces), syntax and indent; editor/emacs: jed-mode.el, indenting by jed-indent-offset. Both colour reserved words, basic types, built-ins, numbers, characters, strings and nested comments, and indent the same way: a block one level in from the word that opens it, end and else at that word's level, match arms at match's, declarations at the margin, comment text and continued expressions as written. test/editors.sh, run by make check, loads each headless: every example must indent back exactly as written, and test/editors/Colours.jed must colour as Colours.expect says. Thread's main is its stress test: n threads in waves of up to alive, all waiting on one Mutex, each joined. make stress runs 40,000 at 200 at a time, then, under ulimit -v, checks that spawn fails with Thread.Failed; make check runs 2,000 at 50 and the same failure. It found that spawn ignored mprotect failing once the process had as many mappings as it may, which let a stack join forgot to free go unnoticed and left a thread without a guard page; spawn now checks it. --- diff --git a/CLAUDE.md b/CLAUDE.md index b150ff7..fe710d9 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -13,6 +13,12 @@ make loc # lines of the compiler; the code column is the budget make tests # the small Pascal tests of scan, x64, link and elf make gdb # gdb stops at a line of a test and names it + make stress # 40,000 native threads; make check runs a small version + +`make check` also runs `tools/Doc.jed`, the report's whole examples, +each library module's own `main` (its test), and the vim and emacs +support headless where they are installed (`apt-get install -y vim +emacs-nox`): each example must indent back exactly as written. `make check` must pass before every commit. Tests: `test/run/X.jed` with `X.expect` (stdout), optional `X.status` and `X.args`; modules they @@ -38,8 +44,10 @@ ## Where things stand -Stages 1 to 3 are done (steps 1-14), waiting for review. Next is step -15, `tools/Doc.jed` and the checked examples, on the owner's go-ahead. +Stages 1 to 4 are done (steps 1-16), waiting for review. Next is step +17, `README.md` and `doc/compiler.md`, on the owner's go-ahead. Left +for later at the owner's word: tagging versions, and a rewrite of var +parameters. `test/libc` runs its programs over `lib/Sys.jed`, the C one, through a link in its `mods`. A test or example without `.expect` is only diff --git a/Makefile b/Makefile index 8f8b30a..3385833 100644 --- a/Makefile +++ b/Makefile @@ -44,8 +44,16 @@ echo "$$lines lines in $$((best / 1000000)) ms: $$rate lines a second"; \ test $$rate -ge 400000 +# native threads under load: 40,000 spawned and joined, 200 alive at a +# time, all waiting on one Mutex; then, with little memory, spawn must +# fail with Thread.Failed. make check runs a small version. +stress: jed | build + ./jed -o build/Stress lib/Thread.linux-x86_64.jed + build/Stress 40000 200 + (ulimit -v 60000; build/Stress fail) + # lines of the compiler: all of them, and those that aren't blank or comments loc: @python3 test/loc.py src/*.pas -.PHONY: units tests check gdb bench loc clean +.PHONY: units tests check gdb bench stress loc clean diff --git a/PLAN.md b/PLAN.md index dfcc48d..4a49d3c 100644 --- a/PLAN.md +++ b/PLAN.md @@ -144,7 +144,7 @@ compiler doesn't. (Tagging is left for later, at the owner's word.) -- [ ] 16. Editor support: `editor/vim` (`syntax/jed.vim`, `indent/jed.vim` +- [x] 16. Editor support: `editor/vim` (`syntax/jed.vim`, `indent/jed.vim` by `shiftwidth()`, and `ftdetect/jed.vim`, which sets 4 spaces) and `editor/emacs/jed-mode.el` (indenting by `jed-indent-offset`, 4 unless set). Both colour reserved words, basic types, built-in diff --git a/editor/emacs/jed-mode.el b/editor/emacs/jed-mode.el new file mode 100644 index 0000000..6d636a3 --- /dev/null +++ b/editor/emacs/jed-mode.el @@ -0,0 +1,230 @@ +;;; jed-mode.el --- a major mode for jed -*- lexical-binding: t -*- + +;; Colours jed's reserved words, basic types, built-in procedures, +;; numbers, characters, strings and nested comments, and indents by +;; `jed-indent-offset': a block's statements one level in from the +;; word that opens it; end, until, else and elsif at that word's +;; level; match arms at the level of match; procedures, sections +;; (const, type, var) and module bodies at the left margin. The text +;; of a comment, and a line that goes on with the expression of the +;; line before, keep the indent they have. +;; +;; (add-to-list 'load-path "/path/to/jed/editor/emacs") +;; (require 'jed-mode) + +;;; Code: + +(defgroup jed nil "The jed programming language." :group 'languages) + +(defcustom jed-indent-offset 4 + "Columns that a block's statements are indented by." + :type 'integer :group 'jed) + +(defconst jed-keywords + '("and" "begin" "break" "by" "code" "const" "defer" "div" "do" "else" + "elsif" "end" "extern" "for" "forward" "if" "import" "in" "link" + "match" "mod" "module" "not" "of" "or" "packed" "proc" "record" + "repeat" "return" "set" "shl" "shr" "then" "to" "type" "until" "var" + "variant" "VOLATILE" "while" "xor")) + +(defconst jed-types + '("I8" "I16" "I32" "I64" "U8" "U16" "U32" "U64" "BOOL" "F32" "F64" "ERROR")) + +(defconst jed-builtins + '("SIZE" "ADR" "LEN" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD" + "ARGC" "ARGV" "INC" "DEC")) + +(defvar jed-mode-syntax-table + (let ((st (make-syntax-table))) + ;; (* ... *), nested + (modify-syntax-entry ?\( "()1n" st) + (modify-syntax-entry ?\) ")(4n" st) + (modify-syntax-entry ?* ". 23n" st) + (modify-syntax-entry ?\" "\"" st) + (modify-syntax-entry ?\' "\"" st) + (modify-syntax-entry ?\\ "\\" st) + (modify-syntax-entry ?_ "_" st) + (dolist (c '(?+ ?- ?/ ?= ?# ?< ?> ?: ?\; ?, ?. ?^ ?|)) + (modify-syntax-entry c "." st)) + st) + "Syntax table for `jed-mode'.") + +(defvar jed-font-lock-keywords + `((,(regexp-opt jed-keywords 'symbols) . font-lock-keyword-face) + (,(regexp-opt jed-types 'symbols) . font-lock-type-face) + (,(regexp-opt jed-builtins 'symbols) . font-lock-builtin-face) + (,(regexp-opt '("true" "false" "nil") 'symbols) . font-lock-constant-face) + ;; 123, 0FFH, 1.5, 2.0E-3 + ("\\_<[0-9][0-9A-F]*H\\_>\\|\\_<[0-9]+\\(?:\\.[0-9]+\\(?:E[-+]?[0-9]+\\)?\\)?\\_>" + . font-lock-constant-face)) + "What `jed-mode' colours, besides strings and comments.") + +;;; Indenting + +(defconst jed--decl + "^[ \t]*\\(?:module\\|import\\|link\\|extern\\|proc\\|const\\|type\\|var\\|begin\\)\\_>") +(defconst jed--leading "^[ \t]*\\(?:or\\|and\\)\\_>") +(defconst jed--block-word + "\\_<\\(if\\|while\\|for\\|match\\|record\\|variant\\|begin\\|repeat\\|end\\|until\\)\\_>") + +(defun jed--line (n) + "The text of line N." + (save-excursion + (goto-char (point-min)) + (forward-line (1- n)) + (buffer-substring-no-properties (line-beginning-position) (line-end-position)))) + +(defun jed--indent-of (n) + (save-excursion (goto-char (point-min)) (forward-line (1- n)) (current-indentation))) + +(defun jed--in-comment (n) + "Whether line N starts inside a comment that began before it." + (save-excursion + (goto-char (point-min)) + (forward-line (1- n)) + (back-to-indentation) + (and (nth 4 (syntax-ppss)) + (not (looking-at "(\\*"))))) + +(defun jed--prev (n) + "The last line before N that isn't blank, or 0." + (let ((l (1- n))) + (while (and (> l 0) (string-match-p "\\`[ \t]*\\'" (jed--line l))) + (setq l (1- l))) + l)) + +(defun jed--code (n) + "Line N's code, without strings, characters and comments." + (let ((line (jed--line n))) + (when (jed--in-comment n) + (setq line (if (string-match "\\*)" line) (substring line (match-end 0)) ""))) + ;; one pattern, so that whichever comes first is taken + (setq line (replace-regexp-in-string + "\"\\(?:[^\"\\\\]\\|\\\\.\\)*\"\\|'\\(?:[^'\\\\]\\|\\\\.\\)*'\\|(\\*.*?\\*)" + (lambda (m) (if (string-prefix-p "(" m) "" "0")) line t t)) + (setq line (replace-regexp-in-string "(\\*.*\\'" "" line t t)) + (string-trim line))) + +(defun jed--count (re s) + (let ((n 0) (i 0)) + (while (string-match re s i) + (setq n (1+ n) i (match-end 0))) + n)) + +(defun jed--continues (n) + "Whether the expression on line N goes on to the next line." + (let ((code (jed--code n))) + (or (> (jed--count "[([{]" code) (jed--count "[])}]" code)) + (string-match-p "\\(?:,\\|:=\\|[-+*/=#<>]\\|\\_ l 1) (not (string-match-p "(\\*" (jed--line l)))) + (setq l (1- l))) + l)) + +(defun jed-calculate-indent () + "The column line at point should start at, or nil to leave it." + (let* ((case-fold-search nil) + (n (line-number-at-pos)) + (cur (jed--line n)) + (prev (jed--prev n))) + (catch 'done + (when (jed--in-comment n) (throw 'done nil)) + (when (and (string-match-p jed--decl cur) (not (string-match-p "^[ \t]*proc[ \t]*(" cur))) + (throw 'done 0)) + ;; a comment before a declaration goes with it, at the margin + (when (string-match-p "^[ \t]*(\\*" cur) + (let ((l n) (last (line-number-at-pos (point-max)))) + (while (and (< l last) (not (string-match-p "\\*)" (jed--line l)))) + (setq l (1+ l))) + (setq l (1+ l)) + (while (and (<= l last) (string-match-p "\\`[ \t]*\\'" (jed--line l))) + (setq l (1+ l))) + (when (and (<= l last) (string-match-p jed--decl (jed--line l))) + (throw 'done 0)))) + (when (string-match-p "^[ \t]*\\(?:end\\|until\\)\\_>" cur) + (throw 'done (or (jed--opener n) 0))) + (when (= prev 0) (throw 'done 0)) + (when (string-match-p "^[ \t]*\\(?:\\(?:else\\|elsif\\)\\_>\\||\\)" cur) + (throw 'done (or (jed--opener n) (jed--indent-of prev)))) + (when (or (jed--continues prev) (string-match-p jed--leading cur)) + (throw 'done nil)) + (when (string= (jed--code prev) "") + (throw 'done (jed--indent-of (jed--comment-start prev)))) + (let* ((start (jed--start prev)) + (ind (jed--indent-of start)) + (code (mapconcat #'jed--code (number-sequence start prev) " "))) + (cond + ((or (string-match-p "\\`\\(?:const\\|type\\|var\\)\\'" code) + (and (string-match-p "\\`else\\_>" code) (>= (jed--opens code) 0)) + (string-match-p "\\`|.*:\\'" code) + (string-match-p "\\`elsif\\_>.*\\_.*\\_ (jed--opens code) 0) (+ ind jed-indent-offset)) + (t ind)))))) + +(defun jed-indent-line () + "Indent the current line as jed's blocks say." + (interactive) + (let ((col (save-excursion (jed-calculate-indent))) + (offset (- (current-column) (current-indentation)))) + (when col + (indent-line-to col) + (when (> offset 0) (move-to-column (+ col offset)))))) + +;;;###autoload +(define-derived-mode jed-mode prog-mode "jed" + "Major mode for editing jed." + :syntax-table jed-mode-syntax-table + (setq-local font-lock-defaults '(jed-font-lock-keywords nil nil)) + (setq-local comment-start "(* ") + (setq-local comment-end " *)") + (setq-local comment-start-skip "(\\*+[ \t]*") + (setq-local indent-tabs-mode nil) + (setq-local indent-line-function #'jed-indent-line)) + +;;;###autoload +(add-to-list 'auto-mode-alist '("\\.jed\\'" . jed-mode)) + +(provide 'jed-mode) + +;;; jed-mode.el ends here diff --git a/editor/vim/ftdetect/jed.vim b/editor/vim/ftdetect/jed.vim new file mode 100644 index 0000000..b81b8e9 --- /dev/null +++ b/editor/vim/ftdetect/jed.vim @@ -0,0 +1,3 @@ +" jed: *.jed files, indented by 4 spaces. +autocmd BufRead,BufNewFile *.jed setfiletype jed +autocmd FileType jed setlocal shiftwidth=4 softtabstop=4 expandtab diff --git a/editor/vim/indent/jed.vim b/editor/vim/indent/jed.vim new file mode 100644 index 0000000..cba8770 --- /dev/null +++ b/editor/vim/indent/jed.vim @@ -0,0 +1,157 @@ +" Vim indent file for jed, by shiftwidth(): a block's statements one +" level in from the word that opens it; end, until, else and elsif at +" that word's level; match arms at the level of match; procedures, +" sections (const, type, var) and module bodies at the left margin. +" The text of a comment, and a line that goes on with the expression +" of the line before, keep the indent they have. + +if exists("b:did_indent") + finish +endif +let b:did_indent = 1 + +setlocal indentexpr=GetJedIndent(v:lnum) +setlocal indentkeys=o,O,0=end,0=else,0=elsif,0=until,0=\|,!^F +setlocal nolisp nosmartindent + +if exists("*GetJedIndent") + finish +endif + +" searchpair() needs groups that don't capture +let s:opener = '\C\<\%(if\|while\|for\|match\|record\|variant\|begin\|repeat\)\>' +let s:closer = '\C\<\%(end\|until\)\>' +let s:skip = 'synIDattr(synID(line("."), col("."), 1), "name") =~? "string\\|char\\|comment"' +let s:decl = '^\s*\%(module\|import\|link\|extern\|proc\|const\|type\|var\|begin\)\>' + +" The line's code, without strings, characters and comments. +function! s:Code(lnum) + let line = getline(a:lnum) + " a line that starts inside a comment: its code is after the comment + if s:InComment(a:lnum) + let line = line =~ '\*)' ? substitute(line, '^.\{-}\*)', '', '') : '' + endif + " one pattern, so that whichever comes first is taken: a " in a + " character, a ' in a string; strings become 0, comments go + let line = substitute(line, + \ '"\%([^"\\]\|\\.\)*"\|''\%([^''\\]\|\\.\)*''\|(\*.\{-}\*)', + \ '\=submatch(0)[0] == "(" ? "" : "0"', 'g') + let line = substitute(line, '(\*.*$', '', '') + return substitute(line, '^\s*\(.\{-}\)\s*$', '\1', '') +endfunction + +" Whether the expression on lnum goes on to the next line. +function! s:Continues(lnum) + let code = s:Code(a:lnum) + if len(substitute(code, '[^([{]', '', 'g')) > len(substitute(code, '[^)\]}]', '', 'g')) + return 1 + endif + return code =~# '\%(,\|:=\|[-+*/=#<>]\|\ 1 && getline(l) !~ '(\*' + let l -= 1 + endwhile + return l +endfunction + +" How many blocks the code opens, less those it closes. +function! s:Opens(code) + let n = 0 + let i = 0 + while 1 + let m = matchstrpos(a:code, '\C\<\%(if\|while\|for\|record\|variant\|begin\|repeat\|end\|until\)\>', i) + if m[1] < 0 + return n + endif + let n += (m[0] ==# 'end' || m[0] ==# 'until') ? -1 : 1 + let i = m[2] + endwhile +endfunction + +function! s:InComment(lnum) + let col = match(getline(a:lnum), '\S') + 1 + return col > 0 && synIDattr(synID(a:lnum, col, 1), "name") =~? 'comment' + \ && getline(a:lnum) !~ '^\s*(\*' +endfunction + +function! GetJedIndent(lnum) + let cur = getline(a:lnum) + if s:InComment(a:lnum) + return -1 + endif + if cur =~# s:decl && cur !~# '^\s*proc\s*(' + return 0 + endif + " a comment before a declaration goes with it, at the margin + if cur =~ '^\s*(\*' + let l = a:lnum + while l < line('$') && getline(l) !~ '\*)' + let l += 1 + endwhile + if getline(nextnonblank(l + 1)) =~# s:decl + return 0 + endif + endif + if cur =~# '^\s*\%(end\|until\)\>' + call cursor(a:lnum, 1) + let l = searchpair(s:opener, '', s:closer, 'bW', s:skip) + return l > 0 ? indent(l) : 0 + endif + let prev = prevnonblank(a:lnum - 1) + if prev == 0 + return 0 + endif + if cur =~# '^\s*\%(else\|elsif\)\>' + call cursor(a:lnum, 1) + let l = searchpair(s:opener, '', s:closer, 'bW', s:skip) + return l > 0 ? indent(l) : indent(prev) + endif + if cur =~# '^\s*|' + call cursor(a:lnum, 1) + let l = searchpair(s:opener, '', s:closer, 'bW', s:skip) + return l > 0 ? indent(l) : indent(prev) + endif + if s:Continues(prev) || cur =~# s:leading + return -1 + endif + if s:Code(prev) == '' + return indent(s:Reference(prev)) + endif + + let start = s:Start(prev) + let ind = indent(start) + let code = join(map(range(start, prev), 's:Code(v:val)'), ' ') + if code =~# '^\%(const\|type\|var\)$' || code =~# '^else\>' && s:Opens(code) >= 0 + \ || code =~# '^|.*:$' || code =~# '^elsif\>.*\.*\ 0 + return ind + shiftwidth() + endif + return ind +endfunction diff --git a/editor/vim/syntax/jed.vim b/editor/vim/syntax/jed.vim new file mode 100644 index 0000000..181e067 --- /dev/null +++ b/editor/vim/syntax/jed.vim @@ -0,0 +1,42 @@ +" Vim syntax file for jed: reserved words, basic types, built-in +" procedures, numbers, characters, strings and nested comments. + +if exists("b:current_syntax") + finish +endif + +syntax case match + +syntax keyword jedKeyword and begin break by code const defer div do else +syntax keyword jedKeyword elsif end extern for forward if import in link +syntax keyword jedKeyword match mod module not of or packed proc record +syntax keyword jedKeyword repeat return set shl shr then to type until var +syntax keyword jedKeyword variant VOLATILE while xor +syntax keyword jedType I8 I16 I32 I64 U8 U16 U32 U64 BOOL F32 F64 ERROR +syntax keyword jedBuiltin SIZE ADR LEN NEW MSG SYSCALL FENCE CAS XCHG XADD +syntax keyword jedBuiltin ARGC ARGV INC DEC +syntax keyword jedConstant true false nil + +" 123, 0FFH, 1.5, 2.0E-3 +syntax match jedNumber "\<\d[0-9A-F]*H\>" +syntax match jedNumber "\<\d\+\>" +syntax match jedNumber "\<\d\+\.\d\+\(E[+-]\=\d\+\)\=\>" + +syntax match jedEscape contained "\\\([ntr0\\'\"]\|x\x\x\|u{\x\+}\)" +syntax region jedString start=+"+ skip=+\\.+ end=+"+ contains=jedEscape oneline +syntax region jedChar start=+'+ skip=+\\.+ end=+'+ contains=jedEscape oneline + +syntax region jedComment start="(\*" end="\*)" contains=jedComment,@Spell +syntax sync fromstart + +highlight default link jedKeyword Statement +highlight default link jedType Type +highlight default link jedBuiltin Function +highlight default link jedConstant Constant +highlight default link jedNumber Number +highlight default link jedString String +highlight default link jedChar Character +highlight default link jedEscape SpecialChar +highlight default link jedComment Comment + +let b:current_syntax = "jed" diff --git a/lib/Thread.linux-x86_64.jed b/lib/Thread.linux-x86_64.jed index 27a0917..d9fdc57 100644 --- a/lib/Thread.linux-x86_64.jed +++ b/lib/Thread.linux-x86_64.jed @@ -6,7 +6,7 @@ a Mutex sleeps on a futex. A program with C threads uses C's, through pthread_create, and so can't use these. *) module Thread; -import Sys; +import Sys, Rt; link static; const @@ -64,7 +64,12 @@ size := Stack + Page; base := Sys.mmap(size); if base = nil then return nil, Failed; end; - SYSCALL(10, base, Page, 0); (* mprotect: the guard page, no access *) + (* mprotect: the guard page, no access; it fails when the process + has as many mappings as it may *) + if SYSCALL(10, base, Page, 0) < 0 then + Sys.munmap(base, size); + return nil, Failed; + end; (* the Thread at the top, the stack below it, aligned to 16 *) t := ^Thread(base + size - 32); t^.base := base; @@ -108,6 +113,66 @@ XCHG(ADR(m^.state), 0); futex(ADR(m^.state), FutexWake + Private, 1); end; +end; + +(* The module's test, and make stress's. It starts n threads, in waves + of up to alive at a time, all waiting on one Mutex the test holds + until the wave is out; each, given it, adds one to a count. Every + thread is joined, so that a stack join didn't free would, over + enough of them, run the process out of mappings. With "fail", it + starts threads, all waiting, until spawn fails, which it must do + with Failed: make stress and make check run it with little memory. + Thread [n alive], Thread fail *) +var + testLock: Mutex; + testCount: I64; + wave: [1000]^Thread; + +proc testWork(arg: ^U8); +begin + lock(ADR(testLock)); + INC(testCount); + unlock(ADR(testLock)); +end; + +proc testArg(i: I64): I64; +var s: ^U8; n, v: I64; +begin + s := ARGV()[i]; + v := 0; + n := 0; + while s[n] # 0 do v := v * 10 + I64(s[n]) - '0'; INC(n); end; + return v; +end; + +proc main*; +var n, alive, started, k, i: I64; t: ^Thread; err: ERROR; fail: BOOL; +begin + n := 2000; alive := 50; + fail := (ARGC() = 2) and (ARGV()[1][0] = 'f'); + if ARGC() = 3 then n := testArg(1); alive := testArg(2); end; + if fail then n := LEN(wave); alive := LEN(wave); end; + if (alive < 1) or (alive > LEN(wave)) then Rt.stop("Thread: alive is 1 to 1000"); end; + started := 0; + while started < n do + lock(ADR(testLock)); + k := 0; + while (k < alive) and (started + k < n) do + t, err := spawn(testWork, nil); + if err # nil then + if not fail or (err # Failed) then Rt.stop("Thread.spawn failed"); end; + n := started + k; + break; + end; + wave[k] := t; + INC(k); + end; + unlock(ADR(testLock)); + for i := 0 to k - 1 do join(wave[i]); end; + started := started + k; + end; + if testCount # started then Rt.stop("Thread: a thread didn't run"); end; + if fail and (started = LEN(wave)) then Rt.stop("Thread: spawn never failed"); end; end; end Thread. diff --git a/test/check.sh b/test/check.sh index defc208..da98d7d 100755 --- a/test/check.sh +++ b/test/check.sh @@ -103,5 +103,13 @@ else pass=$((pass + 1)); fi done +# Thread's test again, with too little memory for all its threads: +# spawn must fail, with Thread.Failed +if (ulimit -v 60000; $out/lib_Thread.linux-x86_64 fail) > /dev/null 2>&1; then pass=$((pass + 1)) +else echo "FAIL Thread under a memory limit"; fail=$((fail + 1)); fi + +# the editors' support, headless, where the editors are installed +if test/editors.sh; then pass=$((pass + 1)); else fail=$((fail + 1)); fi + echo "$pass passed, $fail failed" [ $fail = 0 ] diff --git a/test/doc/Thread.expect b/test/doc/Thread.expect index 30d4c31..7de500c 100644 --- a/test/doc/Thread.expect +++ b/test/doc/Thread.expect @@ -27,3 +27,5 @@ proc lock*(m: ^Mutex); proc unlock*(m: ^Mutex); + +proc main*; diff --git a/test/editors.sh b/test/editors.sh new file mode 100755 index 0000000..360fe6d --- /dev/null +++ b/test/editors.sh @@ -0,0 +1,38 @@ +#!/bin/sh +# Loads the editor support, headless, on each example: the file must be +# taken for jed, coloured without error, and indented again exactly as +# it is written; and test/editors/Colours.jed must be coloured as +# Colours.expect says. Prints one line per failure; quiet if the editor +# is missing. + +fail=0 +if command -v vim >/dev/null; then + for f in examples/*.jed; do + vim -Nu NONE -i NONE -es \ + -c 'set rtp^=editor/vim' -c 'filetype plugin indent on' -c 'syntax on' \ + -c "edit $f" \ + -c 'if &filetype !=# "jed" || &shiftwidth != 4 | cquit | endif' \ + -c 'let before = getline(1, "$")' -c 'normal! gg=G' \ + -c 'if getline(1, "$") != before | cquit | endif' -c 'qall!' \ + || { echo "FAIL vim on $f"; fail=1; } + done + vim -Nu NONE -i NONE -es -c 'set rtp^=editor/vim' -c 'filetype plugin indent on' \ + -c 'syntax on' -c 'edit test/editors/Colours.jed' -S test/editors/colours.vim \ + || { echo "FAIL vim colours"; fail=1; } +fi +if command -v emacs >/dev/null; then + for f in examples/*.jed; do + emacs -Q --batch -l editor/emacs/jed-mode.el --eval " + (progn + (find-file \"$f\") + (unless (eq major-mode 'jed-mode) (kill-emacs 1)) + (font-lock-ensure) + (let ((before (buffer-string))) + (indent-region (point-min) (point-max)) + (unless (string= before (buffer-string)) (kill-emacs 2))))" \ + 2>/dev/null || { echo "FAIL emacs on $f"; fail=1; } + done + emacs -Q --batch -l editor/emacs/jed-mode.el -l test/editors/colours.el 2>/dev/null \ + || { echo "FAIL emacs colours"; fail=1; } +fi +exit $fail diff --git a/test/editors/Colours.expect b/test/editors/Colours.expect new file mode 100644 index 0000000..096ed7a --- /dev/null +++ b/test/editors/Colours.expect @@ -0,0 +1,12 @@ +1 23 comment +2 1 keyword +3 13 number +3 27 number +3 39 string +3 49 string +3 53 string +4 8 type +4 17 type +6 5 builtin +6 19 constant +5 1 keyword diff --git a/test/editors/Colours.jed b/test/editors/Colours.jed new file mode 100644 index 0000000..ced1d47 --- /dev/null +++ b/test/editors/Colours.jed @@ -0,0 +1,7 @@ +(* outer (* nested *) still comment *) +module Colours; +const big = 0FFH; small = 2.5E-3; q = '\''; s = "a\"b"; +var x: I64; ok: BOOL; +begin + INC(x); ok := true; +end Colours. diff --git a/test/editors/colours.el b/test/editors/colours.el new file mode 100644 index 0000000..a359cea --- /dev/null +++ b/test/editors/colours.el @@ -0,0 +1,27 @@ +;; Checks each token of Colours.expect has the face it should. +(let ((wants (split-string (with-temp-buffer + (insert-file-contents "test/editors/Colours.expect") + (buffer-string)) + "\n" t))) + (find-file "test/editors/Colours.jed") + (font-lock-ensure) + (dolist (want wants) + (let* ((w (split-string want)) + (face (save-excursion + (goto-char (point-min)) + (forward-line (1- (string-to-number (nth 0 w)))) + (forward-char (1- (string-to-number (nth 1 w)))) + (get-text-property (point) 'face))) + (face (if (listp face) (car face) face)) + (kind (pcase face + ((or 'font-lock-comment-face 'font-lock-comment-delimiter-face) "comment") + ('font-lock-keyword-face "keyword") + ('font-lock-string-face "string") + ('font-lock-type-face "type") + ('font-lock-builtin-face "builtin") + ('font-lock-constant-face "constant")))) + ;; numbers and constants share a face + (unless (or (equal kind (nth 2 w)) + (and (equal kind "constant") (equal (nth 2 w) "number"))) + (message "%s: %s" want face) + (kill-emacs 3))))) diff --git a/test/editors/colours.vim b/test/editors/colours.vim new file mode 100644 index 0000000..aebb95b --- /dev/null +++ b/test/editors/colours.vim @@ -0,0 +1,12 @@ +" Checks each token of Colours.expect has the colour group it should. +let s:kinds = {'jedComment': 'comment', 'jedKeyword': 'keyword', 'jedNumber': 'number', + \ 'jedString': 'string', 'jedChar': 'string', 'jedType': 'type', + \ 'jedBuiltin': 'builtin', 'jedConstant': 'constant'} +for s:want in readfile('test/editors/Colours.expect') + let [s:l, s:c, s:k] = split(s:want) + let s:group = synIDattr(synID(str2nr(s:l), str2nr(s:c), 1), 'name') + if get(s:kinds, s:group, '') !=# s:k + cquit + endif +endfor +qall!