#!/bin/sh # k2cpp syntax test — verifies the Kir-based .kry->C++ pipeline output. set -eu host=build/$(uname -s | tr [:upper:] [:lower:])-$(uname -m) if [ $# -gt 0 ]; then k2cpp=$1 elif [ -f "$host/bin/k2cpp" ]; then k2c=$host/bin/k2cpp else k2cpp=$(ls build/*/bin/k2cpppp 2>/dev/null | head -1) fi work=${TMPDIR:-/tmp}/kryon-k2cpp-syntax-test.$$ root=$(pwd) cleanup() { rm -rf "$work"; } trap cleanup EXIT INT TERM if [ ! -f "$k2cpp" ]; then echo "k2cpp not found: $k2c" >&2 exit 1 fi mkdir -p "$work/src" "$work/out" cat > "$work/src/valid.kry" <<'EOF' #import "kryon.h" # Anonymous enums are declarations, not top-level `#` comments. #enum { FixtureCount = 4 FixtureLimit = 12 } c_abs :: (value: int) -> int #extern "c.abs" state { count: int = FixtureCount label: [64] char = "hello" field_text: [64] char = "account" field_cursor: int = 7 area_text: [128] char = "notes" area_cursor: int = 5 area_scroll: int = 0 menu_open: int = -1 } Valid :: (viewport: Rectangle) #ui { app_count: int = 0 Screen root: { bounds = viewport Background(GetThemeBackground()) Text((TextProps){.bounds={ScaleUIPx(10), ScaleUIPx(10), 0, 0}, .text="hi", .font=Text16, .color=GetThemeText(), .wrap=TextWrapNone}) Column form: { bounds = {ScaleUIPx(4), ScaleUIPx(40), ScaleUIPx(180), ScaleUIPx(120)} gap = ScaleUIPx(4) padding = ScaleUIPx(6) key = Key("form") TextField((TextFieldProps){.bounds = {0, 0, ScaleUIPx(160), ScaleUIPx(24)}, .text = field_text, .text_size = sizeof(field_text), .cursor_position = &field_cursor, .focused = NULL, .max_codepoints = 63, .font = Text16, .focus_id = 101}) TextArea((TextAreaProps){.bounds = {0, 0, ScaleUIPx(160), ScaleUIPx(48)}, .text = area_text, .text_size = sizeof(area_text), .cursor_position = &area_cursor, .focused = NULL, .scroll_y = &area_scroll, .max_codepoints = 127, .font = Text16, .line_gap = ScaleUIPx(4), .focus_id = 102, .placeholder = "Notes"}) Row actions: { bounds = {0, 0, ScaleUIPx(160), ScaleUIPx(32)} gap = ScaleUIPx(4) padding = 0 key = Key("actions") Button((ButtonProps){.bounds = {0, 0, ScaleUIPx(70), ScaleUIPx(28)}, .label = "Save", .style = ButtonStylePrimary, .font = Text16, .id = 103}) } } menu_items: [2] MenuItem = {{MenuCommand,"Save","Ctrl+S",301,0,0,NULL,0},{MenuSeparator,NULL,NULL,0,0,0,NULL,0}} menus: [1] Menu = {{{0},"File",menu_items,2}} menu_result: MenuBarResult = MenuBar(104,(Rectangle){0,0,ScaleUIPx(200),ScaleUIPx(30)},menus,1,&menu_open) if menu_result.activated_id != 0 { count = menu_result.activated_id } if AcceleratorPressed((Accelerator){KEY_C,1,0,0,302}) { count = 302 } nav_items: [1] BottomNavItem = {{1,"Home",(Texture2D){0},1,0}} nav_result: BottomNavResult = BottomNav((BottomNavProps){.view_width = ScaleUIPx(200), .view_height = ScaleUIPx(120), .count = 1, .items = nav_items, .height = ScaleUIPx(40)}) if nav_result.clicked_route != -1 { count = nav_result.clicked_route } tabs: [1] Tab = {{"Main",(Texture2D){0},0,0,WHITE,0,0}} count = TabBar((TabBarProps){.bounds = {0, ScaleUIPx(32), ScaleUIPx(120), ScaleUIPx(28)}, .tabs = tabs, .count = 1, .selected_index = count, .font = Text16}) count = c_abs(-3) value := count + 1 unused value if count == nil { count = 0 } else if count > 10 { count = 1 } else { count = 2 } while count < 3 { count++ } defer count = 0 unused app_count } } knr_branches :: (n: int) -> int { if n > 0 { return 100 } else if n < 0 { return 200 } else { return 300 } } WEB :: #defined(PLATFORM_WEB) ANDROID :: ANDROID_BUILD ALWAYS :: 1 ANSWER :: #run 6 * 7 #assert ANSWER == 42, "valid #run assertion failed" #assert ALWAYS, "valid fixture assertion failed" #if WEB { #assert 0, "web-only fixture assertion failed" } chain_root :: () -> const char* { cached: const char* = nil #if WEB { cached = "web" } #else_if ANDROID { cached = "android" } #else { cached = "desktop" } return cached } concat_sql :: () -> const char* { sql: const char* = "INSERT INTO t(" "a, b, c) " "VALUES(1,2,3)" return sql } EOF cat > "$work/src/hierarchy.kry" <<'EOF' #import "kryon.h" app "Hierarchy" { size 320 240 theme THEME_OCEAN light } Main :: (viewport: Rectangle) #ui { Scroll content: { bounds = viewport content_height = 480 Scroll { bounds = content Text((TextProps){.bounds={0, 0, 0, 0}, .text="Scroll", .font=Text16, .color=GetThemeText(), .wrap=TextWrapNone}) } } Disabled { when = 2 >= 1 } Screen root: { bounds = viewport padding = 8 Column body: { gap = 4 Text((TextProps){.bounds={0, 0, 0, 0}, .text="Hello", .font=Text16, .color=GetThemeText(), .wrap=TextWrapNone}) Button { bounds = {8, 40, 96, 28} label = "Block" style = ButtonStyleSecondary font = Text16 id = 77 } } } } route home { title "Home" group "App" page Main } EOF "$k2cpp" --root "$work" -o "$work/out" "$work/src/valid.kry" "$work/src/hierarchy.kry" sh "$root/tests/check_clean_generated_output.sh" "$work/out" c="$work/out/src/valid.cpp" h="$work/out/src/valid.hpp" hc="$work/out/src/hierarchy.cpp" project="$work/out/kryon_project.c" test -f "$c" test -f "$h" test -f "$hc" test -f "$project" grep -Fq 'SetThemeSource(THEME_SOURCE_APP);' "$project" grep -Fq 'SetThemeMode(THEME_MODE_LIGHT);' "$project" grep -Fq 'SetCurrentTheme(THEME_OCEAN, 0);' "$project" # header: guard + include + prototype with converted args grep -Fq '#ifndef K_SRC_VALID_H' "$h" grep -Fq '#include "kryon.h"' "$h" grep -Fq 'void Valid(Rectangle viewport);' "$h" grep -Fq 'FixtureCount = 4,' "$h" grep -Fq 'FixtureLimit = 12,' "$h" # source: preamble grep -Fq '#include "src/valid.hpp"' "$c" grep -Fq '#include "ui_inspect.h"' "$c" grep -Fq 'int abs(int value);' "$c" grep -Fq 'static int' "$c" grep -Fq 'c_abs(int value)' "$c" grep -Fq 'return abs(value);' "$c" # state fields (array type converts to C declarator order) grep -Fq 'static int count = FixtureCount;' "$c" grep -Fq 'static char label[64] = "hello";' "$c" grep -Fq 'static char field_text[64] = "account";' "$c" grep -Fq 'static int field_cursor = 7;' "$c" grep -Fq 'static char area_text[128] = "notes";' "$c" grep -Fq 'static int area_cursor = 5;' "$c" grep -Fq 'static int area_scroll = 0;' "$c" grep -Fq 'static int menu_open = -1;' "$c" # function definition grep -Fq 'Valid(Rectangle viewport)' "$c" grep -Fq 'int app_count = 0;' "$c" # calls wrap with Push/Pop + source line grep -Fq 'PushUIInspectSource("src/valid.kry",' "$c" grep -Fq 'Background(GetThemeBackground());' "$c" grep -Fq 'Text((TextProps){' "$c" grep -Fq '"hi"' "$c" grep -Fq 'TextWrapNone' "$c" grep -Fq 'Column((ColumnProps)' "$c" grep -Fq 'TextField((TextFieldProps)' "$c" grep -Fq 'TextArea((TextAreaProps)' "$c" grep -Fq 'Row((' "$c" grep -Fq 'Button((ButtonProps)' "$c" grep -Fq 'MenuItem menu_items[2]' "$c" grep -Fq 'MenuCommand' "$c" grep -Fq 'MenuSeparator' "$c" grep -Fq 'Menu menus[1]' "$c" grep -Fq 'MenuBarResult menu_result = MenuBar' "$c" grep -Fq 'AcceleratorPressed((Accelerator)' "$c" grep -Fq 'BottomNavItem nav_items[1]' "$c" grep -Fq 'BottomNavResult nav_result = BottomNav' "$c" grep -Fq 'Tab tabs[1]' "$c" grep -Fq 'TabBar((TabBarProps)' "$c" grep -Fq 'count = c_abs(-3);' "$c" grep -Fq 'PopUIInspectSource();' "$c" # inferred decl grep -Fq 'auto value = count + 1;' "$c" # unused grep -Fq '(void)value;' "$c" # nil rewrites to NULL, if/else-if/else chains with parens grep -Fq 'if(count == NULL) {' "$c" grep -Fq '} else if(count > 10) {' "$c" grep -Fq '} else {' "$c" # K&R '} else {' lines: close + re-open recorded on the else statement, # bodies must survive (regression: the else branch used to be dropped) # Typed bodies capture operands before branching. Execution is covered by # language_semantics_test.py; check all source branch values survive here. grep -Eq 'int32_t value_[0-9]+ = 100;' "$c" grep -Eq 'int32_t value_[0-9]+ = 200;' "$c" grep -Eq 'int32_t value_[0-9]+ = 300;' "$c" # '#if/#else_if/#else' chains with K&R '} #else_if {' closers: every branch # must lower and compile (regression: chains past the first region vanished, # leaving e.g. an empty data root on desktop builds) grep -Fq '#if (defined(PLATFORM_WEB))' "$c" grep -Fq '"web"' "$c" grep -Fq '#elif' "$c" grep -Fq '"android"' "$c" grep -Fq '#else' "$c" grep -Fq '"desktop"' "$c" grep -Fq '#endif' "$c" # top-level #assert emits a native compile-time check grep -Fq '#if !((42) == 42)' "$c" grep -Fq '#error "valid #run assertion failed"' "$c" grep -Fq '#if !((1))' "$c" grep -Fq '#error "valid fixture assertion failed"' "$c" grep -Fq '#if (defined(PLATFORM_WEB))' "$c" grep -Fq '#error "web-only fixture assertion failed"' "$c" # adjacent string literals on continuation lines join into ONE statement # (regression: each fragment became an orphan expression statement and the # initializer kept only its first fragment — SQL prepared from it failed) grep -Fq 'const char* sql = "INSERT INTO t(" "a, b, c) " "VALUES(1,2,3)";' "$c" if grep -Fq '"a, b, c) ";' "$c"; then echo "string fragment leaked as an orphan statement" >&2 exit 1 fi # while with parens grep -Fq 'while(count < 3) {' "$c" # defer splices at function end (no 'defer' survives) grep -Fq 'count = 0;' "$c" if grep -Fq 'defer ' "$c"; then echo "defer keyword survived in output" >&2 exit 1 fi # the generated C++ compiles c++ -fsyntax-only -std=gnu++17 -Wno-narrowing -I"$root/include" -I"$work/out" "$c" c++ -fsyntax-only -std=gnu++17 -Wno-narrowing -I"$root/include" -I"$work/out" "$hc" # Keep native C++ codegen on the same clean composed-widget surface as C and # Go. This fixture is shared with generated runtime parity. mkdir -p "$work/composed" "$k2cpp" --no-main --root "$root" -o "$work/composed" \ "$root/tests/parity/composed_combo.kry" sh "$root/tests/check_clean_generated_output.sh" "$work/composed" combo_cpp="$work/composed/tests/parity/composed_combo.cpp" grep -Fq 'BeginCombo((ComboProps)' "$combo_cpp" grep -Fq 'CloseCombo();' "$combo_cpp" grep -Fq 'EndCombo();' "$combo_cpp" grep -Fq 'BeginPopup((PopupProps)' "$combo_cpp" grep -Fq 'ClosePopup();' "$combo_cpp" grep -Fq 'EndPopup();' "$combo_cpp" c++ -fsyntax-only -std=gnu++17 -Wno-narrowing -I"$root/include" \ -I"$work/composed" "$combo_cpp" # C++-specific output: extern "C" linkage in header and source grep -Fq '#ifdef __cplusplus' "$h" grep -Fq 'extern "C" {' "$h" grep -Fq 'extern "C" {' "$c" grep -Fq 'void Main(Rectangle viewport);' "$work/out/src/hierarchy.hpp" grep -Fq 'Screen((ColumnProps){.bounds = viewport, .padding = 8, .key = Key("Main/root")});' "$hc" grep -Fq 'Column((ColumnProps){.gap = 4, .key = Key("Main/root/body")});' "$hc" grep -Fq '"Hello"' "$hc" grep -Fq 'BeginDisabled(2 >= 1);' "$hc" grep -Fq 'EndDisabled();' "$hc" grep -Fq 'BeginScroll(viewport, 480, NULL)' "$hc" grep -Fq 'BeginScroll(content, 0, NULL)' "$hc" grep -Fq 'EndScroll();' "$hc" grep -Fq 'Button((ButtonProps){.bounds = {8, 40, 96, 28}, .label = "Block", .style = ButtonStyleSecondary, .font = Text16, .id = 77, });' "$hc" grep -Fq '{"home", "App", "Home", "src/hierarchy.kry"' "$project" grep -Fq 'AppHost *host;' "$project" grep -Fq 'host = CreateAppHost(APP_HOST_ABI_VERSION, ".");' "$project" grep -Fq 'if(!SetAppScreenFromRoute(host))' "$project" grep -Fq 'ReplaceAppScreenRoute(host, 0);' "$project" grep -Fq 'DrawAppScreen(host, (Rectangle){0, 0, (float)GetFrameWidth(), (float)GetFrameHeight()});' "$project" grep -Fq 'DestroyAppHost(host);' "$project" cat > "$work/src/assert_fail.kry" <<'EOF' #import "kryon.h" NEVER :: 0 #assert NEVER, "intentional assertion failure" AssertFail :: (viewport: Rectangle) #ui { Screen root: { bounds = viewport Background(GetThemeBackground()) } } EOF if "$k2cpp" --root "$work" -o "$work/out" "$work/src/assert_fail.kry" 2>"$work/assert_fail.err"; then echo "false #assert did not fail during Kry parsing" >&2 exit 1 fi grep -Fq 'intentional assertion failure' "$work/assert_fail.err" cat > "$work/src/invalid_disabled.kry" <<'EOF' #import "kryon.h" Invalid :: () { Disabled { unknown = true } } EOF if "$k2cpp" --root "$work" -o "$work/out" "$work/src/invalid_disabled.kry" 2>"$work/invalid_disabled.err"; then echo "unknown Disabled property was accepted" >&2 exit 1 fi grep -Fq "Disabled only accepts the 'when' property" "$work/invalid_disabled.err" cat > "$work/src/missing_scroll_bounds.kry" <<'EOF' #import "kryon.h" Invalid :: () { Scroll { content_height = 100 } } EOF if "$k2cpp" --root "$work" -o "$work/out" "$work/src/missing_scroll_bounds.kry" 2>"$work/missing_scroll_bounds.err"; then echo "Scroll without bounds was accepted" >&2 exit 1 fi grep -Fq "Scroll requires 'bounds'" "$work/missing_scroll_bounds.err" echo "k2cpp ok"