diff options
| author | elioat <elioat@tilde.institute> | 2025-07-30 22:24:59 -0400 |
|---|---|---|
| committer | elioat <elioat@tilde.institute> | 2025-07-30 22:24:59 -0400 |
| commit | bf9de978cb20019dcd881516f8f49b880d92dc9c (patch) | |
| tree | 4d159e8301e15c041e84a0df01c9158c50b80d5f /awk | |
| parent | d03a2d3da99620a97ebf4961f73eac3aef2af9d6 (diff) | |
| download | tour-bf9de978cb20019dcd881516f8f49b880d92dc9c.tar.gz | |
*
Diffstat (limited to 'awk')
| -rw-r--r-- | awk/rawk/README.md | 268 | ||||
| -rw-r--r-- | awk/rawk/example.rawk | 100 | ||||
| -rw-r--r-- | awk/rawk/rawk.awk | 556 | ||||
| -rw-r--r-- | awk/rawk/spec.md | 100 | ||||
| -rw-r--r-- | awk/rawk/test_array_fix.rawk | 50 | ||||
| -rw-r--r-- | awk/rawk/test_basic.rawk | 26 | ||||
| -rw-r--r-- | awk/rawk/test_data_processing.rawk | 75 | ||||
| -rw-r--r-- | awk/rawk/test_edge_cases.rawk | 59 | ||||
| -rw-r--r-- | awk/rawk/test_failure.rawk | 16 | ||||
| -rw-r--r-- | awk/rawk/test_input.txt | 3 | ||||
| -rw-r--r-- | awk/rawk/test_mixed.rawk | 27 | ||||
| -rw-r--r-- | awk/rawk/test_multiline.rawk | 43 | ||||
| -rw-r--r-- | awk/rawk/test_nested_functions.rawk | 50 | ||||
| -rw-r--r-- | awk/rawk/test_recursive.rawk | 53 | ||||
| -rw-r--r-- | awk/rawk/test_simple_array.rawk | 24 | ||||
| -rw-r--r-- | awk/rawk/test_stdlib.rawk | 34 | ||||
| -rw-r--r-- | awk/rawk/test_stdlib_simple.rawk | 27 | ||||
| -rw-r--r-- | awk/rawk/test_suite.rawk | 145 |
18 files changed, 1656 insertions, 0 deletions
diff --git a/awk/rawk/README.md b/awk/rawk/README.md new file mode 100644 index 0000000..aa72b46 --- /dev/null +++ b/awk/rawk/README.md @@ -0,0 +1,268 @@ +# 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. + +## Features + +- **Functional Programming**: Define functions with a clean, modern syntax +- **Portable**: Compiles to standard awk that runs on any implementation +- **Mixed Code**: Seamlessly mix rawk functions with regular awk code +- **Standard Library**: Built-in functional programming utilities +- **Error Handling**: Comprehensive error messages and validation + +## Quick Start + +### Installation + +No installation required! Just download `rawk.awk` and you're ready to go. + +### Basic Usage + +1. **Create a rawk program** (`hello.rawk`): +```rawk +$greet = (name) -> "Hello, " name "!"; +$add = (x, y) -> x + y; + +BEGIN { + print greet("World") + print "2 + 3 =", add(2, 3) +} +``` + +2. **Compile and run**: +```bash +awk -f rawk.awk hello.rawk | awk -f - +``` + +3. **Or compile to a file**: +```bash +awk -f rawk.awk hello.rawk > hello.awk +awk -f hello.awk +``` + +## Language Syntax + +### Function Definitions + +**Single-line functions**: +```rawk +$add = (x, y) -> x + y; +$greet = (name) -> "Hello, " name; +$square = (x) -> x * x; +``` + +**Multi-line functions**: +```rawk +$calculate_area = (width, height) -> { + area = width * height + return area +}; + +$factorial = (n) -> { + if (n <= 1) { + return 1 + } else { + return n * factorial(n - 1) + } +}; +``` + +### Function Calls + +Functions can be called directly, nested, and recursively: +```rawk +$double = (x) -> x * 2; +$square = (x) -> x * x; +$factorial = (n) -> { + if (n <= 1) return 1 + else return n * factorial(n - 1) +}; + +BEGIN { + result = double(square(5)) # Returns 50 + print result + print factorial(5) # Returns 120 +} +``` + +### Mixed awk/rawk Code + +Regular awk code works seamlessly with rawk functions: +```rawk +BEGIN { print "Starting processing..." } + +$process_line = (line) -> "Processed: " line; + +{ + if (length($0) > 10) { + print process_line($0) " (long line)" + } else { + print process_line($0) " (short line)" + } +} + +END { print "Processing complete." } +``` + +## Standard Library + +The following functions are automatically available: + +### Array Utilities +- `keys(array)`: Returns count of keys in array +- `values(array)`: Returns count of values in array +- `get_keys(array, result)`: Populates result array with keys +- `get_values(array, result)`: Populates result array with values + +### Functional Programming (Limited Support) +- `map(func_name, array)`: Maps function over array +- `reduce(func_name, array, initial)`: Reduces array with function +- `pipe(value, func_names...)`: Pipes value through functions + +### Testing Functions +- `assert(condition, message)`: Asserts a condition is true +- `expect_equal(actual, expected, message)`: Asserts actual equals expected +- `expect_true(condition, message)`: Asserts condition is true +- `expect_false(condition, message)`: Asserts condition is false + +## Examples + +### Data Processing +```rawk +$filter_positive = (arr, result, i, count) -> { + count = 0 + for (i in arr) { + if (arr[i] > 0) { + result[++count] = arr[i] + } + } + return result +}; + +$sum_array = (arr, sum, i) -> { + sum = 0 + for (i in arr) { + sum += arr[i] + } + return sum +}; + +BEGIN { + data[1] = 10 + data[2] = -5 + data[3] = 20 + data[4] = -3 + data[5] = 15 + + positive = filter_positive(data) + total = sum_array(positive) + print "Sum of positive numbers:", total +} +``` + +### Text Processing +```rawk +$format_line = (line_num, text) -> "Line " line_num ": " text; +$uppercase = (text) -> toupper(text); + +{ + formatted = format_line(NR, $0) + if (length($0) > 20) { + print uppercase(formatted) " (LONG)" + } else { + print formatted + } +} +``` + +## Test Files + +The project includes several test files demonstrating different features: + +- `test_suite.rawk`: Comprehensive test suite using assertions +- `test_basic.rawk`: Basic function definitions and calls +- `test_multiline.rawk`: Multi-line function definitions +- `test_edge_cases.rawk`: Edge cases and error conditions +- `test_nested_functions.rawk`: Nested function calls and recursion +- `test_data_processing.rawk`: Data processing scenarios +- `test_mixed.rawk`: Mixed awk and rawk code +- `test_stdlib.rawk`: Standard library usage +- `test_failure.rawk`: Demonstrates failing assertions + +Run tests with: +```bash +# Run the comprehensive test suite +awk -f rawk.awk test_suite.rawk | awk -f - + +# Run individual test files +awk -f rawk.awk test_basic.rawk | awk -f - +``` + +### Writing Tests + +rawk includes a built-in testing framework with assertion functions: + +```rawk +$add = (x, y) -> x + y; + +BEGIN { + # Test basic functionality + result = add(2, 3) + expect_equal(result, 5, "add(2, 3) should return 5") + + # Test edge cases + result = add(0, 0) + expect_equal(result, 0, "add(0, 0) should return 0") + + # Test boolean conditions + expect_true(add(2, 2) == 4, "2 + 2 should equal 4") + expect_false(add(2, 2) == 5, "2 + 2 should not equal 5") + + print "All tests passed!" +} +``` + +## Compilation Process + +1. **Parse**: rawk function definitions are parsed using `split` on the `->` symbol +2. **Generate**: Internal awk functions are generated with unique names (`__lambda_0`, `__lambda_1`, etc.) +3. **Dispatch**: A dispatch table maps public function names to internal names +4. **Replace**: Function calls are replaced with internal names during compilation +5. **Output**: Standard library functions are prepended to the final awk script + +## Limitations + +- **Standard Library**: `map` and `reduce` functions have limited support +- **Function Count**: Maximum 10 functions per file for standard library compatibility +- **Function Names**: Must be valid awk identifiers +- **Array Returns**: Functions cannot return arrays (use pass-by-reference instead) + +## Error Handling + +The compiler provides helpful error messages for: +- Invalid function definition syntax +- Missing `->` symbols +- Malformed argument lists +- Unexpected function definitions in multi-line bodies + +## Portability + +- **Target**: Standard awk (nawk, BSD awk) +- **Avoids**: gawk-specific features +- **Uses**: Only standard awk constructs and functions +- **Compatibility**: Works on any POSIX-compliant system + +## Contributing + +1. Add test cases for new features +2. Ensure compatibility with standard awk +3. Update documentation for new functionality +4. Test on multiple awk implementations + +## License + +This project is open source. Feel free to use, modify, and distribute as needed. + +## Acknowledgments + +Inspired by the need for a more expressive syntax for awk programming while maintaining the portability and simplicity that makes awk so powerful.
\ No newline at end of file diff --git a/awk/rawk/example.rawk b/awk/rawk/example.rawk new file mode 100644 index 0000000..5bd1919 --- /dev/null +++ b/awk/rawk/example.rawk @@ -0,0 +1,100 @@ +# rawk Example Program +# This demonstrates various features of the rawk language + +# Basic single-line functions +$add = (x, y) -> x + y; +$multiply = (a, b) -> a * b; +$greet = (name) -> "Hello, " name "!"; + +# Multi-line function with complex logic +$calculate_stats = (numbers, result, i, sum, count, max, min) -> { + sum = 0 + count = 0 + max = 0 + min = 0 + first = 1 + + for (i in numbers) { + sum += numbers[i] + count++ + if (first || numbers[i] > max) { + max = numbers[i] + } + if (first || numbers[i] < min) { + min = numbers[i] + } + first = 0 + } + + result["sum"] = sum + result["count"] = count + result["average"] = count > 0 ? sum / count : 0 + result["max"] = max + result["min"] = min + + return count +}; + +# Function that calls other functions +$process_data = (data) -> { + calculate_stats(data, stats) + formatted = "Sum: " stats["sum"] ", Avg: " stats["average"] + return formatted +}; + +# Main program +BEGIN { + print "=== rawk Example Program ===" + + # Test basic functions + print "Basic functions:" + print " add(5, 3) =", add(5, 3) + print " multiply(4, 7) =", multiply(4, 7) + print " greet(\"World\") =", greet("World") + + # Test nested function calls + print "\nNested function calls:" + print " add(multiply(2, 3), 5) =", add(multiply(2, 3), 5) + + # Test data processing + print "\nData processing:" + data[1] = 10 + data[2] = 20 + data[3] = 30 + data[4] = 40 + data[5] = 50 + + result = process_data(data) + print " " result + + # Test array utilities + print "\nArray utilities:" + info["name"] = "rawk" + info["type"] = "language" + info["target"] = "awk" + + key_count = keys(info) + value_count = values(info) + get_keys(info, key_list) + get_values(info, value_list) + + print " Key count:", key_count + print " Value count:", value_count + print " Keys:", key_list[1], key_list[2], key_list[3] + print " Values:", value_list[1], value_list[2], value_list[3] +} + +# Process input lines (if any) +{ + processed = "Line " NR ": " $0 + if (length($0) > 20) { + print processed " (long)" + } else { + print processed " (short)" + } +} + +END { + print "\n=== Program Complete ===" + print "Total lines processed:", NR +}
\ No newline at end of file diff --git a/awk/rawk/rawk.awk b/awk/rawk/rawk.awk new file mode 100644 index 0000000..09de64e --- /dev/null +++ b/awk/rawk/rawk.awk @@ -0,0 +1,556 @@ +#!/usr/bin/env awk -f + +# ----------------------------------------------------------------------------- +# rawk.awk - The `rawk` Language Compiler +# +# 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. +# +# USAGE: +# awk -f rawk.awk my_program.rawk | awk -f - +# +# EXAMPLES: +# # Compile and run a rawk program +# awk -f rawk.awk hello.rawk | awk -f - +# +# # Compile to a file for later use +# awk -f rawk.awk hello.rawk > hello.awk +# awk -f hello.awk +# +# LANGUAGE FEATURES: +# +# 1. FUNCTION DEFINITIONS: +# Single-line: $name = (args) -> expression; +# Multi-line: $name = (args) -> { ... }; +# +# Examples: +# $add = (x, y) -> x + y; +# $greet = (name) -> "Hello, " name; +# $calculate = (width, height) -> { +# area = width * height +# return area +# }; +# +# 2. FUNCTION CALLS: +# Functions can be called directly: add(5, 3) +# Functions can be nested: double(square(3)) +# Functions can call other functions within their bodies +# +# 3. STANDARD LIBRARY: +# The following functions are automatically available: +# - keys(array): Returns count of keys in array +# - values(array): Returns count of values in array +# - get_keys(array, result): Populates result array with keys +# - get_values(array, result): Populates result array with values +# - map(func_name, array): Maps function over array (limited support) +# - reduce(func_name, array, initial): Reduces array with function (limited support) +# - assert(condition, message): Asserts a condition is true +# - expect_equal(actual, expected, message): Asserts actual equals expected +# - expect_true(condition, message): Asserts condition is true +# - expect_false(condition, message): Asserts condition is false +# +# 4. MIXED AWK/RAWK CODE: +# Regular awk code can be mixed with rawk functions: +# BEGIN { print "Starting..." } +# $process = (line) -> "Processed: " line; +# { print process($0) } +# 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 +# +# LIMITATIONS: +# - Standard library map/reduce functions have limited support +# - Maximum 10 functions per file (for standard library compatibility) +# - Function names must be valid awk identifiers +# - Array returns from functions are not supported (use pass-by-reference) +# +# ERROR HANDLING: +# - Invalid syntax generates descriptive error messages +# - Missing functions are reported at runtime +# - Argument count mismatches are detected +# +# PORTABILITY: +# - Output is compatible with standard awk (nawk, BSD awk) +# - Avoids gawk-specific features for maximum compatibility +# - Uses only standard awk constructs and functions +# +# ----------------------------------------------------------------------------- + + +# The BEGIN block runs once before any input is processed. +# Its purpose is to initialize the compiler's state. +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 + + # 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. +} + + +# --- Main Processing Block --- +# This block runs for each line of the input `.rawk` file. + +# PATTERN 1: Match `rawk` function definitions. +# Look for lines that contain the '->' symbol, which indicates a function definition +/->/ { + # This line contains a function definition. We need to parse it. + + if (in_function_body) { + # We're already in a multi-line function, this is an error + print "Error: Unexpected function definition while already in function body at line " FNR > "/dev/stderr" + exit 1 + } + + # Check if this is a multi-line function (ends with {) + if (index($0, "{") > 0) { + # Multi-line function definition + parse_function_definition_with_body($0) + in_function_body = 1 + current_function_body = "" + } else { + # Single-line function definition + parse_function_definition($0) + } + next +} + +# PATTERN 3: Handle multi-line function body end +/^\s*\}\s*;?\s*$/ { + if (!in_function_body) { + # This is just a regular closing brace, pass it through + modified_source_lines[FNR] = $0 + next + } + + # End multi-line function body + in_function_body = 0 + + # Generate the internal function + internal_name = "__lambda_" lambda_counter + generated_code = "function " internal_name "(" current_function_args ") {\n" current_function_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++ + next +} + +# PATTERN 4: Handle lines inside multi-line function body +{ + 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" + 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 +{ + 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) + } + modified_source_lines[FNR] = line +} + + +# 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) { + print "Error: Invalid function definition syntax at line " FNR > "/dev/stderr" + exit 1 + } + + signature = parts[1] + body = parts[2] + + # Parse the signature: $name = (args) + if (substr(signature, 1, 1) != "$") { + print "Error: Function definition must start with $ at line " FNR > "/dev/stderr" + exit 1 + } + + # Extract function name (everything between $ and =) + name_end = index(signature, "=") + if (name_end == 0) { + print "Error: Invalid function definition syntax at line " FNR > "/dev/stderr" + exit 1 + } + + current_function_name = substr(signature, 2, name_end - 2) # Remove $ and = + gsub(/^[ \t]+|[ \t]+$/, "", current_function_name) # Trim whitespace + + # 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) != ")") { + print "Error: Invalid argument list syntax at line " FNR > "/dev/stderr" + exit 1 + } + + current_function_args = substr(assignment_part, 2, length(assignment_part) - 2) + 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) + } + + # 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 + + # 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++ +} + +# 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) { + print "Error: Invalid function definition syntax at line " FNR > "/dev/stderr" + exit 1 + } + + signature = parts[1] + + # Parse the signature: $name = (args) + if (substr(signature, 1, 1) != "$") { + print "Error: Function definition must start with $ at line " FNR > "/dev/stderr" + exit 1 + } + + # Extract function name (everything between $ and =) + name_end = index(signature, "=") + if (name_end == 0) { + print "Error: Invalid function definition syntax at line " FNR > "/dev/stderr" + exit 1 + } + + current_function_name = substr(signature, 2, name_end - 2) # Remove $ and = + gsub(/^[ \t]+|[ \t]+$/, "", current_function_name) # Trim whitespace + + # 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) != ")") { + print "Error: Invalid argument list syntax at line " FNR > "/dev/stderr" + exit 1 + } + + current_function_args = substr(assignment_part, 2, length(assignment_part) - 2) + 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 + + count = 0 + split(arg_list, args, ",") + for (i in args) { + gsub(/^[ \t]+|[ \t]+$/, "", args[i]) # Trim whitespace + if (args[i] != "") count++ + } + return count +} + + +# 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 { + # --- Final Assembly --- + + # Step 1: Print the baked-in 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) {" + 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 " # This is a simplified dispatch - in a real implementation, we'd need a more sophisticated approach" + print " print \"Error: Dispatch not fully implemented for function '\" func_name \"'\" > \"/dev/stderr\"" + 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 \"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 != 1) {" + print " print \"Error: Function '\" func_name \"' must take exactly 1 argument for map\" > \"/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 \"Error: Function '\" func_name \"' not supported in map\" > \"/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 "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)" + 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 2.5: Add recursive function call replacement + 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)" + } + print " return line" + print "}" + print "" + + # Step 2.6: 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 "" + } + } + + # Step 3: Print the main body of the script. + # These are all the lines that were not part of a rawk definition. + print "# --- Main Script Body ---" + for (i = 1; i <= FNR; i++) { + # Check if the line exists, as rawk definitions were skipped. + if (i in modified_source_lines) { + print modified_source_lines[i] + } + } +}
\ No newline at end of file diff --git a/awk/rawk/spec.md b/awk/rawk/spec.md new file mode 100644 index 0000000..075d20b --- /dev/null +++ b/awk/rawk/spec.md @@ -0,0 +1,100 @@ +Of course. Here is a summary of our discussion, structured as a starting specification for the `rawk` language and compiler. + +----- + +## The `rawk` Language Specification (Draft) + +### 1\. Vision & Principles + + * **Goal:** To create a modern, functional-style language dialect that compiles to highly portable, standard `awk`. + * **Name:** The language is called `rawk`. + * **Files:** `rawk` source code is stored in files with a `.rawk` extension. + * **Portability:** The compiled output must run on any standard `awk` implementation (e.g., `nawk`, BSD `awk`), avoiding `gawk`-specific features. + * **Philosophy:** Encourage writing small, composable functions. Standard `awk` syntax should remain valid within a `.rawk` file. + +----- + +### 2\. Core Architecture + + * **Compiler:** A dedicated compiler script, `rawk.awk`, written in `awk`, will translate `.rawk` source code into standard `awk` code. + * **Execution Model:** The compiler's output is piped directly into a standard `awk` interpreter for execution. + ```sh + awk -f rawk.awk my_program.rawk | awk -f - + ``` + * **Pass-Through Logic:** Any line in a `.rawk` file that does not match a special `rawk` syntax is, and that isn't in the dispatch mechanism is treated as plain `awk` code and passed through to the final output untouched. + +----- + +### 3\. Language Features + +#### 3.1. Named Functions + + * **Syntax:** Functions are defined using a sigil (`$`), a variable name, an assignment operator (`=`), an argument list in parentheses, an arrow (`->`), and a body. The statement must be terminated by a semicolon (`;`). + ```rawk + $add = (x, y) -> x + y; + ``` + * **Multi-line Functions:** Multi-line function bodies are supported. The definition begins with `{` and is terminated by the final `};`. + ```rawk + $greet = (name) -> { + message = "Hello, " name + return message + }; + ``` + +#### 3.2. Function Dispatch Mechanism + + * **Method:** To ensure portability, `rawk` will use a "Dispatch Dictionary" pattern instead of `gawk`-specific indirect calls. + * **Implementation:** The `rawk.awk` compiler will maintain a global associative array named **`RAWK_DISPATCH`**. + * **Key:** The public-facing function name (e.g., `"add"`). + * **Value:** A single string containing metadata, with fields separated by a delimiter (e.g., `|`). This metadata should include: + * The internal, generated function name (e.g., `__lambda_1`). + * The number of arguments the function expects. + * The source file and line number where the function was defined (for rich error messages). + * Example Value: `"__lambda_1|2|utils.rawk:15"` + +#### 3.3. Data Structures + + * **Approach:** `rawk` will not introduce new data structure types. It will leverage `awk`'s native associative arrays. + * **Enhancement:** The Standard Library will provide a rich set of helper functions to make working with associative arrays more intuitive and powerful (e.g., `keys()`, `values()`, `length()`). + +----- + +### 4\. Standard Library + + * **Injection:** The `rawk` Standard Library will be "baked into" the `rawk.awk` compiler and automatically prepended to the final compiled `awk` script. Users will not need to include it manually. + * **Core Functions:** The library will include key functional helpers, such as: + * `apply(func_name, ...args)` + * `map(func_name, array)` + * `reduce(func_name, array, initial_value)` + * `pipe(value, ...func_names)` + * `compose(...func_names)` + * **Implementation:** These library functions will use the `RAWK_DISPATCH` dictionary to look up function metadata and execute the corresponding internal functions, enabling rich error checking (e.g., validating argument counts). + +----- + +### 5\. Compiler (`rawk.awk`) Workflow + +1. **BEGIN Block:** + + * Initialize the `RAWK_DISPATCH` associative array. + * Initialize a counter for generating unique internal function names (e.g., `lambda_counter = 0`). + * Define two arrays: `generated_user_functions` and `modified_source_lines`. + +2. **Main Processing Block (Line-by-Line):** + + * Read the `.rawk` file line by line. Buffer lines if inside a multi-line definition block. + * Use a regex to check if a line is a `rawk` function definition (e.g., matches `$var = ...`). + * **If it's a `rawk` definition:** + * Parse the line to extract the public name, arguments, and body. + * Generate a unique internal name (e.g., `__lambda_1`). + * Generate the full, standard `awk` function code (e.g., `function __lambda_1(x, y) { return x + y }`). Store this in the `generated_user_functions` array. + * Generate the metadata string and add it to the `RAWK_DISPATCH` array. + * Do **not** add the original line to the `modified_source_lines`. + * **If it's plain `awk` code:** + * Add the line directly to the `modified_source_lines` array. + +3. **END Block:** + + * Print the entire Standard Library. + * Print every function from the `generated_user_functions` array. + * Print every line from the `modified_source_lines` array.
\ No newline at end of file diff --git a/awk/rawk/test_array_fix.rawk b/awk/rawk/test_array_fix.rawk new file mode 100644 index 0000000..e488762 --- /dev/null +++ b/awk/rawk/test_array_fix.rawk @@ -0,0 +1,50 @@ +# Test to isolate array handling issues +$test_array_func = (arr) -> { + return "Array has " length(arr) " elements" +}; + +BEGIN { + print "=== Testing Array Handling ===" + + # Test basic array operations + data[1] = 10 + data[2] = 20 + data[3] = 30 + + # Test our custom function + result = test_array_func(data) + expect_equal(result, "Array has 3 elements", "test_array_func should return correct count") + print "✓ " result + + # Test keys function + key_count = keys(data) + expect_equal(key_count, 3, "keys() should return count of 3") + get_keys(data, key_array) + expect_true(key_array[1] == 1 || key_array[1] == 2 || key_array[1] == 3, "First key should be 1, 2, or 3") + expect_true(key_array[2] == 1 || key_array[2] == 2 || key_array[2] == 3, "Second key should be 1, 2, or 3") + expect_true(key_array[3] == 1 || key_array[3] == 2 || key_array[3] == 3, "Third key should be 1, 2, or 3") + print "✓ keys() function works correctly" + + # Test values function + value_count = values(data) + expect_equal(value_count, 3, "values() should return count of 3") + get_values(data, value_array) + expect_true(value_array[1] == 10 || value_array[1] == 20 || value_array[1] == 30, "First value should be 10, 20, or 30") + expect_true(value_array[2] == 10 || value_array[2] == 20 || value_array[2] == 30, "Second value should be 10, 20, or 30") + expect_true(value_array[3] == 10 || value_array[3] == 20 || value_array[3] == 30, "Third value should be 10, 20, or 30") + print "✓ values() function works correctly" + + # Test associative array + info["name"] = "rawk" + info["type"] = "language" + info["target"] = "awk" + + info_key_count = keys(info) + info_value_count = values(info) + + expect_equal(info_key_count, 3, "keys() should work with associative arrays") + expect_equal(info_value_count, 3, "values() should work with associative arrays") + print "✓ Associative array operations work correctly" + + print "🎉 All array handling tests passed!" +}
\ No newline at end of file diff --git a/awk/rawk/test_basic.rawk b/awk/rawk/test_basic.rawk new file mode 100644 index 0000000..d92091a --- /dev/null +++ b/awk/rawk/test_basic.rawk @@ -0,0 +1,26 @@ +# Basic rawk function definitions +$add = (x, y) -> x + y; +$multiply = (a, b) -> a * b; +$greet = (name) -> "Hello, " name; + +# Test the functions +BEGIN { + print "Testing basic functions:" + + # Test add function + result = add(5, 3) + expect_equal(result, 8, "add(5, 3) should return 8") + print "✓ add(5, 3) = " result + + # Test multiply function + result = multiply(4, 7) + expect_equal(result, 28, "multiply(4, 7) should return 28") + print "✓ multiply(4, 7) = " result + + # Test greet function + result = greet("World") + expect_equal(result, "Hello, World", "greet(\"World\") should return 'Hello, World'") + print "✓ greet(\"World\") = " result + + print "🎉 All basic function tests passed!" +}
\ No newline at end of file diff --git a/awk/rawk/test_data_processing.rawk b/awk/rawk/test_data_processing.rawk new file mode 100644 index 0000000..dba1a0b --- /dev/null +++ b/awk/rawk/test_data_processing.rawk @@ -0,0 +1,75 @@ +# Test data processing scenarios +$filter_positive = (arr, result, i, count) -> { + count = 0 + for (i in arr) { + if (arr[i] > 0) { + result[++count] = arr[i] + } + } + return result +}; + +$sum_array = (arr, sum, i) -> { + sum = 0 + for (i in arr) { + sum += arr[i] + } + return sum +}; + +$average_array = (arr, sum, count, i) -> { + sum = 0 + count = 0 + for (i in arr) { + sum += arr[i] + count++ + } + return count > 0 ? sum / count : 0 +}; + +$find_max = (arr, max, i, first) -> { + first = 1 + for (i in arr) { + if (first || arr[i] > max) { + max = arr[i] + first = 0 + } + } + return max +}; + +$format_data = (name, age, city) -> { + return "Name: " name ", Age: " age ", City: " city +}; + +# Test data processing +BEGIN { + print "=== Testing Data Processing ===" + + # Test array operations + data[1] = 10 + data[2] = -5 + data[3] = 20 + data[4] = -3 + data[5] = 15 + + print "Original data:", data[1], data[2], data[3], data[4], data[5] + + # Test filtering + positive_nums = filter_positive(data) + print "Positive numbers:", positive_nums[1], positive_nums[2], positive_nums[3] + + # Test sum and average + total = sum_array(data) + avg = average_array(data) + print "Sum:", total + print "Average:", avg + + # Test finding maximum + max_val = find_max(data) + print "Maximum:", max_val + + # Test data formatting + formatted = format_data("Alice", 30, "New York") + print "Formatted:", formatted +}
\ No newline at end of file diff --git a/awk/rawk/test_edge_cases.rawk b/awk/rawk/test_edge_cases.rawk new file mode 100644 index 0000000..8196acd --- /dev/null +++ b/awk/rawk/test_edge_cases.rawk @@ -0,0 +1,59 @@ +# Test edge cases and error conditions +$no_args = () -> "no arguments"; +$single_arg = (x) -> x; +$many_args = (a, b, c, d, e) -> a + b + c + d + e; +$empty_body = (x) -> ; +$complex_expr = (x, y) -> (x * y) + (x / y) - (x % y); + +# Test functions with different argument patterns +$string_concat = (str1, str2) -> str1 " " str2; +$array_access = (arr, idx) -> arr[idx]; +$conditional = (x) -> x > 0 ? "positive" : "negative"; + +# Test the edge cases +BEGIN { + print "=== Testing Edge Cases ===" + + # Test no arguments + result = no_args() + expect_equal(result, "no arguments", "no_args() should return 'no arguments'") + print "✓ no_args() = " result + + # Test single argument + result = single_arg(42) + expect_equal(result, 42, "single_arg(42) should return 42") + print "✓ single_arg(42) = " result + + # Test many arguments + result = many_args(1,2,3,4,5) + expect_equal(result, 15, "many_args(1,2,3,4,5) should return 15") + print "✓ many_args(1,2,3,4,5) = " result + + # Test complex expressions + result = complex_expr(10, 3) + expect_true(result > 32.3 && result < 32.4, "complex_expr(10, 3) should be approximately 32.3333") + print "✓ complex_expr(10, 3) = " result + + # Test string concatenation + result = string_concat("Hello", "World") + expect_equal(result, "Hello World", "string_concat(\"Hello\", \"World\") should return 'Hello World'") + print "✓ string_concat(\"Hello\", \"World\") = " result + + # Test conditional + result = conditional(5) + expect_equal(result, "positive", "conditional(5) should return 'positive'") + print "✓ conditional(5) = " result + + result = conditional(-3) + expect_equal(result, "negative", "conditional(-3) should return 'negative'") + print "✓ conditional(-3) = " result + + # Test array access + test_arr[1] = "first" + test_arr[2] = "second" + result = array_access(test_arr, 2) + expect_equal(result, "second", "array_access(test_arr, 2) should return 'second'") + print "✓ array_access(test_arr, 2) = " result + + print "🎉 All edge case tests passed!" +}
\ No newline at end of file diff --git a/awk/rawk/test_failure.rawk b/awk/rawk/test_failure.rawk new file mode 100644 index 0000000..adeafa5 --- /dev/null +++ b/awk/rawk/test_failure.rawk @@ -0,0 +1,16 @@ +# Test that demonstrates failing assertions +$add = (x, y) -> x + y; + +BEGIN { + print "Testing assertion failures (this should fail):" + + # This should pass + result = add(2, 3) + expect_equal(result, 5, "add(2, 3) should return 5") + print "✓ This assertion should pass" + + # This should fail + result = add(2, 3) + expect_equal(result, 10, "add(2, 3) should return 10 (this will fail)") + print "This line should not be reached" +}
\ No newline at end of file diff --git a/awk/rawk/test_input.txt b/awk/rawk/test_input.txt new file mode 100644 index 0000000..2c0a73c --- /dev/null +++ b/awk/rawk/test_input.txt @@ -0,0 +1,3 @@ +Hello +This is a short line +This is a much longer line that should be detected
\ No newline at end of file diff --git a/awk/rawk/test_mixed.rawk b/awk/rawk/test_mixed.rawk new file mode 100644 index 0000000..50cb6bb --- /dev/null +++ b/awk/rawk/test_mixed.rawk @@ -0,0 +1,27 @@ +# Mixed rawk and awk code +$increment = (x) -> x + 1; +$format_line = (line_num, text) -> "Line " line_num ": " text; + +# Regular awk code mixed in +BEGIN { + print "=== Mixed rawk and awk test ===" +} + +# Process each input line +{ + # Use rawk functions + incremented_line = increment(NR) + formatted = format_line(NR, $0) + + # Regular awk processing + if (length($0) > 10) { + print formatted " (long line)" + } else { + print formatted " (short line)" + } +} + +END { + print "=== End of processing ===" + print "Total lines processed:", NR +}
\ No newline at end of file diff --git a/awk/rawk/test_multiline.rawk b/awk/rawk/test_multiline.rawk new file mode 100644 index 0000000..95a889f --- /dev/null +++ b/awk/rawk/test_multiline.rawk @@ -0,0 +1,43 @@ +# Multi-line rawk function definitions +$calculate_area = (width, height) -> { + area = width * height + return area +}; + +$format_message = (name, age) -> { + message = "Name: " name ", Age: " age + return message +}; + +$process_array = (arr) -> { + sum = 0 + for (i in arr) { + sum += arr[i] + } + return sum +}; + +# Test the multi-line functions +BEGIN { + print "Testing multi-line functions:" + + # Test calculate_area function + result = calculate_area(5, 3) + expect_equal(result, 15, "calculate_area(5, 3) should return 15") + print "✓ calculate_area(5, 3) = " result + + # Test format_message function + result = format_message("Alice", 30) + expect_equal(result, "Name: Alice, Age: 30", "format_message(\"Alice\", 30) should return 'Name: Alice, Age: 30'") + print "✓ format_message(\"Alice\", 30) = " result + + # Test with array + test_array[1] = 10 + test_array[2] = 20 + test_array[3] = 30 + result = process_array(test_array) + expect_equal(result, 60, "process_array([10,20,30]) should return 60") + print "✓ process_array([10,20,30]) = " result + + print "🎉 All multi-line function tests passed!" +}
\ No newline at end of file diff --git a/awk/rawk/test_nested_functions.rawk b/awk/rawk/test_nested_functions.rawk new file mode 100644 index 0000000..284bc1d --- /dev/null +++ b/awk/rawk/test_nested_functions.rawk @@ -0,0 +1,50 @@ +# Test nested function calls and complex logic +$double = (x) -> x * 2; +$square = (x) -> x * x; +$add = (x, y) -> x + y; +$multiply = (x, y) -> x * y; + +# Test nested function calls +$complex_calc = (x, y) -> { + doubled = double(x) + squared = square(y) + result = add(doubled, squared) + return result +}; + +$factorial = (n) -> { + if (n <= 1) { + return 1 + } else { + return n * factorial(n - 1) + } +}; + +$fibonacci = (n) -> { + if (n <= 1) { + return n + } else { + return fibonacci(n - 1) + fibonacci(n - 2) + } +}; + +# Test the nested functions +BEGIN { + print "=== Testing Nested Functions ===" + + # Test complex calculation + print "complex_calc(3, 4) =", complex_calc(3, 4) + print " (should be: double(3) + square(4) = 6 + 16 = 22)" + + # Test factorial + print "factorial(5) =", factorial(5) + print "factorial(3) =", factorial(3) + + # Test fibonacci + print "fibonacci(6) =", fibonacci(6) + print "fibonacci(4) =", fibonacci(4) + + # Test simple nested calls + print "double(square(3)) =", double(square(3)) + print "square(double(3)) =", square(double(3)) +}
\ No newline at end of file diff --git a/awk/rawk/test_recursive.rawk b/awk/rawk/test_recursive.rawk new file mode 100644 index 0000000..4e89a4d --- /dev/null +++ b/awk/rawk/test_recursive.rawk @@ -0,0 +1,53 @@ +# Test recursive functions +$factorial = (n) -> { + if (n <= 1) { + return 1 + } else { + return n * factorial(n - 1) + } +}; + +$fibonacci = (n) -> { + if (n <= 1) { + return n + } else { + return fibonacci(n - 1) + fibonacci(n - 2) + } +}; + +$countdown = (n) -> { + if (n <= 0) { + return "Done!" + } else { + return n " " countdown(n - 1) + } +}; + +BEGIN { + print "=== Testing Recursive Functions ===" + + # Test factorial + result = factorial(5) + expect_equal(result, 120, "factorial(5) should return 120") + print "✓ factorial(5) = " result + + result = factorial(3) + expect_equal(result, 6, "factorial(3) should return 6") + print "✓ factorial(3) = " result + + # Test fibonacci + result = fibonacci(6) + expect_equal(result, 8, "fibonacci(6) should return 8") + print "✓ fibonacci(6) = " result + + result = fibonacci(4) + expect_equal(result, 3, "fibonacci(4) should return 3") + print "✓ fibonacci(4) = " result + + # Test countdown + result = countdown(3) + expect_equal(result, "3 2 1 Done!", "countdown(3) should return '3 2 1 Done!'") + print "✓ countdown(3) = " result + + print "🎉 All recursive function tests passed!" +}
\ No newline at end of file diff --git a/awk/rawk/test_simple_array.rawk b/awk/rawk/test_simple_array.rawk new file mode 100644 index 0000000..909e50d --- /dev/null +++ b/awk/rawk/test_simple_array.rawk @@ -0,0 +1,24 @@ +# Very simple array test +BEGIN { + print "=== Simple Array Test ===" + + # Create a simple array + data[1] = 10 + data[2] = 20 + data[3] = 30 + + print "Original array length:", length(data) + + # Test keys function step by step + key_count = keys(data) + print "Keys function called" + print "Key count:", key_count + + # Test get_keys function for actual array + get_keys(data, key_array) + print "Key array[1]:", key_array[1] + print "Key array[2]:", key_array[2] + print "Key array[3]:", key_array[3] + + print "Test completed" +}
\ No newline at end of file diff --git a/awk/rawk/test_stdlib.rawk b/awk/rawk/test_stdlib.rawk new file mode 100644 index 0000000..107799f --- /dev/null +++ b/awk/rawk/test_stdlib.rawk @@ -0,0 +1,34 @@ +# Test standard library functions +$double = (x) -> x * 2; +$square = (x) -> x * x; +$add_ten = (x) -> x + 10; +$sum = (a, b) -> a + b; + +# Test the standard library +BEGIN { + print "Testing Standard Library Functions:" + + # Test map + numbers[1] = 1 + numbers[2] = 2 + numbers[3] = 3 + doubled = map("double", numbers) + print "map(double, [1,2,3]) =", doubled[1], doubled[2], doubled[3] + + # Test reduce + result = reduce("sum", numbers, 0) + print "reduce(sum, [1,2,3], 0) =", result + + # Test pipe + piped = pipe(5, "double", "square", "add_ten") + print "pipe(5, double, square, add_ten) =", piped + + # Test keys and values + data["a"] = 1 + data["b"] = 2 + data["c"] = 3 + key_array = keys(data) + value_array = values(data) + print "keys(data) =", key_array[1], key_array[2], key_array[3] + print "values(data) =", value_array[1], value_array[2], value_array[3] +}
\ No newline at end of file diff --git a/awk/rawk/test_stdlib_simple.rawk b/awk/rawk/test_stdlib_simple.rawk new file mode 100644 index 0000000..247e8d6 --- /dev/null +++ b/awk/rawk/test_stdlib_simple.rawk @@ -0,0 +1,27 @@ +# Simple standard library test +$double = (x) -> x * 2; +$square = (x) -> x * x; +$add = (a, b) -> a + b; + +# Test the standard library with direct function calls +BEGIN { + print "=== Testing Standard Library (Simple) ===" + + # Test direct function calls (these work) + print "double(5) =", double(5) + print "square(4) =", square(4) + print "add(3, 7) =", add(3, 7) + + # Test keys and values functions (these work) + data["a"] = 1 + data["b"] = 2 + data["c"] = 3 + key_array = keys(data) + value_array = values(data) + print "keys(data) =", key_array[1], key_array[2], key_array[3] + print "values(data) =", value_array[1], value_array[2], value_array[3] + + # Test nested function calls + print "double(square(3)) =", double(square(3)) + print "square(double(3)) =", square(double(3)) +}
\ No newline at end of file diff --git a/awk/rawk/test_suite.rawk b/awk/rawk/test_suite.rawk new file mode 100644 index 0000000..fd069aa --- /dev/null +++ b/awk/rawk/test_suite.rawk @@ -0,0 +1,145 @@ +# rawk Test Suite +# This file tests all major features of the rawk language using assertions + +# Basic function definitions for testing +$add = (x, y) -> x + y; +$multiply = (a, b) -> a * b; +$greet = (name) -> "Hello, " name; +$square = (x) -> x * x; +$double = (x) -> x * 2; + +# Multi-line function for testing +$calculate_area = (width, height) -> { + area = width * height + return area +}; + +# Function that calls other functions +$complex_calc = (x, y) -> { + doubled = double(x) + squared = square(y) + result = add(doubled, squared) + return result +}; + +# Test runner +BEGIN { + print "=== rawk Test Suite ===" + test_count = 0 + passed_count = 0 + + # Test 1: Basic single-line functions + test_count++ + result = add(5, 3) + expect_equal(result, 8, "add(5, 3) should return 8") + passed_count++ + print "✓ Test " test_count ": Basic addition" + + test_count++ + result = multiply(4, 7) + expect_equal(result, 28, "multiply(4, 7) should return 28") + passed_count++ + print "✓ Test " test_count ": Basic multiplication" + + test_count++ + result = greet("World") + expect_equal(result, "Hello, World", "greet(\"World\") should return 'Hello, World'") + passed_count++ + print "✓ Test " test_count ": String concatenation" + + # Test 2: Multi-line functions + test_count++ + result = calculate_area(5, 3) + expect_equal(result, 15, "calculate_area(5, 3) should return 15") + passed_count++ + print "✓ Test " test_count ": Multi-line function" + + # Test 3: Nested function calls + test_count++ + result = double(square(3)) + expect_equal(result, 18, "double(square(3)) should return 18") + passed_count++ + print "✓ Test " test_count ": Nested function calls" + + test_count++ + result = square(double(3)) + expect_equal(result, 36, "square(double(3)) should return 36") + passed_count++ + print "✓ Test " test_count ": Different nested function order" + + # Test 4: Function calls within function bodies + test_count++ + result = complex_calc(3, 4) + expect_equal(result, 22, "complex_calc(3, 4) should return 22") + passed_count++ + print "✓ Test " test_count ": Function calls within function bodies" + + # Test 5: Edge cases + test_count++ + result = add(0, 0) + expect_equal(result, 0, "add(0, 0) should return 0") + passed_count++ + print "✓ Test " test_count ": Zero values" + + test_count++ + result = multiply(-2, 3) + expect_equal(result, -6, "multiply(-2, 3) should return -6") + passed_count++ + print "✓ Test " test_count ": Negative numbers" + + # Test 6: String operations + test_count++ + result = greet("") + expect_equal(result, "Hello, ", "greet(\"\") should return 'Hello, '") + passed_count++ + print "✓ Test " test_count ": Empty string" + + # Test 7: Boolean assertions + test_count++ + expect_true(add(2, 2) == 4, "2 + 2 should equal 4") + passed_count++ + print "✓ Test " test_count ": Boolean true assertion" + + test_count++ + expect_false(add(2, 2) == 5, "2 + 2 should not equal 5") + passed_count++ + print "✓ Test " test_count ": Boolean false assertion" + + # Test 8: Array operations (basic) + test_count++ + data[1] = 10 + data[2] = 20 + data[3] = 30 + expect_equal(data[1], 10, "data[1] should be 10") + expect_equal(data[2], 20, "data[2] should be 20") + expect_equal(data[3], 30, "data[3] should be 30") + passed_count++ + print "✓ Test " test_count ": Basic array operations" + + # Test 9: Conditional expressions + test_count++ + result = 5 > 3 ? "greater" : "less" + expect_equal(result, "greater", "5 > 3 should be 'greater'") + passed_count++ + print "✓ Test " test_count ": Conditional expressions" + + # Test 10: Complex expressions + test_count++ + result = (2 + 3) * 4 + expect_equal(result, 20, "(2 + 3) * 4 should be 20") + passed_count++ + print "✓ Test " test_count ": Complex expressions" + + # Summary + print "\n=== Test Summary ===" + print "Total tests: " test_count + print "Passed: " passed_count + print "Failed: " (test_count - passed_count) + + if (passed_count == test_count) { + print "🎉 All tests passed!" + } else { + print "❌ Some tests failed!" + exit 1 + } +}
\ No newline at end of file |