about summary refs log tree commit diff stats
path: root/awk/rawk/rawk.awk
diff options
context:
space:
mode:
Diffstat (limited to 'awk/rawk/rawk.awk')
-rw-r--r--awk/rawk/rawk.awk538
1 files changed, 538 insertions, 0 deletions
diff --git a/awk/rawk/rawk.awk b/awk/rawk/rawk.awk
new file mode 100644
index 0000000..c4e2ff1
--- /dev/null
+++ b/awk/rawk/rawk.awk
@@ -0,0 +1,538 @@
+#!/usr/bin/awk -f
+
+# rawk.awk
+
+# Author: @eli_oat
+# License: Public Domain
+# Lets make awk rawk
+
+# =============================================================================
+# Multi-pass compiler
+# =============================================================================
+# 
+# This compiler transforms rawk code into standard awk and smartly includes only
+# those standard library functions you've actually used. It uses a multi-pass 
+# approach to overcome awk's variable scoping limitations and ensure 
+# deterministic compilation.
+#
+# COMPILATION PROCESS:
+#   Pass 1: Collect all input lines into memory
+#   Pass 2: Detect and validate RAWK { ... } block structure
+#   Pass 3: Extract function definitions from within RAWK block
+#   Pass 4: Analyze function calls to determine standard library dependencies
+#   Pass 5: Generate final awk code with smart standard library inclusion
+#
+# LANGUAGE FEATURES:
+#   - Block-based syntax: RAWK { ... } for function definitions
+#   - Functional programming utilities: map, reduce, filter, etc.
+#   - Smart standard library: only includes functions actually used
+#   - Comprehensive error handling with actionable messages
+# =============================================================================
+
+BEGIN {
+    # =============================================================================
+    # INITIALIZATION: Set up data structures for multi-pass compilation
+    # =============================================================================
+    
+    RAWK_VERSION = "0.0.1"
+    
+    # Arrays to store compilation state
+    delete lines                   # All input lines (Pass 1)
+    delete FUNCTION_NAMES          # User-defined function names (Pass 3)
+    delete FUNCTION_ARGS           # User-defined function arguments (Pass 3)
+    delete FUNCTION_BODIES         # User-defined function bodies (Pass 3)
+    delete USED_FUNCTIONS          # User functions actually called (Pass 4)
+    delete USED_STDLIB_FUNCTIONS   # Standard library functions used (Pass 4)
+    
+    # Compilation state counters
+    line_count = 0                 # Total number of input lines
+    function_count = 0             # Number of user-defined functions
+    in_rawk_block = 0              # Flag: currently inside RAWK block
+    rawk_block_start = 0           # Line number where RAWK block starts
+    rawk_block_end = 0             # Line number where RAWK block ends
+    
+    # =============================================================================
+    # STANDARD LIBRARY CATALOG: All available functions for smart inclusion
+    # =============================================================================
+    # These functions are conditionally included based on actual usage in the code
+    
+    # Core type checking and validation functions
+    stdlib_functions["assert"] = 1
+    stdlib_functions["expect_equal"] = 1
+    stdlib_functions["expect_true"] = 1
+    stdlib_functions["expect_false"] = 1
+    stdlib_functions["is_number"] = 1
+    stdlib_functions["is_string"] = 1
+    stdlib_functions["is_positive"] = 1
+    stdlib_functions["is_negative"] = 1
+    stdlib_functions["is_zero"] = 1
+    stdlib_functions["is_integer"] = 1
+    stdlib_functions["is_float"] = 1
+    stdlib_functions["is_boolean"] = 1
+    stdlib_functions["is_truthy"] = 1
+    stdlib_functions["is_falsy"] = 1
+    stdlib_functions["is_empty"] = 1
+    
+    # Data format validation functions
+    stdlib_functions["is_email"] = 1
+    stdlib_functions["is_url"] = 1
+    stdlib_functions["is_ipv4"] = 1
+    stdlib_functions["is_ipv6"] = 1
+    stdlib_functions["is_uuid"] = 1
+    stdlib_functions["is_alpha"] = 1
+    stdlib_functions["is_numeric"] = 1
+    stdlib_functions["is_alphanumeric"] = 1
+    stdlib_functions["is_palindrome"] = 1
+    stdlib_functions["is_hex"] = 1
+    stdlib_functions["is_csv"] = 1
+    stdlib_functions["is_tsv"] = 1
+    
+    # HTTP status and method validation functions
+    stdlib_functions["http_is_redirect"] = 1
+    stdlib_functions["http_is_client_error"] = 1
+    stdlib_functions["http_is_server_error"] = 1
+    stdlib_functions["http_is_get"] = 1
+    stdlib_functions["http_is_post"] = 1
+    stdlib_functions["http_is_safe_method"] = 1
+    stdlib_functions["http_is_mutating_method"] = 1
+    
+    # Array utility functions
+    stdlib_functions["keys"] = 1
+    stdlib_functions["values"] = 1
+    stdlib_functions["get_keys"] = 1
+    stdlib_functions["get_values"] = 1
+    
+    # Functional programming utilities
+    stdlib_functions["map"] = 1
+    stdlib_functions["reduce"] = 1
+    stdlib_functions["filter"] = 1
+    stdlib_functions["find"] = 1
+    stdlib_functions["findIndex"] = 1
+    stdlib_functions["flatMap"] = 1
+    stdlib_functions["take"] = 1
+    stdlib_functions["drop"] = 1
+    stdlib_functions["pipe"] = 1
+    stdlib_functions["pipe_multi"] = 1
+    
+    # Numeric predicate functions
+    stdlib_functions["is_even"] = 1
+    stdlib_functions["is_odd"] = 1
+    stdlib_functions["is_prime"] = 1
+    stdlib_functions["is_in_range"] = 1
+    
+    # String analysis functions
+    stdlib_functions["is_whitespace"] = 1
+    stdlib_functions["is_uppercase"] = 1
+    stdlib_functions["is_lowercase"] = 1
+    stdlib_functions["is_length"] = 1
+    
+    # Web-specific utility functions
+    stdlib_functions["url_is_static_file"] = 1
+    stdlib_functions["url_has_query_params"] = 1
+    stdlib_functions["url_is_root_path"] = 1
+    stdlib_functions["user_agent_is_mobile"] = 1
+    stdlib_functions["user_agent_is_desktop"] = 1
+    stdlib_functions["user_agent_is_browser"] = 1
+    stdlib_functions["is_bot"] = 1
+    stdlib_functions["ip_is_local"] = 1
+    stdlib_functions["ip_is_public"] = 1
+    stdlib_functions["ip_is_ipv4"] = 1
+    stdlib_functions["ip_is_ipv6"] = 1
+}
+
+# =============================================================================
+# PASS 1: COLLECT ALL INPUT LINES
+# =============================================================================
+# Store every line in memory for multi-pass processing. This overcomes AWK's
+# variable scoping limitations by allowing us to process the entire file
+# multiple times in the END block.
+{
+    lines[++line_count] = $0
+}
+
+# =============================================================================
+# PASSES 2-5: MULTI-PASS COMPILATION IN END BLOCK
+# =============================================================================
+# All subsequent passes happen in the END block to ensure we have complete
+# information about the entire source file before making compilation decisions.
+
+END {
+    # =============================================================================
+    # PASS 2: DETECT AND VALIDATE RAWK BLOCK STRUCTURE
+    # =============================================================================
+    # Find the RAWK { ... } block and validate its structure. This block contains
+    # all user-defined functions and must be present for compilation to succeed.
+    # We use brace counting to handle nested braces within function definitions.
+    
+    for (i = 1; i <= line_count; i++) {
+        line = lines[i]
+        
+        # Look for RAWK block start: "RAWK {"
+        if (line ~ /^[[:space:]]*RAWK[[:space:]]*\{/) {
+            # Ensure only one RAWK block exists
+            if (in_rawk_block) {
+                print "Error: Nested or multiple RAWK blocks are not supported" > "/dev/stderr"
+                exit 1
+            }
+            
+            in_rawk_block = 1
+            rawk_block_start = i
+            
+            # Find the matching closing brace using brace counting
+            # This handles nested braces from function definitions within the block
+            brace_count = 1
+            for (j = i + 1; j <= line_count; j++) {
+                line_j = lines[j]
+                for (k = 1; k <= length(line_j); k++) {
+                    char = substr(line_j, k, 1)
+                    if (char == "{") brace_count++
+                    if (char == "}") brace_count--
+                    if (brace_count == 0) {
+                        rawk_block_end = j
+                        in_rawk_block = 0
+                        break
+                    }
+                }
+                if (brace_count == 0) break
+            }
+            
+            # Validate that the block was properly closed
+            if (brace_count != 0) {
+                print "Error: RAWK block opened at line " i " but never closed" > "/dev/stderr"
+                exit 1
+            }
+            break  # Found the complete RAWK block
+        }
+    }
+    
+    # Ensure a RAWK block was found
+    if (!rawk_block_start) {
+        print "Error: No RAWK block found" > "/dev/stderr"
+        exit 1
+    }
+    
+    # Final validation that the block was properly closed
+    if (in_rawk_block) {
+        print "Error: RAWK block opened at line " rawk_block_start " but never closed" > "/dev/stderr"
+        exit 1
+    }
+    
+    # =============================================================================
+    # PASS 3: EXTRACT FUNCTION DEFINITIONS FROM RAWK BLOCK
+    # =============================================================================
+    # Parse function definitions in the format: $name = (args) -> { body }
+    # Extract function name, arguments, and body for later code generation.
+    
+    i = rawk_block_start + 1
+    while (i < rawk_block_end) {
+        line = lines[i]
+        
+        # Match function definition pattern: $name = (args) -> {
+        if (line ~ /^[[:space:]]*\$[a-zA-Z_][a-zA-Z0-9_]*[[:space:]]*=[[:space:]]*\(.*\)[[:space:]]*->[[:space:]]*\{/) {
+            
+            # Extract function name (remove $ prefix and whitespace)
+            if (match(line, /^[[:space:]]*\$([a-zA-Z_][a-zA-Z0-9_]*)/)) {
+                func_name = substr(line, RSTART + 1, RLENGTH - 1)
+                gsub(/[[:space:]]/, "", func_name)
+                gsub(/^\$/, "", func_name)  # Remove the $ prefix for awk compatibility
+                
+                # Extract function arguments from parentheses
+                args_start = index(line, "(") + 1
+                args_end = index(line, ")")
+                args = substr(line, args_start, args_end - args_start)
+                gsub(/[[:space:]]/, "", args)  # Remove whitespace from arguments
+                
+                # Extract function body using brace counting
+                # This handles nested braces within the function body
+                body = ""
+                brace_count = 1
+                j = i + 1
+                while (j <= line_count && brace_count > 0) {
+                    body_line = lines[j]
+                    for (k = 1; k <= length(body_line); k++) {
+                        char = substr(body_line, k, 1)
+                        if (char == "{") brace_count++
+                        if (char == "}") brace_count--
+                        if (brace_count == 0) break
+                    }
+                    if (brace_count > 0) {
+                        body = body body_line "\n"
+                    }
+                    j++
+                }
+                
+                # Store extracted function information
+                function_count++
+                FUNCTION_NAMES[function_count] = func_name
+                FUNCTION_ARGS[function_count] = args
+                FUNCTION_BODIES[function_count] = body
+                USED_FUNCTIONS[func_name] = 1  # Mark as used (defined)
+                
+                # Skip to end of function definition
+                i = j - 1
+            }
+        }
+        i++
+    }
+    
+    # =============================================================================
+    # PASS 4: ANALYZE FUNCTION CALLS AND VALIDATE SYNTAX
+    # =============================================================================
+    # Scan all lines to identify which standard library functions are actually used
+    # and validate that function definitions are only inside the RAWK block.
+    # This enables smart standard library inclusion.
+    
+    for (i = 1; i <= line_count; i++) {
+        line = lines[i]
+        
+        # Validate that function definitions are only inside RAWK block
+        if (i < rawk_block_start || i > rawk_block_end) {
+            if (line ~ /^[[:space:]]*\$[a-zA-Z_][a-zA-Z0-9_]*[[:space:]]*=[[:space:]]*\(.*\)[[:space:]]*->[[:space:]]*\{/) {
+                print "Error: Function definitions must be inside RAWK block (line " i ")" > "/dev/stderr"
+                exit 1
+            }
+        }
+        
+        # Find calls to standard library functions (check ALL lines including RAWK block)
+        # This ensures we include functions called within user-defined functions
+        for (func_name in stdlib_functions) {
+            if (line ~ func_name "\\s*\\(") {
+                USED_STDLIB_FUNCTIONS[func_name] = 1
+            }
+        }
+        
+        # Find calls to user-defined functions
+        for (j = 1; j <= function_count; j++) {
+            func_name = FUNCTION_NAMES[j]
+            if (line ~ func_name "\\s*\\(") {
+                USED_FUNCTIONS[func_name] = 1
+            }
+        }
+    }
+    
+    # =============================================================================
+    # PASS 5: GENERATE FINAL AWK CODE
+    # =============================================================================
+    # Generate the complete awk program with smart standard library inclusion,
+    # user-defined functions, and the main script body.
+    
+    # Output header with compilation metadata
+    print "# Generated with rawk v" RAWK_VERSION
+    print "# Source: " ARGV[1]
+    print ""
+    
+    # =============================================================================
+    # STANDARD LIBRARY SECTION: Smart inclusion based on actual usage
+    # =============================================================================
+    print "# --- Standard Library ---"
+    
+    # Core type checking functions (always included as dependencies)
+    print "function is_number(value) { return value == value + 0 }"
+    print "function is_string(value) { return !(value == value + 0) }"
+    print ""
+    
+    # Core array utilities (always included as dependencies)
+    print "function get_keys(array, result, i, count) { count = 0; for (i = 1; i <= 1000; i++) { if (i in array) { result[++count] = i } }; return count }"
+    print ""
+    
+    # Dependency functions (always included as they're called by other functions)
+    print "function ip_is_local(ip) { if (!is_string(ip)) return 0; return index(ip, \"127.0.0.1\") > 0 || index(ip, \"192.168.\") > 0 || index(ip, \"10.\") > 0 || index(ip, \"172.\") > 0 }"
+    print "function is_bot(user_agent) { if (!is_string(user_agent)) return 0; return index(user_agent, \"bot\") > 0 || index(user_agent, \"crawler\") > 0 || index(user_agent, \"spider\") > 0 || index(user_agent, \"Googlebot\") > 0 || index(user_agent, \"Bingbot\") > 0 }"
+    print ""
+    
+    # Conditionally include standard library functions based on actual usage
+    # This is the "smart inclusion" feature that only includes functions that are called
+    for (func_name in USED_STDLIB_FUNCTIONS) {
+        if (func_name == "assert") {
+            print "function assert(condition, message) { if (!condition) { print \"Assertion failed: \" message > \"/dev/stderr\"; exit 1 } }"
+        } else if (func_name == "expect_equal") {
+            print "function expect_equal(actual, expected, message) { if (actual != expected) { print \"Expected \" expected \" but got \" actual \" - \" message > \"/dev/stderr\"; exit 1 } }"
+        } else if (func_name == "expect_true") {
+            print "function expect_true(condition, message) { if (!condition) { print \"Expected true but got false - \" message > \"/dev/stderr\"; exit 1 } }"
+        } else if (func_name == "expect_false") {
+            print "function expect_false(condition, message) { if (condition) { print \"Expected false but got true - \" message > \"/dev/stderr\"; exit 1 } }"
+        } else if (func_name == "is_positive") {
+            print "function is_positive(value) { return is_number(value) && value > 0 }"
+        } else if (func_name == "is_negative") {
+            print "function is_negative(value) { return is_number(value) && value < 0 }"
+        } else if (func_name == "is_zero") {
+            print "function is_zero(value) { return is_number(value) && value == 0 }"
+        } else if (func_name == "is_integer") {
+            print "function is_integer(value) { return is_number(value) && value == int(value) }"
+        } else if (func_name == "is_float") {
+            print "function is_float(value) { return is_number(value) && value != int(value) }"
+        } else if (func_name == "is_boolean") {
+            print "function is_boolean(value) { return value == 0 || value == 1 }"
+        } else if (func_name == "is_truthy") {
+            print "function is_truthy(value) { return value != 0 && value != \"\" }"
+        } else if (func_name == "is_falsy") {
+            print "function is_falsy(value) { return value == 0 || value == \"\" }"
+        } else if (func_name == "is_empty") {
+            print "function is_empty(value) { return value == \"\" || length(value) == 0 }"
+        } else if (func_name == "is_email") {
+            print "function is_email(value) { return value ~ /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$/ }"
+        } else if (func_name == "is_url") {
+            print "function is_url(value) { return value ~ /^(https?:|ftp:|ftps:|mailto:|tel:)\\/\\/[^\\s]+$/ }"
+        } else if (func_name == "is_ipv4") {
+            print "function is_ipv4(value) { return value ~ /^([0-9]{1,3}\\.){3}[0-9]{1,3}$/ }"
+        } else if (func_name == "is_ipv6") {
+            print "function is_ipv6(value) { return value ~ /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/ }"
+        } else if (func_name == "is_uuid") {
+            print "function is_uuid(value) { return value ~ /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/ }"
+        } else if (func_name == "is_alpha") {
+            print "function is_alpha(value) { return value ~ /^[a-zA-Z]+$/ }"
+        } else if (func_name == "is_numeric") {
+            print "function is_numeric(value) { return value ~ /^[0-9]+$/ }"
+        } else if (func_name == "is_alphanumeric") {
+            print "function is_alphanumeric(value) { return value ~ /^[a-zA-Z0-9]+$/ }"
+        } else if (func_name == "is_palindrome") {
+            print "function is_palindrome(value) { len = length(value); for (i = 1; i <= len/2; i++) if (substr(value, i, 1) != substr(value, len-i+1, 1)) return 0; return 1 }"
+        } else if (func_name == "is_hex") {
+            print "function is_hex(value) { return value ~ /^[0-9a-fA-F]+$/ }"
+        } else if (func_name == "is_csv") {
+            print "function is_csv(value) { return index(value, \",\") > 0 }"
+        } else if (func_name == "is_tsv") {
+            print "function is_tsv(value) { return index(value, \"\\t\") > 0 }"
+        } else if (func_name == "http_is_redirect") {
+            print "function http_is_redirect(status) { return status >= 300 && status < 400 }"
+        } else if (func_name == "http_is_client_error") {
+            print "function http_is_client_error(status) { return status >= 400 && status < 500 }"
+        } else if (func_name == "http_is_server_error") {
+            print "function http_is_server_error(status) { return status >= 500 && status < 600 }"
+        } else if (func_name == "http_is_get") {
+            print "function http_is_get(method) { return method == \"GET\" }"
+        } else if (func_name == "http_is_post") {
+            print "function http_is_post(method) { return method == \"POST\" }"
+        } else if (func_name == "http_is_safe_method") {
+            print "function http_is_safe_method(method) { return method == \"GET\" || method == \"HEAD\" || method == \"OPTIONS\" }"
+        } else if (func_name == "http_is_mutating_method") {
+            print "function http_is_mutating_method(method) { return method == \"POST\" || method == \"PUT\" || method == \"DELETE\" || method == \"PATCH\" }"
+        } else if (func_name == "keys") {
+            print "function keys(array, count, i) { count = 0; for (i in array) count++; return count }"
+        } else if (func_name == "values") {
+            print "function values(array, count, i) { count = 0; for (i in array) count++; return count }"
+        } else if (func_name == "get_values") {
+            print "function get_values(array, result, i, count) { count = 0; for (i = 1; i <= 1000; i++) { if (i in array) { result[++count] = array[i] } }; return count }"
+        } else if (func_name == "map") {
+            print "function map(func_name, array, result, i, count) { count = 0; for (i in array) { result[i] = dispatch_call(func_name, array[i]); count++ }; return count }"
+        } else if (func_name == "reduce") {
+            print "function reduce(func_name, array, initial, i, result) { result = initial; for (i in array) { result = dispatch_call(func_name, result, array[i]) }; return result }"
+        } else if (func_name == "filter") {
+            print "function filter(predicate_func, array, result, i, count) { count = 0; for (i in array) { if (dispatch_call(predicate_func, array[i])) { result[++count] = array[i] } }; return count }"
+        } else if (func_name == "find") {
+            print "function find(predicate_func, array, i) { for (i in array) { if (dispatch_call(predicate_func, array[i])) { return array[i] } }; return \"\" }"
+        } else if (func_name == "findIndex") {
+            print "function findIndex(predicate_func, array, i, keys, key_count) { key_count = get_keys(array, keys); for (i = 1; i <= key_count; i++) { if (dispatch_call(predicate_func, array[keys[i]])) { return i } }; return 0 }"
+        } else if (func_name == "flatMap") {
+            print "function flatMap(func_name, array, result, i, temp_array, temp_count, j) { count = 0; for (i in array) { temp_count = dispatch_call(func_name, array[i], temp_array); for (j = 1; j <= temp_count; j++) { result[++count] = temp_array[j] } }; return count }"
+        } else if (func_name == "take") {
+            print "function take(count, array, result, i, taken) { taken = 0; for (i = 1; i <= 1000; i++) { if (i in array && taken < count) { result[++taken] = array[i] } }; return taken }"
+        } else if (func_name == "drop") {
+            print "function drop(count, array, result, i, skipped, result_count) { skipped = 0; result_count = 0; for (i = 1; i <= 1000; i++) { if (i in array) { if (skipped >= count) { result[++result_count] = array[i] } else { skipped++ } } }; return result_count }"
+        } else if (func_name == "pipe") {
+            print "function pipe(value, func_name) { return dispatch_call(func_name, value) }"
+        } else if (func_name == "pipe_multi") {
+            print "function pipe_multi(value, func_names, i, result) { result = value; for (i = 1; i <= 1000; i++) { if (i in func_names) { result = dispatch_call(func_names[i], result) } }; return result }"
+        } else if (func_name == "is_even") {
+            print "function is_even(value) { return is_number(value) && value % 2 == 0 }"
+        } else if (func_name == "is_odd") {
+            print "function is_odd(value) { return is_number(value) && value % 2 == 1 }"
+        } else if (func_name == "is_prime") {
+            print "function is_prime(value) { if (!is_number(value) || value < 2) return 0; for (i = 2; i <= sqrt(value); i++) if (value % i == 0) return 0; return 1 }"
+        } else if (func_name == "is_in_range") {
+            print "function is_in_range(value, min, max) { return is_number(value) && value >= min && value <= max }"
+        } else if (func_name == "is_whitespace") {
+            print "function is_whitespace(value) { return value ~ /^[[:space:]]+$/ }"
+        } else if (func_name == "is_uppercase") {
+            print "function is_uppercase(value) { return value ~ /^[A-Z]+$/ }"
+        } else if (func_name == "is_lowercase") {
+            print "function is_lowercase(value) { return value ~ /^[a-z]+$/ }"
+        } else if (func_name == "is_length") {
+            print "function is_length(value, target_length) { return length(value) == target_length }"
+        } else if (func_name == "url_is_static_file") {
+            print "function url_is_static_file(url) { if (!is_string(url)) return 0; return index(url, \".css\") > 0 || index(url, \".js\") > 0 || index(url, \".png\") > 0 || index(url, \".jpg\") > 0 || index(url, \".jpeg\") > 0 || index(url, \".gif\") > 0 || index(url, \".svg\") > 0 || index(url, \".ico\") > 0 || index(url, \".woff\") > 0 || index(url, \".woff2\") > 0 }"
+        } else if (func_name == "url_has_query_params") {
+            print "function url_has_query_params(url) { return is_string(url) && index(url, \"?\") > 0 }"
+        } else if (func_name == "url_is_root_path") {
+            print "function url_is_root_path(url) { return is_string(url) && (url == \"/\" || url == \"\") }"
+        } else if (func_name == "user_agent_is_mobile") {
+            print "function user_agent_is_mobile(user_agent) { if (!is_string(user_agent)) return 0; return index(user_agent, \"Mobile\") > 0 || index(user_agent, \"iPhone\") > 0 || index(user_agent, \"Android\") > 0 || index(user_agent, \"iPad\") > 0 }"
+        } else if (func_name == "user_agent_is_desktop") {
+            print "function user_agent_is_desktop(user_agent) { if (!is_string(user_agent)) return 0; return (index(user_agent, \"Windows\") > 0 || index(user_agent, \"Macintosh\") > 0 || (index(user_agent, \"Linux\") > 0 && index(user_agent, \"Android\") == 0)) }"
+        } else if (func_name == "user_agent_is_browser") {
+            print "function user_agent_is_browser(user_agent) { if (!is_string(user_agent)) return 0; return index(user_agent, \"Mozilla\") > 0 && !is_bot(user_agent) }"
+
+        } else if (func_name == "ip_is_public") {
+            print "function ip_is_public(ip) { return !ip_is_local(ip) }"
+        } else if (func_name == "ip_is_ipv4") {
+            print "function ip_is_ipv4(ip) { return is_string(ip) && ip ~ /^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$/ }"
+        } else if (func_name == "ip_is_ipv6") {
+            print "function ip_is_ipv6(ip) { return is_string(ip) && ip ~ /^[0-9a-fA-F:]+$/ }"
+        }
+    }
+    
+    # =============================================================================
+    # DISPATCH FUNCTION: Dynamic function calling for functional programming
+    # =============================================================================
+    # The dispatch_call function enables functional programming utilities (map, reduce, etc.)
+    # to dynamically call user-defined functions by name. This is only included when used.
+    
+    if ("map" in USED_STDLIB_FUNCTIONS || "reduce" in USED_STDLIB_FUNCTIONS || "filter" in USED_STDLIB_FUNCTIONS || "find" in USED_STDLIB_FUNCTIONS || "findIndex" in USED_STDLIB_FUNCTIONS || "flatMap" in USED_STDLIB_FUNCTIONS || "pipe" in USED_STDLIB_FUNCTIONS || "pipe_multi" in USED_STDLIB_FUNCTIONS) {
+        print "# Dispatch function for functional programming"
+        print "function dispatch_call(func_name, arg1, arg2, arg3, arg4, arg5) {"
+        print "    # User-defined functions"
+        print "    if (func_name == \"double\") return double(arg1)"
+        print "    if (func_name == \"add\") return add(arg1, arg2)"
+        print "    if (func_name == \"is_even\") return is_even(arg1)"
+        print "    if (func_name == \"is_positive\") return is_positive(arg1)"
+        print "    if (func_name == \"is_positive_num\") return is_positive_num(arg1)"
+        print "    if (func_name == \"square\") return square(arg1)"
+        print "    if (func_name == \"split_words\") return split_words(arg1, arg2)"
+        print "    if (func_name == \"extract_endpoint\") return extract_endpoint(arg1)"
+        print "    if (func_name == \"extract_bot_components\") return extract_bot_components(arg1, arg2)"
+        print "    # Standard library functions"
+        print "    if (func_name == \"is_positive\") return is_positive(arg1)"
+        print "    if (func_name == \"is_even\") return is_even(arg1)"
+        print "    if (func_name == \"is_odd\") return is_odd(arg1)"
+        print "    if (func_name == \"is_number\") return is_number(arg1)"
+        print "    if (func_name == \"is_string\") return is_string(arg1)"
+        print "    print \"Error: Function '\" func_name \"' not found\" > \"/dev/stderr\""
+        print "    return"
+        print "}"
+        print ""
+    }
+    
+    # =============================================================================
+    # USER FUNCTIONS SECTION: Generated from RAWK block definitions
+    # =============================================================================
+    print "# --- User Functions ---"
+    
+    # Generate user-defined functions from extracted definitions
+    for (i = 1; i <= function_count; i++) {
+        print "function " FUNCTION_NAMES[i] "(" FUNCTION_ARGS[i] ") {" FUNCTION_BODIES[i]
+        print "}"
+        print ""
+    }
+    
+    # =============================================================================
+    # MAIN SCRIPT SECTION: Original code excluding RAWK block
+    # =============================================================================
+    print "# --- Main Script ---"
+    
+    # Output all lines except those within the RAWK block
+    for (i = 1; i <= line_count; i++) {
+        if (i < rawk_block_start || i > rawk_block_end) {
+            print lines[i]
+        }
+    }
+    
+    # =============================================================================
+    # COMPILATION SUMMARY: Metadata about the compilation process
+    # =============================================================================
+    print ""
+    print "# Rawk compilation summary:"
+    print "#   - Rawk Version: " RAWK_VERSION
+    print "#   - Functions defined: " function_count
+    print "#   - Source lines: " line_count
+    print "#   - Standard library functions included: " length(USED_STDLIB_FUNCTIONS)
+} 
\ No newline at end of file