Commit Graph
41 Commits
Author SHA1 Message Date
waozi 2726d54f72 Advance Kry compile-time checks and expression KIR 2026-08-19 18:12:26 -03:00
waozi 49206d66d4 k2c: emit a runnable main for screen-only apps
Projects without an explicit frame hook got a bare ScreenName() call in
the generated main: no drawing frame and no viewport argument, which
did not compile against screens lowered as fn(Rectangle viewport).
Wrap the call in BeginDrawing/BeginUIFrame and pass the screen
rectangle, mirroring what hosts do around DrawAppScreen. The example
font helper is now included in kryon_project.c when the app block asks
for the examples font, and the examples link rule compiles
kryon_project.c alongside the module so the binaries actually contain
main.
2026-08-18 16:55:59 -03:00
waozi 2b700da903 Revert "Fix C-style local declarations in k2c"
This reverts commit 2aae0190b2.
2026-08-17 19:44:34 -03:00
waozi 2aae0190b2 Fix C-style local declarations in k2c 2026-08-17 19:42:58 -03:00
waozi f8f5cd4c71 ui: add retained declarative tree and typed events 2026-08-17 18:55:05 -03:00
waozi f2559ee56c k2c: initializer items take no inspect wrapper 2026-08-17 00:34:26 -03:00
waozi 0efd236bd5 k2c: multi-line array initializers
'name: [N] T = {' opens an initializer: the declaration emits without
a semicolon, item statements join without one, and the brace closes
as '};'. Fixes the stray '{;' that broke compiling generated code
for array initializers spanning PushUIInspectSource lines.
2026-08-17 00:27:34 -03:00
waozi 4622c9a374 k2c: emit '#define' constants in the generated header
'Name :: #define value' constants emitted in the .c only, but the
header's array bounds and extern declarations reference them (e.g.
extern char pending_path[ANDROID_IMPORT_PATH_MAX]), so every consumer
of the header failed to compile. Emit them in the header too, first and
under their #if guard, exactly like the .c emission.
2026-08-14 22:14:47 -05:00
waozi 577681423a kc is fully gone: CHANGELOG/docs/comments now say k2c
- k2b drops its KC_* macro aliases (56 uses) for the KIR_* names.
- CHANGELOG, plan docs, examples README, scene-source comments all say
  k2c; the removed 512-statement cap entry no longer names the dead
  KC_BODY_MAX constant.
- k2c_lower: fix '&&' within '||' precedence in the member-access skip
  and three scope_stack[scope_top++] sequence-point UBs that -Wextra
  on the Android host build surfaced.
2026-08-14 21:00:34 -05:00
waozi 058c277359 k2c: #private functions are file-static with forward prototypes
Legacy rule restored: non-public functions emit 'static
KRYON_PRIVATE_UNUSED' definitions with no header prototype, so two
modules can each define their own #private 'finish' without a link
collision. Forward static prototypes precede globals/state/functions
because initializers reference private functions ('.is_active =
practice_whm_is_active') before their definition.
2026-08-14 16:49:27 -05:00
waozi 9f6bf22b33 k2c: resolve bare function references passed as call arguments
('set_cb(name)' / 'f(a, name)'), and grow LOWER_TEXT_MAX with
KIR_TEXT_MAX so long initializers stop truncating mid-identifier.
2026-08-14 16:49:27 -05:00
waozi d5473a3233 k2c: guard statements, array params, alias-call stem matching, init rewrites
- 'guard cond' lowers to if(cond) { defers; return; } (legacy rule).
- '[N] T name' parameters emit C declarator order.
- Cross-module alias calls match the import's file-path target against
  the module source stem too — inbe imports name file paths
  ('src/screens/settings/settings_theme'), not dotted module names.
- Global initializers pass through rewrite_body2 (nil -> NULL, local
  function-name resolution) and KIR_TEXT_MAX grew to 4096 so joined
  multi-element initializers stop truncating mid-list.
2026-08-14 16:49:25 -05:00
waozi 76b21a607e kir joiner + k2c name-resolution fixes
- A trailing ':' (open ternary) continues the statement, matching the
  existing leading-':' rule — multi-line ternaries now join.
- A pending that is just '{' is a scope-open, not a compound-literal
  brace: the whole following block used to join into one statement
  ('{ labels: [7] const char* = {...} slot_w := ... for {...}').
- k2c bare-reference resolution never fires for 'name->'/'name.'
  accesses: 'habits->count' resolved the screen name 'habits' into
  'habits_kry_draw' whenever a module had a screen of that name.
2026-08-14 16:48:52 -05:00
waozi dc811b61c0 kir+k2c: '#if' regions lower to C preprocessor conditionals
Ports the legacy compile-conditional model onto Kir instead of skipping
unevaluated regions (which dropped '#if !WEB' desktop code inbe needs):

- kir_parse tracks a frame stack: '#if COND {', '} #else_if COND {',
  '#else'. Conditions expand through 'Name :: expr' constants ('WEB ::
  #defined(PLATFORM_WEB)' -> 'defined(PLATFORM_WEB)', '#defined' ->
  'defined'); unknown identifiers stay for the C preprocessor.
- Top-level captures inside a region are stamped with the combined
  guard (new 'guard' field on KirImport/KirGlobal/KirType/KirStateField/
  KirFunction); k2c wraps every stamped item in '#if cond / #endif' in
  both .h and .c. Body-level regions lower to raw #if/#elif/#else/
  #endif statements with their braces consumed.
- 'static'/'#private' globals no longer emit a header extern; the .c
  definition carries the 'static' prefix.
- 'name :: Type = init #global' splits type/init at ' = ' (the type
  previously swallowed the initializer); '#private' ::-globals capture
  as internal-linkage statics.
- Header emission order fixed: named enums and structs now precede the
  extern globals referencing them.
2026-08-14 16:48:52 -05:00
waozi b81a2bdb72 k2c: array-typed parameters emit C syntax ('T name[N]')
'out_hex: [65] char' lowered as '[65] char out_hex' -- invalid C. Run the
parameter type through split_array_type like globals and fields, so it
emits 'char out_hex[65]'.
2026-08-14 14:47:57 -05:00
waozi aa27c7e07a kir+k2c: capture the 'args' header directive; C-style params pass through
'args <decl>' inside a screen ('args InbeApp *app') appends parameters to
the function signature (colon screens only, top level of the body).
convert_args now emits colon-less parts as-is (C-style 'InbeApp *app')
instead of dropping them.
2026-08-14 13:54:07 -05:00
waozi 44e969d284 kir+k2c: '#private' imports include in the .c only
required=0 marks '#import ... #private'; the header skips them and the
.c preamble includes them (implementation-only headers).
2026-08-14 13:39:28 -05:00
waozi 3f4ee655b8 kir+k2c: named enums ('Name :: enum { ... }' -> typedef enum)
Parser captures ':: enum' bodies like structs (is_enum flag); lowering
emits 'typedef enum {...} Name;' with comma-terminated members.
2026-08-14 13:25:39 -05:00
waozi 274d68b9e2 k2c: angled includes stay angled; extension-less header imports append .h 2026-08-14 13:17:25 -05:00
waozi ba73453d6d k2c_project: drop IDE name from comment (boundary rule) 2026-08-14 13:07:47 -05:00
waozi 15d1247b23 k2c: emit kryon_project.h/.c (app-host ABI) + main() from Kir
k2c_write_project runs after all files lower: kryon_project.h includes
every generated header; kryon_project.c auto-collects routes from public
screen-keyword functions (screen/preview/page/frame/scene/fn — the legacy
exact_name rule: colon helpers stay public-but-unrouted), emits
per-route wrappers dispatching on Rectangle/app-pointer/void args, the
route table, and CreateAppHost/DestroyAppHost over the weak
CreateApp/DestroyApp/ApplyRoute/BeginScreenDraw hooks. main() appends to
kryon_project.c from KirAppMeta (InitWindow/SetTargetFPS/InitUI/theme/
init-frame-shutdown hooks) unless --no-main.

Verified: a frame-app fixture emits a compiling project (main + route +
host ABI); the full krait corpus still compiles clean; make test green.
2026-08-14 13:04:54 -05:00
waozi 2748599ba2 k2c: #global variables have external linkage; header order typedefs->enums->globals->structs
Globals emit without 'static' and get 'extern' declarations in the header
(krait's main.c references istate). Emission order fixed so enum constants
used as global array sizes are declared first.
2026-08-14 09:02:45 -05:00
waozi e6e40d68be k2c: colon functions keep their bare C names; bare-ref resolution only in assignment-RHS
Legacy naming restored: 'Name :: (...) {' functions get the module prefix
but NO _kry_draw suffix (screens keep it) — krait's module.kry references
these exact names. Bare identifier resolution now fires only in '= name'
assignment-RHS position, so locals shadowing function names (strlen(name))
are never rewritten. All 9 krait .kry files (ide/* + modules/kryon) now
compile with zero errors through the pure Kir pipeline.
2026-08-14 08:55:15 -05:00
waozi af3936f684 k2c: don't rewrite member-access calls (x->fn(...) / x.fn(...))
Only bare identifiers followed by '(' resolve to module function C names;
field calls through '->' or '.' stay as-is (function-pointer typedef
fields like ModuleState.file_syntax).
2026-08-14 08:46:48 -05:00
waozi 21e263c521 k2c: krait's full IDE corpus compiles — all 8 generated files, zero errors
- Typed-decl classification requires an identifier LHS (ternary fragments
  like '1 : 0' no longer misclassify as decls).
- Ternary else-branches (': value' leading lines) join via one-line
  read-ahead (lookahead stash).
- Typed decls convert array types to C declarator order with alias-stripped
  bases (temp buffers, no src==dst snprintf UB).

krait ide/*.kry -> C now syntax-checks clean across state/app/editor/
widgets/preview/console/problems/start_page. kryon make test green.
2026-08-14 08:43:18 -05:00
waozi a7197d7f48 k2c: emit #extern prototypes with alias-stripped args
Extern imports emit C prototypes parsed from their signature, converting
args through the real module (aliases resolve).
2026-08-14 08:33:47 -05:00
waozi 9f470e6a94 kir+k2c: typedefs with names, enum commas, type emission order, ternary/&& joins
- '#type' lines build real C declarators (name inserted after '(*' for
  function pointers) and are matched BEFORE the struct '::' catch-all that
  silently swallowed them.
- Enum members emitted with commas (newline-separated .kry members).
- Header emits typedefs, then enums, then structs (structs reference both).
- Ternary '?' and '&&'/'||' line continuations; pointer types ('char*') and
  '<header.h>' closes no longer join.
- Global array sizes strip interior aliases via the body rewriter
  ('[state.MAX]' -> '[MAX]').
2026-08-14 08:28:59 -05:00
waozi e491d932de kir+k2c: '#type' typedefs; angle-bracket includes don't join
'Name :: C-type #type' is captured as a #typedef KirType and emitted as
'typedef C-type;'. The '>' closing '#import <header.h>' no longer triggers
the operator continuation (space required before < and > too).
2026-08-14 08:06:21 -05:00
waozi a25d72b15e kir+k2c: join operator continuations; alias-strip array sizes
Lines ending in a binary operator (&&, ||, +, -, *, /, %, =, <, >, comma)
now continue onto the next line (legacy line_needs_continuation), so
multi-line if conditions stay one statement. ++/-- are excluded. Global
array sizes with alias qualifiers (state.IDE_MAX_PANE_TABS) are stripped.
2026-08-14 07:53:44 -05:00
waozi ebd2d35d4a k2c: cross-module call resolution via a whole-program symbol table
main now parses ALL inputs first (two passes), builds a per-module symbol
table (module slash-path + every function's .kry -> C name), then lowers
with it. alias.fn( calls resolve through the import target's module table
(start.draw_start_page(...) -> ide_start_page_draw_start_page_kry_draw(...));
same-module bare calls still resolve locally.
2026-08-14 07:50:06 -05:00
waozi 4376871a38 k2c: resolve module-local function calls to their full C names
rewrite_body now rewrites bare calls ('draw_pane_group(...)') to the
module-prefixed C name ('ide_app_draw_pane_group_kry_draw(...)') when the
identifier names a function in the same module — the rewrite_kry_expr alias
rule.
2026-08-14 07:37:04 -05:00
waozi 4ab5b277aa kir+k2c: fix UB alias-strip (src==dst snprintf), emit #enum blocks
strip_alias_type(src==dst) is UB via snprintf overlap — it emptied every
struct field type ('left_width;' with no type). Route through a temp.
#enum { A, B } blocks are captured (ENUM mode) and emitted as C enums.
2026-08-14 07:34:55 -05:00
waozi 481913395f kir+k2c: struct fields converted to C declarators; '#' comments skipped in bodies
Struct fields ('name: [N] Type') now emit 'Type name[N];' with alias
stripping, instead of verbatim .kry text. '#' comment lines inside struct
and function bodies are skipped (directives are top-level only).
2026-08-14 07:30:19 -05:00
waozi edc6cb6269 kir+k2c: fix block-brace depth tracking + alias rewrite in body statements
Brace tracking: depth now counts NET braces at paren/bracket depth 0 only —
compound-literal braces inside calls (e.g. (Props){...}) no longer
mis-increment block depth, which previously ended function bodies early and
swallowed following top-level definitions.

Body rewrite: rewrite_nil became rewrite_body(m, ...) which additionally
strips 'alias.' qualifiers from identifiers when the alias is a module
import (state.IDE_PANE_LEFT -> IDE_PANE_LEFT), matching legacy
rewrite_kry_expr's alias rule.
2026-08-14 07:25:20 -05:00
waozi 90b6f7660d kir+k2c: struct types (Name :: struct), return types stop at '{'
KirType captures the struct body verbatim (TYPE mode in the parser); the
lowering emits typedef struct {...} Name; in the header. Return-type
extraction no longer swallows the opening brace of the function body.
2026-08-14 07:17:06 -05:00
waozi 2d43dd2f47 k2c: emit #include for module imports (alias :: #import "path" -> #include "path.h") 2026-08-14 07:12:54 -05:00
waozi 7c8b79f151 kir+k2c: return types, #extern prototypes, #global vars, module prefixes, alias stripping
Frontend (kir_parse.c): extract '-> T' return types from function headers;
capture #extern (body-less prototypes stay TOP-mode); capture
'name :: Type #global' module globals (KirGlobal in kir.h/kir.c).

Lowering (k2c_lower.c): functions named <module>_<name>_kry_draw (module
dots->underscores); return types in prototypes + definitions; #extern
emitted as prototypes only; #global emitted as statics; alias-qualified
types ('st: state.IdeState*') stripped to bare C types when the alias is a
module import of the same module.

Verified: k2c_syntax_test green; k2ir shows module imports captured.
2026-08-14 07:11:27 -05:00
waozi 07222de4f8 k2c: Kir is the only pipeline — delete the entire legacy compiler
The legacy kc line-matcher is gone. k2c is now exclusively:
  .kry -> kir_parse_file() -> KirProgram -> k2c_lower() -> .c/.h

Deleted (the whole legacy pipeline):
- cmd/k2c/kc.c (parse_kry line-matcher, 3300 lines)
- kc_codegen.c (write_generated/write_body_line), kc_defer.c (apply_defers
  brace-counting splice), kc_ast.{c,h} (Phase-1 AST reconstruction),
- kc_ir.c, kc_resolve.c, kc_macros.c, kc_diag.c, kc_project.c, kc_util.c,
- kc_internal.h, and the --emit/--dump-ast/--kir flags.

New k2c main.c is a thin CLI over kir_parse_file + k2c_lower. The lowering
implements the essential translation rules on Kir statements:
- ':=' decls -> __auto_type; typed decls -> 'T x = e;' with {0} zero-init
- multi-assign temps (__kryon_assign_<line>_<k>, temps-first ordering)
- nil -> NULL rewrite; unused X -> (void)X;
- if/else-if/else chains with parens + close-suppression before else
- while/for/switch headers with parens; case at parent indent
- defer registered per scope, spliced LIFO at block close and return
- call statements wrapped in PushUIInspectSource/PopUIInspectSource with
  source spans; goto/labels; state fields with C array declarator order
- screens named <Name>_kry_draw; args 'name: Type' -> 'Type name';
  header guards K_<PATH>_H with _H suffix

tests/k2c_syntax_test.sh rewritten for the Kir-based output (fixture +
assertions + compile check of the generated C). make test green.
2026-08-14 06:59:58 -05:00
waozi 44fe2fcbfe k2c --kir: PushUIInspectSource wrapping for call statements + ui_inspect.h include
Call statements (EXPR kind with a '(' token) now get Push/Pop wrapping using
the KirStmt's source span (path:line). This is the first byte-identity detail
toward matching kc's output. Also adds #include "ui_inspect.h" to the
generated .c preamble.

Example output:
    PushUIInspectSource("test.kry", 4);
    Background(GetThemeBackground());
    PopUIInspectSource();

make test green. Next byte-identity steps: __auto_type for ':=' decls,
function naming (_kry_draw), rewrite_kry_expr (nil->NULL), indent recompute.
2026-08-13 22:29:32 -05:00
waozi 1c5ad13152 k2c: Kir→C lowering skeleton (--kir flag, structurally valid output)
Stage 2 start: k2c now has a --kir flag that parses .kry via the shared
kir_parse_file frontend and lowers KirProgram to .c/.h via k2c_lower.c.

The lowering currently produces structurally valid C (function definitions,
body statements, header guards, includes) but does NOT yet reproduce kc's
byte-for-byte output (PushUIInspectSource wrapping, __auto_type temps, defer
splicing, indent recompute, rewrite_kry_expr, module prefixing). Those land
fixture-by-fixture against the k2c_syntax_test.sh oracle.

The legacy pipeline (parse_kry + write_generated) remains the default path.
k2c links kir.c + kir_parse.c; make test green.
2026-08-13 22:18:22 -05:00
waozi 2f11e1e773 Rename kc to k2c (kry-to-C compiler)
The .kry->C compiler is now k2c, not kc. This is the first step toward making
Kir the single source of truth (k2c and k2b will both lower from Kir).

- cmd/kc/ -> cmd/k2c/ (directory moved; internal file names unchanged).
- Makefile: KC -> K2C, LEGACY_KC -> LEGACY_K2C, build/bin/kc -> build/bin/k2c.
- tests/kc_syntax_test.sh -> tests/k2c_syntax_test.sh (default binary updated).
- examples/Makefile: KC -> K2C; the two stale --emit-krb lines now use k2b.
- make test green (k2c_syntax_test passes via k2c).
2026-08-13 20:09:53 -05:00