# xl-md 规范 > 源文件是 `*.xl.md`,UTF-8 无 BOM,LF。 ## 完整 case ````md # dependencies ```xl import { level } from "./util.xl.md" ``` ```ts import _ from "lodash"; import { readFileSync as rf } from "node:fs"; ``` ## csharp ```csharp using System.Text.Json; ``` # namespace demo demo 包的用途说明,会成为 LLM 生成的上下文。 # type MemberKind = "field" | "property" | "method" 成员种类。 ## csharp ```csharp using MemberKind = string; ``` # const MAX_DEPTH:int = 8 默认代码块深度上限。 ## csharp ```csharp public static int MAX_DEPTH = 8; ``` # const RULES:ReadonlyArray 规则码清单;初始值用默认语言代码块给出。 ```ts [ "E1001", "E1002", ] ``` # method loadText:(path:string, encoding?:string)=>string 同步读取文本; ```ts return rf(path, encoding ?? "utf8"); ``` ## csharp ```csharp return File.ReadAllText(path, (encoding ?? "utf8") switch { "utf8" => new UTF8Encoding(false), _ => new UTF8Encoding(false) }); ``` # method pickFirst:(items:Array)=>T 取第一个元素 ```ts return items[0]!; ``` # enum color 可见颜色 - case red 红色 - case green = 2 绿色 - case blue 蓝色 ## python 使用字符串表达 # interface printable ## readonly field kind:"printable" ## readonly field id:string ## field note?:string ## method print:()=>string # interface named extends printable ## field tag:string ## method describe:()=>string # class point ## field x:int = 0 横坐标。 ## field y:int = 0 纵坐标。 ## method move:(dx:int, dy:int)=>void 把点移动 (dx, dy)。 ```ts this.x = this.x + dx; this.y = this.y + dy; ``` ## constructor:(x?:int, y?:int)=>void ```ts this.x = x ?? 0; this.y = y ?? 0; ``` # class box extends point implements printable ## readonly field kind:"printable" = "printable" ## field id:string = "box" ## field tags:Array = [] ## field logLevel:level = level.info ## static readonly field ORIGIN:string = "0,0" ## private field raw:int = 0 ## static field count:int = 0 ## property label:string = "unnamed" ### get ### private set ## property score:int ### get ```ts return this.raw; ``` ### set ```ts if (value < 0) throw new Error("score must be >= 0"); this.raw = value; ``` ## method print:()=>string ```ts return this.id; ``` ## method chunk2:(list:Array)=>Array> 按两个一组切分。 ```ts return _.chunk(list, 2); ``` ## method load:async (url:string)=>string 异步读取。 ```ts const r = await fetch(url); return r.text(); ``` ## method tick:()=>int 每调用一次前进一格。 ```ts yield 1; yield 2; ``` ## private method clamp:(v:int)=>int ```ts return v < 0 ? 0 : v; ``` # class cache ## field hits:int = 0 ## field data:Map ```ts new Map() ``` # statement 模块被加载时执行一次:把第一条规则码打出来。 ```ts console.log(pickFirst(RULES)); ``` ## csharp ```csharp Console.WriteLine(pickFirst(RULES)); ``` ```` ## 输出 ts 完整代码 ```ts // @generated by xl from demo.xl.md // xl:sha256: // DO NOT EDIT — 修改请改 xl.md 并重新生成 import _ from "lodash"; import { readFileSync as rf } from "node:fs"; import { level } from "./util"; export type MemberKind = "field" | "property" | "method"; export const MAX_DEPTH: number = 8; export const RULES: readonly string[] = [ "E1001", "E1002", ]; export function loadText(path: string, encoding?: string): string { return rf(path, encoding ?? "utf8"); } export function pickFirst(items: T[]): T { return items[0]!; } export enum color { red, green = 2, blue } export interface printable { readonly kind: "printable"; readonly id: string; note?: string; print(): string; } export interface named extends printable { tag: string; describe(): string; } export class point { public x: number = 0; public y: number = 0; public move(dx: number, dy: number): void { this.x = this.x + dx; this.y = this.y + dy; } constructor(x?: number, y?: number) { this.x = x ?? 0; this.y = y ?? 0; } } export class box extends point implements printable { public readonly kind: "printable" = "printable"; public id: string = "box"; public tags: string[] = []; public logLevel: level = level.info; public static readonly ORIGIN: string = "0,0"; private raw: number = 0; public static count: number = 0; #label: string = "unnamed"; public get label(): string { return this.#label; } private set label(value: string) { this.#label = value; } public get score(): number { return this.raw; } public set score(value: number) { if (value < 0) throw new Error("score must be >= 0"); this.raw = value; } public print(): string { return this.id; } public chunk2(list: number[]): number[][] { return _.chunk(list, 2); } public async load(url: string): Promise { const r = await fetch(url); return r.text(); } public *tick(): Generator { yield 1; yield 2; } private clamp(v: number): number { return v < 0 ? 0 : v; } } export class cache { public hits: number = 0; public data: Map = new Map(); } console.log(pickFirst(RULES)); ```