Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.3

0.7.3 / src / code / api.lucb

# Luce's view: `Code.check` for the editor (diagnostics and spare parameters, no
# geometry) and `Code.run` for the cook.

## One diagnostic, in the user's terms.
struct CheckLine:
    let part: i64
    let line: i64
    let column: i64
    let end: i64
    let severity: i64
    let message: str

## One spare parameter: a uniform the snippet asks the node for.
struct CheckParameter:
    let name: str
    var components: i64
    let integer: bool
    var defaults: f64[4]
    ## 1 a float ramp, 3 a color ramp, 0 a number or vector.
    var ramp: i64 = 0

## What checking a snippet found: its diagnostics (positions in characters, 1-based),
## its spare parameters in order of first use, and whether it reads the timeline.
pub struct CodeCheck:
    var passed: bool
    var lines: collections.List[CheckLine]
    var parameters: collections.List[CheckParameter]
    var timeline: bool
    var texts: collections.TextPool

    ## Whether the snippet checked and lowered.
    pub func ok() -> bool:
        return self.passed

    ## How many diagnostics there are, and each one's part (0 body, 1 header), line,
    ## column and end column (characters, end exclusive), severity (0 error, 1 warning,
    ## 2 info) and message. Out-of-range indices answer 0 or "".
    pub func diagnostic_count() -> i64:
        return (i64)self.lines.length()

    pub func diagnostic_part(index: i64) -> i64:
        return self.line_at(index).part

    pub func diagnostic_line(index: i64) -> i64:
        return self.line_at(index).line

    pub func diagnostic_column(index: i64) -> i64:
        return self.line_at(index).column

    pub func diagnostic_end(index: i64) -> i64:
        return self.line_at(index).end

    pub func diagnostic_severity(index: i64) -> i64:
        return self.line_at(index).severity

    pub func diagnostic_message(index: i64) -> str:
        return self.line_at(index).message

    ## How many spare parameters there are, and each one's name, components (1 to 4; 0
    ## for a ramp), whether it is an integer, and the default of a component.
    pub func parameter_count() -> i64:
        return (i64)self.parameters.length()

    pub func parameter_name(index: i64) -> str:
        if index < 0 or (usize)index >= self.parameters.length(): return ""
        return self.parameters.at((usize)index).name

    pub func parameter_components(index: i64) -> i64:
        if index < 0 or (usize)index >= self.parameters.length(): return 0
        return self.parameters.at((usize)index).components

    pub func parameter_integer(index: i64) -> bool:
        if index < 0 or (usize)index >= self.parameters.length(): return false
        return self.parameters.at((usize)index).integer

    pub func parameter_default(index: i64, component: i64) -> f64:
        if index < 0 or (usize)index >= self.parameters.length() or component < 0 or component > 3: return 0.0
        return self.parameters.at((usize)index).defaults[(usize)component]

    ## Whether parameter `index` is a ramp: 1 a float ramp (`k.ramp`), 3 a color ramp
    ## (`k.ramp_color`), 0 a number or vector. A ramp has 0 components and no default.
    pub func parameter_ramp(index: i64) -> i64:
        if index < 0 or (usize)index >= self.parameters.length(): return 0
        return self.parameters.at((usize)index).ramp

    ## Whether the snippet reads `k.frame`, `k.time` or `k.fps`.
    pub func reads_time() -> bool:
        return self.timeline

    func line_at(index: i64) -> CheckLine:
        if index < 0 or (usize)index >= self.lines.length():
            return CheckLine(part = 0, line = 0, column = 0, end = 0, severity = 0, message = "")
        return self.lines.at((usize)index)

    ## Give back the diagnostics and parameters (Luce calls it when the last owner goes).
    pub func close():
        self.lines.destroy()
        self.parameters.destroy()
        self.texts.destroy()

pub let code_check_type: interop.Type[CodeCheck] = interop.Type[CodeCheck]("CodeCheck", CodeCheck.close, closeable = true)

## A cook's result for the node: the geometry, the snippet's printf output and its
## warnings (from `Code.cook`).
pub struct CodeRun:
    var made: interop.Reference[GeometrySet]
    var report: CookReport

    ## The new geometry.
    pub func geometry() -> interop.Reference[GeometrySet]:
        return self.made.clone()

    ## The printf output, element by element: the first 200 messages, then "... and N
    ## more messages" when there were more ("" when nothing printed).
    pub func output() -> str:
        return (str)self.report.output.view()

    ## How many printf messages the cook made, those cut off included.
    pub func output_count() -> i64:
        return (i64)self.report.printed

    ## The first warning in element order ("" when none) and how many there were.
    pub func warning() -> str:
        return (str)self.report.warning.view()

    pub func warning_count() -> i64:
        return (i64)self.report.warnings

    pub func close():
        self.made.release()
        self.report.close()

pub let code_run_type: interop.Type[CodeRun] = interop.Type[CodeRun]("CodeRun", CodeRun.close, closeable = true)

## Luce's view of the Code node.
pub struct Code:
    let reserved: u8 = 0

    ## Check and lower `body` (and `header`) for Run Over `run_over` (0 detail, 1 points,
    ## 2 vertices, 3 primitives, 4 numbers, 5 voxels) with no geometry: attribute reads take the
    ## type their accessor names.
    pub static func check(run_over: i64, header: str, body: str) -> interop.Reference[CodeCheck]!:
        let mode = try RunOver.of(run_over)
        let entry = try acquire(mode, header, body, none)
        defer release(entry)
        let storage = try new CodeCheck[1] --- in memory.heap
        storage[0] = CodeCheck(passed = false, lines = collections.List[CheckLine](), parameters = collections.List[CheckParameter](), timeline = false, texts = collections.TextPool())
        try fill_check(&storage[0], &entry.compiled, header, body) catch failure:
            storage[0].close()
            free(storage) in memory.heap
            error(failure.code, failure.message)
        return try interop.Reference[CodeCheck].adopt(storage.data, code_check_type, memory.heap)

    ## What may come at line `line`, column `column` (1-based, in characters; the caret
    ## sits before that column) of `part` (0 body, 1 header) of the snippet for Run Over
    ## `run_over`: after `p.`, `k.`, `g.` or any value's `.` that type's members, else the
    ## locals in scope, the header's helpers, the lib and Base's keywords. Half-typed code
    ## still completes (see CodeCompletion).
    pub static func complete(run_over: i64, header: str, body: str, part: i64, line: i64, column: i64) -> interop.Reference[CodeCompletion]!:
        return try complete_code(run_over, header, body, part, line, column)

    ## The signature and doc of the word at `line`, `column` of `part` (a lib function, an
    ## API member, a local's type), "" when nothing applies.
    pub static func hover(run_over: i64, header: str, body: str, part: i64, line: i64, column: i64) -> interop.Owned[str]!:
        return try hover_code(run_over, header, body, part, line, column)

    ## Run `body` (and `header`) over `set` for Run Over `run_over`, on the elements of
    ## `group` (a group expression read on `group_type`'s domain, 0 guessing; empty: all).
    ## `k.input(0)` reads `set` as it was, `k.input(1)` reads `input1` (none: an empty
    ## geometry, as when the second input is not connected); edits the snippet queues
    ## apply after the run. Spare parameters take `values[i]` by `names[i]` (a vector's
    ## components are named `name[0]`, `name[1]`, ...; a parameter not named keeps its
    ## default). A ramp's flat data (luce-std `ramp`) is the name `"<name>#ramp"` with the
    ## data's length L, then L entries named "" holding it; a ramp not sent reads its
    ## kind's default, and data `ramp.check` refuses fails at the line naming the ramp.
    ## `count` is the lane count of Run Over Numbers. A fault, `k.error("text")` or a
    ## snippet that does not check fails with "line L, column C: message (point N)"
    ## (`header line ...` in the header). The result: the geometry, the snippet's
    ## `k.printf` output and its `k.warning`s (see CodeRun). Run Over Voxels (5) runs
    ## over the active voxels of grid `volume` ("" the first grid, "#2" the third),
    ## after making every voxel of its leaves' box active when `dense`; without that
    ## grid the set passes through.
    pub static func cook(set: interop.Reference[GeometrySet], run_over: i64, header: str, body: str, group: str, group_type: i64, names: const str[], values: const f64[], frame: f64, time: f64, fps: f64, count: i64, input1: interop.Reference[GeometrySet]? = none, volume: str = "", dense: bool = false) -> interop.Reference[CodeRun]!:
        var second: const GeometrySet*? = none
        if let given = input1: second = try given.get()
        var report = CookReport()
        let made = run_code(try set.get(), second, run_over, header, body, group, group_type, names, values, frame, time, fps, count, 0, &report, volume, dense) catch failure:
            report.close()
            error(failure.code, failure.message)
        let geometry = adopt_set(made) catch failure:
            report.close()
            error(failure.code, failure.message)
        let storage = new CodeRun[1] --- in memory.heap catch failure:
            geometry.release()
            report.close()
            error(failure.code, failure.message)
        storage[0] = CodeRun(made = geometry, report = report)
        return try interop.Reference[CodeRun].adopt(storage.data, code_run_type, memory.heap)

    ## `cook`'s geometry alone (the printf output and warnings are dropped).
    pub static func run(set: interop.Reference[GeometrySet], run_over: i64, header: str, body: str, group: str, group_type: i64, names: const str[], values: const f64[], frame: f64, time: f64, fps: f64, count: i64, input1: interop.Reference[GeometrySet]? = none, volume: str = "", dense: bool = false) -> interop.Reference[GeometrySet]!:
        var second: const GeometrySet*? = none
        if let given = input1: second = try given.get()
        let made = try run_code(try set.get(), second, run_over, header, body, group, group_type, names, values, frame, time, fps, count, 0, none, volume, dense)
        return try adopt_set(made)

## Copy a compiled snippet's diagnostics and parameters into `check`.
func fill_check(check: CodeCheck*, compiled: const Compiled*, header: str, body: str) -> !:
    check.passed = compiled.ok()
    for d in compiled.diagnostics():
        let part: i64 = 1 if d.part == .header else 0
        let text = line_of(part_text(d.part, header, body), d.line)
        let wrapper = d.part == .wrapper
        let severity: i64 = 0 if d.severity == .error else (1 if d.severity == .warning else 2)
        let message = try check.texts.keep_text(d.message.bytes)
        let column = 1 if wrapper else (i64)character_column(text, d.column)
        let end = 2 if wrapper else (i64)word_end(text, d.column)
        try check.lines.append(CheckLine(part = part, line = 1 if wrapper else (i64)d.line, column = column, end = end, severity = severity, message = message))
    if not compiled.ok():
        return
    for slot in compiled.program.uniforms():
        let name = slot.name
        if name == "frame" or name == "time" or name == "fps":
            check.timeline = true
            continue
        if name == "numpt" or name == "numprim" or name == "numvtx" or strings.starts_with(name, "detail:"):
            continue
        let (base, component) = split_component(name)
        let value = default_number(slot.kind, slot.default)
        var found = false
        for at in 0..<check.parameters.length():
            var parameter = check.parameters.at(at)
            if parameter.name == base:
                if component < 4:
                    parameter.defaults[component] = value
                    if (i64)component + 1 > parameter.components: parameter.components = (i64)component + 1
                check.parameters.set(at, parameter)
                found = true
        if not found:
            var defaults: f64[4]
            if component < 4: defaults[component] = value
            let kept = try check.texts.keep_text(base.bytes)
            let integer = slot.kind == .i32 or slot.kind == .i64
            try check.parameters.append(CheckParameter(name = kept, components = (i64)component + 1, integer = integer, defaults = defaults))
    # Ramps, by the calls that read them.
    let program = &compiled.program
    for site in 0..<program.call_count():
        let kind = ramp_kind(program.call_name(site))
        if kind == 0: continue
        let name = program.call_text(site, 0)
        var known = false
        for parameter in check.parameters.view(): known = known or parameter.name == name
        if known: continue
        let kept = try check.texts.keep_text(name.bytes)
        try check.parameters.append(CheckParameter(name = kept, components = 0, integer = false, defaults = [0.0, 0.0, 0.0, 0.0], ramp = kind))

## A uniform's default bits as a number.
func default_number(kind: Kind, raw: u64) -> f64:
    return match kind:
        .f32 => (f64)f32.bits((u32)raw)
        .f64 => f64.bits(raw)
        .i32 => (f64)(i32)(u32)raw
        .i64 => (f64)(i64)raw
        _ => (f64)raw