diff options
| author | elioat <elioat@tilde.institute> | 2025-07-31 07:42:38 -0400 |
|---|---|---|
| committer | elioat <elioat@tilde.institute> | 2025-07-31 07:42:38 -0400 |
| commit | a2f022b911f5a1616bfa71f929b3881e366d6017 (patch) | |
| tree | 4a33f3dc52a95760aa6a7e1558ca9e6e62d8d497 /awk | |
| parent | 83e80777b3262fbd4b1bdeed1e232414a728ee81 (diff) | |
| download | tour-a2f022b911f5a1616bfa71f929b3881e366d6017.tar.gz | |
*
Diffstat (limited to 'awk')
| -rw-r--r-- | awk/rawk/README.md | 6 | ||||
| -rw-r--r-- | awk/rawk/rawk.awk | 1218 | ||||
| -rw-r--r-- | awk/rawk/scratch_debug_pattern_matching.txt | 45 | ||||
| -rw-r--r-- | awk/rawk/temp.awk | 541 | ||||
| -rw-r--r-- | awk/rawk/test_pattern_simple.rawk | 23 |
5 files changed, 326 insertions, 1507 deletions
diff --git a/awk/rawk/README.md b/awk/rawk/README.md index ff297b2..7193e3d 100644 --- a/awk/rawk/README.md +++ b/awk/rawk/README.md @@ -1,3 +1,7 @@ +Random idea: isolate each stdlib function's output, add an additional pass in the compiler so that you only need to include the standard library functions that the program actually uses + +add predicates: is_hex + # rawk - A Functional Programming Language for awk **rawk** is a modern, functional-style language dialect that compiles to highly portable, standard `awk`. It provides a more expressive syntax for writing awk programs while maintaining full compatibility with existing awk code. @@ -162,6 +166,8 @@ The following functions are automatically available: - `is_email(value)`: Basic email validation - `is_url(value)`: Basic URL validation - `is_ipv4(value)`: Basic IPv4 validation +- `is_ipv6(value)`: Basic IPv6 validation +- `is_uuid(value)`: UUID validation (standard format) ## Examples diff --git a/awk/rawk/rawk.awk b/awk/rawk/rawk.awk index b4128e2..1f39005 100644 --- a/awk/rawk/rawk.awk +++ b/awk/rawk/rawk.awk @@ -1,11 +1,12 @@ #!/usr/bin/env awk -f # ----------------------------------------------------------------------------- -# rawk.awk - The `rawk` Language Compiler +# rawk.awk - The `rawk` Language Compiler (Multi-Pass Version) # # This script translates a `.rawk` source file into standard, portable awk code. -# It parses special `rawk` syntax, generates standard awk functions, and manages -# a dispatch table for functional programming features. +# It uses a multi-pass approach: +# Pass 1: Parse and collect all function definitions and source lines +# Pass 2: Generate the final AWK code (in END block) # # USAGE: # awk -f rawk.awk my_program.rawk | awk -f - @@ -58,11 +59,10 @@ # END { print "Done." } # # COMPILATION PROCESS: -# 1. Parse rawk function definitions and generate internal awk functions -# 2. Build dispatch table mapping public names to internal names -# 3. Replace function calls with internal names -# 4. Generate standard library functions -# 5. Output final awk script +# 1. Pass 1: Parse rawk function definitions and collect them +# 2. Pass 2: Generate internal awk functions and dispatch table +# 3. Pass 3: Replace function calls with internal names +# 4. Pass 4: Output final awk script # # LIMITATIONS: # - Standard library map/reduce functions have limited support @@ -83,638 +83,178 @@ # # ----------------------------------------------------------------------------- - -# The BEGIN block runs once before any input is processed. -# Its purpose is to initialize the compiler's state. +# Global state for multi-pass compilation BEGIN { # --- Compiler State Initialization --- - - # Counter to generate unique internal names for lambda functions (e.g., __lambda_1, __lambda_2). - lambda_counter = 0 - - # State tracking for multi-line function definitions - in_function_body = 0 - current_function_body = "" - current_function_name = "" - current_function_args = "" - current_function_arg_count = 0 - - # Enhanced error tracking - error_count = 0 - warning_count = 0 - source_lines[0] = "" # Store source lines for better error reporting - - # The Dispatch Dictionary. This is the core of the portable dispatch system. - # Key: The public function name (e.g., "my_add"). - # Value: A pipe-delimited string of metadata -> "internal_name|arg_count|source_info" - # We initialize it here, though it's a global array. - delete RAWK_DISPATCH # Ensures it's empty - - # Arrays to store the generated code before printing it in the END block. - # This ensures the correct final order of the output script. - delete generated_user_functions - delete modified_source_lines - - # --- Standard Library Injection --- - # The standard library functions are now hardcoded in the END block - # to avoid issues with array initialization in the BEGIN block. -} - -# Enhanced error reporting function -function report_error(message, line_num, line_content, suggestion) { - error_count++ - print "❌ rawk compilation error at line " line_num ":" > "/dev/stderr" - if (line_content != "") { - print " " line_content > "/dev/stderr" - # Add a caret to point to the error location - print " " "^" > "/dev/stderr" - } - print " " message > "/dev/stderr" - if (suggestion != "") { - print "💡 Suggestion: " suggestion > "/dev/stderr" - } - print "" > "/dev/stderr" -} - -# Enhanced warning reporting function -function report_warning(message, line_num, line_content, suggestion) { - warning_count++ - print "⚠️ rawk warning at line " line_num ":" > "/dev/stderr" - if (line_content != "") { - print " " line_content > "/dev/stderr" - } - print " " message > "/dev/stderr" - if (suggestion != "") { - print "💡 Suggestion: " suggestion > "/dev/stderr" - } - print "" > "/dev/stderr" -} - -# Function to validate function name -function validate_function_name(name, line_num, line_content) { - if (name == "") { - report_error("Function name cannot be empty", line_num, line_content, "Use a valid identifier like 'add', 'process_data', etc.") - return 0 - } - if (name ~ /^[0-9]/) { - report_error("Function name cannot start with a number", line_num, line_content, "Use a letter or underscore first, like '_add' or 'add'") - return 0 - } - if (name ~ /[^a-zA-Z0-9_]/) { - report_error("Function name contains invalid characters", line_num, line_content, "Use only letters, numbers, and underscores") - return 0 - } - return 1 -} - -# Function to validate argument list -function validate_argument_list(args, line_num, line_content) { - if (args == "") return 1 # Empty args are valid - # Check for balanced parentheses - paren_count = 0 - for (i = 1; i <= length(args); i++) { - char = substr(args, i, 1) - if (char == "(") paren_count++ - else if (char == ")") paren_count-- - if (paren_count < 0) { - report_error("Unmatched closing parenthesis in argument list", line_num, line_content, "Check your parentheses: " args) - return 0 - } - } - if (paren_count != 0) { - report_error("Unmatched opening parenthesis in argument list", line_num, line_content, "Check your parentheses: " args) - return 0 - } + # Function collection arrays + delete FUNCTION_NAMES + delete FUNCTION_ARGS + delete FUNCTION_BODIES + delete FUNCTION_TYPES # "single" or "multi" + delete FUNCTION_LINES # source line numbers - return 1 -} - -# Function to suggest corrections for common syntax errors -function suggest_correction(line, line_num) { - if (line ~ /\$[a-zA-Z_][a-zA-Z0-9_]*\s*=\s*\([^)]*\)\s*[^-]/) { - # Missing arrow - report_error("Missing '->' in function definition", line_num, line, "Add '->' after the argument list: " gensub(/(\$[a-zA-Z_][a-zA-Z0-9_]*\s*=\s*\([^)]*\))\s*/, "\\1 -> ", 1, line)) - return 1 - } - if (line ~ /\$[a-zA-Z_][a-zA-Z0-9_]*\s*=\s*\([^)]*->/) { - # Missing closing parenthesis - report_error("Missing closing parenthesis in argument list", line_num, line, "Add ')' before '->'") - return 1 - } - if (line ~ /\$[a-zA-Z_][a-zA-Z0-9_]*\s*=.*->/) { - # Missing opening parenthesis - report_error("Missing opening parenthesis in argument list", line_num, line, "Add '(' after the function name") - return 1 - } - # Check for function-like syntax without arrow - if (line ~ /^\s*\$/ && line ~ /=.*\(.*\)/ && line !~ /->/) { - report_error("Missing '->' in function definition", line_num, line, "Add '->' after the argument list") - return 1 - } - return 0 -} - -# --- Pattern Matching Support --- - -# Function to parse pattern matching expressions -function parse_pattern_matching(body, line_num) { - # Check if this is a pattern matching function - if (body ~ /case[ \t]+[^o]+[ \t]+of/) { - return convert_pattern_matching_to_awk(body, line_num) -} -return body -} - -# Function to convert pattern matching to standard awk if/else -function convert_pattern_matching_to_awk(body, line_num) { - # Extract the case expression - if (body !~ /case[ \t]+[^o]+[ \t]+of/) { - report_error("Invalid pattern matching syntax", line_num, body, "Use format: case value of | pattern -> result") - return body - } - - # Extract the value being matched - if (match(body, /case[ \t]+([^o]+)[ \t]+of/)) { - # Find the start of the value after "case" - case_start = index(body, "case") - if (case_start > 0) { - # Find the end of "case" and skip whitespace - after_case = substr(body, case_start + 4) - # Find the start of "of" - of_start = index(after_case, "of") - if (of_start > 0) { - match_value = substr(after_case, 1, of_start - 1) - gsub(/^[ \t]+|[ \t]+$/, "", match_value) # Trim whitespace - - } else { - report_error("Invalid pattern matching syntax", line_num, body, "Use format: case value of | pattern -> result") - return body - } - } else { - report_error("Invalid pattern matching syntax", line_num, body, "Use format: case value of | pattern -> result") - return body - } - } else { - report_error("Invalid pattern matching syntax", line_num, body, "Use format: case value of | pattern -> result") - return body - } - - # Split the body into lines to process patterns - split(body, lines, "\n") - result = "" - first_pattern = 1 - - for (i = 1; i <= length(lines); i++) { - line = lines[i] - - # Skip empty lines and case/of lines - if (line ~ /^\s*$/ || line ~ /^\s*case.*of\s*$/) continue - - # Check if this is a pattern line (starts with |) - if (line ~ /^[ \t]*\|/) { - # Parse the pattern - pattern_code = parse_pattern_line(line, match_value, line_num) - - # Build the if/else chain - if (first_pattern) { - result = " " pattern_code - first_pattern = 0 - } else { - result = result "\n else " pattern_code - } - } - } - - # Clean up and fix variable references - gsub(/is_positive\(n\)/, "is_positive(" match_value ")", result) - gsub(/is_negative\(n\)/, "is_negative(" match_value ")", result) - gsub(/is_alpha\(s\)/, "is_alpha(" match_value ")", result) - gsub(/is_numeric\(s\)/, "is_numeric(" match_value ")", result) - gsub(/is_alphanumeric\(s\)/, "is_alphanumeric(" match_value ")", result) - gsub(/is_palindrome\(s\)/, "is_palindrome(" match_value ")", result) - gsub(/is_number\(v\)/, "is_number(" match_value ")", result) - gsub(/is_string\(v\)/, "is_string(" match_value ")", result) - gsub(/is_empty\(v\)/, "is_empty(" match_value ")", result) - gsub(/is_email\(v\)/, "is_email(" match_value ")", result) - gsub(/is_url\(v\)/, "is_url(" match_value ")", result) - gsub(/is_ipv4\(v\)/, "is_ipv4(" match_value ")", result) - gsub(/is_in_range\(v,/, "is_in_range(" match_value ",", result) - - # Clean up any leftover text and ensure proper formatting - gsub(/^[ \t]*"[^"]*"[ \t]*/, "", result) # Remove any leftover quoted text at the beginning - - return result -} - -# Function to parse a single pattern line -function parse_pattern_line(line, match_value, line_num) { - # Remove the leading | and whitespace - gsub(/^[ \t]*\|[ \t]*/, "", line) - - # Split on -> to separate pattern from result - if (line !~ /->/) { - report_error("Invalid pattern syntax - missing '->'", line_num, line, "Use format: | pattern -> result") - return "if (1) { return \"ERROR\" }" - } + # Counters + function_count = 0 + line_count = 0 - split(line, parts, "->") - pattern = parts[1] - pattern_result = parts[2] + # Source lines for pass 2 + delete SOURCE_LINES + delete SOURCE_LINE_TYPES # "function_def", "function_body", "code" - # Trim whitespace - gsub(/^\s+|\s+$/, "", pattern) - gsub(/^\s+|\s+$/, "", pattern_result) + # State tracking for multi-line function definitions + in_function_body = 0 + current_function_index = 0 - # Parse the pattern - condition = parse_pattern_condition(pattern, match_value, line_num) + # Enhanced error tracking + error_count = 0 + warning_count = 0 - return "if (" condition ") { return " pattern_result " }" + # Compilation statistics + functions_defined = 0 + source_lines = 0 + errors = 0 + warnings = 0 } -# Function to parse pattern condition -function parse_pattern_condition(pattern, match_value, line_num) { - # Handle wildcard pattern - if (pattern == "_") { - return "1" - } - - # Handle guard patterns (pattern if condition) - if (pattern ~ /if/) { - split(pattern, parts, "if") - value_pattern = parts[1] - guard_condition = parts[2] - - # Trim whitespace - gsub(/^[ \t]+|[ \t]+$/, "", value_pattern) - gsub(/^[ \t]+|[ \t]+$/, "", guard_condition) - - # Parse the value pattern - value_condition = parse_simple_pattern(value_pattern, match_value, line_num) - - # Parse the guard condition (replace variable references) - guard_condition = replace_pattern_variables(guard_condition, value_pattern, match_value) - - return value_condition " && (" guard_condition ")" - } - - # Handle simple patterns - return parse_simple_pattern(pattern, match_value, line_num) -} +# ----------------------------------------------------------------------------- +# PASS 1: Parse and collect function definitions and source lines +# ----------------------------------------------------------------------------- -# Function to parse simple patterns -function parse_simple_pattern(pattern, match_value, line_num) { - # Trim leading and trailing whitespace - gsub(/^[ \t]+|[ \t]+$/, "", pattern) - # Handle string literals - if (pattern ~ /^".*"$/) { - return match_value " == " pattern - } - - # Handle numeric literals - if (pattern ~ /^[0-9]+(\.[0-9]+)?$/) { - return match_value " == " pattern - } - - # Handle zero - if (pattern == "0") { - return match_value " == 0" - } - - # Handle empty string - if (pattern == "\"\"") { - return match_value " == \"\"" - } - - # Handle wildcard pattern - if (pattern == "_") { - return "1" # Always match - } - - # Handle variable patterns (like 'n' in 'n if is_positive(n)') - if (pattern ~ /^[a-zA-Z_][a-zA-Z0-9_]*$/) { - return "1" # Always match, the guard will handle the condition - } +{ + line_count++ + SOURCE_LINES[line_count] = $0 - # Handle predicate function calls - if (pattern ~ /^[a-zA-Z_][a-zA-Z0-9_]*\(/) { - # Extract function name and arguments - paren_start = index(pattern, "(") - paren_end = index(pattern, ")") - if (paren_start > 0 && paren_end > paren_start) { - func_name = substr(pattern, 1, paren_start - 1) - func_args = substr(pattern, paren_start + 1, paren_end - paren_start - 1) - - # Replace variable references in arguments - func_args = replace_pattern_variables(func_args, pattern, match_value) - - return func_name "(" func_args ")" - } + # Skip comments and empty lines + if ($0 ~ /^[ \t]*#/ || $0 ~ /^[ \t]*$/) { + SOURCE_LINE_TYPES[line_count] = "comment" + next } - # Default: treat as exact match - return match_value " == " pattern -} - -# Function to replace pattern variables in expressions -function replace_pattern_variables(expression, pattern, match_value) { - # Extract variable name from pattern (e.g., 'n' from 'n if is_positive(n)') - if (pattern ~ /^[a-zA-Z_][a-zA-Z0-9_]*$/) { - var_name = pattern - # Replace the variable with the match value, but only as a whole word - gsub("\\<" var_name "\\>", match_value, expression) + # Pattern 1: Multi-line function definition start + if ($0 ~ /^[ \t]*\$[a-zA-Z_][a-zA-Z0-9_]*[ \t]*=[ \t]*\([^)]*\)[ \t]*->[ \t]*\{/) { + parse_multi_line_function($0, line_count) + SOURCE_LINE_TYPES[line_count] = "function_def" + next } - return expression -} - -# --- Main Processing Block --- -# This block runs for each line of the input `.rawk` file. - -# Store source lines for better error reporting -{ - source_lines[FNR] = $0 -} - -# Robustly match function definitions (single-line and multi-line), even if indented -/^[ \t]*\$[a-zA-Z_][a-zA-Z0-9_]*[ \t]*=[ \t]*\([^)]*\)[ \t]*->[ \t]*\{/ { - print "DEBUG: Matched multi-line function def at line " FNR ": [" $0 "]" > "/dev/stderr" - if (in_function_body) { - report_error("Unexpected function definition while already in function body", FNR, $0, "Close the previous function '" current_function_name "' with '}' before defining a new one") - exit 1 - } - parse_function_definition_with_body($0) - in_function_body = 1 - current_function_body = "" - skip_function_lines = 1 - next -} - -/^[ \t]*\$[a-zA-Z_][a-zA-Z0-9_]*[ \t]*=[ \t]*\([^)]*\)[ \t]*->[ \t]*[^\{]/ { - print "DEBUG: Matched single-line function def at line " FNR ": [" $0 "]" > "/dev/stderr" - if (in_function_body) { - report_error("Unexpected function definition while already in function body", FNR, $0, "Close the previous function '" current_function_name "' with '}' before defining a new one") - exit 1 - } - parse_function_definition($0) - next -} - -# PATTERN 3: Handle multi-line function body end (robust for indented braces) -/^[ \t]*\}[ \t]*;?[ \t]*$/ { - if (!in_function_body) { - # This is just a regular closing brace, pass it through - if (skip_function_lines == 0) { - modified_source_lines[FNR] = $0 - } + # Pattern 2: Single-line function definition + if ($0 ~ /^[ \t]*\$[a-zA-Z_][a-zA-Z0-9_]*[ \t]*=[ \t]*\([^)]*\)[ \t]*->[ \t]*[^\{]/) { + parse_single_line_function($0, line_count) + SOURCE_LINE_TYPES[line_count] = "function_def" next } - # End multi-line function body - in_function_body = 0 - # Do NOT add this line to modified_source_lines (even if skip_function_lines was 1) - # Remove any trailing closing brace from the function body - gsub(/[ \t]*\}[ \t]*\n?$/, "", current_function_body) - - # Check if this is a pattern matching function and convert it - processed_body = parse_pattern_matching(current_function_body, FNR) - - # Generate the internal function - internal_name = "__lambda_" lambda_counter - generated_code = "function " internal_name "(" current_function_args ") {\n" processed_body "\n}" - generated_user_functions[lambda_counter] = generated_code - - # Populate the Dispatch Dictionary - source_info = FILENAME ":" FNR - metadata = internal_name "|" current_function_arg_count "|" source_info - RAWK_DISPATCH[current_function_name] = metadata - - lambda_counter++ - skip_function_lines = 0 - next -} - -# PATTERN 4: Handle lines inside multi-line function body -{ + # Pattern 3: Multi-line function body continuation if (in_function_body) { - # Replace function calls in the function body line - line = $0 - for (func_name in RAWK_DISPATCH) { - # Replace function calls like func_name(...) with internal_name(...) - metadata = RAWK_DISPATCH[func_name] - split(metadata, parts, "|") - internal_name = parts[1] - # Simple replacement - this could be enhanced with proper regex - gsub(func_name "\\(", internal_name "(", line) - } - # Add line to current function body (with proper indentation) - current_function_body = current_function_body " " line "\n" - # Do NOT add this line to modified_source_lines - next - } -} - -# PATTERN 4.5: Catch common syntax errors that don't match function patterns -{ - # Check for common syntax errors in lines that look like function definitions - if ($0 ~ /^\s*\$/ && $0 !~ /->/ && $0 ~ /=.*\(.*\)/) { - # Looks like a function definition but missing arrow - if (suggest_correction($0, FNR)) { - exit 1 + if ($0 ~ /^[ \t]*\}[ \t]*;?[ \t]*$/) { + # End of function body + in_function_body = 0 + SOURCE_LINE_TYPES[line_count] = "function_body_end" + # Add closing brace to function body + FUNCTION_BODIES[current_function_index] = FUNCTION_BODIES[current_function_index] "\n}" + next + } else { + # Add line to current function body + FUNCTION_BODIES[current_function_index] = FUNCTION_BODIES[current_function_index] "\n " $0 + SOURCE_LINE_TYPES[line_count] = "function_body" + next } } -} - -# PATTERN 5: Handle all other lines. -# If a line does not match the special syntax above, it's treated as -# plain awk code and should be passed through to the final script. -# But first, we need to replace function calls with their internal names -{ - if (FNR in modified_source_lines) { + + # Pattern 4: Start of multi-line function body + if ($0 ~ /^[ \t]*\{/) { + in_function_body = 1 + SOURCE_LINE_TYPES[line_count] = "function_body_start" next } - if (skip_function_lines == 0) { - line = $0 - for (func_name in RAWK_DISPATCH) { - metadata = RAWK_DISPATCH[func_name] - split(metadata, parts, "|") - internal_name = parts[1] - gsub(func_name "\\(", internal_name "(", line) - } - modified_source_lines[FNR] = line + + # Pattern 5: Regular code (but exclude function definition endings) + if ($0 ~ /^[ \t]*\}[ \t]*;[ \t]*$/) { + SOURCE_LINE_TYPES[line_count] = "function_end" + } else { + SOURCE_LINE_TYPES[line_count] = "code" } } +# ----------------------------------------------------------------------------- +# HELPER FUNCTIONS +# ----------------------------------------------------------------------------- -# Helper function to parse single-line function definitions -function parse_function_definition(line, parts, signature, body) { - # Split on -> to separate signature from body - split(line, parts, "->") - if (length(parts) != 2) { - report_error("Invalid function definition syntax - missing '->' or too many '->' symbols", FNR, line, "Use format: $name = (args) -> expression;") - exit 1 - } - - signature = parts[1] - body = parts[2] - - # Parse the signature: $name = (args) - if (substr(signature, 1, 1) != "$") { - report_error("Function definition must start with '$'", FNR, line, "Use format: $function_name = (args) -> expression;") - exit 1 - } - - # Extract function name (everything between $ and =) - name_end = index(signature, "=") - if (name_end == 0) { - report_error("Invalid function definition syntax - missing '='", FNR, line, "Use format: $name = (args) -> expression;") - exit 1 - } - - current_function_name = substr(signature, 2, name_end - 2) # Remove $ and = - gsub(/^[ \t]+|[ \t]+$/, "", current_function_name) # Trim whitespace - - # Validate function name - if (!validate_function_name(current_function_name, FNR, line)) { - exit 1 - } - - # Extract argument list (everything between = and the end) - assignment_part = substr(signature, name_end + 1) - gsub(/^[ \t]+|[ \t]+$/, "", assignment_part) # Trim whitespace - - # Parse the argument list - if (substr(assignment_part, 1, 1) != "(" || substr(assignment_part, length(assignment_part), 1) != ")") { - report_error("Invalid argument list syntax - missing parentheses", FNR, line, "Use format: $name = (arg1, arg2) -> expression;") - exit 1 - } - - current_function_args = substr(assignment_part, 2, length(assignment_part) - 2) - - # Validate argument list - if (!validate_argument_list(current_function_args, FNR, line)) { - exit 1 +# Parse multi-line function definition +function parse_multi_line_function(line, line_num) { + # Extract function name + if (match(line, /\$([a-zA-Z_][a-zA-Z0-9_]*)/)) { + func_name = substr(line, RSTART + 1, RLENGTH - 1) + } else { + report_error("Invalid function name", line_num, line, "Function name must be a valid identifier") + return } - current_function_arg_count = count_arguments(current_function_args) - - # Clean up the body - gsub(/^[ \t]+|[ \t]+$/, "", body) # Trim whitespace - # Remove trailing semicolon if present - if (substr(body, length(body), 1) == ";") { - body = substr(body, 1, length(body) - 1) + # Extract arguments + if (match(line, /\(([^)]*)\)/)) { + args = substr(line, RSTART + 1, RLENGTH - 2) + } else { + report_error("Invalid argument list", line_num, line, "Arguments must be enclosed in parentheses") + return } - # Generate the internal function - internal_name = "__lambda_" lambda_counter - generated_code = "function " internal_name "(" current_function_args ") { return " body " }" - generated_user_functions[lambda_counter] = generated_code + # Store function information + function_count++ + current_function_index = function_count + FUNCTION_NAMES[function_count] = func_name + FUNCTION_ARGS[function_count] = args + FUNCTION_BODIES[function_count] = "" + FUNCTION_TYPES[function_count] = "multi" + FUNCTION_LINES[function_count] = line_num - # Populate the Dispatch Dictionary - source_info = FILENAME ":" FNR - metadata = internal_name "|" current_function_arg_count "|" source_info - RAWK_DISPATCH[current_function_name] = metadata + # Start collecting function body + in_function_body = 1 - lambda_counter++ + functions_defined++ } -# Helper function to parse function definitions that start multi-line bodies -function parse_function_definition_with_body(line, parts, signature) { - # Split on -> to separate signature from body - split(line, parts, "->") - if (length(parts) != 2) { - report_error("Invalid function definition syntax - missing '->' or too many '->' symbols", FNR, line, "Use format: $name = (args) -> { ... }") - exit 1 - } - - signature = parts[1] - gsub(/^[ \t]+/, "", signature) # Trim leading whitespace - - # Parse the signature: $name = (args) - if (substr(signature, 1, 1) != "$") { - report_error("Function definition must start with '$'", FNR, line, "Use format: $function_name = (args) -> { ... }") - exit 1 - } - - # Extract function name (everything between $ and =) - name_end = index(signature, "=") - if (name_end == 0) { - report_error("Invalid function definition syntax - missing '='", FNR, line, "Use format: $name = (args) -> { ... }") - exit 1 - } - - current_function_name = substr(signature, 2, name_end - 2) # Remove $ and = - gsub(/^[ \t]+|[ \t]+$/, "", current_function_name) # Trim whitespace - - # Validate function name - if (!validate_function_name(current_function_name, FNR, line)) { - exit 1 +# Parse single-line function definition +function parse_single_line_function(line, line_num) { + # Extract function name + if (match(line, /\$([a-zA-Z_][a-zA-Z0-9_]*)/)) { + func_name = substr(line, RSTART + 1, RLENGTH - 1) + } else { + report_error("Invalid function name", line_num, line, "Function name must be a valid identifier") + return } - # Extract argument list (everything between = and the end) - assignment_part = substr(signature, name_end + 1) - gsub(/^[ \t]+|[ \t]+$/, "", assignment_part) # Trim whitespace - - # Parse the argument list - if (substr(assignment_part, 1, 1) != "(" || substr(assignment_part, length(assignment_part), 1) != ")") { - report_error("Invalid argument list syntax - missing parentheses", FNR, line, "Use format: $name = (arg1, arg2) -> { ... }") - exit 1 + # Extract arguments + if (match(line, /\(([^)]*)\)/)) { + args = substr(line, RSTART + 1, RLENGTH - 2) + } else { + report_error("Invalid argument list", line_num, line, "Arguments must be enclosed in parentheses") + return } - current_function_args = substr(assignment_part, 2, length(assignment_part) - 2) - - # Validate argument list - if (!validate_argument_list(current_function_args, FNR, line)) { - exit 1 + # Extract body (everything after ->) + if (match(line, /->[ \t]*(.+)/)) { + body = substr(line, RSTART + 2) + # Remove trailing semicolon if present + gsub(/[ \t]*;[ \t]*$/, "", body) + } else { + report_error("Invalid function body", line_num, line, "Function body must follow '->'") + return } - current_function_arg_count = count_arguments(current_function_args) -} - -# Helper function to count arguments in a comma-separated list -function count_arguments(arg_list, count, i, args) { - if (arg_list == "") return 0 + # Store function information + function_count++ + FUNCTION_NAMES[function_count] = func_name + FUNCTION_ARGS[function_count] = args + FUNCTION_BODIES[function_count] = body + FUNCTION_TYPES[function_count] = "single" + FUNCTION_LINES[function_count] = line_num - count = 0 - split(arg_list, args, ",") - for (i in args) { - gsub(/^[ \t]+|[ \t]+$/, "", args[i]) # Trim whitespace - if (args[i] != "") count++ - } - return count + functions_defined++ } - -# The END block runs once after all input lines have been processed. -# Its purpose is to assemble and print the final, compiled awk script. -END { - # --- Validate Function Bodies Are Closed --- - if (in_function_body) { - report_error("Unclosed function body at end of file", FNR, "Missing closing '}'", "Add '}' to close the function '" current_function_name "'") - exit 1 - } - - # --- Compilation Summary --- - if (error_count > 0) { - print "❌ Compilation failed with " error_count " error(s)" > "/dev/stderr" - exit 1 - } - - if (warning_count > 0) { - print "⚠️ Compilation completed with " warning_count " warning(s)" > "/dev/stderr" - } - - # Print compilation summary - print "# rawk compilation summary:" > "/dev/stderr" - print "# - Functions defined: " lambda_counter > "/dev/stderr" - print "# - Source lines: " FNR > "/dev/stderr" - print "# - Errors: " error_count > "/dev/stderr" - print "# - Warnings: " warning_count > "/dev/stderr" - print "" > "/dev/stderr" - - # --- Final Assembly --- - - # Step 1: Print the baked-in Standard Library. +# Generate standard library functions +function generate_standard_library() { print "# --- rawk Standard Library ---" print "# Dispatch mechanism for rawk functions" print "function dispatch_call(func_name, arg1, arg2, arg3, arg4, arg5, metadata, parts, internal_name, arg_count) {" @@ -731,145 +271,7 @@ END { print " return" print "}" print "" - print "function apply(func_name, args, i, metadata, parts, internal_name, arg_count) {" - print " if (!(func_name in RAWK_DISPATCH)) {" - print " print \"Error: Function '\" func_name \"' not found\" > \"/dev/stderr\"" - print " return" - print " }" - print " metadata = RAWK_DISPATCH[func_name]" - print " split(metadata, parts, \"|\")" - print " internal_name = parts[1]" - print " arg_count = parts[2]" - print " if (length(args) != arg_count) {" - print " print \"Error: Function '\" func_name \"' expects \" arg_count \" arguments, got \" length(args) > \"/dev/stderr\"" - print " return" - print " }" - print " return args[1]" - print "}" - print "" - print "function map(func_name, array, result, i, metadata, parts, internal_name, arg_count) {" - print " if (!(func_name in RAWK_DISPATCH)) {" - print " print \"❌ rawk runtime error: Function '\" func_name \"' not found\" > \"/dev/stderr\"" - print " print \"💡 Available functions: \" > \"/dev/stderr\"" - print " for (f in RAWK_DISPATCH) {" - print " print \" - \" f > \"/dev/stderr\"" - print " }" - print " return" - print " }" - print " metadata = RAWK_DISPATCH[func_name]" - print " split(metadata, parts, \"|\")" - print " internal_name = parts[1]" - print " arg_count = parts[2]" - print " if (arg_count != 1) {" - print " print \"❌ rawk runtime error: Function '\" func_name \"' must take exactly 1 argument for map\" > \"/dev/stderr\"" - print " print \"💡 Function '\" func_name \"' takes \" arg_count \" arguments\" > \"/dev/stderr\"" - print " return" - print " }" - print " # Use a switch-based dispatch for standard awk compatibility" - print " for (i in array) {" - print " if (internal_name == \"__lambda_0\") result[i] = __lambda_0(array[i])" - print " else if (internal_name == \"__lambda_1\") result[i] = __lambda_1(array[i])" - print " else if (internal_name == \"__lambda_2\") result[i] = __lambda_2(array[i])" - print " else if (internal_name == \"__lambda_3\") result[i] = __lambda_3(array[i])" - print " else if (internal_name == \"__lambda_4\") result[i] = __lambda_4(array[i])" - print " else if (internal_name == \"__lambda_5\") result[i] = __lambda_5(array[i])" - print " else if (internal_name == \"__lambda_6\") result[i] = __lambda_6(array[i])" - print " else if (internal_name == \"__lambda_7\") result[i] = __lambda_7(array[i])" - print " else if (internal_name == \"__lambda_8\") result[i] = __lambda_8(array[i])" - print " else if (internal_name == \"__lambda_9\") result[i] = __lambda_9(array[i])" - print " else {" - print " print \"❌ rawk runtime error: Function '\" func_name \"' not supported in map\" > \"/dev/stderr\"" - print " print \"💡 This is a limitation of the current implementation\" > \"/dev/stderr\"" - print " return" - print " }" - print " }" - print " return result" - print "}" - print "" - print "function reduce(func_name, array, initial_value, result, i, metadata, parts, internal_name, arg_count) {" - print " if (!(func_name in RAWK_DISPATCH)) {" - print " print \"Error: Function '\" func_name \"' not found\" > \"/dev/stderr\"" - print " return" - print " }" - print " metadata = RAWK_DISPATCH[func_name]" - print " split(metadata, parts, \"|\")" - print " internal_name = parts[1]" - print " arg_count = parts[2]" - print " if (arg_count != 2) {" - print " print \"Error: Function '\" func_name \"' must take exactly 2 arguments for reduce\" > \"/dev/stderr\"" - print " return" - print " }" - print " result = initial_value" - print " for (i in array) {" - print " if (internal_name == \"__lambda_0\") result = __lambda_0(result, array[i])" - print " else if (internal_name == \"__lambda_1\") result = __lambda_1(result, array[i])" - print " else if (internal_name == \"__lambda_2\") result = __lambda_2(result, array[i])" - print " else if (internal_name == \"__lambda_3\") result = __lambda_3(result, array[i])" - print " else if (internal_name == \"__lambda_4\") result = __lambda_4(result, array[i])" - print " else if (internal_name == \"__lambda_5\") result = __lambda_5(result, array[i])" - print " else if (internal_name == \"__lambda_6\") result = __lambda_6(result, array[i])" - print " else if (internal_name == \"__lambda_7\") result = __lambda_7(result, array[i])" - print " else if (internal_name == \"__lambda_8\") result = __lambda_8(result, array[i])" - print " else if (internal_name == \"__lambda_9\") result = __lambda_9(result, array[i])" - print " else {" - print " print \"Error: Function '\" func_name \"' not supported in reduce\" > \"/dev/stderr\"" - print " return" - print " }" - print " }" - print " return result" - print "}" - print "" - print "function pipe(value, func_names, result, i, metadata, parts, internal_name) {" - print " result = value" - print " for (i = 1; i <= length(func_names); i++) {" - print " if (!(func_names[i] in RAWK_DISPATCH)) {" - print " print \"Error: Function '\" func_names[i] \"' not found\" > \"/dev/stderr\"" - print " return" - print " }" - print " metadata = RAWK_DISPATCH[func_names[i]]" - print " split(metadata, parts, \"|\")" - print " internal_name = parts[1]" - print " result = result * 2" - print " }" - print " return result" - print "}" - print "" - print "function get_keys(array, result, i, count) {" - print " count = 0" - print " for (i in array) {" - print " count++" - print " result[count] = i" - print " }" - print " return count" - print "}" - print "" - print "function get_values(array, result, i, count) {" - print " count = 0" - print " for (i in array) {" - print " count++" - print " result[count] = array[i]" - print " }" - print " return count" - print "}" - print "" - print "function keys(array) {" - print " # This is a simplified version that just returns the count" - print " count = 0" - print " for (i in array) {" - print " count++" - print " }" - print " return count" - print "}" - print "" - print "function values(array) {" - print " # This is a simplified version that just returns the count" - print " count = 0" - print " for (i in array) {" - print " count++" - print " }" - print " return count" - print "}" - print "" + print "# --- Predicate Functions ---" print "# Type checking and validation functions" print "" @@ -883,29 +285,32 @@ END { print " return value != value + 0" print "}" print "" - print "function is_array(value, i) {" - print " # Check if value is an array by trying to iterate over it" - print " # This is a heuristic - in awk, arrays are associative" - print " # Note: This function has limitations in standard awk" - print " # It can only detect arrays that have been passed as parameters" - print " count = 0" - print " for (i in value) {" - print " count++" - print " if (count > 0) return 1" + print "function assert(condition, message) {" + print " if (!condition) {" + print " print \"ASSERTION FAILED: \" message > \"/dev/stderr\"" + print " print \" at line \" FNR \" in \" FILENAME > \"/dev/stderr\"" + print " exit 1" print " }" - print " return 0" + print " return 1" print "}" print "" - print "function is_empty(value) {" - print " # Check if value is empty (empty string, 0, or empty array)" - print " if (value == \"\") return 1" - print " if (value == 0) return 1" - print " if (is_array(value)) {" - print " count = 0" - print " for (i in value) count++" - print " return count == 0" + print "function expect_equal(actual, expected, message) {" + print " if (actual != expected) {" + print " print \"EXPECTATION FAILED: \" message > \"/dev/stderr\"" + print " print \" Expected: \" expected > \"/dev/stderr\"" + print " print \" Actual: \" actual > \"/dev/stderr\"" + print " print \" at line \" FNR \" in \" FILENAME > \"/dev/stderr\"" + print " exit 1" print " }" - print " return 0" + print " return 1" + print "}" + print "" + print "function expect_true(condition, message) {" + print " return assert(condition, message)" + print "}" + print "" + print "function expect_false(condition, message) {" + print " return assert(!condition, message)" print "}" print "" print "function is_positive(value) {" @@ -942,19 +347,21 @@ END { print " # Check if value is truthy (non-zero, non-empty)" print " if (is_number(value)) return value != 0" print " if (is_string(value)) return value != \"\"" - print " if (is_array(value)) {" - print " count = 0" - print " for (i in value) count++" - print " return count > 0" - print " }" print " return 0" print "}" print "" print "function is_falsy(value) {" - print " # Check if value is falsy (zero, empty string, empty array)" + print " # Check if value is falsy (zero, empty string)" print " return !is_truthy(value)" print "}" print "" + print "function is_empty(value) {" + print " # Check if value is empty (empty string, 0)" + print " if (value == \"\") return 1" + print " if (value == 0) return 1" + print " return 0" + print "}" + print "" print "function is_email(value) {" print " # Basic email validation" print " if (!is_string(value)) return 0" @@ -962,7 +369,6 @@ END { print " # Check for @ symbol and basic format" print " if (index(value, \"@\") == 0) return 0" print " if (index(value, \"@\") == length(value)) return 0" - print " if (index(value, \"@\") == 0) return 0" print " # Check for domain part" print " split(value, parts, \"@\")" print " if (length(parts) != 2) return 0" @@ -996,6 +402,50 @@ END { print " return 1" print "}" print "" + print "function is_ipv6(value) {" + print " # Basic IPv6 validation" + print " if (!is_string(value)) return 0" + print " if (value == \"\") return 0" + print " # Check for IPv6 format with colons" + print " if (index(value, \":\") == 0) return 0" + print " # Check for :: notation (double colon)" + print " has_double_colon = (index(value, \"::\") > 0)" + print " # Split by colons and validate" + print " split(value, segments, \":\")" + print " # Count non-empty segments" + print " non_empty_count = 0" + print " for (i = 1; i <= length(segments); i++) {" + print " if (segments[i] != \"\") non_empty_count++" + print " }" + print " # Should have exactly 8 non-empty segments, or fewer if :: is used" + print " if (has_double_colon && non_empty_count > 6) return 0" + print " if (!has_double_colon && non_empty_count != 8) return 0" + print " # Check each segment is valid hex" + print " for (i = 1; i <= length(segments); i++) {" + print " if (segments[i] == \"\") continue # Allow empty segments for :: notation" + print " if (length(segments[i]) > 4) return 0" + print " # Check if segment contains only hex characters" + print " gsub(/[0-9a-fA-F]/, \"\", segments[i])" + print " if (segments[i] != \"\") return 0" + print " }" + print " return 1" + print "}" + print "" + print "function is_uuid(value) {" + print " # UUID validation (standard format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)" + print " if (!is_string(value)) return 0" + print " if (value == \"\") return 0" + print " # Check length (36 characters including hyphens)" + print " if (length(value) != 36) return 0" + print " # Check format with hyphens at positions 9, 14, 19, 24" + print " if (substr(value, 9, 1) != \"-\" || substr(value, 14, 1) != \"-\" ||" + print " substr(value, 19, 1) != \"-\" || substr(value, 24, 1) != \"-\") return 0" + print " # Check that all characters are hex" + print " uuid_without_hyphens = substr(value, 1, 8) substr(value, 10, 4) substr(value, 15, 4) substr(value, 20, 4) substr(value, 25, 12)" + print " gsub(/[0-9a-fA-F]/, \"\", uuid_without_hyphens)" + print " return uuid_without_hyphens == \"\"" + print "}" + print "" print "function is_alpha(value) {" print " # Check if string contains only alphabetic characters" print " if (!is_string(value)) return 0" @@ -1023,31 +473,6 @@ END { print " return value == \"\"" print "}" print "" - print "function is_whitespace(value) {" - print " # Check if string contains only whitespace characters" - print " if (!is_string(value)) return 0" - print " if (value == \"\") return 0" - print " # Remove all whitespace characters and check if empty" - print " gsub(/[ \\t\\n\\r]/, \"\", value)" - print " return value == \"\"" - print "}" - print "" - print "function is_uppercase(value) {" - print " # Check if string is all uppercase" - print " if (!is_string(value)) return 0" - print " if (value == \"\") return 0" - print " # Convert to uppercase and compare" - print " return toupper(value) == value" - print "}" - print "" - print "function is_lowercase(value) {" - print " # Check if string is all lowercase" - print " if (!is_string(value)) return 0" - print " if (value == \"\") return 0" - print " # Convert to lowercase and compare" - print " return tolower(value) == value" - print "}" - print "" print "function is_palindrome(value) {" print " # Check if string is a palindrome" print " if (!is_string(value)) return 0" @@ -1063,144 +488,141 @@ END { print " return 1" print "}" print "" - print "function is_prime(value) {" - print " # Check if number is prime" - print " if (!is_integer(value)) return 0" - print " if (value < 2) return 0" - print " if (value == 2) return 1" - print " if (value % 2 == 0) return 0" - print " # Check odd divisors up to square root" - print " for (i = 3; i <= sqrt(value); i += 2) {" - print " if (value % i == 0) return 0" - print " }" - print " return 1" - print "}" - print "" - print "function is_even(value) {" - print " # Check if number is even" - print " return is_integer(value) && value % 2 == 0" - print "}" - print "" - print "function is_odd(value) {" - print " # Check if number is odd" - print " return is_integer(value) && value % 2 == 1" - print "}" - print "" print "function is_in_range(value, min, max) {" print " # Check if number is within range [min, max]" print " return is_number(value) && value >= min && value <= max" print "}" print "" - print "function is_length(value, expected_length, i, count) {" - print " # Check if string or array has specific length" - print " if (is_string(value)) return length(value) == expected_length" - print " if (is_array(value)) {" - print " count = 0" - print " for (i in value) count++" - print " return count == expected_length" - print " }" - print " return 0" - print "}" - print "" - print "function assert(condition, message) {" - print " if (!condition) {" - print " print \"ASSERTION FAILED: \" message > \"/dev/stderr\"" - print " print \" at line \" FNR \" in \" FILENAME > \"/dev/stderr\"" - print " exit 1" - print " }" - print " return 1" - print "}" - print "" - print "function expect_equal(actual, expected, message) {" - print " if (actual != expected) {" - print " print \"EXPECTATION FAILED: \" message > \"/dev/stderr\"" - print " print \" Expected: \" expected > \"/dev/stderr\"" - print " print \" Actual: \" actual > \"/dev/stderr\"" - print " print \" at line \" FNR \" in \" FILENAME > \"/dev/stderr\"" - print " exit 1" - print " }" - print " return 1" - print "}" - print "" - print "function expect_true(condition, message) {" - print " return assert(condition, message)" - print "}" - print "" - print "function expect_false(condition, message) {" - print " return assert(!condition, message)" +} + +# Generate function definitions +function generate_function_definitions() { + if (function_count == 0) return + + print "# --- User Functions ---" + + # Build dispatch table + print "# Dispatch table" + print "BEGIN {" + for (i = 1; i <= function_count; i++) { + internal_name = "__lambda_" (i - 1) + arg_count = split(FUNCTION_ARGS[i], args_array, ",") + print " RAWK_DISPATCH[\"" FUNCTION_NAMES[i] "\"] = \"" internal_name "|" arg_count "|" FUNCTION_LINES[i] "\"" + } print "}" print "" - - # Step 2: Store the user's compiled functions for post-processing. - # These are the standard awk functions we generated from the rawk syntax. - # (They will be printed after recursive call replacement) - # Step 3: Add recursive function call replacement and user functions (BEFORE main script) - if (lambda_counter > 0) { - print "# --- Recursive Function Call Replacement ---" - print "function replace_recursive_calls(line) {" - print " # This function replaces any remaining function calls with internal names" - print " # This handles recursive calls that weren't replaced in the first pass" - for (func_name in RAWK_DISPATCH) { - metadata = RAWK_DISPATCH[func_name] - split(metadata, parts, "|") - internal_name = parts[1] - print " gsub(\"" func_name "\\\\(\", \"" internal_name "(\", line)" + # Generate function definitions + for (i = 1; i <= function_count; i++) { + internal_name = "__lambda_" (i - 1) + body = FUNCTION_BODIES[i] + + # Replace recursive calls + for (j = 1; j <= function_count; j++) { + gsub(FUNCTION_NAMES[j] "\\(", "__lambda_" (j - 1) "(", body) } - print " return line" - print "}" - print "" - - # Step 3.1: Post-process function bodies to replace recursive calls - print "# --- Post-processed User Functions ---" - for (i = 0; i < lambda_counter; i++) { - # Get the original function body - original_body = generated_user_functions[i] - - # Replace recursive calls in the function body - processed_body = original_body - for (func_name in RAWK_DISPATCH) { - metadata = RAWK_DISPATCH[func_name] - split(metadata, parts, "|") - internal_name = parts[1] - gsub(func_name "\\(", internal_name "(", processed_body) - } - - print processed_body - print "" + + print "function " internal_name "(" FUNCTION_ARGS[i] ") {" + if (FUNCTION_TYPES[i] == "single") { + print " return " body + } else { + print body } + print "}" + print "" } +} - # Step 4: Print the main body of the script. - # These are all the lines that were not part of a rawk definition. +# Generate main script body +function generate_main_script() { print "# --- Main Script Body ---" - # Check if the main script body already contains a BEGIN block + # Check if there's already a BEGIN block has_begin = 0 - for (i = 1; i <= FNR; i++) { - if (i in modified_source_lines) { - if (modified_source_lines[i] ~ /^[ \t]*BEGIN[ \t]*\{/) { - has_begin = 1 - break - } + for (i = 1; i <= line_count; i++) { + if (SOURCE_LINE_TYPES[i] == "code" && SOURCE_LINES[i] ~ /^[ \t]*BEGIN[ \t]*\{/) { + has_begin = 1 + break } } if (has_begin) { - # If there's already a BEGIN block, just print the lines as-is - for (i = 1; i <= FNR; i++) { - if (i in modified_source_lines) { - print modified_source_lines[i] + # Print lines as-is + for (i = 1; i <= line_count; i++) { + if (SOURCE_LINE_TYPES[i] == "code") { + line = SOURCE_LINES[i] + + # Replace function calls + for (j = 1; j <= function_count; j++) { + gsub(FUNCTION_NAMES[j] "\\(", "__lambda_" (j - 1) "(", line) + } + + print line } } } else { - # If there's no BEGIN block, wrap in one + # Wrap in BEGIN block print "BEGIN {" - for (i = 1; i <= FNR; i++) { - if (i in modified_source_lines) { - print " " modified_source_lines[i] + for (i = 1; i <= line_count; i++) { + if (SOURCE_LINE_TYPES[i] == "code") { + line = SOURCE_LINES[i] + + # Replace function calls + for (j = 1; j <= function_count; j++) { + gsub(FUNCTION_NAMES[j] "\\(", "__lambda_" (j - 1) "(", line) + } + + print " " line } } print "}" } +} + +# Error reporting function +function report_error(message, line_num, line, suggestion) { + print "❌ rawk compilation error: " message > "/dev/stderr" + print " at line " line_num " in " FILENAME > "/dev/stderr" + print " context: " line > "/dev/stderr" + if (suggestion != "") { + print " 💡 " suggestion > "/dev/stderr" + } + print "" > "/dev/stderr" + error_count++ + errors++ +} + +# Warning reporting function +function report_warning(message, line_num, line, suggestion) { + print "⚠️ rawk compilation warning: " message > "/dev/stderr" + print " at line " line_num " in " FILENAME > "/dev/stderr" + print " context: " line > "/dev/stderr" + if (suggestion != "") { + print " 💡 " suggestion > "/dev/stderr" + } + print "" > "/dev/stderr" + warning_count++ + warnings++ +} + +# END block to generate final output +END { + source_lines = line_count + + # Generate standard library + generate_standard_library() + + # Generate function definitions + generate_function_definitions() + + # Generate main script body + generate_main_script() + + # Print compilation summary + print "# rawk compilation summary:" + print "# - Functions defined: " functions_defined + print "# - Source lines: " source_lines + print "# - Errors: " errors + print "# - Warnings: " warnings + print "" }
\ No newline at end of file diff --git a/awk/rawk/scratch_debug_pattern_matching.txt b/awk/rawk/scratch_debug_pattern_matching.txt deleted file mode 100644 index d23f946..0000000 --- a/awk/rawk/scratch_debug_pattern_matching.txt +++ /dev/null @@ -1,45 +0,0 @@ -# Pattern Matching Debugging Context (rawk) - -## Current State -- Pattern matching functions are parsed and detected correctly. -- Each pattern line is processed, but only the last pattern is present in the generated function body. -- Debug output shows that the result variable is being overwritten instead of concatenated. -- The parse_pattern_line function returns a string like 'if (...) { return ... }', and the main loop is supposed to append each to the result. -- However, the result variable is not accumulating all patterns as expected. - -## Root Cause Analysis -- The result variable is being overwritten instead of appended to in the pattern matching conversion loop. -- This is likely due to a misplaced assignment or a logic error in the loop or in parse_pattern_line. -- The cleanup logic previously removed concatenation, but even after removing it, the result is still not correct. -- Debug output shows that after each pattern, result only contains the most recent pattern. - -## Next Steps -1. Double-check that result is only initialized once before the loop and only appended to inside the loop. -2. Add debug prints for pattern_code and result before and after the concatenation in the loop. -3. Check the final assembly of the function body after the loop. -4. If the logic is correct, but the output is still wrong, check how the function body is constructed and printed after the loop. -5. Once fixed, remove debug output and document the fix in this file. - -## Findings -- Debug output confirms that each new pattern overwrites the result instead of appending to it. -- The result variable was initialized with a string (e.g., 'case value of\n'), and each new pattern was not being appended to the accumulating result. -- The correct approach is to initialize result as an empty string and append each pattern's code to it. -- Plan: Patch the code so result is initialized as an empty string and each pattern is appended, not overwritten. Remove any leftover 'case value of' initialization. - -## Deeper Debug -- After patching, the generated function body still only contains the last pattern. -- This suggests that either parse_pattern_line is not returning the correct string for each pattern, or the result concatenation logic is still faulty. -- Next step: Add a print of the full result variable after the loop, before any cleanup, to see what is actually being accumulated. -- Also check if parse_pattern_line is returning the correct string for each pattern. - -## Holistic Review Plan -- Review the pattern matching conversion loop and parse_pattern_line function. -- Check the initialization and update of the result variable. -- Confirm that each pattern is being appended, not overwritten. -- Review the final assembly and output of the function body. -- Document all findings and next steps here as we continue to resolve this last remaining issue. - -## Resolution (2024-06-10) -- **Root cause:** awk variable shadowing: the local variable 'result' in parse_pattern_line was clobbering the outer result accumulator in the pattern matching conversion function. -- **Fix:** Renamed the local variable to 'pattern_result'. -- **Validation:** The generated function body now correctly includes all patterns as an if/else chain. The pattern matching test suite passes.
\ No newline at end of file diff --git a/awk/rawk/temp.awk b/awk/rawk/temp.awk deleted file mode 100644 index 16a433b..0000000 --- a/awk/rawk/temp.awk +++ /dev/null @@ -1,541 +0,0 @@ -# --- rawk Standard Library --- -# Dispatch mechanism for rawk functions -function dispatch_call(func_name, arg1, arg2, arg3, arg4, arg5, metadata, parts, internal_name, arg_count) { - if (!(func_name in RAWK_DISPATCH)) { - print "Error: Function '" func_name "' not found" > "/dev/stderr" - return - } - metadata = RAWK_DISPATCH[func_name] - split(metadata, parts, "|") - internal_name = parts[1] - arg_count = parts[2] - # This is a simplified dispatch - in a real implementation, we'd need a more sophisticated approach - print "Error: Dispatch not fully implemented for function '" func_name "'" > "/dev/stderr" - return -} - -function apply(func_name, args, i, metadata, parts, internal_name, arg_count) { - if (!(func_name in RAWK_DISPATCH)) { - print "Error: Function '" func_name "' not found" > "/dev/stderr" - return - } - metadata = RAWK_DISPATCH[func_name] - split(metadata, parts, "|") - internal_name = parts[1] - arg_count = parts[2] - if (length(args) != arg_count) { - print "Error: Function '" func_name "' expects " arg_count " arguments, got " length(args) > "/dev/stderr" - return - } - return args[1] -} - -function map(func_name, array, result, i, metadata, parts, internal_name, arg_count) { - if (!(func_name in RAWK_DISPATCH)) { - print "❌ rawk runtime error: Function '" func_name "' not found" > "/dev/stderr" - print "💡 Available functions: " > "/dev/stderr" - for (f in RAWK_DISPATCH) { - print " - " f > "/dev/stderr" - } - return - } - metadata = RAWK_DISPATCH[func_name] - split(metadata, parts, "|") - internal_name = parts[1] - arg_count = parts[2] - if (arg_count != 1) { - print "❌ rawk runtime error: Function '" func_name "' must take exactly 1 argument for map" > "/dev/stderr" - print "💡 Function '" func_name "' takes " arg_count " arguments" > "/dev/stderr" - return - } - # Use a switch-based dispatch for standard awk compatibility - for (i in array) { - if (internal_name == "__lambda_0") result[i] = __lambda_0(array[i]) - else if (internal_name == "__lambda_1") result[i] = __lambda_1(array[i]) - else if (internal_name == "__lambda_2") result[i] = __lambda_2(array[i]) - else if (internal_name == "__lambda_3") result[i] = __lambda_3(array[i]) - else if (internal_name == "__lambda_4") result[i] = __lambda_4(array[i]) - else if (internal_name == "__lambda_5") result[i] = __lambda_5(array[i]) - else if (internal_name == "__lambda_6") result[i] = __lambda_6(array[i]) - else if (internal_name == "__lambda_7") result[i] = __lambda_7(array[i]) - else if (internal_name == "__lambda_8") result[i] = __lambda_8(array[i]) - else if (internal_name == "__lambda_9") result[i] = __lambda_9(array[i]) - else { - print "❌ rawk runtime error: Function '" func_name "' not supported in map" > "/dev/stderr" - print "💡 This is a limitation of the current implementation" > "/dev/stderr" - return - } - } - return result -} - -function reduce(func_name, array, initial_value, result, i, metadata, parts, internal_name, arg_count) { - if (!(func_name in RAWK_DISPATCH)) { - print "Error: Function '" func_name "' not found" > "/dev/stderr" - return - } - metadata = RAWK_DISPATCH[func_name] - split(metadata, parts, "|") - internal_name = parts[1] - arg_count = parts[2] - if (arg_count != 2) { - print "Error: Function '" func_name "' must take exactly 2 arguments for reduce" > "/dev/stderr" - return - } - result = initial_value - for (i in array) { - if (internal_name == "__lambda_0") result = __lambda_0(result, array[i]) - else if (internal_name == "__lambda_1") result = __lambda_1(result, array[i]) - else if (internal_name == "__lambda_2") result = __lambda_2(result, array[i]) - else if (internal_name == "__lambda_3") result = __lambda_3(result, array[i]) - else if (internal_name == "__lambda_4") result = __lambda_4(result, array[i]) - else if (internal_name == "__lambda_5") result = __lambda_5(result, array[i]) - else if (internal_name == "__lambda_6") result = __lambda_6(result, array[i]) - else if (internal_name == "__lambda_7") result = __lambda_7(result, array[i]) - else if (internal_name == "__lambda_8") result = __lambda_8(result, array[i]) - else if (internal_name == "__lambda_9") result = __lambda_9(result, array[i]) - else { - print "Error: Function '" func_name "' not supported in reduce" > "/dev/stderr" - return - } - } - return result -} - -function pipe(value, func_names, result, i, metadata, parts, internal_name) { - result = value - for (i = 1; i <= length(func_names); i++) { - if (!(func_names[i] in RAWK_DISPATCH)) { - print "Error: Function '" func_names[i] "' not found" > "/dev/stderr" - return - } - metadata = RAWK_DISPATCH[func_names[i]] - split(metadata, parts, "|") - internal_name = parts[1] - result = result * 2 - } - return result -} - -function get_keys(array, result, i, count) { - count = 0 - for (i in array) { - count++ - result[count] = i - } - return count -} - -function get_values(array, result, i, count) { - count = 0 - for (i in array) { - count++ - result[count] = array[i] - } - return count -} - -function keys(array) { - # This is a simplified version that just returns the count - count = 0 - for (i in array) { - count++ - } - return count -} - -function values(array) { - # This is a simplified version that just returns the count - count = 0 - for (i in array) { - count++ - } - return count -} - -# --- Predicate Functions --- -# Type checking and validation functions - -function is_number(value) { - # Check if value is a number (including 0) - return value == value + 0 -} - -function is_string(value) { - # Check if value is a string (not a number) - return value != value + 0 -} - -function is_array(value, i) { - # Check if value is an array by trying to iterate over it - # This is a heuristic - in awk, arrays are associative - # Note: This function has limitations in standard awk - # It can only detect arrays that have been passed as parameters - count = 0 - for (i in value) { - count++ - if (count > 0) return 1 - } - return 0 -} - -function is_empty(value) { - # Check if value is empty (empty string, 0, or empty array) - if (value == "") return 1 - if (value == 0) return 1 - if (is_array(value)) { - count = 0 - for (i in value) count++ - return count == 0 - } - return 0 -} - -function is_positive(value) { - # Check if value is a positive number - return is_number(value) && value > 0 -} - -function is_negative(value) { - # Check if value is a negative number - return is_number(value) && value < 0 -} - -function is_zero(value) { - # Check if value is zero - return is_number(value) && value == 0 -} - -function is_integer(value) { - # Check if value is an integer - return is_number(value) && int(value) == value -} - -function is_float(value) { - # Check if value is a floating point number - return is_number(value) && int(value) != value -} - -function is_boolean(value) { - # Check if value is a boolean (0 or 1) - return value == 0 || value == 1 -} - -function is_truthy(value) { - # Check if value is truthy (non-zero, non-empty) - if (is_number(value)) return value != 0 - if (is_string(value)) return value != "" - if (is_array(value)) { - count = 0 - for (i in value) count++ - return count > 0 - } - return 0 -} - -function is_falsy(value) { - # Check if value is falsy (zero, empty string, empty array) - return !is_truthy(value) -} - -function is_email(value) { - # Basic email validation - if (!is_string(value)) return 0 - if (value == "") return 0 - # Check for @ symbol and basic format - if (index(value, "@") == 0) return 0 - if (index(value, "@") == length(value)) return 0 - if (index(value, "@") == 0) return 0 - # Check for domain part - split(value, parts, "@") - if (length(parts) != 2) return 0 - if (parts[1] == "" || parts[2] == "") return 0 - if (index(parts[2], ".") == 0) return 0 - if (index(parts[2], ".") == length(parts[2])) return 0 - return 1 -} - -function is_url(value) { - # Basic URL validation - if (!is_string(value)) return 0 - if (value == "") return 0 - # Check for http:// or https:// - if (substr(value, 1, 7) == "http://") return 1 - if (substr(value, 1, 8) == "https://") return 1 - return 0 -} - -function is_ipv4(value) { - # Basic IPv4 validation - if (!is_string(value)) return 0 - if (value == "") return 0 - # Split by dots and check each octet - split(value, octets, ".") - if (length(octets) != 4) return 0 - for (i = 1; i <= 4; i++) { - if (!is_number(octets[i])) return 0 - if (octets[i] < 0 || octets[i] > 255) return 0 - } - return 1 -} - -function is_alpha(value) { - # Check if string contains only alphabetic characters - if (!is_string(value)) return 0 - if (value == "") return 0 - # Remove all alphabetic characters and check if empty - gsub(/[a-zA-Z]/, "", value) - return value == "" -} - -function is_numeric(value) { - # Check if string contains only numeric characters - if (!is_string(value)) return 0 - if (value == "") return 0 - # Remove all numeric characters and check if empty - gsub(/[0-9]/, "", value) - return value == "" -} - -function is_alphanumeric(value) { - # Check if string contains only alphanumeric characters - if (!is_string(value)) return 0 - if (value == "") return 0 - # Remove all alphanumeric characters and check if empty - gsub(/[a-zA-Z0-9]/, "", value) - return value == "" -} - -function is_whitespace(value) { - # Check if string contains only whitespace characters - if (!is_string(value)) return 0 - if (value == "") return 0 - # Remove all whitespace characters and check if empty - gsub(/[ \t\n\r]/, "", value) - return value == "" -} - -function is_uppercase(value) { - # Check if string is all uppercase - if (!is_string(value)) return 0 - if (value == "") return 0 - # Convert to uppercase and compare - return toupper(value) == value -} - -function is_lowercase(value) { - # Check if string is all lowercase - if (!is_string(value)) return 0 - if (value == "") return 0 - # Convert to lowercase and compare - return tolower(value) == value -} - -function is_palindrome(value) { - # Check if string is a palindrome - if (!is_string(value)) return 0 - if (value == "") return 1 - # Remove non-alphanumeric characters and convert to lowercase - gsub(/[^a-zA-Z0-9]/, "", value) - value = tolower(value) - # Check if it reads the same forwards and backwards - 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 -} - -function is_prime(value) { - # Check if number is prime - if (!is_integer(value)) return 0 - if (value < 2) return 0 - if (value == 2) return 1 - if (value % 2 == 0) return 0 - # Check odd divisors up to square root - for (i = 3; i <= sqrt(value); i += 2) { - if (value % i == 0) return 0 - } - return 1 -} - -function is_even(value) { - # Check if number is even - return is_integer(value) && value % 2 == 0 -} - -function is_odd(value) { - # Check if number is odd - return is_integer(value) && value % 2 == 1 -} - -function is_in_range(value, min, max) { - # Check if number is within range [min, max] - return is_number(value) && value >= min && value <= max -} - -function is_length(value, expected_length, i, count) { - # Check if string or array has specific length - if (is_string(value)) return length(value) == expected_length - if (is_array(value)) { - count = 0 - for (i in value) count++ - return count == expected_length - } - return 0 -} - -function assert(condition, message) { - if (!condition) { - print "ASSERTION FAILED: " message > "/dev/stderr" - print " at line " FNR " in " FILENAME > "/dev/stderr" - exit 1 - } - return 1 -} - -function expect_equal(actual, expected, message) { - if (actual != expected) { - print "EXPECTATION FAILED: " message > "/dev/stderr" - print " Expected: " expected > "/dev/stderr" - print " Actual: " actual > "/dev/stderr" - print " at line " FNR " in " FILENAME > "/dev/stderr" - exit 1 - } - return 1 -} - -function expect_true(condition, message) { - return assert(condition, message) -} - -function expect_false(condition, message) { - return assert(!condition, message) -} - -# --- Recursive Function Call Replacement --- -function replace_recursive_calls(line) { - # This function replaces any remaining function calls with internal names - # This handles recursive calls that weren't replaced in the first pass - gsub("classify_string\\(", "__lambda_2(", line) - gsub("run_test\\(", "__lambda_0(", line) - gsub("classify_number\\(", "__lambda_1(", line) - gsub("validate_input\\(", "__lambda_4(", line) - gsub("classify_type\\(", "__lambda_3(", line) - return line -} - -# --- Post-processed User Functions --- -function __lambda_0(name, actual, expected) { - total_tests++ - if (actual == expected) { - passed_tests++ - print "✓ " name - } else { - failed_tests++ - print "❌ " name " (expected '" expected "', got '" actual "')" - -} - -function __lambda_1(value) { - if (value == 0) { return "zero" } - else if (1 && (is_positive(value))) { return "positive" } - else if (1 && (is_negative(value))) { return "negative" } - else if (1) { return "unknown" } -} - -function __lambda_2(str) { - if (str == "") { return "empty" } - else if (str == && (is_alpha(str))) { return "alphabetic" } - else if (str == && (is_numeric(str))) { return "numeric" } - else if (str == && (is_alphanumeric(str))) { return "alphanumeric" } - else if (str == && (is_palindrome(str))) { return "palindrome" } - else if (1) { return "other" } -} - -function __lambda_3(value) { - if (1 && (is_number(value))) { return "number" } - else if (1 && (is_string(value))) { return "string" } - else if (1 && (is_empty(value))) { return "empty" } - else if (1) { return "unknown" } -} - -function __lambda_4(value) { - if (value == "") { return "empty input" } - else if (1 && (is_email(value))) { return "valid email" } - else if (1 && (is_url(value))) { return "valid url" } - else if (1 && (is_ipv4(value))) { return "valid ipv4" } - else if (1 && (is_number(value) && is_in_range(value, 1, 100))) { return "valid number in range" } - else if (1) { return "invalid input" } -} - -# --- Main Script Body --- -# Test suite for rawk pattern matching -# This demonstrates the new pattern matching capabilities - -BEGIN { - print "=== rawk Pattern Matching Test Suite ===" - print "" - - # Test counters - total_tests = 0 - passed_tests = 0 - failed_tests = 0 - - # Helper function to run tests - } - - # Pattern matching function for number classification - - # Pattern matching function for string classification - - # Pattern matching function for type checking - - # Pattern matching function for validation - - # Test number classification - print "=== Number Classification Tests ===" - __lambda_0("__lambda_1(0)", __lambda_1(0), "zero") - __lambda_0("__lambda_1(42)", __lambda_1(42), "positive") - __lambda_0("__lambda_1(-5)", __lambda_1(-5), "negative") - __lambda_0("__lambda_1(3.14)", __lambda_1(3.14), "positive") - - print "" - print "=== String Classification Tests ===" - __lambda_0("__lambda_2(\"\")", __lambda_2(""), "empty") - __lambda_0("__lambda_2(\"hello\")", __lambda_2("hello"), "alphabetic") - __lambda_0("__lambda_2(\"123\")", __lambda_2("123"), "numeric") - __lambda_0("__lambda_2(\"Hello123\")", __lambda_2("Hello123"), "alphanumeric") - __lambda_0("__lambda_2(\"racecar\")", __lambda_2("racecar"), "palindrome") - __lambda_0("__lambda_2(\"hello world\")", __lambda_2("hello world"), "other") - - print "" - print "=== Type Classification Tests ===" - __lambda_0("__lambda_3(42)", __lambda_3(42), "number") - __lambda_0("__lambda_3(\"hello\")", __lambda_3("hello"), "string") - __lambda_0("__lambda_3(\"\")", __lambda_3(""), "empty") - __lambda_0("__lambda_3(0)", __lambda_3(0), "number") - - print "" - print "=== Validation Tests ===" - __lambda_0("__lambda_4(\"\")", __lambda_4(""), "empty input") - __lambda_0("__lambda_4(\"user@example.com\")", __lambda_4("user@example.com"), "valid email") - __lambda_0("__lambda_4(\"http://example.com\")", __lambda_4("http://example.com"), "valid url") - __lambda_0("__lambda_4(\"192.168.1.1\")", __lambda_4("192.168.1.1"), "valid ipv4") - __lambda_0("__lambda_4(50)", __lambda_4(50), "valid number in range") - __lambda_0("__lambda_4(150)", __lambda_4(150), "invalid input") - __lambda_0("__lambda_4(\"invalid\")", __lambda_4("invalid"), "invalid input") - - # Print summary - print "" - print "=== Test Summary ===" - print "Total tests: " total_tests - print "Passed: " passed_tests - print "Failed: " failed_tests - - if (failed_tests == 0) { - print "🎉 All pattern matching tests passed!" - } else { - print "❌ Some tests failed!" - } -} diff --git a/awk/rawk/test_pattern_simple.rawk b/awk/rawk/test_pattern_simple.rawk deleted file mode 100644 index 830fb7c..0000000 --- a/awk/rawk/test_pattern_simple.rawk +++ /dev/null @@ -1,23 +0,0 @@ -# Simple test for pattern matching - -BEGIN { - print "=== Simple Pattern Matching Test ===" - print "" - - # Simple pattern matching function -$classify = (value) -> { - case value of - | 0 -> "zero" - | n if is_positive(n) -> "positive" - | n if is_negative(n) -> "negative" - | _ -> "unknown" - } - - # Test the function - print "classify(0): " classify(0) - print "classify(42): " classify(42) - print "classify(-5): " classify(-5) - - print "" - print "🎉 Pattern matching test completed!" -}
\ No newline at end of file |