refactor(lang): rename the fn keyword to function
Expand the function-declaration keyword to the full word across the whole
language and toolchain:
fn name(...) -> T { ... } -> function name(...) -> T { ... }
Done as a self-hosting migration: teach the parser both spellings, reseed,
rewrite every .ludic definition to `function`, then drop `fn`. The compiler
now rejects `fn`. Touches the parser, all selfhost/tools/runtime/example/test
sources, the grammars (TextMate shared+vscode, ludic_syntax.h, JetBrains
LudicTokens.kt), the LSP and formatter, the Python doc/vocab tools
(check-impl, check-docs, validate, palette, test-lsp), and the docs
(fences, prose, kw-fn -> kw-function).
Reseeded; C-free bootstrap fixpoint holds. All suites green (45 regression,
24 self-host, 29 tool); the docs site generates and check.py passes.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
2f19c8d8e2
commit
4c48077d68
86 changed files with 793 additions and 793 deletions
|
|
@ -34,7 +34,7 @@ game Atlas {
|
|||
|
||||
# build "<DIR>/tile_%04d.png" without a formatter: the runtime's string
|
||||
# helpers write into a scratch buffer we own
|
||||
fn tile_path(i: int) -> str {
|
||||
function tile_path(i: int) -> str {
|
||||
let buf = atlas_buf()
|
||||
let n = rt_put_str(buf, 0, DIR)
|
||||
n = rt_put_str(buf, n, "/tile_")
|
||||
|
|
@ -49,7 +49,7 @@ game Atlas {
|
|||
|
||||
var atlas_scratch: ptr = ptr_null()
|
||||
|
||||
fn atlas_buf() -> ptr {
|
||||
function atlas_buf() -> ptr {
|
||||
if ptr_is_null(atlas_scratch) { atlas_scratch = mem_alloc(512) }
|
||||
return atlas_scratch
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ A bare fragment is wrapped first: a declaration list goes inside
|
|||
import re, subprocess, sys, os, tempfile
|
||||
|
||||
LC = './build/ludicc'
|
||||
DECL = ('program','property','model','handler','enum','fn',
|
||||
DECL = ('program','property','model','handler','enum','function',
|
||||
'extern','const','var','ui','struct','import')
|
||||
|
||||
def classify(body):
|
||||
|
|
|
|||
|
|
@ -33,12 +33,12 @@ def read(*parts):
|
|||
|
||||
|
||||
def fn_body(text, name):
|
||||
"""The source of `fn <name>` up to the next top-level `fn ` (or EOF)."""
|
||||
m = re.search(r"^fn %s\b" % re.escape(name), text, re.M)
|
||||
"""The source of `function <name>` up to the next top-level `function ` (or EOF)."""
|
||||
m = re.search(r"^function %s\b" % re.escape(name), text, re.M)
|
||||
if not m:
|
||||
return ""
|
||||
rest = text[m.end():]
|
||||
nxt = re.search(r"^fn ", rest, re.M)
|
||||
nxt = re.search(r"^function ", rest, re.M)
|
||||
return text[m.start(): m.end() + (nxt.start() if nxt else len(rest))]
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -266,7 +266,7 @@
|
|||
"kw-const",
|
||||
"kw-var",
|
||||
"kw-let",
|
||||
"kw-fn",
|
||||
"kw-function",
|
||||
"kw-return",
|
||||
"kw-import",
|
||||
"kw-extern",
|
||||
|
|
|
|||
|
|
@ -269,7 +269,7 @@ def emit_ludic(path):
|
|||
lines.append("# Edit the palette there and regenerate; do not hand-edit this file.")
|
||||
lines.append("# ============================================================================")
|
||||
lines.append("")
|
||||
lines.append("fn color_lookup(name: ptr) -> int {")
|
||||
lines.append("function color_lookup(name: ptr) -> int {")
|
||||
for (nm, hexv, grp) in PALETTE:
|
||||
lines.append(f' if (name == "{nm}") {{ return 0x{hexv:06X} }}')
|
||||
lines.append(" return -1")
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ Full programs (start with program/module) compile directly; fragments are wrappe
|
|||
import os, re, subprocess, tempfile, sys
|
||||
REPO=os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))); LC=os.path.join(REPO,"bin","ludicc")
|
||||
FENCE=re.compile(r"```ludic\n(.*?)\n```", re.S)
|
||||
DECL=("program","property","model","handler","enum","fn","extern","const","var","ui","event","module","import","scene","machine")
|
||||
DECL=("program","property","model","handler","enum","function","extern","const","var","ui","event","module","import","scene","machine")
|
||||
|
||||
def first_kw(body):
|
||||
for l in body.split("\n"):
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ object LudicTokens {
|
|||
val IDENTIFIER = LudicTokenType("IDENTIFIER")
|
||||
val TYPE_NAME = LudicTokenType("TYPE_NAME") // Capitalised: component, archetype, scene
|
||||
val KEYWORD = LudicTokenType("KEYWORD")
|
||||
val DECL_KEYWORD = LudicTokenType("DECL_KEYWORD") // game, component, system, fn, ...
|
||||
val DECL_KEYWORD = LudicTokenType("DECL_KEYWORD") // game, component, system, function, ...
|
||||
val CLAUSE = LudicTokenType("CLAUSE") // phase, query, reads, writes, ...
|
||||
val PRIMITIVE = LudicTokenType("PRIMITIVE") // int, fixed, bool, entity, str, ptr, void
|
||||
val PHASE = LudicTokenType("PHASE") // Start, Update, Render, ...
|
||||
|
|
@ -42,7 +42,7 @@ object LudicTokens {
|
|||
object LudicVocabulary {
|
||||
val DECL = setOf(
|
||||
"program", "import", "property", "model", "enum", "ui",
|
||||
"const", "var", "fn", "extern", "handler", "entry", "event", "scene"
|
||||
"const", "var", "function", "extern", "handler", "entry", "event", "scene"
|
||||
)
|
||||
val CLAUSE = setOf(
|
||||
"phase", "query", "on", "cancellable", "public", "layer", "start"
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@
|
|||
}
|
||||
},
|
||||
{
|
||||
"match": "\\b(extern\\s+)?(fn)\\s+([A-Za-z_][A-Za-z0-9_]*)",
|
||||
"match": "\\b(extern\\s+)?(function)\\s+([A-Za-z_][A-Za-z0-9_]*)",
|
||||
"captures": {
|
||||
"1": { "name": "storage.modifier.extern.ludic" },
|
||||
"2": { "name": "storage.type.function.ludic" },
|
||||
|
|
@ -176,7 +176,7 @@
|
|||
{ "name": "keyword.operator.logical.ludic", "match": "\\b(and|or|not)\\b" },
|
||||
{ "name": "keyword.other.clause.ludic", "match": "\\b(phase|query|on)\\b" },
|
||||
{ "name": "keyword.other.ludic", "match": "\\b(import|extern)\\b" },
|
||||
{ "name": "storage.type.ludic", "match": "\\b(program|property|model|enum|ui|const|var|let|fn|handler|entry|state)\\b" },
|
||||
{ "name": "storage.type.ludic", "match": "\\b(program|property|model|enum|ui|const|var|let|function|handler|entry|state)\\b" },
|
||||
{ "name": "support.type.primitive.ludic", "match": "\\b(int|long|fixed|bool|entity|str|ptr|byte|words|fixeds|ptrs|void)\\b" },
|
||||
{ "name": "constant.language.boolean.ludic", "match": "\\b(true|false|null)\\b" },
|
||||
{ "name": "constant.language.phase.ludic", "match": "\\b(Start|Input|FixedUpdate|Update|LateUpdate|Render)\\b" }
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@
|
|||
}
|
||||
},
|
||||
{
|
||||
"match": "\\b(extern\\s+)?(fn)\\s+([A-Za-z_][A-Za-z0-9_]*)",
|
||||
"match": "\\b(extern\\s+)?(function)\\s+([A-Za-z_][A-Za-z0-9_]*)",
|
||||
"captures": {
|
||||
"1": { "name": "storage.modifier.extern.ludic" },
|
||||
"2": { "name": "storage.type.function.ludic" },
|
||||
|
|
@ -176,7 +176,7 @@
|
|||
{ "name": "keyword.operator.logical.ludic", "match": "\\b(and|or|not)\\b" },
|
||||
{ "name": "keyword.other.clause.ludic", "match": "\\b(phase|query|on)\\b" },
|
||||
{ "name": "keyword.other.ludic", "match": "\\b(import|extern)\\b" },
|
||||
{ "name": "storage.type.ludic", "match": "\\b(program|property|model|enum|ui|const|var|let|fn|handler|entry|state)\\b" },
|
||||
{ "name": "storage.type.ludic", "match": "\\b(program|property|model|enum|ui|const|var|let|function|handler|entry|state)\\b" },
|
||||
{ "name": "support.type.primitive.ludic", "match": "\\b(int|long|fixed|bool|entity|str|ptr|byte|words|fixeds|ptrs|void)\\b" },
|
||||
{ "name": "constant.language.boolean.ludic", "match": "\\b(true|false|null)\\b" },
|
||||
{ "name": "constant.language.phase.ludic", "match": "\\b(Start|Input|FixedUpdate|Update|LateUpdate|Render)\\b" }
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ program LudicFmt {
|
|||
const LT_ERR: int = 14
|
||||
|
||||
# ---- tiny stdio + string helpers (self-contained) ----
|
||||
fn read_file(path: str) -> ptr {
|
||||
function read_file(path: str) -> ptr {
|
||||
let f = file_open(path, "rb")
|
||||
if (f == null) { return null }
|
||||
file_seek(f, 0, 2)
|
||||
|
|
@ -40,16 +40,16 @@ program LudicFmt {
|
|||
file_close(f)
|
||||
return buf
|
||||
}
|
||||
fn cstr_len(s: ptr) -> int { var n = 0; while s[n] != 0 { n = n + 1 }; return n }
|
||||
fn char_is_digit(c: int) -> bool { return c >= 48 and c <= 57 }
|
||||
fn char_is_alpha(c: int) -> bool {
|
||||
function cstr_len(s: ptr) -> int { var n = 0; while s[n] != 0 { n = n + 1 }; return n }
|
||||
function char_is_digit(c: int) -> bool { return c >= 48 and c <= 57 }
|
||||
function char_is_alpha(c: int) -> bool {
|
||||
if c >= 65 and c <= 90 { return true }
|
||||
if c >= 97 and c <= 122 { return true }
|
||||
return c == 95
|
||||
}
|
||||
fn char_is_alnum(c: int) -> bool { return char_is_alpha(c) or char_is_digit(c) }
|
||||
fn char_is_hex(c: int) -> bool { return char_is_digit(c) or (c >= 97 and c <= 102) or (c >= 65 and c <= 70) }
|
||||
fn itoa(v: int) -> ptr {
|
||||
function char_is_alnum(c: int) -> bool { return char_is_alpha(c) or char_is_digit(c) }
|
||||
function char_is_hex(c: int) -> bool { return char_is_digit(c) or (c >= 97 and c <= 102) or (c >= 65 and c <= 70) }
|
||||
function itoa(v: int) -> ptr {
|
||||
if v == 0 { let z = bytes(2); z[0] = 48; z[1] = 0; return z }
|
||||
var neg = false; var x = v
|
||||
if x < 0 { neg = true; x = 0 - x }
|
||||
|
|
@ -67,18 +67,18 @@ program LudicFmt {
|
|||
|
||||
# ---- a growable byte buffer ----
|
||||
property Buf { data: ptr = null, len: int = 0, cap: int = 0 }
|
||||
fn buf_new() -> Buf { let b = new Buf; b.cap = 256; b.data = bytes(b.cap); b.len = 0; return b }
|
||||
fn buf_ensure(b: Buf, extra: int) -> void {
|
||||
function buf_new() -> Buf { let b = new Buf; b.cap = 256; b.data = bytes(b.cap); b.len = 0; return b }
|
||||
function buf_ensure(b: Buf, extra: int) -> void {
|
||||
if b.len + extra + 1 <= b.cap { return }
|
||||
while b.len + extra + 1 > b.cap { b.cap = b.cap * 2 }
|
||||
b.data = resize(b.data, b.cap)
|
||||
}
|
||||
fn buf_putc(b: Buf, c: int) -> void { buf_ensure(b, 1); b.data[b.len] = c; b.len = b.len + 1 }
|
||||
fn buf_puts(b: Buf, s: ptr) -> void { var i = 0; while s[i] != 0 { buf_putc(b, s[i]); i = i + 1 } }
|
||||
fn buf_indent(b: Buf, n: int) -> void { var i = 0; while i < n { buf_putc(b, 32); i = i + 1 } }
|
||||
fn buf_str(b: Buf) -> ptr { b.data[b.len] = 0; return b.data }
|
||||
function buf_putc(b: Buf, c: int) -> void { buf_ensure(b, 1); b.data[b.len] = c; b.len = b.len + 1 }
|
||||
function buf_puts(b: Buf, s: ptr) -> void { var i = 0; while s[i] != 0 { buf_putc(b, s[i]); i = i + 1 } }
|
||||
function buf_indent(b: Buf, n: int) -> void { var i = 0; while i < n { buf_putc(b, 32); i = i + 1 } }
|
||||
function buf_str(b: Buf) -> ptr { b.data[b.len] = 0; return b.data }
|
||||
# append src[a..b) raw
|
||||
fn buf_addrange(b: Buf, s: ptr, a: int, e: int) -> void { var k = a; while k < e { buf_putc(b, s[k]); k = k + 1 } }
|
||||
function buf_addrange(b: Buf, s: ptr, a: int, e: int) -> void { var k = a; while k < e { buf_putc(b, s[k]); k = k + 1 } }
|
||||
|
||||
# ---- the token stream (parallel slices) ----
|
||||
var src: ptr = null
|
||||
|
|
@ -88,35 +88,35 @@ program LudicFmt {
|
|||
var tk_line: []int
|
||||
var linestart: []int
|
||||
|
||||
fn ntok() -> int { return len(tk_kind) }
|
||||
fn tok_len(i: int) -> int { return tk_end[i] - tk_start[i] }
|
||||
fn tok_text(i: int) -> ptr { return src[tk_start[i]..tk_end[i]] }
|
||||
fn push_tok(k: int, st: int, en: int, ln: int) -> void {
|
||||
function ntok() -> int { return len(tk_kind) }
|
||||
function tok_len(i: int) -> int { return tk_end[i] - tk_start[i] }
|
||||
function tok_text(i: int) -> ptr { return src[tk_start[i]..tk_end[i]] }
|
||||
function push_tok(k: int, st: int, en: int, ln: int) -> void {
|
||||
push(tk_kind, k); push(tk_start, st); push(tk_end, en); push(tk_line, ln)
|
||||
}
|
||||
|
||||
# ---- vocabulary classifiers ----
|
||||
fn is_type_word(w: ptr) -> bool {
|
||||
function is_type_word(w: ptr) -> bool {
|
||||
return (w == "int") or (w == "long") or (w == "fixed") or (w == "bool") or (w == "entity") or (w == "str") or (w == "ptr") or (w == "byte") or (w == "words") or (w == "fixeds") or (w == "ptrs") or (w == "void")
|
||||
}
|
||||
fn is_phase_word(w: ptr) -> bool {
|
||||
function is_phase_word(w: ptr) -> bool {
|
||||
return (w == "Start") or (w == "Input") or (w == "FixedUpdate") or (w == "Update") or (w == "LateUpdate") or (w == "Render")
|
||||
}
|
||||
fn is_keyword_word(w: ptr) -> bool {
|
||||
function is_keyword_word(w: ptr) -> bool {
|
||||
if (w == "program") or (w == "import") or (w == "property") or (w == "model") or (w == "enum") or (w == "ui") { return true }
|
||||
if (w == "const") or (w == "var") or (w == "fn") or (w == "extern") or (w == "handler") or (w == "entry") or (w == "event") or (w == "scene") { return true }
|
||||
if (w == "const") or (w == "var") or (w == "function") or (w == "extern") or (w == "handler") or (w == "entry") or (w == "event") or (w == "scene") { return true }
|
||||
if (w == "phase") or (w == "query") or (w == "on") or (w == "cancellable") or (w == "public") or (w == "layer") or (w == "start") { return true }
|
||||
if (w == "let") or (w == "return") or (w == "if") or (w == "else") or (w == "while") or (w == "for") or (w == "in") or (w == "spawn") or (w == "despawn") { return true }
|
||||
if (w == "enable") or (w == "disable") or (w == "match") or (w == "machine") or (w == "state") or (w == "become") or (w == "where") { return true }
|
||||
if (w == "and") or (w == "or") or (w == "not") or (w == "break") or (w == "continue") or (w == "new") or (w == "emit") or (w == "cancel") { return true }
|
||||
return false
|
||||
}
|
||||
fn is_clause_word(w: ptr) -> bool {
|
||||
function is_clause_word(w: ptr) -> bool {
|
||||
return (w == "phase") or (w == "query") or (w == "reads") or (w == "writes") or (w == "needs") or (w == "uses") or (w == "requires") or (w == "ensures") or (w == "invariant") or (w == "effects")
|
||||
}
|
||||
|
||||
# ---- the lexer: keeps comments, newlines, byte spans; never exits on bad input ----
|
||||
fn is_op2(c0: int, c1: int) -> bool {
|
||||
function is_op2(c0: int, c1: int) -> bool {
|
||||
if c0 == 45 and c1 == 62 { return true } # ->
|
||||
if c1 == 61 and (c0 == 43 or c0 == 45 or c0 == 42 or c0 == 47 or c0 == 61 or c0 == 33 or c0 == 60 or c0 == 62) { return true } # += -= *= /= == != <= >=
|
||||
if c0 == 38 and c1 == 38 { return true } # &&
|
||||
|
|
@ -125,11 +125,11 @@ program LudicFmt {
|
|||
if c0 == 61 and c1 == 62 { return true } # =>
|
||||
return false
|
||||
}
|
||||
fn is_op1(c: int) -> bool {
|
||||
function is_op1(c: int) -> bool {
|
||||
return c == 43 or c == 45 or c == 42 or c == 47 or c == 37 or c == 60 or c == 62 or c == 61 or c == 40 or c == 41 or c == 123 or c == 125 or c == 91 or c == 93 or c == 44 or c == 58 or c == 46 or c == 33 or c == 64 or c == 59
|
||||
}
|
||||
|
||||
fn lex(s: ptr) -> void {
|
||||
function lex(s: ptr) -> void {
|
||||
src = s
|
||||
tk_kind = new []int; tk_start = new []int; tk_end = new []int; tk_line = new []int
|
||||
linestart = new []int
|
||||
|
|
@ -197,19 +197,19 @@ program LudicFmt {
|
|||
}
|
||||
|
||||
# ---- token-stream helpers ----
|
||||
fn next_sig(i: int) -> int {
|
||||
function next_sig(i: int) -> int {
|
||||
var j = i + 1
|
||||
while j < ntok() { let k = tk_kind[j]; if k != LT_NL and k != LT_COMMENT { return j }; j = j + 1 }
|
||||
return 0 - 1
|
||||
}
|
||||
fn prev_sig(i: int) -> int {
|
||||
function prev_sig(i: int) -> int {
|
||||
var j = i - 1
|
||||
while j >= 0 { let k = tk_kind[j]; if k != LT_NL and k != LT_COMMENT { return j }; j = j - 1 }
|
||||
return 0 - 1
|
||||
}
|
||||
fn name_like(k: int) -> bool { return k == LT_ID or k == LT_KW or k == LT_TYPE or k == LT_PHASE or k == LT_BOOL }
|
||||
function name_like(k: int) -> bool { return k == LT_ID or k == LT_KW or k == LT_TYPE or k == LT_PHASE or k == LT_BOOL }
|
||||
# a '.' hugs an operand, a closing bracket, and another dot
|
||||
fn dot_tight(t: int) -> bool {
|
||||
function dot_tight(t: int) -> bool {
|
||||
if name_like(tk_kind[t]) { return true }
|
||||
if tk_kind[t] != LT_OP { return false }
|
||||
let n = tok_len(t); let c0 = src[tk_start[t]]
|
||||
|
|
@ -218,7 +218,7 @@ program LudicFmt {
|
|||
return false
|
||||
}
|
||||
# is the token at index i a unary '-'/'!' rather than a binary operator?
|
||||
fn is_unary(i: int) -> bool {
|
||||
function is_unary(i: int) -> bool {
|
||||
if tk_kind[i] != LT_OP { return false }
|
||||
let n = tok_len(i)
|
||||
if not (n == 1 and (src[tk_start[i]] == 45 or src[tk_start[i]] == 33)) { return false }
|
||||
|
|
@ -234,7 +234,7 @@ program LudicFmt {
|
|||
}
|
||||
|
||||
# whitespace between the previous emitted token (prev) and the current one (cur)
|
||||
fn space_before(prev: int, cur: int) -> int {
|
||||
function space_before(prev: int, cur: int) -> int {
|
||||
if prev < 0 { return 0 }
|
||||
let pk = tk_kind[prev]; let ck = tk_kind[cur]
|
||||
let plen = tok_len(prev); let clen = tok_len(cur)
|
||||
|
|
@ -248,7 +248,7 @@ program LudicFmt {
|
|||
if pk == LT_OP and is_unary(prev) { return 0 }
|
||||
if pk == LT_ANNO and c1 and c0 == 40 { return 0 } # @anno(
|
||||
if c1 and c0 == 40 and ck == LT_OP {
|
||||
if pk == LT_ID or pk == LT_TYPE or pk == LT_PHASE { return 0 } # fn move( / clear(
|
||||
if pk == LT_ID or pk == LT_TYPE or pk == LT_PHASE { return 0 } # function move( / clear(
|
||||
if pk == LT_OP { if p1 and (p0 == 41 or p0 == 93) { return 1 }; return 0 }
|
||||
return 1
|
||||
}
|
||||
|
|
@ -256,7 +256,7 @@ program LudicFmt {
|
|||
}
|
||||
|
||||
# ---- the formatting pass ----
|
||||
fn format(indent_width: int) -> Buf {
|
||||
function format(indent_width: int) -> Buf {
|
||||
let o = buf_new()
|
||||
let stack = words(600)
|
||||
let hang = words(600)
|
||||
|
|
@ -394,7 +394,7 @@ program LudicFmt {
|
|||
var g_info: int = 0
|
||||
var g_marker: int = 0
|
||||
# detect a fence at line offset i; sets g_flen/g_info/g_marker; returns bool
|
||||
fn md_fence_at(s: ptr, i: int) -> bool {
|
||||
function md_fence_at(s: ptr, i: int) -> bool {
|
||||
var j = i; while s[j] == 32 { j = j + 1 }
|
||||
let m = s[j]
|
||||
if m != 96 and m != 126 { return false } # ` or ~
|
||||
|
|
@ -403,16 +403,16 @@ program LudicFmt {
|
|||
g_flen = n; g_info = j; g_marker = m
|
||||
return true
|
||||
}
|
||||
fn md_info_is_ludic(s: ptr, at: int) -> bool {
|
||||
function md_info_is_ludic(s: ptr, at: int) -> bool {
|
||||
var a = at; while s[a] == 32 or s[a] == 9 { a = a + 1 }
|
||||
# case-insensitive "ludic"
|
||||
if not (((s[a] == 108 or s[a] == 76)) and ((s[a + 1] == 117 or s[a + 1] == 85)) and ((s[a + 2] == 100 or s[a + 2] == 68)) and ((s[a + 3] == 105 or s[a + 3] == 73)) and ((s[a + 4] == 99 or s[a + 4] == 67))) { return false }
|
||||
let af = s[a + 5]
|
||||
return af == 0 or af == 10 or af == 32 or af == 9 or af == 13
|
||||
}
|
||||
fn line_end(s: ptr, i: int) -> int { var e = i; while s[e] != 0 and s[e] != 10 { e = e + 1 }; return e }
|
||||
function line_end(s: ptr, i: int) -> int { var e = i; while s[e] != 0 and s[e] != 10 { e = e + 1 }; return e }
|
||||
|
||||
fn format_markdown(s: ptr, indent_width: int) -> Buf {
|
||||
function format_markdown(s: ptr, indent_width: int) -> Buf {
|
||||
let o = buf_new()
|
||||
var i = 0
|
||||
while s[i] != 0 {
|
||||
|
|
@ -478,14 +478,14 @@ program LudicFmt {
|
|||
}
|
||||
|
||||
# ---- ends-with helpers for extension detection ----
|
||||
fn ends_with(s: ptr, suf: ptr) -> bool {
|
||||
function ends_with(s: ptr, suf: ptr) -> bool {
|
||||
let n = cstr_len(s); let m = cstr_len(suf)
|
||||
if m > n { return false }
|
||||
return (s[n - m..n] == suf)
|
||||
}
|
||||
|
||||
# ---- stdin slurp (for `-`) ----
|
||||
fn slurp_stdin() -> ptr {
|
||||
function slurp_stdin() -> ptr {
|
||||
let b = buf_new()
|
||||
var c = read_char()
|
||||
while c >= 0 { buf_putc(b, c); c = read_char() }
|
||||
|
|
@ -493,14 +493,14 @@ program LudicFmt {
|
|||
}
|
||||
|
||||
# format one source string according to its kind (markdown vs ludic)
|
||||
fn format_source(text: ptr, is_md: bool, indent_width: int) -> ptr {
|
||||
function format_source(text: ptr, is_md: bool, indent_width: int) -> ptr {
|
||||
if is_md { let m = format_markdown(text, indent_width); return buf_str(m) }
|
||||
lex(text)
|
||||
let f = format(indent_width)
|
||||
return buf_str(f)
|
||||
}
|
||||
|
||||
fn streq(a: ptr, b: ptr) -> bool { return (a == b) }
|
||||
function streq(a: ptr, b: ptr) -> bool { return (a == b) }
|
||||
|
||||
entry {
|
||||
var write = false
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ program LudicLsp {
|
|||
const SC_UNKNOWN: int = 24
|
||||
|
||||
# ---- tiny helpers ----
|
||||
fn read_file(path: ptr) -> ptr {
|
||||
function read_file(path: ptr) -> ptr {
|
||||
let f = file_open(path, "rb")
|
||||
if (f == null) { return null }
|
||||
file_seek(f, 0, 2)
|
||||
|
|
@ -84,16 +84,16 @@ program LudicLsp {
|
|||
file_close(f)
|
||||
return buf
|
||||
}
|
||||
fn cstr_len(s: ptr) -> int { var n = 0; while s[n] != 0 { n = n + 1 }; return n }
|
||||
fn char_is_digit(c: int) -> bool { return c >= 48 and c <= 57 }
|
||||
fn char_is_alpha(c: int) -> bool {
|
||||
function cstr_len(s: ptr) -> int { var n = 0; while s[n] != 0 { n = n + 1 }; return n }
|
||||
function char_is_digit(c: int) -> bool { return c >= 48 and c <= 57 }
|
||||
function char_is_alpha(c: int) -> bool {
|
||||
if c >= 65 and c <= 90 { return true }
|
||||
if c >= 97 and c <= 122 { return true }
|
||||
return c == 95
|
||||
}
|
||||
fn char_is_alnum(c: int) -> bool { return char_is_alpha(c) or char_is_digit(c) }
|
||||
fn char_is_hex(c: int) -> bool { return char_is_digit(c) or (c >= 97 and c <= 102) or (c >= 65 and c <= 70) }
|
||||
fn itoa(v: int) -> ptr {
|
||||
function char_is_alnum(c: int) -> bool { return char_is_alpha(c) or char_is_digit(c) }
|
||||
function char_is_hex(c: int) -> bool { return char_is_digit(c) or (c >= 97 and c <= 102) or (c >= 65 and c <= 70) }
|
||||
function itoa(v: int) -> ptr {
|
||||
if v == 0 { let z = bytes(2); z[0] = 48; z[1] = 0; return z }
|
||||
var neg = false; var x = v
|
||||
if x < 0 { neg = true; x = 0 - x }
|
||||
|
|
@ -108,7 +108,7 @@ program LudicLsp {
|
|||
out[total] = 0
|
||||
return out
|
||||
}
|
||||
fn atoi(s: ptr) -> int {
|
||||
function atoi(s: ptr) -> int {
|
||||
var n = 0; var i = 0; var neg = false
|
||||
while s[i] == 32 or s[i] == 9 { i = i + 1 }
|
||||
if s[i] == 45 { neg = true; i = i + 1 }
|
||||
|
|
@ -119,27 +119,27 @@ program LudicLsp {
|
|||
|
||||
# ---- growable byte buffer ----
|
||||
property Buf { data: ptr = null, len: int = 0, cap: int = 0 }
|
||||
fn buf_new() -> Buf { let b = new Buf; b.cap = 256; b.data = bytes(b.cap); b.len = 0; return b }
|
||||
fn buf_ensure(b: Buf, extra: int) -> void {
|
||||
function buf_new() -> Buf { let b = new Buf; b.cap = 256; b.data = bytes(b.cap); b.len = 0; return b }
|
||||
function buf_ensure(b: Buf, extra: int) -> void {
|
||||
if b.len + extra + 1 <= b.cap { return }
|
||||
while b.len + extra + 1 > b.cap { b.cap = b.cap * 2 }
|
||||
b.data = resize(b.data, b.cap)
|
||||
}
|
||||
fn buf_putc(b: Buf, c: int) -> void { buf_ensure(b, 1); b.data[b.len] = c; b.len = b.len + 1 }
|
||||
fn buf_puts(b: Buf, s: ptr) -> void { var i = 0; while s[i] != 0 { buf_putc(b, s[i]); i = i + 1 } }
|
||||
fn buf_puti(b: Buf, n: int) -> void { buf_puts(b, itoa(n)) }
|
||||
fn buf_addrange(b: Buf, s: ptr, a: int, e: int) -> void { var k = a; while k < e { buf_putc(b, s[k]); k = k + 1 } }
|
||||
fn buf_str(b: Buf) -> ptr { b.data[b.len] = 0; return b.data }
|
||||
function buf_putc(b: Buf, c: int) -> void { buf_ensure(b, 1); b.data[b.len] = c; b.len = b.len + 1 }
|
||||
function buf_puts(b: Buf, s: ptr) -> void { var i = 0; while s[i] != 0 { buf_putc(b, s[i]); i = i + 1 } }
|
||||
function buf_puti(b: Buf, n: int) -> void { buf_puts(b, itoa(n)) }
|
||||
function buf_addrange(b: Buf, s: ptr, a: int, e: int) -> void { var k = a; while k < e { buf_putc(b, s[k]); k = k + 1 } }
|
||||
function buf_str(b: Buf) -> ptr { b.data[b.len] = 0; return b.data }
|
||||
|
||||
# ---- string helpers ----
|
||||
fn streq(a: ptr, b: ptr) -> bool { return (a == b) }
|
||||
fn concat3(a: ptr, b: ptr, c: ptr) -> ptr { return ((a + b) + c) }
|
||||
fn ends_with(s: ptr, suf: ptr) -> bool {
|
||||
function streq(a: ptr, b: ptr) -> bool { return (a == b) }
|
||||
function concat3(a: ptr, b: ptr, c: ptr) -> ptr { return ((a + b) + c) }
|
||||
function ends_with(s: ptr, suf: ptr) -> bool {
|
||||
let n = cstr_len(s); let m = cstr_len(suf)
|
||||
if m > n { return false }
|
||||
return (s[n - m..n] == suf)
|
||||
}
|
||||
fn starts_with(s: ptr, pre: ptr) -> bool {
|
||||
function starts_with(s: ptr, pre: ptr) -> bool {
|
||||
let m = cstr_len(pre)
|
||||
if m > cstr_len(s) { return false }
|
||||
return (s[0..m] == pre)
|
||||
|
|
@ -191,7 +191,7 @@ program LudicLsp {
|
|||
var g_shutdown: int = 0
|
||||
|
||||
# ---- the lexer, over a Doc ----
|
||||
fn is_op2(c0: int, c1: int) -> bool {
|
||||
function is_op2(c0: int, c1: int) -> bool {
|
||||
if c0 == 45 and c1 == 62 { return true }
|
||||
if c1 == 61 and (c0 == 43 or c0 == 45 or c0 == 42 or c0 == 47 or c0 == 61 or c0 == 33 or c0 == 60 or c0 == 62) { return true }
|
||||
if c0 == 38 and c1 == 38 { return true }
|
||||
|
|
@ -200,29 +200,29 @@ program LudicLsp {
|
|||
if c0 == 61 and c1 == 62 { return true }
|
||||
return false
|
||||
}
|
||||
fn is_op1(c: int) -> bool {
|
||||
function is_op1(c: int) -> bool {
|
||||
return c == 43 or c == 45 or c == 42 or c == 47 or c == 37 or c == 60 or c == 62 or c == 61 or c == 40 or c == 41 or c == 123 or c == 125 or c == 91 or c == 93 or c == 44 or c == 58 or c == 46 or c == 33 or c == 64 or c == 59
|
||||
}
|
||||
fn is_type_word(w: ptr) -> bool {
|
||||
function is_type_word(w: ptr) -> bool {
|
||||
return (w == "int") or (w == "long") or (w == "fixed") or (w == "bool") or (w == "entity") or (w == "str") or (w == "ptr") or (w == "byte") or (w == "words") or (w == "fixeds") or (w == "ptrs") or (w == "void")
|
||||
}
|
||||
fn is_phase_word(w: ptr) -> bool {
|
||||
function is_phase_word(w: ptr) -> bool {
|
||||
return (w == "Start") or (w == "Input") or (w == "FixedUpdate") or (w == "Update") or (w == "LateUpdate") or (w == "Render")
|
||||
}
|
||||
fn is_keyword_word(w: ptr) -> bool {
|
||||
function is_keyword_word(w: ptr) -> bool {
|
||||
if (w == "program") or (w == "import") or (w == "property") or (w == "model") or (w == "enum") or (w == "ui") { return true }
|
||||
if (w == "const") or (w == "var") or (w == "fn") or (w == "extern") or (w == "handler") or (w == "entry") or (w == "event") or (w == "scene") { return true }
|
||||
if (w == "const") or (w == "var") or (w == "function") or (w == "extern") or (w == "handler") or (w == "entry") or (w == "event") or (w == "scene") { return true }
|
||||
if (w == "phase") or (w == "query") or (w == "on") or (w == "cancellable") or (w == "public") or (w == "layer") or (w == "start") { return true }
|
||||
if (w == "let") or (w == "return") or (w == "if") or (w == "else") or (w == "while") or (w == "for") or (w == "in") or (w == "spawn") or (w == "despawn") { return true }
|
||||
if (w == "enable") or (w == "disable") or (w == "match") or (w == "machine") or (w == "state") or (w == "become") or (w == "where") { return true }
|
||||
if (w == "and") or (w == "or") or (w == "not") or (w == "break") or (w == "continue") or (w == "new") or (w == "emit") or (w == "cancel") { return true }
|
||||
return false
|
||||
}
|
||||
fn is_widget_word(w: ptr) -> bool {
|
||||
function is_widget_word(w: ptr) -> bool {
|
||||
return (w == "panel") or (w == "col") or (w == "row") or (w == "label") or (w == "button") or (w == "image") or (w == "spacer")
|
||||
}
|
||||
|
||||
fn lex_doc(D: Doc) -> void {
|
||||
function lex_doc(D: Doc) -> void {
|
||||
let s = D.src
|
||||
D.tk_kind = new []int; D.tk_start = new []int; D.tk_end = new []int; D.tk_line = new []int
|
||||
D.linestart = new []int
|
||||
|
|
@ -265,15 +265,15 @@ program LudicLsp {
|
|||
push(D.tk_kind, LT_EOF); push(D.tk_start, i); push(D.tk_end, i); push(D.tk_line, line)
|
||||
}
|
||||
|
||||
fn ntok(D: Doc) -> int { return len(D.tk_kind) }
|
||||
fn tlen(D: Doc, i: int) -> int { return D.tk_end[i] - D.tk_start[i] }
|
||||
fn ttext(D: Doc, i: int) -> ptr { return D.src[D.tk_start[i]..D.tk_end[i]] }
|
||||
fn tis(D: Doc, i: int, s: ptr) -> bool { if i < 0 or i >= ntok(D) { return false }; return (ttext(D, i) == s) }
|
||||
fn next_sig(D: Doc, i: int) -> int { var j = i + 1; while j < ntok(D) { let k = D.tk_kind[j]; if k != LT_NL and k != LT_COMMENT { return j }; j = j + 1 }; return 0 - 1 }
|
||||
fn prev_sig(D: Doc, i: int) -> int { var j = i - 1; while j >= 0 { let k = D.tk_kind[j]; if k != LT_NL and k != LT_COMMENT { return j }; j = j - 1 }; return 0 - 1 }
|
||||
function ntok(D: Doc) -> int { return len(D.tk_kind) }
|
||||
function tlen(D: Doc, i: int) -> int { return D.tk_end[i] - D.tk_start[i] }
|
||||
function ttext(D: Doc, i: int) -> ptr { return D.src[D.tk_start[i]..D.tk_end[i]] }
|
||||
function tis(D: Doc, i: int, s: ptr) -> bool { if i < 0 or i >= ntok(D) { return false }; return (ttext(D, i) == s) }
|
||||
function next_sig(D: Doc, i: int) -> int { var j = i + 1; while j < ntok(D) { let k = D.tk_kind[j]; if k != LT_NL and k != LT_COMMENT { return j }; j = j + 1 }; return 0 - 1 }
|
||||
function prev_sig(D: Doc, i: int) -> int { var j = i - 1; while j >= 0 { let k = D.tk_kind[j]; if k != LT_NL and k != LT_COMMENT { return j }; j = j - 1 }; return 0 - 1 }
|
||||
|
||||
# ---- positions (UTF-16 for LSP) ----
|
||||
fn utf16_len(s: ptr, a: int, nbytes: int) -> int {
|
||||
function utf16_len(s: ptr, a: int, nbytes: int) -> int {
|
||||
var u = 0; var i = 0
|
||||
while i < nbytes {
|
||||
let c = s[a + i]
|
||||
|
|
@ -284,13 +284,13 @@ program LudicLsp {
|
|||
}
|
||||
return u
|
||||
}
|
||||
fn line_of(D: Doc, off: int) -> int {
|
||||
function line_of(D: Doc, off: int) -> int {
|
||||
var lo = 0; var hi = len(D.linestart) - 1
|
||||
while lo < hi { let mid = (lo + hi + 1) / 2; if D.linestart[mid] <= off { lo = mid } else { hi = mid - 1 } }
|
||||
return lo
|
||||
}
|
||||
fn col_of(D: Doc, off: int) -> int { let l = line_of(D, off); return utf16_len(D.text, D.linestart[l], off - D.linestart[l]) }
|
||||
fn offset_of(D: Doc, line: int, ch: int) -> int {
|
||||
function col_of(D: Doc, off: int) -> int { let l = line_of(D, off); return utf16_len(D.text, D.linestart[l], off - D.linestart[l]) }
|
||||
function offset_of(D: Doc, line: int, ch: int) -> int {
|
||||
if line < 0 { return 0 }
|
||||
if line >= len(D.linestart) { return cstr_len(D.text) }
|
||||
var off = D.linestart[line]
|
||||
|
|
@ -306,7 +306,7 @@ program LudicLsp {
|
|||
}
|
||||
return off
|
||||
}
|
||||
fn tok_at(D: Doc, off: int) -> int {
|
||||
function tok_at(D: Doc, off: int) -> int {
|
||||
var touching = 0 - 1
|
||||
var i = 0
|
||||
let N = ntok(D)
|
||||
|
|
@ -323,7 +323,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- brace matching ----
|
||||
fn match_braces(D: Doc) -> void {
|
||||
function match_braces(D: Doc) -> void {
|
||||
let N = ntok(D)
|
||||
D.tmatch = new []int
|
||||
var i = 0
|
||||
|
|
@ -342,7 +342,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- doc comment: the comment block directly above a declaration ----
|
||||
fn collect_doc(D: Doc, decl_tok: int) -> ptr {
|
||||
function collect_doc(D: Doc, decl_tok: int) -> ptr {
|
||||
var line = D.tk_line[decl_tok]
|
||||
var first = 0 - 1
|
||||
var i = decl_tok - 1
|
||||
|
|
@ -379,17 +379,17 @@ program LudicLsp {
|
|||
var pD: Doc = null
|
||||
var pi: int = 0
|
||||
|
||||
fn pk() -> int { if pi < ntok(pD) { return pD.tk_kind[pi] }; return LT_EOF }
|
||||
fn pis(s: ptr) -> bool { return tis(pD, pi, s) }
|
||||
fn pskipnl() -> void { while pi < ntok(pD) and (pk() == LT_NL or pk() == LT_COMMENT) { pi = pi + 1 } }
|
||||
fn padv() -> void { if pi < ntok(pD) { pi = pi + 1 }; pskipnl() }
|
||||
fn pword() -> ptr { if pi < ntok(pD) { return ttext(pD, pi) }; return "" }
|
||||
fn pname() -> int {
|
||||
function pk() -> int { if pi < ntok(pD) { return pD.tk_kind[pi] }; return LT_EOF }
|
||||
function pis(s: ptr) -> bool { return tis(pD, pi, s) }
|
||||
function pskipnl() -> void { while pi < ntok(pD) and (pk() == LT_NL or pk() == LT_COMMENT) { pi = pi + 1 } }
|
||||
function padv() -> void { if pi < ntok(pD) { pi = pi + 1 }; pskipnl() }
|
||||
function pword() -> ptr { if pi < ntok(pD) { return ttext(pD, pi) }; return "" }
|
||||
function pname() -> int {
|
||||
let k = pk()
|
||||
if k == LT_ID or k == LT_TYPE or k == LT_PHASE or k == LT_KW or k == LT_BOOL { let t = pi; padv(); return t }
|
||||
return 0 - 1
|
||||
}
|
||||
fn sym_add(kind: int, nametok: int, parent: int) -> int {
|
||||
function sym_add(kind: int, nametok: int, parent: int) -> int {
|
||||
let s = new Sym
|
||||
s.kind = kind; s.tok = nametok; s.parent = parent
|
||||
s.name = ""; s.ty = ""; s.detail = ""; s.doc = ""
|
||||
|
|
@ -398,11 +398,11 @@ program LudicLsp {
|
|||
push(pD.syms, s)
|
||||
return len(pD.syms) - 1
|
||||
}
|
||||
fn cur_match() -> int { if pi < ntok(pD) { return pD.tmatch[pi] }; return 0 - 1 }
|
||||
fn skip_group() -> void { let m = cur_match(); if m < 0 { padv(); return }; pi = m + 1; pskipnl() }
|
||||
function cur_match() -> int { if pi < ntok(pD) { return pD.tmatch[pi] }; return 0 - 1 }
|
||||
function skip_group() -> void { let m = cur_match(); if m < 0 { padv(); return }; pi = m + 1; pskipnl() }
|
||||
|
||||
# `name: Type` pairs inside ( ) — record params, build the signature into sig
|
||||
fn lp_params(owner: int, kind: int, sig: Buf) -> void {
|
||||
function lp_params(owner: int, kind: int, sig: Buf) -> void {
|
||||
if not pis("(") { return }
|
||||
let close = cur_match()
|
||||
buf_putc(sig, 40) # '('
|
||||
|
|
@ -428,7 +428,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# (a, b) variable list -> token indices into out; returns count
|
||||
fn lp_varlist(out: []int) -> int {
|
||||
function lp_varlist(out: []int) -> int {
|
||||
var n = 0
|
||||
if not pis("(") { return 0 }
|
||||
let close = cur_match()
|
||||
|
|
@ -446,7 +446,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# [Pos, Vel, {Enemy}] — bind listed components to vars in order (skip {Tag})
|
||||
fn lp_query_terms(owner: int, vars: []int, nvars: int, scope_start: int, scope_end: int) -> void {
|
||||
function lp_query_terms(owner: int, vars: []int, nvars: int, scope_start: int, scope_end: int) -> void {
|
||||
if not pis("[") { return }
|
||||
let close = cur_match()
|
||||
padv()
|
||||
|
|
@ -470,7 +470,7 @@ program LudicLsp {
|
|||
if pis("where") { padv() }
|
||||
}
|
||||
|
||||
fn lp_block(owner: int, scope_end: int) -> void {
|
||||
function lp_block(owner: int, scope_end: int) -> void {
|
||||
if not pis("{") { return }
|
||||
let close = cur_match()
|
||||
var endo = scope_end
|
||||
|
|
@ -487,7 +487,7 @@ program LudicLsp {
|
|||
if close >= 0 { pi = close + 1 } else { padv() }
|
||||
pskipnl()
|
||||
}
|
||||
fn lp_stmt(owner: int, scope_end: int) -> void {
|
||||
function lp_stmt(owner: int, scope_end: int) -> void {
|
||||
if pis("let") {
|
||||
padv(); let nt = pname(); if nt < 0 { return }
|
||||
var ty = ""
|
||||
|
|
@ -522,7 +522,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# panel id=Root w=288 { … } — each id= mints a UI_<Name> handle
|
||||
fn lp_widget(owner: int, uisym: int) -> void {
|
||||
function lp_widget(owner: int, uisym: int) -> void {
|
||||
let type_tok = pname()
|
||||
if type_tok < 0 { return }
|
||||
while pi < ntok(pD) and pk() == LT_ID {
|
||||
|
|
@ -565,7 +565,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# one top-level declaration
|
||||
fn lp_decl(parent: int) -> void {
|
||||
function lp_decl(parent: int) -> void {
|
||||
let w = pword()
|
||||
let decl_tok = pi
|
||||
|
||||
|
|
@ -638,11 +638,11 @@ program LudicLsp {
|
|||
return
|
||||
}
|
||||
if w == "extern" {
|
||||
padv(); if pis("fn") { padv() }
|
||||
padv(); if pis("function") { padv() }
|
||||
let nt = pname(); if nt < 0 { return }
|
||||
let sidx = sym_add(LS_EXTERN, nt, parent)
|
||||
pD.syms[sidx].doc = collect_doc(pD, decl_tok)
|
||||
let sig = buf_new(); buf_puts(sig, "extern fn "); buf_puts(sig, pD.syms[sidx].name)
|
||||
let sig = buf_new(); buf_puts(sig, "extern function "); buf_puts(sig, pD.syms[sidx].name)
|
||||
lp_params(sidx, LS_PARAM, sig)
|
||||
if pis("->") { padv(); let tt = pname(); if tt >= 0 { pD.syms[sidx].ty = ttext(pD, tt); buf_puts(sig, " -> "); buf_puts(sig, pD.syms[sidx].ty) } }
|
||||
pD.syms[sidx].detail = buf_str(sig)
|
||||
|
|
@ -650,10 +650,10 @@ program LudicLsp {
|
|||
pskipnl()
|
||||
return
|
||||
}
|
||||
if (w == "fn") or (w == "pure") or (w == "export") {
|
||||
if (w == "function") or (w == "pure") or (w == "export") {
|
||||
let exported = (w == "export")
|
||||
if (w == "pure") or exported { padv(); if pis("pure") { padv() } }
|
||||
if pis("fn") { padv() }
|
||||
if pis("function") { padv() }
|
||||
let nt = pname(); if nt < 0 { return }
|
||||
let sidx = sym_add(LS_FN, nt, parent)
|
||||
if exported { pD.syms[sidx].exported = 1 }
|
||||
|
|
@ -795,7 +795,7 @@ program LudicLsp {
|
|||
padv()
|
||||
}
|
||||
|
||||
fn parse_doc(D: Doc) -> void {
|
||||
function parse_doc(D: Doc) -> void {
|
||||
D.syms = new []Sym
|
||||
D.imports = new []ptr
|
||||
D.is_unit = 0; D.is_module = 0; D.unit = ""
|
||||
|
|
@ -833,22 +833,22 @@ program LudicLsp {
|
|||
# ============================================================================
|
||||
# workspace: many documents, one project
|
||||
# ============================================================================
|
||||
fn is_local_kind(k: int) -> bool { return k == LS_LOCAL or k == LS_PARAM or k == LS_QUERYVAR or k == LS_STATE }
|
||||
fn is_toplevel_kind(k: int) -> bool {
|
||||
function is_local_kind(k: int) -> bool { return k == LS_LOCAL or k == LS_PARAM or k == LS_QUERYVAR or k == LS_STATE }
|
||||
function is_toplevel_kind(k: int) -> bool {
|
||||
return k == LS_COMPONENT or k == LS_ARCHETYPE or k == LS_CONST or k == LS_VAR or k == LS_FN or k == LS_EXTERN or k == LS_SYSTEM or k == LS_UI or k == LS_WIDGET or k == LS_SCENE or k == LS_LAYER or k == LS_UNIT
|
||||
}
|
||||
fn is_outline_kind(k: int) -> bool {
|
||||
function is_outline_kind(k: int) -> bool {
|
||||
return k == LS_UNIT or k == LS_COMPONENT or k == LS_FIELD or k == LS_ARCHETYPE or k == LS_CONST or k == LS_VAR or k == LS_FN or k == LS_EXTERN or k == LS_SYSTEM or k == LS_UI or k == LS_WIDGET or k == LS_SCENE or k == LS_LAYER
|
||||
}
|
||||
|
||||
# ---- paths & URIs ----
|
||||
fn hexval(c: int) -> int {
|
||||
function hexval(c: int) -> int {
|
||||
if c >= 48 and c <= 57 { return c - 48 }
|
||||
let l = c | 32
|
||||
if l >= 97 and l <= 102 { return l - 97 + 10 }
|
||||
return 0 - 1
|
||||
}
|
||||
fn uri_to_path(uri: ptr) -> ptr {
|
||||
function uri_to_path(uri: ptr) -> ptr {
|
||||
var s = uri
|
||||
if starts_with(uri, "file://") { s = uri[7..cstr_len(uri)] }
|
||||
let n = cstr_len(s)
|
||||
|
|
@ -861,12 +861,12 @@ program LudicLsp {
|
|||
out[j] = 0
|
||||
return out
|
||||
}
|
||||
fn uri_safe(c: int) -> bool {
|
||||
function uri_safe(c: int) -> bool {
|
||||
if char_is_alnum(c) { return true }
|
||||
return c == 45 or c == 46 or c == 126 or c == 47 # - . ~ /
|
||||
}
|
||||
fn hexch(n: int) -> int { if n < 10 { return 48 + n }; return 55 + n }
|
||||
fn path_to_uri(path: ptr) -> ptr {
|
||||
function hexch(n: int) -> int { if n < 10 { return 48 + n }; return 55 + n }
|
||||
function path_to_uri(path: ptr) -> ptr {
|
||||
let out = buf_new()
|
||||
buf_puts(out, "file://")
|
||||
var i = 0
|
||||
|
|
@ -878,7 +878,7 @@ program LudicLsp {
|
|||
}
|
||||
return buf_str(out)
|
||||
}
|
||||
fn dirname(p: ptr) -> ptr {
|
||||
function dirname(p: ptr) -> ptr {
|
||||
var last = 0 - 1
|
||||
var i = 0
|
||||
while p[i] != 0 { if p[i] == 47 { last = i }; i = i + 1 }
|
||||
|
|
@ -886,7 +886,7 @@ program LudicLsp {
|
|||
if last == 0 { return "/" }
|
||||
return p[0..last]
|
||||
}
|
||||
fn normpath(p: ptr) -> ptr {
|
||||
function normpath(p: ptr) -> ptr {
|
||||
let n = cstr_len(p)
|
||||
let abs = (n > 0 and p[0] == 47)
|
||||
let list = new []ptr
|
||||
|
|
@ -914,16 +914,16 @@ program LudicLsp {
|
|||
while k < cnt { if k > 0 { buf_putc(out, 47) }; buf_puts(out, list[k]); k = k + 1 }
|
||||
return buf_str(out)
|
||||
}
|
||||
fn join_path(dir: ptr, rel: ptr) -> ptr {
|
||||
function join_path(dir: ptr, rel: ptr) -> ptr {
|
||||
if rel[0] == 47 { return normpath(rel) }
|
||||
return normpath(concat3(dir, "/", rel))
|
||||
}
|
||||
|
||||
# ---- markdown scrub: blank everything outside a ```ludic fence ----
|
||||
fn word_ludic_at(s: ptr, at: int) -> bool {
|
||||
function word_ludic_at(s: ptr, at: int) -> bool {
|
||||
return ((s[at] == 108 or s[at] == 76)) and ((s[at + 1] == 117 or s[at + 1] == 85)) and ((s[at + 2] == 100 or s[at + 2] == 68)) and ((s[at + 3] == 105 or s[at + 3] == 73)) and ((s[at + 4] == 99 or s[at + 4] == 67))
|
||||
}
|
||||
fn scrub_markdown(src: ptr) -> ptr {
|
||||
function scrub_markdown(src: ptr) -> ptr {
|
||||
let n = cstr_len(src)
|
||||
let out = bytes(n + 1)
|
||||
var c0 = 0
|
||||
|
|
@ -974,12 +974,12 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- documents ----
|
||||
fn set_text(D: Doc, raw: ptr) -> void {
|
||||
function set_text(D: Doc, raw: ptr) -> void {
|
||||
D.raw = raw
|
||||
if D.is_md == 1 { D.text = scrub_markdown(raw) } else { D.text = raw }
|
||||
D.src = D.text
|
||||
}
|
||||
fn reindex(D: Doc) -> void {
|
||||
function reindex(D: Doc) -> void {
|
||||
lex_doc(D)
|
||||
match_braces(D)
|
||||
parse_doc(D)
|
||||
|
|
@ -988,7 +988,7 @@ program LudicLsp {
|
|||
let N = ntok(D)
|
||||
while i < N { push(D.cls, SC_NONE); i = i + 1 }
|
||||
}
|
||||
fn new_doc(path: ptr, text: ptr) -> Doc {
|
||||
function new_doc(path: ptr, text: ptr) -> Doc {
|
||||
let D = new Doc
|
||||
D.path = normpath(path)
|
||||
D.uri = path_to_uri(D.path)
|
||||
|
|
@ -999,13 +999,13 @@ program LudicLsp {
|
|||
reindex(D)
|
||||
return D
|
||||
}
|
||||
fn by_path(path: ptr) -> Doc {
|
||||
function by_path(path: ptr) -> Doc {
|
||||
let p = normpath(path)
|
||||
var i = 0
|
||||
while i < len(g_docs) { if (g_docs[i].path == p) { return g_docs[i] }; i = i + 1 }
|
||||
return null
|
||||
}
|
||||
fn by_uri(uri: ptr) -> Doc {
|
||||
function by_uri(uri: ptr) -> Doc {
|
||||
let p = uri_to_path(uri)
|
||||
let D = by_path(p)
|
||||
if (D != null) { return D }
|
||||
|
|
@ -1013,7 +1013,7 @@ program LudicLsp {
|
|||
while i < len(g_docs) { if (g_docs[i].uri == uri) { return g_docs[i] }; i = i + 1 }
|
||||
return null
|
||||
}
|
||||
fn ensure_doc(path: ptr) -> Doc {
|
||||
function ensure_doc(path: ptr) -> Doc {
|
||||
let D = by_path(path)
|
||||
if (D != null) { return D }
|
||||
let text = read_file(path)
|
||||
|
|
@ -1023,7 +1023,7 @@ program LudicLsp {
|
|||
return nd
|
||||
}
|
||||
|
||||
fn scan_root(root: ptr) -> void {
|
||||
function scan_root(root: ptr) -> void {
|
||||
if (root == null) { return }
|
||||
let listfile = "/tmp/ludic_lsp_scan.txt"
|
||||
run(concat3(concat3("find '", root, "' -name '*.ludic' -not -path '*/build/*' -not -path '*/node_modules/*' -not -path '*/.*' > "), listfile, " 2>/dev/null"))
|
||||
|
|
@ -1041,7 +1041,7 @@ program LudicLsp {
|
|||
var g_owner: Doc = null
|
||||
var g_symidx: int = 0
|
||||
|
||||
fn imports_of(D: Doc, out: []Doc) -> void {
|
||||
function imports_of(D: Doc, out: []Doc) -> void {
|
||||
var i = 0
|
||||
while i < len(D.imports) {
|
||||
let full = join_path(dirname(D.path), D.imports[i])
|
||||
|
|
@ -1055,7 +1055,7 @@ program LudicLsp {
|
|||
i = i + 1
|
||||
}
|
||||
}
|
||||
fn related(D: Doc) -> []Doc {
|
||||
function related(D: Doc) -> []Doc {
|
||||
let out = new []Doc
|
||||
if (D == null) { return out }
|
||||
push(out, D)
|
||||
|
|
@ -1088,7 +1088,7 @@ program LudicLsp {
|
|||
}
|
||||
return out
|
||||
}
|
||||
fn unit_root(D: Doc) -> Doc {
|
||||
function unit_root(D: Doc) -> Doc {
|
||||
if (D == null) or D.is_md == 1 { return null }
|
||||
if D.is_unit == 1 { return D }
|
||||
let rel = related(D)
|
||||
|
|
@ -1096,7 +1096,7 @@ program LudicLsp {
|
|||
while i < len(rel) { if rel[i].is_unit == 1 and rel[i].is_md == 0 { return rel[i] }; i = i + 1 }
|
||||
return null
|
||||
}
|
||||
fn find_local(D: Doc, name: ptr, off: int) -> int {
|
||||
function find_local(D: Doc, name: ptr, off: int) -> int {
|
||||
var best = 0 - 1
|
||||
var i = 0
|
||||
while i < len(D.syms) {
|
||||
|
|
@ -1109,7 +1109,7 @@ program LudicLsp {
|
|||
return best
|
||||
}
|
||||
# returns sym index in g_owner (set), or -1
|
||||
fn find_top(D: Doc, name: ptr) -> int {
|
||||
function find_top(D: Doc, name: ptr) -> int {
|
||||
let rel = related(D)
|
||||
var i = 0
|
||||
while i < len(rel) {
|
||||
|
|
@ -1132,7 +1132,7 @@ program LudicLsp {
|
|||
return 0 - 1
|
||||
}
|
||||
# component sym index (in g_owner) for a dotted receiver, or -1
|
||||
fn receiver_component(D: Doc, dot_tok: int) -> int {
|
||||
function receiver_component(D: Doc, dot_tok: int) -> int {
|
||||
let recv = prev_sig(D, dot_tok)
|
||||
if recv < 0 or D.tk_kind[recv] != LT_ID { return 0 - 1 }
|
||||
let name = ttext(D, recv)
|
||||
|
|
@ -1145,7 +1145,7 @@ program LudicLsp {
|
|||
return find_top(D, ty)
|
||||
}
|
||||
# resolve the token: sets g_owner/g_symidx; returns the token index (or -1)
|
||||
fn resolve_tok(D: Doc, tok: int) -> int {
|
||||
function resolve_tok(D: Doc, tok: int) -> int {
|
||||
g_owner = null; g_symidx = 0 - 1
|
||||
if tok < 0 { return 0 - 1 }
|
||||
if D.tk_kind[tok] != LT_ID and D.tk_kind[tok] != LT_STR { return 0 - 1 }
|
||||
|
|
@ -1173,10 +1173,10 @@ program LudicLsp {
|
|||
g_owner = null; g_symidx = 0 - 1
|
||||
return tok
|
||||
}
|
||||
fn resolve_at(D: Doc, off: int) -> int { return resolve_tok(D, tok_at(D, off)) }
|
||||
function resolve_at(D: Doc, off: int) -> int { return resolve_tok(D, tok_at(D, off)) }
|
||||
|
||||
fn first_on_line(D: Doc, tok: int) -> bool { let p = prev_sig(D, tok); return p < 0 or D.tk_line[p] != D.tk_line[tok] }
|
||||
fn in_ui_body(D: Doc, off: int) -> bool {
|
||||
function first_on_line(D: Doc, tok: int) -> bool { let p = prev_sig(D, tok); return p < 0 or D.tk_line[p] != D.tk_line[tok] }
|
||||
function in_ui_body(D: Doc, off: int) -> bool {
|
||||
var i = 0
|
||||
while i < len(D.syms) { if D.syms[i].kind == LS_UI and off > D.syms[i].bstart and off < D.syms[i].bend { return true }; i = i + 1 }
|
||||
return false
|
||||
|
|
@ -1185,16 +1185,16 @@ program LudicLsp {
|
|||
# ---- builtins / intrinsics (name -> signature) ----
|
||||
var g_builtins: []ptr
|
||||
var g_bsigs: []ptr
|
||||
fn badd(name: ptr, sig: ptr) -> void { push(g_builtins, name); push(g_bsigs, sig) }
|
||||
fn is_builtin_name(name: ptr) -> bool { var i = 0; while i < len(g_builtins) { if (g_builtins[i] == name) { return true }; i = i + 1 }; return false }
|
||||
fn builtin_sig(name: ptr) -> ptr { var i = 0; while i < len(g_builtins) { if (g_builtins[i] == name) { return g_bsigs[i] }; i = i + 1 }; return null }
|
||||
function badd(name: ptr, sig: ptr) -> void { push(g_builtins, name); push(g_bsigs, sig) }
|
||||
function is_builtin_name(name: ptr) -> bool { var i = 0; while i < len(g_builtins) { if (g_builtins[i] == name) { return true }; i = i + 1 }; return false }
|
||||
function builtin_sig(name: ptr) -> ptr { var i = 0; while i < len(g_builtins) { if (g_builtins[i] == name) { return g_bsigs[i] }; i = i + 1 }; return null }
|
||||
|
||||
# The namespaced standard-library surface: `Namespace.method`, mirroring the
|
||||
# compiler's emit_ns_call (selfhost/emit_expr.ludic). Screen/Random/Input/Map
|
||||
# methods resolve to the underlying bare builtin's signature; Math/Text/List
|
||||
# are pure fixed-point / string / slice ops with their own signatures. Returns
|
||||
# the signature label for signature help, or null if it is not a known method.
|
||||
fn ns_method_sig(ns: ptr, meth: ptr) -> ptr {
|
||||
function ns_method_sig(ns: ptr, meth: ptr) -> ptr {
|
||||
if (ns == "Screen") {
|
||||
if (meth == "clear") { return builtin_sig("clear") }
|
||||
if (meth == "fill_rectangle") { return builtin_sig("fill_rect") }
|
||||
|
|
@ -1395,7 +1395,7 @@ program LudicLsp {
|
|||
}
|
||||
return null
|
||||
}
|
||||
fn init_builtins() -> void {
|
||||
function init_builtins() -> void {
|
||||
g_builtins = new []ptr; g_bsigs = new []ptr
|
||||
badd("min", "min(a: int, b: int) -> int")
|
||||
badd("max", "max(a: int, b: int) -> int")
|
||||
|
|
@ -1478,7 +1478,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- classification ----
|
||||
fn class_of_symkind(k: int) -> int {
|
||||
function class_of_symkind(k: int) -> int {
|
||||
if k == LS_COMPONENT { return SC_COMPONENT }
|
||||
if k == LS_ARCHETYPE { return SC_ARCHETYPE }
|
||||
if k == LS_FIELD { return SC_FIELD }
|
||||
|
|
@ -1495,7 +1495,7 @@ program LudicLsp {
|
|||
if k == LS_UNIT { return SC_UI }
|
||||
return SC_UNKNOWN
|
||||
}
|
||||
fn classify(D: Doc) -> void {
|
||||
function classify(D: Doc) -> void {
|
||||
let N = ntok(D)
|
||||
if len(D.cls) < N { D.cls = new []int; var z = 0; while z < N { push(D.cls, SC_NONE); z = z + 1 } }
|
||||
var i = 0
|
||||
|
|
@ -1542,7 +1542,7 @@ program LudicLsp {
|
|||
|
||||
# semantic token type index + modifiers (via g_semmods)
|
||||
var g_semmods: int = 0
|
||||
fn sem_type_of(cls: int) -> int {
|
||||
function sem_type_of(cls: int) -> int {
|
||||
g_semmods = 0
|
||||
if cls == SC_KEYWORD { return 0 }
|
||||
if cls == SC_TYPE { g_semmods = 4; return 1 }
|
||||
|
|
@ -1580,23 +1580,23 @@ program LudicLsp {
|
|||
const JARR: int = 4
|
||||
const JOBJ: int = 5
|
||||
property JVal { t: int = 0, num: int = 0, b: int = 0, s: ptr = null, kids: []JVal, keys: []ptr }
|
||||
fn jval(t: int) -> JVal { let v = new JVal; v.t = t; v.kids = new []JVal; v.keys = new []ptr; return v }
|
||||
function jval(t: int) -> JVal { let v = new JVal; v.t = t; v.kids = new []JVal; v.keys = new []ptr; return v }
|
||||
|
||||
var jsrc: ptr = null
|
||||
var jp: int = 0
|
||||
fn jskip() -> void { while jsrc[jp] == 32 or jsrc[jp] == 9 or jsrc[jp] == 10 or jsrc[jp] == 13 { jp = jp + 1 } }
|
||||
fn jhex4(s: ptr, at: int) -> int {
|
||||
function jskip() -> void { while jsrc[jp] == 32 or jsrc[jp] == 9 or jsrc[jp] == 10 or jsrc[jp] == 13 { jp = jp + 1 } }
|
||||
function jhex4(s: ptr, at: int) -> int {
|
||||
var v = 0; var i = 0
|
||||
while i < 4 { let h = hexval(s[at + i]); if h < 0 { return v }; v = v * 16 + h; i = i + 1 }
|
||||
return v
|
||||
}
|
||||
fn utf8_emit(o: Buf, cp: int) -> void {
|
||||
function utf8_emit(o: Buf, cp: int) -> void {
|
||||
if cp < 128 { buf_putc(o, cp) }
|
||||
else { if cp < 2048 { buf_putc(o, 192 | (cp >> 6)); buf_putc(o, 128 | (cp & 63)) }
|
||||
else { if cp < 65536 { buf_putc(o, 224 | (cp >> 12)); buf_putc(o, 128 | ((cp >> 6) & 63)); buf_putc(o, 128 | (cp & 63)) }
|
||||
else { buf_putc(o, 240 | (cp >> 18)); buf_putc(o, 128 | ((cp >> 12) & 63)); buf_putc(o, 128 | ((cp >> 6) & 63)); buf_putc(o, 128 | (cp & 63)) } } }
|
||||
}
|
||||
fn jparse_string() -> ptr {
|
||||
function jparse_string() -> ptr {
|
||||
jp = jp + 1 # skip opening quote
|
||||
let o = buf_new()
|
||||
while jsrc[jp] != 0 and jsrc[jp] != 34 {
|
||||
|
|
@ -1621,7 +1621,7 @@ program LudicLsp {
|
|||
if jsrc[jp] == 34 { jp = jp + 1 }
|
||||
return buf_str(o)
|
||||
}
|
||||
fn jparse() -> JVal {
|
||||
function jparse() -> JVal {
|
||||
jskip()
|
||||
let c = jsrc[jp]
|
||||
if c == 123 { # '{'
|
||||
|
|
@ -1671,18 +1671,18 @@ program LudicLsp {
|
|||
jp = jp + 1
|
||||
return jval(JNULL)
|
||||
}
|
||||
fn jget(v: JVal, key: ptr) -> JVal {
|
||||
function jget(v: JVal, key: ptr) -> JVal {
|
||||
if (v == null) { return null }
|
||||
if v.t != JOBJ { return null }
|
||||
var i = 0
|
||||
while i < len(v.keys) { if (v.keys[i] == key) { return v.kids[i] }; i = i + 1 }
|
||||
return null
|
||||
}
|
||||
fn jat(v: JVal, i: int) -> JVal { if (v == null) { return null }; if i < 0 or i >= len(v.kids) { return null }; return v.kids[i] }
|
||||
fn jlen(v: JVal) -> int { if (v == null) { return 0 }; return len(v.kids) }
|
||||
fn jstr(v: JVal, def: ptr) -> ptr { if (v != null) and v.t == JSTR and (v.s != null) { return v.s }; return def }
|
||||
fn jint(v: JVal, def: int) -> int { if (v != null) and v.t == JNUM { return v.num }; return def }
|
||||
fn jpath(v: JVal, path: ptr) -> JVal {
|
||||
function jat(v: JVal, i: int) -> JVal { if (v == null) { return null }; if i < 0 or i >= len(v.kids) { return null }; return v.kids[i] }
|
||||
function jlen(v: JVal) -> int { if (v == null) { return 0 }; return len(v.kids) }
|
||||
function jstr(v: JVal, def: ptr) -> ptr { if (v != null) and v.t == JSTR and (v.s != null) { return v.s }; return def }
|
||||
function jint(v: JVal, def: int) -> int { if (v != null) and v.t == JNUM { return v.num }; return def }
|
||||
function jpath(v: JVal, path: ptr) -> JVal {
|
||||
var cur = v
|
||||
let n = cstr_len(path)
|
||||
var start = 0; var i = 0
|
||||
|
|
@ -1701,8 +1701,8 @@ program LudicLsp {
|
|||
# ============================================================================
|
||||
# the formatter (Doc-based; reused for LSP formatting) — mirrors fmt.ludic
|
||||
# ============================================================================
|
||||
fn f_name_like(k: int) -> bool { return k == LT_ID or k == LT_KW or k == LT_TYPE or k == LT_PHASE or k == LT_BOOL }
|
||||
fn f_dot_tight(F: Doc, t: int) -> bool {
|
||||
function f_name_like(k: int) -> bool { return k == LT_ID or k == LT_KW or k == LT_TYPE or k == LT_PHASE or k == LT_BOOL }
|
||||
function f_dot_tight(F: Doc, t: int) -> bool {
|
||||
if f_name_like(F.tk_kind[t]) { return true }
|
||||
if F.tk_kind[t] != LT_OP { return false }
|
||||
let n = tlen(F, t); let c0 = F.src[F.tk_start[t]]
|
||||
|
|
@ -1710,7 +1710,7 @@ program LudicLsp {
|
|||
if n == 2 and c0 == 46 and F.src[F.tk_start[t] + 1] == 46 { return true }
|
||||
return false
|
||||
}
|
||||
fn f_is_unary(F: Doc, i: int) -> bool {
|
||||
function f_is_unary(F: Doc, i: int) -> bool {
|
||||
if F.tk_kind[i] != LT_OP { return false }
|
||||
let n = tlen(F, i)
|
||||
if not (n == 1 and (F.src[F.tk_start[i]] == 45 or F.src[F.tk_start[i]] == 33)) { return false }
|
||||
|
|
@ -1721,7 +1721,7 @@ program LudicLsp {
|
|||
if pk2 == LT_OP { let c = F.src[F.tk_start[p]]; return not (tlen(F, p) == 1 and (c == 41 or c == 93 or c == 125)) }
|
||||
return true
|
||||
}
|
||||
fn f_space_before(F: Doc, prev: int, cur: int) -> int {
|
||||
function f_space_before(F: Doc, prev: int, cur: int) -> int {
|
||||
if prev < 0 { return 0 }
|
||||
let pk2 = F.tk_kind[prev]; let ck = F.tk_kind[cur]
|
||||
let plen = tlen(F, prev); let clen = tlen(F, cur)
|
||||
|
|
@ -1741,16 +1741,16 @@ program LudicLsp {
|
|||
}
|
||||
return 1
|
||||
}
|
||||
fn f_is_clause(w: ptr) -> bool {
|
||||
function f_is_clause(w: ptr) -> bool {
|
||||
return (w == "phase") or (w == "query") or (w == "reads") or (w == "writes") or (w == "needs") or (w == "uses") or (w == "requires") or (w == "ensures") or (w == "invariant") or (w == "effects")
|
||||
}
|
||||
fn make_text_doc(text: ptr) -> Doc {
|
||||
function make_text_doc(text: ptr) -> Doc {
|
||||
let F = new Doc
|
||||
F.raw = text; F.text = text; F.src = text; F.is_md = 0
|
||||
lex_doc(F)
|
||||
return F
|
||||
}
|
||||
fn format_doc(F: Doc, iw: int) -> ptr {
|
||||
function format_doc(F: Doc, iw: int) -> ptr {
|
||||
let o = buf_new()
|
||||
let stack = words(600)
|
||||
let hang = words(600)
|
||||
|
|
@ -1855,7 +1855,7 @@ program LudicLsp {
|
|||
var mf_flen: int = 0
|
||||
var mf_info: int = 0
|
||||
var mf_marker: int = 0
|
||||
fn mf_fence_at(s: ptr, i: int) -> bool {
|
||||
function mf_fence_at(s: ptr, i: int) -> bool {
|
||||
var j = i; while s[j] == 32 { j = j + 1 }
|
||||
let m = s[j]
|
||||
if m != 96 and m != 126 { return false }
|
||||
|
|
@ -1864,8 +1864,8 @@ program LudicLsp {
|
|||
mf_flen = n; mf_info = j; mf_marker = m
|
||||
return true
|
||||
}
|
||||
fn f_line_end(s: ptr, i: int) -> int { var e = i; while s[e] != 0 and s[e] != 10 { e = e + 1 }; return e }
|
||||
fn format_md(s: ptr, iw: int) -> ptr {
|
||||
function f_line_end(s: ptr, i: int) -> int { var e = i; while s[e] != 0 and s[e] != 10 { e = e + 1 }; return e }
|
||||
function format_md(s: ptr, iw: int) -> ptr {
|
||||
let o = buf_new()
|
||||
var i = 0
|
||||
while s[i] != 0 {
|
||||
|
|
@ -1925,14 +1925,14 @@ program LudicLsp {
|
|||
}
|
||||
return buf_str(o)
|
||||
}
|
||||
fn skip_ws2(s: ptr, at: int) -> int { var a = at; while s[a] == 32 or s[a] == 9 { a = a + 1 }; return a }
|
||||
function skip_ws2(s: ptr, at: int) -> int { var a = at; while s[a] == 32 or s[a] == 9 { a = a + 1 }; return a }
|
||||
|
||||
# ============================================================================
|
||||
# framing + output
|
||||
# ============================================================================
|
||||
extern fn fflush(f: ptr) -> int = "fflush"
|
||||
extern function fflush(f: ptr) -> int = "fflush"
|
||||
|
||||
fn jstr_out(o: Buf, z: ptr) -> void {
|
||||
function jstr_out(o: Buf, z: ptr) -> void {
|
||||
buf_putc(o, 34)
|
||||
if (z == null) { buf_putc(o, 34); return }
|
||||
var i = 0
|
||||
|
|
@ -1950,7 +1950,7 @@ program LudicLsp {
|
|||
buf_putc(o, 34)
|
||||
}
|
||||
|
||||
fn send_raw(body: ptr) -> void {
|
||||
function send_raw(body: ptr) -> void {
|
||||
let hdr = buf_new()
|
||||
buf_puts(hdr, "Content-Length: "); buf_puti(hdr, cstr_len(body)); buf_putc(hdr, 13); buf_putc(hdr, 10); buf_putc(hdr, 13); buf_putc(hdr, 10)
|
||||
let hs = buf_str(hdr)
|
||||
|
|
@ -1959,7 +1959,7 @@ program LudicLsp {
|
|||
file_write(out, body, cstr_len(body))
|
||||
let z = fflush(out)
|
||||
}
|
||||
fn send_result(idv: JVal, result: ptr) -> void {
|
||||
function send_result(idv: JVal, result: ptr) -> void {
|
||||
let o = buf_new()
|
||||
buf_puts(o, "{\"jsonrpc\":\"2.0\",\"id\":")
|
||||
if (idv == null) { buf_puts(o, "null") }
|
||||
|
|
@ -1969,19 +1969,19 @@ program LudicLsp {
|
|||
buf_putc(o, 125)
|
||||
send_raw(buf_str(o))
|
||||
}
|
||||
fn send_notify(method: ptr, params: ptr) -> void {
|
||||
function send_notify(method: ptr, params: ptr) -> void {
|
||||
let o = buf_new()
|
||||
buf_puts(o, "{\"jsonrpc\":\"2.0\",\"method\":"); jstr_out(o, method); buf_puts(o, ",\"params\":"); buf_puts(o, params); buf_putc(o, 125)
|
||||
send_raw(buf_str(o))
|
||||
}
|
||||
|
||||
fn lower(c: int) -> int { if c >= 65 and c <= 90 { return c + 32 }; return c }
|
||||
fn starts_with_ci(s: ptr, pre: ptr) -> bool {
|
||||
function lower(c: int) -> int { if c >= 65 and c <= 90 { return c + 32 }; return c }
|
||||
function starts_with_ci(s: ptr, pre: ptr) -> bool {
|
||||
var i = 0
|
||||
while pre[i] != 0 { if s[i] == 0 or lower(s[i]) != lower(pre[i]) { return false }; i = i + 1 }
|
||||
return true
|
||||
}
|
||||
fn read_message() -> ptr {
|
||||
function read_message() -> ptr {
|
||||
var length = 0 - 1
|
||||
let line = buf_new()
|
||||
while true {
|
||||
|
|
@ -2004,26 +2004,26 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- position output ----
|
||||
fn put_pos(o: Buf, D: Doc, off: int) -> void { buf_puts(o, "{\"line\":"); buf_puti(o, line_of(D, off)); buf_puts(o, ",\"character\":"); buf_puti(o, col_of(D, off)); buf_putc(o, 125) }
|
||||
fn put_range(o: Buf, D: Doc, a: int, b: int) -> void { buf_puts(o, "{\"start\":"); put_pos(o, D, a); buf_puts(o, ",\"end\":"); put_pos(o, D, b); buf_putc(o, 125) }
|
||||
fn put_location(o: Buf, D: Doc, a: int, b: int) -> void { buf_puts(o, "{\"uri\":"); jstr_out(o, D.uri); buf_puts(o, ",\"range\":"); put_range(o, D, a, b); buf_putc(o, 125) }
|
||||
function put_pos(o: Buf, D: Doc, off: int) -> void { buf_puts(o, "{\"line\":"); buf_puti(o, line_of(D, off)); buf_puts(o, ",\"character\":"); buf_puti(o, col_of(D, off)); buf_putc(o, 125) }
|
||||
function put_range(o: Buf, D: Doc, a: int, b: int) -> void { buf_puts(o, "{\"start\":"); put_pos(o, D, a); buf_puts(o, ",\"end\":"); put_pos(o, D, b); buf_putc(o, 125) }
|
||||
function put_location(o: Buf, D: Doc, a: int, b: int) -> void { buf_puts(o, "{\"uri\":"); jstr_out(o, D.uri); buf_puts(o, ",\"range\":"); put_range(o, D, a, b); buf_putc(o, 125) }
|
||||
|
||||
fn doc_of(msg: JVal) -> Doc {
|
||||
function doc_of(msg: JVal) -> Doc {
|
||||
let uri = jstr(jpath(msg, "params.textDocument.uri"), null)
|
||||
if (uri == null) { return null }
|
||||
let D = by_uri(uri)
|
||||
if (D != null) { return D }
|
||||
return ensure_doc(uri_to_path(uri))
|
||||
}
|
||||
fn off_of(msg: JVal, D: Doc, base: ptr) -> int {
|
||||
function off_of(msg: JVal, D: Doc, base: ptr) -> int {
|
||||
let line = jint(jpath(msg, concat3(base, ".line", "")), 0)
|
||||
let ch = jint(jpath(msg, concat3(base, ".character", "")), 0)
|
||||
return offset_of(D, line, ch)
|
||||
}
|
||||
fn basename(p: ptr) -> ptr { var last = 0 - 1; var i = 0; while p[i] != 0 { if p[i] == 47 { last = i }; i = i + 1 }; return p[last + 1..i] }
|
||||
function basename(p: ptr) -> ptr { var last = 0 - 1; var i = 0; while p[i] != 0 { if p[i] == 47 { last = i }; i = i + 1 }; return p[last + 1..i] }
|
||||
|
||||
# ---- LSP symbol kinds & completion kinds ----
|
||||
fn lsp_symbol_kind(k: int) -> int {
|
||||
function lsp_symbol_kind(k: int) -> int {
|
||||
if k == LS_UNIT { return 2 }
|
||||
if k == LS_COMPONENT { return 23 }
|
||||
if k == LS_FIELD { return 8 }
|
||||
|
|
@ -2038,7 +2038,7 @@ program LudicLsp {
|
|||
if k == LS_LAYER { return 3 }
|
||||
return 13
|
||||
}
|
||||
fn comp_kind_of(k: int) -> int {
|
||||
function comp_kind_of(k: int) -> int {
|
||||
if k == LS_COMPONENT { return 22 }
|
||||
if k == LS_ARCHETYPE { return 7 }
|
||||
if k == LS_CONST { return 21 }
|
||||
|
|
@ -2054,7 +2054,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- diagnostics (structural) ----
|
||||
fn diag_structural(D: Doc, o: Buf) -> void {
|
||||
function diag_structural(D: Doc, o: Buf) -> void {
|
||||
let ds_s = new []int; let ds_e = new []int; let ds_m = new []ptr
|
||||
let stack = words(1200); var top = 0
|
||||
var i = 0; let N = ntok(D)
|
||||
|
|
@ -2081,7 +2081,7 @@ program LudicLsp {
|
|||
j = j + 1
|
||||
}
|
||||
}
|
||||
fn publish_diag(D: Doc) -> void {
|
||||
function publish_diag(D: Doc) -> void {
|
||||
let o = buf_new()
|
||||
buf_puts(o, "{\"uri\":"); jstr_out(o, D.uri); buf_puts(o, ",\"diagnostics\":[")
|
||||
if D.is_md == 0 { diag_structural(D, o) }
|
||||
|
|
@ -2090,7 +2090,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- document symbols ----
|
||||
fn put_doc_symbols(o: Buf, D: Doc, parent: int) -> void {
|
||||
function put_doc_symbols(o: Buf, D: Doc, parent: int) -> void {
|
||||
buf_putc(o, 91)
|
||||
var first = 1
|
||||
var i = 0
|
||||
|
|
@ -2116,13 +2116,13 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- hover ----
|
||||
fn hover_md(o: Buf, code: ptr, docstr: ptr) -> void {
|
||||
function hover_md(o: Buf, code: ptr, docstr: ptr) -> void {
|
||||
let m = buf_new()
|
||||
buf_puts(m, "```ludic\n"); buf_puts(m, code); buf_puts(m, "\n```")
|
||||
if (docstr != null) and (docstr[0] != 0) { buf_puts(m, "\n\n"); buf_puts(m, docstr) }
|
||||
buf_puts(o, "{\"contents\":{\"kind\":\"markdown\",\"value\":"); jstr_out(o, buf_str(m)); buf_puts(o, "}}")
|
||||
}
|
||||
fn on_hover(msg: JVal, id: JVal) -> void {
|
||||
function on_hover(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "null"); return }
|
||||
let off = off_of(msg, D, "params.position")
|
||||
|
|
@ -2147,7 +2147,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- definition ----
|
||||
fn on_definition(msg: JVal, id: JVal) -> void {
|
||||
function on_definition(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "null"); return }
|
||||
let off = off_of(msg, D, "params.position")
|
||||
|
|
@ -2172,7 +2172,7 @@ program LudicLsp {
|
|||
# ---- occurrences (references / rename) ----
|
||||
var g_occ_doc: []Doc
|
||||
var g_occ_tok: []int
|
||||
fn find_occurrences(from: Doc, tdoc: Doc, tsym: int) -> void {
|
||||
function find_occurrences(from: Doc, tdoc: Doc, tsym: int) -> void {
|
||||
g_occ_doc = new []Doc; g_occ_tok = new []int
|
||||
let want = tdoc.syms[tsym].name
|
||||
var docs = new []Doc
|
||||
|
|
@ -2191,7 +2191,7 @@ program LudicLsp {
|
|||
i = i + 1
|
||||
}
|
||||
}
|
||||
fn on_references(msg: JVal, id: JVal) -> void {
|
||||
function on_references(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "[]"); return }
|
||||
let off = off_of(msg, D, "params.position")
|
||||
|
|
@ -2209,7 +2209,7 @@ program LudicLsp {
|
|||
}
|
||||
buf_putc(o, 93); send_result(id, buf_str(o))
|
||||
}
|
||||
fn on_rename(msg: JVal, id: JVal) -> void {
|
||||
function on_rename(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
let newname = jstr(jpath(msg, "params.newName"), null)
|
||||
if (D == null) or (newname == null) or (newname[0] == 0) { send_result(id, "null"); return }
|
||||
|
|
@ -2250,14 +2250,14 @@ program LudicLsp {
|
|||
|
||||
# ---- completion ----
|
||||
var g_first: int = 1
|
||||
fn comp_item(o: Buf, label: ptr, kind: int, detail: ptr) -> void {
|
||||
function comp_item(o: Buf, label: ptr, kind: int, detail: ptr) -> void {
|
||||
if g_first == 0 { buf_putc(o, 44) }
|
||||
g_first = 0
|
||||
buf_puts(o, "{\"label\":"); jstr_out(o, label); buf_puts(o, ",\"kind\":"); buf_puti(o, kind)
|
||||
if (detail != null) and (detail[0] != 0) { buf_puts(o, ",\"detail\":"); jstr_out(o, detail) }
|
||||
buf_putc(o, 125)
|
||||
}
|
||||
fn comp_scope(o: Buf, D: Doc, off: int, types_only: int) -> void {
|
||||
function comp_scope(o: Buf, D: Doc, off: int, types_only: int) -> void {
|
||||
if types_only == 0 {
|
||||
var i = 0
|
||||
while i < len(D.syms) {
|
||||
|
|
@ -2285,12 +2285,12 @@ program LudicLsp {
|
|||
r = r + 1
|
||||
}
|
||||
}
|
||||
fn comp_types(o: Buf) -> void {
|
||||
function comp_types(o: Buf) -> void {
|
||||
comp_item(o, "int", 14, "built-in type"); comp_item(o, "long", 14, "built-in type"); comp_item(o, "fixed", 14, "built-in type"); comp_item(o, "bool", 14, "built-in type")
|
||||
comp_item(o, "entity", 14, "built-in type"); comp_item(o, "str", 14, "built-in type"); comp_item(o, "ptr", 14, "built-in type"); comp_item(o, "void", 14, "built-in type")
|
||||
}
|
||||
fn comp_builtins(o: Buf) -> void { var i = 0; while i < len(g_builtins) { comp_item(o, g_builtins[i], 3, g_bsigs[i]); i = i + 1 } }
|
||||
fn on_completion(msg: JVal, id: JVal) -> void {
|
||||
function comp_builtins(o: Buf) -> void { var i = 0; while i < len(g_builtins) { comp_item(o, g_builtins[i], 3, g_bsigs[i]); i = i + 1 } }
|
||||
function on_completion(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "{\"isIncomplete\":false,\"items\":[]}"); return }
|
||||
let off = off_of(msg, D, "params.position")
|
||||
|
|
@ -2350,7 +2350,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- inlay hints, signature help, semantic tokens, folding ----
|
||||
fn on_inlay_hint(msg: JVal, id: JVal) -> void {
|
||||
function on_inlay_hint(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "[]"); return }
|
||||
let o = buf_new(); buf_putc(o, 91)
|
||||
|
|
@ -2367,7 +2367,7 @@ program LudicLsp {
|
|||
}
|
||||
buf_putc(o, 93); send_result(id, buf_str(o))
|
||||
}
|
||||
fn on_signature_help(msg: JVal, id: JVal) -> void {
|
||||
function on_signature_help(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "null"); return }
|
||||
let off = off_of(msg, D, "params.position")
|
||||
|
|
@ -2412,7 +2412,7 @@ program LudicLsp {
|
|||
buf_puts(o, "}],\"activeSignature\":0,\"activeParameter\":"); buf_puti(o, arg); buf_putc(o, 125)
|
||||
send_result(id, buf_str(o))
|
||||
}
|
||||
fn on_semantic_tokens(msg: JVal, id: JVal) -> void {
|
||||
function on_semantic_tokens(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "null"); return }
|
||||
classify(D)
|
||||
|
|
@ -2442,7 +2442,7 @@ program LudicLsp {
|
|||
}
|
||||
buf_puts(o, "]}"); send_result(id, buf_str(o))
|
||||
}
|
||||
fn on_folding(msg: JVal, id: JVal) -> void {
|
||||
function on_folding(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "[]"); return }
|
||||
let o = buf_new(); buf_putc(o, 91); var first = 1
|
||||
|
|
@ -2463,7 +2463,7 @@ program LudicLsp {
|
|||
}
|
||||
buf_putc(o, 93); send_result(id, buf_str(o))
|
||||
}
|
||||
fn on_workspace_symbol(msg: JVal, id: JVal) -> void {
|
||||
function on_workspace_symbol(msg: JVal, id: JVal) -> void {
|
||||
let q = jstr(jpath(msg, "params.query"), "")
|
||||
let o = buf_new(); buf_putc(o, 91); var first = 1
|
||||
var d = 0
|
||||
|
|
@ -2487,7 +2487,7 @@ program LudicLsp {
|
|||
}
|
||||
buf_putc(o, 93); send_result(id, buf_str(o))
|
||||
}
|
||||
fn contains_ci(hay: ptr, needle: ptr) -> bool {
|
||||
function contains_ci(hay: ptr, needle: ptr) -> bool {
|
||||
let hn = cstr_len(hay); let nn = cstr_len(needle)
|
||||
if nn == 0 { return true }
|
||||
var i = 0
|
||||
|
|
@ -2500,7 +2500,7 @@ program LudicLsp {
|
|||
}
|
||||
return false
|
||||
}
|
||||
fn on_formatting(msg: JVal, id: JVal) -> void {
|
||||
function on_formatting(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "null"); return }
|
||||
var tab = jint(jpath(msg, "params.options.tabSize"), 2)
|
||||
|
|
@ -2518,7 +2518,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- document lifecycle ----
|
||||
fn on_did_open(msg: JVal) -> void {
|
||||
function on_did_open(msg: JVal) -> void {
|
||||
let uri = jstr(jpath(msg, "params.textDocument.uri"), null)
|
||||
let text = jstr(jpath(msg, "params.textDocument.text"), "")
|
||||
if (uri == null) { return }
|
||||
|
|
@ -2529,7 +2529,7 @@ program LudicLsp {
|
|||
D.isopen = 1
|
||||
publish_diag(D)
|
||||
}
|
||||
fn on_did_change(msg: JVal) -> void {
|
||||
function on_did_change(msg: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { return }
|
||||
let changes = jpath(msg, "params.contentChanges")
|
||||
|
|
@ -2540,7 +2540,7 @@ program LudicLsp {
|
|||
reindex(D)
|
||||
publish_diag(D)
|
||||
}
|
||||
fn on_did_close(msg: JVal) -> void {
|
||||
function on_did_close(msg: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { return }
|
||||
D.isopen = 0
|
||||
|
|
@ -2548,7 +2548,7 @@ program LudicLsp {
|
|||
if (disk != null) { set_text(D, disk); reindex(D) }
|
||||
}
|
||||
|
||||
fn caps() -> ptr {
|
||||
function caps() -> ptr {
|
||||
let o = buf_new()
|
||||
buf_puts(o, "{\"capabilities\":{")
|
||||
buf_puts(o, "\"positionEncoding\":\"utf-16\"")
|
||||
|
|
@ -2572,14 +2572,14 @@ program LudicLsp {
|
|||
buf_puts(o, "},\"serverInfo\":{\"name\":\"ludic-lsp\",\"version\":\"1.0.0\"}}")
|
||||
return buf_str(o)
|
||||
}
|
||||
fn on_initialize(msg: JVal, id: JVal) -> void {
|
||||
function on_initialize(msg: JVal, id: JVal) -> void {
|
||||
let root = jpath(msg, "params.rootUri")
|
||||
if (root != null) and root.t == JSTR { g_root = uri_to_path(root.s) }
|
||||
if (g_root != null) { scan_root(g_root) }
|
||||
send_result(id, caps())
|
||||
}
|
||||
|
||||
fn handle(msg: JVal) -> void {
|
||||
function handle(msg: JVal) -> void {
|
||||
let method = jstr(jget(msg, "method"), null)
|
||||
let id = jget(msg, "id")
|
||||
if (method == null) { return }
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ typedef struct {
|
|||
* declaration keyword, anything stmt() dispatches on is a statement keyword. */
|
||||
static const char* LUDIC_KW_DECL[] = {
|
||||
"program","import","property","model","enum","ui",
|
||||
"const","var","fn","extern","handler","entry","event","scene", 0
|
||||
"const","var","function","extern","handler","entry","event","scene", 0
|
||||
};
|
||||
static const char* LUDIC_KW_CLAUSE[] = {
|
||||
"phase","query","on","cancellable","public","layer","start", 0
|
||||
|
|
|
|||
|
|
@ -246,7 +246,7 @@ def main():
|
|||
fh.write("program R {\n"
|
||||
" property Pos { x: int = 0, y: int = 0 }\n"
|
||||
" property Vel { x: int = 0 }\n"
|
||||
" fn helper(x: int) -> int { return x + 1 }\n"
|
||||
" function helper(x: int) -> int { return x + 1 }\n"
|
||||
" handler A phase Update {\n"
|
||||
" for (p) in query [Pos] {\n"
|
||||
" let x = 5\n"
|
||||
|
|
@ -275,7 +275,7 @@ def main():
|
|||
lines_touched(field), [1, 7])
|
||||
local = rename_at(position_of(atext, "let x = 5", 4))
|
||||
check("renaming a local touches only that local", lines_touched(local), [6, 7])
|
||||
param = rename_at(position_of(atext, "fn helper(x", 10))
|
||||
param = rename_at(position_of(atext, "function helper(x", 16))
|
||||
check("renaming a parameter touches only that function", lines_touched(param), [3])
|
||||
check("renaming a parameter makes exactly two edits", len(param), 2)
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
# compile a Ludic source to a native binary in bin/ via ludicc + clang (-O2).
|
||||
# Returns true on success. Used for x itself and the editor tools.
|
||||
fn build_tool(name: ptr, src: ptr) -> bool {
|
||||
function build_tool(name: ptr, src: ptr) -> bool {
|
||||
let ll = `build/{name}.ll`
|
||||
if not shq(`bin/ludicc {src} --emit-llvm -o {ll} 2>/dev/null`) { print(`build failed: {name} (compile)`); return false }
|
||||
# write to a temp then move, so a running bin/x can rebuild itself in place
|
||||
|
|
@ -18,7 +18,7 @@ fn build_tool(name: ptr, src: ptr) -> bool {
|
|||
# Both ludicc and ludic are the SAME multi-call binary assembled from the IR seed
|
||||
# with clang alone; invoked as `ludic` it compiles-and-runs, as `ludicc` it just
|
||||
# compiles.
|
||||
fn cmd_build_cli() -> int {
|
||||
function cmd_build_cli() -> int {
|
||||
run("mkdir -p bin build")
|
||||
print("cc: selfhost/ludicc.seed.ll -> bin/ludicc, bin/ludic (from the IR seed, no C compiler)")
|
||||
if not shq(`{cc()} selfhost/ludicc.seed.ll -o bin/ludicc`) { err("x: seed did not assemble\n"); return 1 }
|
||||
|
|
@ -28,7 +28,7 @@ fn cmd_build_cli() -> int {
|
|||
}
|
||||
|
||||
# ---- build: the whole toolchain (ludicc, ludic, x, ludic-fmt, ludic-lsp) -----
|
||||
fn cmd_build() -> int {
|
||||
function cmd_build() -> int {
|
||||
if cmd_build_cli() != 0 { return 1 }
|
||||
print("ludicc: tools/x/main.ludic -> bin/x (the task runner rebuilds itself)")
|
||||
if not build_tool("x", "tools/x/main.ludic") { return 1 }
|
||||
|
|
@ -44,7 +44,7 @@ fn cmd_build() -> int {
|
|||
# ---- app: compile a Ludic program to a native app ----------------------------
|
||||
# usage: x app <file.ludic> [--headless|--windowed] [--save-temps]
|
||||
# windowed is the default for a game; headless renders the last frame to out.ppm.
|
||||
fn cmd_app() -> int {
|
||||
function cmd_app() -> int {
|
||||
ensure_ludicc()
|
||||
var src = null
|
||||
var mode = 1 # 1 = windowed, 2 = headless
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ program X {
|
|||
import "selfhost_test.ludic"
|
||||
import "test.ludic"
|
||||
|
||||
fn usage() -> void {
|
||||
function usage() -> void {
|
||||
print("x — the Ludic task runner (run from the repository root)")
|
||||
print("")
|
||||
print("build & run:")
|
||||
|
|
@ -43,7 +43,7 @@ program X {
|
|||
}
|
||||
|
||||
# a positional argument, or a default when absent
|
||||
fn argn(i: int, dflt: ptr) -> ptr {
|
||||
function argn(i: int, dflt: ptr) -> ptr {
|
||||
if (i < arg_count()) { return arg(i) }
|
||||
return dflt
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
# ---- file IO ----------------------------------------------------------------
|
||||
|
||||
# read a whole file into a fresh NUL-terminated buffer (null if it cannot open)
|
||||
fn read_file(path: ptr) -> ptr {
|
||||
function read_file(path: ptr) -> ptr {
|
||||
let f = file_open(path, "rb")
|
||||
if (f == null) { return null }
|
||||
file_seek(f, 0, 2)
|
||||
|
|
@ -27,7 +27,7 @@ fn read_file(path: ptr) -> ptr {
|
|||
}
|
||||
|
||||
# overwrite `path` with `s`; returns false if it could not be opened
|
||||
fn write_file(path: ptr, s: ptr) -> bool {
|
||||
function write_file(path: ptr, s: ptr) -> bool {
|
||||
let f = file_open(path, "wb")
|
||||
if (f == null) { return false }
|
||||
file_write(f, s, len(s))
|
||||
|
|
@ -35,22 +35,22 @@ fn write_file(path: ptr, s: ptr) -> bool {
|
|||
return true
|
||||
}
|
||||
|
||||
fn file_exists(path: ptr) -> bool { return shq(`test -e {path}`) }
|
||||
fn is_exec(path: ptr) -> bool { return shq(`test -x {path}`) }
|
||||
function file_exists(path: ptr) -> bool { return shq(`test -e {path}`) }
|
||||
function is_exec(path: ptr) -> bool { return shq(`test -x {path}`) }
|
||||
# is `a` newer than `b` (like the shell's `-nt`)?
|
||||
fn newer(a: ptr, b: ptr) -> bool { return shq(`test {a} -nt {b}`) }
|
||||
function newer(a: ptr, b: ptr) -> bool { return shq(`test {a} -nt {b}`) }
|
||||
|
||||
# ---- process control --------------------------------------------------------
|
||||
|
||||
# `run` returns the raw wait status; the program's exit code is the high byte.
|
||||
fn exit_code(st: int) -> int { return (st >> 8) & 255 }
|
||||
function exit_code(st: int) -> int { return (st >> 8) & 255 }
|
||||
# run a command, returning its exit code (0 = success)
|
||||
fn sh(cmd: ptr) -> int { return exit_code(run(cmd)) }
|
||||
function sh(cmd: ptr) -> int { return exit_code(run(cmd)) }
|
||||
# run a command, true when it succeeded
|
||||
fn shq(cmd: ptr) -> bool { return exit_code(run(cmd)) == 0 }
|
||||
function shq(cmd: ptr) -> bool { return exit_code(run(cmd)) == 0 }
|
||||
|
||||
# run `cmd` and return its stdout (stderr discarded). Never null.
|
||||
fn capture(cmd: ptr) -> ptr {
|
||||
function capture(cmd: ptr) -> ptr {
|
||||
let tmp = "/tmp/x_capture.out"
|
||||
run(`{cmd} > {tmp} 2>/dev/null`)
|
||||
let s = read_file(tmp)
|
||||
|
|
@ -60,16 +60,16 @@ fn capture(cmd: ptr) -> ptr {
|
|||
|
||||
# run `cmd`, join its output lines with single spaces and trim — the Ludic twin
|
||||
# of the shell idiom `$(cmd | tr '\n' ' ' | sed 's/ *$//')`.
|
||||
fn capture_line(cmd: ptr) -> ptr {
|
||||
function capture_line(cmd: ptr) -> ptr {
|
||||
return capture(`{cmd} | tr '\n' ' ' | sed 's/ *$//'`)
|
||||
}
|
||||
|
||||
# the last line of a command's output (for one-line error messages)
|
||||
fn capture_tail(cmd: ptr) -> ptr {
|
||||
function capture_tail(cmd: ptr) -> ptr {
|
||||
return capture(`{cmd} 2>&1 | tail -1`)
|
||||
}
|
||||
|
||||
fn getenv_or(name: ptr, dflt: ptr) -> ptr {
|
||||
function getenv_or(name: ptr, dflt: ptr) -> ptr {
|
||||
let v = getenv(name)
|
||||
if (v == null) { return dflt }
|
||||
return v
|
||||
|
|
@ -78,40 +78,40 @@ fn getenv_or(name: ptr, dflt: ptr) -> ptr {
|
|||
# ---- stdout helpers ---------------------------------------------------------
|
||||
|
||||
# write `s` with no trailing newline (print() always adds one)
|
||||
fn out(s: ptr) -> void { file_write(file_stdout(), s, len(s)) }
|
||||
fn err(s: ptr) -> void { file_write(file_stderr(), s, len(s)) }
|
||||
function out(s: ptr) -> void { file_write(file_stdout(), s, len(s)) }
|
||||
function err(s: ptr) -> void { file_write(file_stderr(), s, len(s)) }
|
||||
|
||||
# an ESC byte — the lexer has no \033, so build it by hand
|
||||
fn esc() -> ptr { let b = bytes(2); b[0] = 27; b[1] = 0; return b }
|
||||
fn c_green() -> ptr { return esc() + "[32m" }
|
||||
fn c_red() -> ptr { return esc() + "[31m" }
|
||||
fn c_reset() -> ptr { return esc() + "[0m" }
|
||||
function esc() -> ptr { let b = bytes(2); b[0] = 27; b[1] = 0; return b }
|
||||
function c_green() -> ptr { return esc() + "[32m" }
|
||||
function c_red() -> ptr { return esc() + "[31m" }
|
||||
function c_reset() -> ptr { return esc() + "[0m" }
|
||||
|
||||
# ---- the PASS/FAIL test harness ---------------------------------------------
|
||||
|
||||
var PASS: int = 0
|
||||
var FAIL: int = 0
|
||||
|
||||
fn ok(msg: ptr) -> void {
|
||||
function ok(msg: ptr) -> void {
|
||||
PASS = PASS + 1
|
||||
print(` {c_green()}PASS{c_reset()} {msg}`)
|
||||
}
|
||||
fn bad(msg: ptr) -> void {
|
||||
function bad(msg: ptr) -> void {
|
||||
FAIL = FAIL + 1
|
||||
print(` {c_red()}FAIL{c_reset()} {msg}`)
|
||||
}
|
||||
fn bad2(msg: ptr, detail: ptr) -> void {
|
||||
function bad2(msg: ptr, detail: ptr) -> void {
|
||||
bad(msg)
|
||||
print(` {detail}`)
|
||||
}
|
||||
# assert two strings equal, reporting the mismatch
|
||||
fn check(label: ptr, got: ptr, want: ptr) -> void {
|
||||
function check(label: ptr, got: ptr, want: ptr) -> void {
|
||||
if (got == want) { ok(label) }
|
||||
else { bad2(label, `expected [{want}] got [{got}]`) }
|
||||
}
|
||||
|
||||
# print the "== N passed, M failed ==" footer and return the process exit code
|
||||
fn report() -> int {
|
||||
function report() -> int {
|
||||
print("")
|
||||
print(`== {str(PASS)} passed, {str(FAIL)} failed ==`)
|
||||
if (FAIL == 0) { return 0 }
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@
|
|||
# now lives here once, in selfhost_frags().
|
||||
|
||||
# the C toolchain driver (clang) that assembles and links the emitted IR
|
||||
fn cc() -> ptr { return getenv_or("LUDIC_CC", "clang") }
|
||||
function cc() -> ptr { return getenv_or("LUDIC_CC", "clang") }
|
||||
|
||||
# the self-host compiler's source fragments, in link order. This is THE list;
|
||||
# the old scripts each carried their own copy.
|
||||
fn selfhost_frags() -> []ptr {
|
||||
function selfhost_frags() -> []ptr {
|
||||
let f = new []ptr
|
||||
push(f, "selfhost/str.ludic")
|
||||
push(f, "selfhost/buf.ludic")
|
||||
|
|
@ -52,7 +52,7 @@ fn selfhost_frags() -> []ptr {
|
|||
|
||||
# concatenate the fragments into `program SelfHost { ... }` at `outpath`.
|
||||
# This is the pure-Ludic equivalent of the scripts' `{ echo; for; cat; }` block.
|
||||
fn write_selfhost_src(outpath: ptr) -> bool {
|
||||
function write_selfhost_src(outpath: ptr) -> bool {
|
||||
let f = file_open(outpath, "wb")
|
||||
if (f == null) { return false }
|
||||
let hdr = "program SelfHost {\n"
|
||||
|
|
@ -72,13 +72,13 @@ fn write_selfhost_src(outpath: ptr) -> bool {
|
|||
}
|
||||
|
||||
# count the lines in a file (for the progress notes the scripts printed)
|
||||
fn line_count(path: ptr) -> ptr {
|
||||
function line_count(path: ptr) -> ptr {
|
||||
return capture_line(`wc -l < {path}`)
|
||||
}
|
||||
|
||||
# ensure bin/ludicc exists and is current with the seed. Built from the checked-in
|
||||
# IR seed with clang alone — no C compiler is ever involved.
|
||||
fn ensure_ludicc() -> void {
|
||||
function ensure_ludicc() -> void {
|
||||
run("mkdir -p bin build")
|
||||
if (not is_exec("bin/ludicc")) or newer("selfhost/ludicc.seed.ll", "bin/ludicc") {
|
||||
print("cc: selfhost/ludicc.seed.ll -> bin/ludicc (from the IR seed, no C compiler)")
|
||||
|
|
@ -90,7 +90,7 @@ fn ensure_ludicc() -> void {
|
|||
|
||||
# ---- selfhost-build: assemble + compile the self-host compiler ---------------
|
||||
# usage: x selfhost-build [ludicc] [outbin] (defaults: bin/ludicc, build/selfhost)
|
||||
fn cmd_selfhost_build(lc: ptr, outbin: ptr) -> int {
|
||||
function cmd_selfhost_build(lc: ptr, outbin: ptr) -> int {
|
||||
run("mkdir -p build")
|
||||
let src = "build/selfhost.ludic"
|
||||
if not write_selfhost_src(src) { err("x: cannot write build/selfhost.ludic\n"); return 1 }
|
||||
|
|
@ -104,7 +104,7 @@ fn cmd_selfhost_build(lc: ptr, outbin: ptr) -> int {
|
|||
|
||||
# ---- sh-compile: compile one .ludic with a given selfhost binary and link ----
|
||||
# usage: x sh-compile <selfhost-binary> <input.ludic> <output-binary>
|
||||
fn cmd_sh_compile(shbin: ptr, in: ptr, outbin: ptr) -> int {
|
||||
function cmd_sh_compile(shbin: ptr, in: ptr, outbin: ptr) -> int {
|
||||
if not shq(`{shbin} {in} > {outbin}.ll`) { return 1 }
|
||||
if not shq(`{cc()} {outbin}.ll -o {outbin}`) { return 1 }
|
||||
return 0
|
||||
|
|
@ -116,7 +116,7 @@ fn cmd_sh_compile(shbin: ptr, in: ptr, outbin: ptr) -> int {
|
|||
|
||||
# the quiet core, reused by the test suites; returns true on success. On failure
|
||||
# the self-host/link diagnostics are left in /tmp/x_gb.err.
|
||||
fn game_build_ok(shbin: ptr, game: ptr, outbin: ptr) -> bool {
|
||||
function game_build_ok(shbin: ptr, game: ptr, outbin: ptr) -> bool {
|
||||
let ll = `{outbin}.ll`
|
||||
if not shq(`{shbin} {game} > {ll} 2>/tmp/x_gb.err`) { return false }
|
||||
if not shq(`{cc()} -O2 {ll} -o {outbin} 2>/tmp/x_gb.err`) { return false }
|
||||
|
|
@ -125,7 +125,7 @@ fn game_build_ok(shbin: ptr, game: ptr, outbin: ptr) -> bool {
|
|||
}
|
||||
|
||||
# usage: x game-build <selfhost-binary> <game.ludic> <out-binary>
|
||||
fn cmd_game_build(shbin: ptr, game: ptr, outbin: ptr) -> int {
|
||||
function cmd_game_build(shbin: ptr, game: ptr, outbin: ptr) -> int {
|
||||
if game_build_ok(shbin, game, outbin) { print(`built {outbin}`); return 0 }
|
||||
print("build failed:")
|
||||
out(capture("grep -i error /tmp/x_gb.err | head -5"))
|
||||
|
|
@ -136,7 +136,7 @@ fn cmd_game_build(shbin: ptr, game: ptr, outbin: ptr) -> int {
|
|||
# stage0 bin/ludicc compiles the self-host source -> gen1 (built by a different
|
||||
# compiler, so its IR legitimately differs); gen1 -> gen2, gen2 -> gen3, and
|
||||
# gen2.ll == gen3.ll is the fixpoint that proves the compiler reproduces itself.
|
||||
fn cmd_bootstrap() -> int {
|
||||
function cmd_bootstrap() -> int {
|
||||
ensure_ludicc()
|
||||
run("mkdir -p build/boot")
|
||||
if cmd_selfhost_build("bin/ludicc", "build/boot/gen1") != 0 { print("FAIL: stage0 build"); return 1 }
|
||||
|
|
@ -163,7 +163,7 @@ fn cmd_bootstrap() -> int {
|
|||
# The seed is the compiler's own IR, a proven fixed point: clang assembles it,
|
||||
# the seed-built compiler compiles its own source, and the result must equal the
|
||||
# seed byte for byte.
|
||||
fn cmd_bootstrap_cfree() -> int {
|
||||
function cmd_bootstrap_cfree() -> int {
|
||||
run("mkdir -p build/cfree")
|
||||
if not shq(`{cc()} selfhost/ludicc.seed.ll -o build/cfree/sh_seed 2>/dev/null`) { print("FAIL: assemble seed"); return 1 }
|
||||
print(" seed.ll --clang--> sh_seed (no C compiler used)")
|
||||
|
|
@ -184,7 +184,7 @@ fn cmd_bootstrap_cfree() -> int {
|
|||
# fixed point of the NEW compiler, not a one-step image of the old. Falls back to
|
||||
# bin/ludicc only if the seed itself no longer assembles. Verify with
|
||||
# `x bootstrap-cfree` afterwards.
|
||||
fn cmd_reseed() -> int {
|
||||
function cmd_reseed() -> int {
|
||||
run("mkdir -p build/cfree")
|
||||
if not write_selfhost_src("build/cfree/selfhost.ludic") { print("FAIL: write source"); return 1 }
|
||||
if shq(`{cc()} selfhost/ludicc.seed.ll -o build/cfree/sh_old 2>/dev/null`) {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
# fixpoint). Replaces selfhost/test.sh.
|
||||
|
||||
# compile selfhost/tests/<name>.ludic with bin/ludicc, assemble, run, compare
|
||||
fn sh_case(name: ptr, exp: ptr) -> void {
|
||||
function sh_case(name: ptr, exp: ptr) -> void {
|
||||
let ll = `/tmp/x_sh_{name}.ll`
|
||||
let er = `/tmp/x_sh_{name}.err`
|
||||
let bn = `/tmp/x_sh_{name}`
|
||||
|
|
@ -16,14 +16,14 @@ fn sh_case(name: ptr, exp: ptr) -> void {
|
|||
}
|
||||
|
||||
# compile examples/<name>.ludic as a game and diff its render against the golden
|
||||
fn game_case(name: ptr, keys: ptr) -> void {
|
||||
function game_case(name: ptr, keys: ptr) -> void {
|
||||
if not game_build_ok("bin/ludicc", `examples/{name}.ludic`, `/tmp/x_g_{name}`) { bad(`{name}: build`); return }
|
||||
run(`printf '%s' '{keys}' | /tmp/x_g_{name} >/dev/null 2>&1`)
|
||||
if shq(`cmp -s selfhost/golden/{name}.ppm out.ppm`) { ok(`{name} matches the golden render`) }
|
||||
else { bad(`{name}: differs from golden`) }
|
||||
}
|
||||
|
||||
fn cmd_selfhost_test() -> int {
|
||||
function cmd_selfhost_test() -> int {
|
||||
PASS = 0
|
||||
FAIL = 0
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
# compile a feature example as a game, run it (optional keys on stdin) and match
|
||||
# its stdout against the expected space-joined output.
|
||||
fn feat_case(name: ptr, keys: ptr, exp: ptr, label: ptr) -> void {
|
||||
function feat_case(name: ptr, keys: ptr, exp: ptr, label: ptr) -> void {
|
||||
if not game_build_ok("bin/ludicc", `examples/{name}.ludic`, `/tmp/x_e_{name}`) {
|
||||
bad2(label, capture_line("grep -i error /tmp/x_gb.err | head -1")); return
|
||||
}
|
||||
|
|
@ -20,7 +20,7 @@ fn feat_case(name: ptr, keys: ptr, exp: ptr, label: ptr) -> void {
|
|||
}
|
||||
|
||||
# a "does it still compile" smoke test (parse -> lower -> link), no run
|
||||
fn qsmoke(name: ptr) -> void {
|
||||
function qsmoke(name: ptr) -> void {
|
||||
if game_build_ok("bin/ludicc", `examples/{name}.ludic`, `/tmp/x_q_{name}`) {
|
||||
ok(`{name} compiles (@Queries desugars to S_QUERY)`)
|
||||
} else { bad2(name, capture_line("grep -i error /tmp/x_gb.err | head -1")) }
|
||||
|
|
@ -28,7 +28,7 @@ fn qsmoke(name: ptr) -> void {
|
|||
|
||||
# a pure-Ludic example that drives and asserts itself: compile headless to IR,
|
||||
# let clang assemble it, run, and compare stdout. No C host and no C compiled.
|
||||
fn net_case(name: ptr, exp: ptr) -> void {
|
||||
function net_case(name: ptr, exp: ptr) -> void {
|
||||
let ll = `/tmp/x_n_{name}.ll`
|
||||
let log = `/tmp/x_n_{name}.out`
|
||||
if not shq(`bin/ludicc --headless examples/{name}.ludic --emit-llvm -o {ll} > {log} 2>&1`) {
|
||||
|
|
@ -43,7 +43,7 @@ fn net_case(name: ptr, exp: ptr) -> void {
|
|||
if (got == exp) { ok(`{name} ({got})`) } else { bad2(name, `got [{got}] want [{exp}]`) }
|
||||
}
|
||||
|
||||
fn cmd_test() -> int {
|
||||
function cmd_test() -> int {
|
||||
PASS = 0
|
||||
FAIL = 0
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
# driven through python3/node (they are not project shell scripts), invoked here.
|
||||
|
||||
# refresh one editor-copy of a shared grammar file if it has drifted
|
||||
fn sync_one(shared: ptr, dst: ptr) -> void {
|
||||
function sync_one(shared: ptr, dst: ptr) -> void {
|
||||
if not shq(`cmp -s {shared} {dst}`) {
|
||||
run(`cp {shared} {dst}`)
|
||||
print(`sync: {shared} -> {dst}`)
|
||||
|
|
@ -16,7 +16,7 @@ fn sync_one(shared: ptr, dst: ptr) -> void {
|
|||
|
||||
# VS Code needs the grammar inside its own extension directory, so it gets a
|
||||
# copy that build refreshes rather than trusts (test-tools asserts they match).
|
||||
fn sync_vscode_grammar() -> void {
|
||||
function sync_vscode_grammar() -> void {
|
||||
if not shq("test -d tools/editors/vscode") { return }
|
||||
run("mkdir -p tools/editors/vscode/syntaxes")
|
||||
sync_one("tools/editors/shared/ludic.tmLanguage.json", "tools/editors/vscode/syntaxes/ludic.tmLanguage.json")
|
||||
|
|
@ -25,7 +25,7 @@ fn sync_vscode_grammar() -> void {
|
|||
}
|
||||
|
||||
# is `flag` present anywhere in argv?
|
||||
fn has_flag(flag: ptr) -> bool {
|
||||
function has_flag(flag: ptr) -> bool {
|
||||
var i = 2
|
||||
while i < arg_count() { if (arg(i) == flag) { return true }; i = i + 1 }
|
||||
return false
|
||||
|
|
@ -33,7 +33,7 @@ fn has_flag(flag: ptr) -> bool {
|
|||
|
||||
# ---- tools: build the editor toolchain --------------------------------------
|
||||
# usage: x tools [--install] [--test]
|
||||
fn cmd_tools() -> int {
|
||||
function cmd_tools() -> int {
|
||||
ensure_ludicc()
|
||||
print("ludicc: tools/ludic-tools/fmt.ludic -> bin/ludic-fmt (Ludic -> LLVM IR -> binary, no C)")
|
||||
if not build_tool("ludic-fmt", "tools/ludic-tools/fmt.ludic") { return 1 }
|
||||
|
|
@ -53,7 +53,7 @@ fn cmd_tools() -> int {
|
|||
}
|
||||
|
||||
# ---- test-tools: the editor-toolchain regression suite -----------------------
|
||||
fn cmd_test_tools() -> int {
|
||||
function cmd_test_tools() -> int {
|
||||
PASS = 0
|
||||
FAIL = 0
|
||||
let fmt = "bin/ludic-fmt"
|
||||
|
|
@ -172,13 +172,13 @@ fn cmd_test_tools() -> int {
|
|||
return report()
|
||||
}
|
||||
|
||||
fn test_json(path: ptr) -> void {
|
||||
function test_json(path: ptr) -> void {
|
||||
let bn = capture_line(`basename {path}`)
|
||||
if shq(`python3 -c 'import json,sys;json.load(open(sys.argv[1]))' {path} 2>/dev/null`) {
|
||||
ok(`valid JSON: {bn}`)
|
||||
} else { bad(`invalid JSON: {path}`) }
|
||||
}
|
||||
fn test_xml(path: ptr) -> void {
|
||||
function test_xml(path: ptr) -> void {
|
||||
let bn = capture_line(`basename {path}`)
|
||||
if shq(`python3 -c 'import xml.dom.minidom,sys;xml.dom.minidom.parse(sys.argv[1])' {path} 2>/dev/null`) {
|
||||
ok(`valid XML: {bn}`)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue