#!/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 k2cpp=$host/bin/k2cpp else k2cpp=$(ls build/*/bin/k2cpppp 2>/dev/null | head -1) fi work=${TMPDIR:-/tmp}/kryon-k2cpp-syntax-test.$$ root=$(pwd) generated_include="$root/$host/generated/src" internal_include="$root/src/ui" internal_header="$root/src/ui/ui_internal.h" cleanup() { rm -rf "$work"; } trap cleanup EXIT INT TERM if [ ! -f "$k2cpp" ]; then echo "k2cpp not found: $k2cpp" >&2 exit 1 fi mkdir -p "$work/src" "$work/out" cat > "$work/src/brand.kss" <<'EOF' @pack brand; Button { background: #123456; foreground: #ffffff; } EOF cat > "$work/src/valid.kry" <<'EOF' #import "kryon.h" #style as tk #style "brand.kss" as brand # 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={Scale(10), Scale(10), 0, 0}, .text="hi", .wrap=TextWrapNone}) Column form: { bounds = {Scale(4), Scale(40), Scale(180), Scale(120)} gap = Scale(4) padding = Scale(6) key = Key("form") TextField((TextFieldProps){.bounds = {0, 0, Scale(160), Scale(24)}, .text = field_text, .text_size = sizeof(field_text), .cursor_position = &field_cursor, .focused = NULL, .max_codepoints = 63, .focus_id = 101}) TextArea((TextAreaProps){.bounds = {0, 0, Scale(160), Scale(48)}, .text = area_text, .text_size = sizeof(area_text), .cursor_position = &area_cursor, .focused = NULL, .scroll_y = &area_scroll, .max_codepoints = 127, .focus_id = 102, .placeholder = "Notes"}) Row actions: { bounds = {0, 0, Scale(160), Scale(32)} gap = Scale(4) padding = 0 key = Key("actions") Button((ButtonProps){.bounds = {0, 0, Scale(70), Scale(28)}, .label = "Save", .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] MenuGroup = {{{0},"File",menu_items,2}} menu_result: MenuResult = Menu((MenuProps){.id = 104, .mode = MenuModeBar, .bounds = {0,0,Scale(200),Scale(30)}, .menus = menus, .menu_count = 1, .open_index = &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] NavigationBarItem = {{1,"Home",(Texture2D){0},ICON_NONE,1,0}} nav_result: NavigationBarResult = NavigationBar((NavigationBarProps){.view_width = Scale(200), .view_height = Scale(120), .count = 1, .items = nav_items, .height = Scale(40)}) if nav_result.clicked_route != -1 { count = nav_result.clicked_route } tabs: [1] Tab = {{"Main",(Texture2D){0},0,0}} count = TabBar((TabBarProps){.bounds = {0, Scale(32), Scale(120), Scale(28)}, .tabs = tabs, .count = 1, .selected_index = count}) 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", .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", .wrap=TextWrapNone}) Button { bounds = {8, 40, 96, 28} label = "Block" tone = ButtonToneNeutral emphasis = ButtonEmphasisSoft 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" grep -Fq 'EnsureBuiltInStylePacks();' "$project" grep -Fq 'SetActiveStylePack("tk");' "$project" grep -Fq 'RegisterStylePackSource("@pack brand;\nButton { background: #123456; foreground: #ffffff; }\n", "brand", "")' "$project" grep -Fq 'SetActiveStylePack("brand");' "$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 'PushInspectSource("src/valid.kry",' "$c" grep -Fq 'Background(GetThemeBackground());' "$c" grep -Fq 'Text(([&]() { TextProps record_value_0{};' "$c" grep -Fq '"hi"' "$c" grep -Fq 'TextWrapNone' "$c" grep -Fq 'Column(([&]() { ColumnProps record_value_0{};' "$c" grep -Fq 'TextField(([&]() { TextFieldProps record_value_0{};' "$c" grep -Fq 'TextArea(([&]() { TextAreaProps record_value_0{};' "$c" grep -Fq 'Row((' "$c" grep -Fq 'Button(([&]() { ButtonProps record_value_0{};' "$c" grep -Fq 'MenuItem menu_items[2]' "$c" grep -Fq 'MenuCommand' "$c" grep -Fq 'MenuSeparator' "$c" grep -Fq 'MenuGroup menus[1]' "$c" grep -Fq 'MenuResult menu_result = Menu' "$c" grep -Fq 'AcceleratorPressed((Accelerator)' "$c" grep -Fq 'NavigationBarItem nav_items[1]' "$c" grep -Fq 'NavigationBarResult nav_result = NavigationBar' "$c" grep -Fq 'Tab tabs[1]' "$c" grep -Fq 'TabBar(([&]() { TabBarProps record_value_0{};' "$c" grep -Fq 'count = c_abs(-3);' "$c" grep -Fq 'PopInspectSource();' "$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"$generated_include" -I"$internal_include" -I"$work/out" "$c" c++ -fsyntax-only -std=gnu++17 -Wno-narrowing -I"$root/include" \ -I"$generated_include" -I"$internal_include" -I"$work/out" \ -include "$internal_header" "$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_popup.kry" sh "$root/tests/check_clean_generated_output.sh" "$work/composed" popup_cpp="$work/composed/tests/parity/composed_popup.cpp" grep -Fq 'BeginPopup(([&]() { PopupProps record_value_0{};' "$popup_cpp" grep -Fq 'context_open = 0;' "$popup_cpp" grep -Fq 'popup_content_open = 0;' "$popup_cpp" grep -Fq 'popup_open = 0;' "$popup_cpp" grep -Fq 'EndPopup();' "$popup_cpp" c++ -fsyntax-only -std=gnu++17 -Wno-narrowing -I"$root/include" \ -I"$generated_include" -I"$internal_include" -I"$work/composed" \ -include "$internal_header" "$popup_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 record_value_0{}; record_value_0.bounds = viewport; record_value_0.padding = 8; record_value_0.key = Key("Main/root"); return record_value_0; }()));' "$hc" grep -Fq 'Column(([&]() { ColumnProps record_value_0{}; record_value_0.gap = 4; record_value_0.key = Key("Main/root/body"); return record_value_0; }()));' "$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 record_value_0{}; record_value_0.bounds = {8, 40, 96, 28}; record_value_0.label = "Block"; record_value_0.tone = ButtonToneNeutral; record_value_0.emphasis = ButtonEmphasisSoft; record_value_0.id = 77; return record_value_0; }()));' "$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" cat > "$work/src/named_records.kry" <<'EOF' Sample :: struct { first: int second: int omitted: int label: const char* } state { trace: int = 0 } step :: (n: int) -> int { trace = trace * 10 + n return n } check :: () -> int #export { record_value_0: int = 7 value: Sample = (Sample){.second=step(1), .first=step(2), .label="nil (Sample){.first=9}"} if trace != 12 { return 1 } if value.first != 2 { return 2 } if value.second != 1 { return 3 } if value.omitted != 0 { return 4 } if value.label[0] != 'n' { return 5 } captured: Sample = (Sample){.second=record_value_0, .first=3} if captured.second != 7 { return 6 } return 0 } EOF "$k2cpp" --no-main --root "$work" -o "$work/records" "$work/src/named_records.kry" cat > "$work/records/driver.cpp" <<'EOF' #include "src/named_records.cpp" void PushInspectSource(const char *, int) {} void PopInspectSource(void) {} int main() { return check(); } EOF c++ -std=c++17 -I"$root/include" -I"$generated_include" -I"$internal_include" -I"$work/records" "$work/records/driver.cpp" -o "$work/records/check" "$work/records/check" echo "k2cpp ok"