diff options
| author | elioat <{ID}+{username}@users.noreply.github.com> | 2025-07-31 22:35:38 -0400 |
|---|---|---|
| committer | elioat <{ID}+{username}@users.noreply.github.com> | 2025-07-31 22:35:38 -0400 |
| commit | bb344902c1ab24f56a722096726b6fe453ccaa94 (patch) | |
| tree | c313b8424976cd3dfb82d70163b455968cadbbeb /awk | |
| parent | 94f9179d1a4a3685138976e354734b92c3ebc65a (diff) | |
| download | tour-bb344902c1ab24f56a722096726b6fe453ccaa94.tar.gz | |
*
Diffstat (limited to 'awk')
| -rw-r--r-- | awk/rawk/README.md | 68 | ||||
| -rw-r--r-- | awk/rawk/rawk.awk | 35 |
2 files changed, 40 insertions, 63 deletions
diff --git a/awk/rawk/README.md b/awk/rawk/README.md index e906e1f..1656029 100644 --- a/awk/rawk/README.md +++ b/awk/rawk/README.md @@ -1,6 +1,7 @@ -# rawk v2.0.0 - Functional Programming for awk +# rawk +## Make awk rawk. -**rawk** compiles functional-style code to standard `awk`. Write modern functions, get portable awk output. +Rawk helps to bring some modern developer comforts to awk while maintaining awk's portability and inbuilt goodness. ## Quick Start @@ -49,12 +50,15 @@ awk -f example_output.awk sample.log # Or run with just a few lines head -10 sample.log | awk -f example_output.awk + +# Or compile and run without outputting an awk file to disk +awk -f rawk.awk example.rawk | awk -f - sample.log ``` ## Syntax ### Function Definitions -All functions go inside `RAWK { ... }` blocks: +All functions go inside an `RAWK { ... }` block, ```rawk RAWK { @@ -65,7 +69,8 @@ RAWK { ``` ### Function Calls -Call functions anywhere in regular awk code: +Call rawk functions from anywhere in the code, + ```rawk { result = add(5, 3); @@ -74,7 +79,8 @@ Call functions anywhere in regular awk code: ``` ### Mixed Code -Regular awk works seamlessly: +Mix and match awk and rawk code, + ```rawk BEGIN { FS = "," } @@ -92,7 +98,6 @@ RAWK { ``` ## Standard Library - Rawk boasts a rather large standard library. ### Testing @@ -101,19 +106,19 @@ expect_equal(add(2, 3), 5, "Addition should work"); expect_true(is_positive(5), "5 should be positive"); ``` -### Type Checking +### Type Checking Predicates ```rawk if (is_number(value)) { ... } if (is_string(value)) { ... } ``` -### Validation +### Varuius Validation Predicates ```rawk if (is_email(email)) { ... } if (is_url(url)) { ... } ``` -### Functional Programming +### Functional Programming Patterns ```rawk # Transform array elements count = map("double", numbers, doubled); @@ -125,49 +130,10 @@ count = filter("is_positive", numbers, positive); sum = reduce("add", numbers); ``` -## Examples - -### Log Processing -```rawk -RAWK { - $parse_log = (line) -> { - split(line, parts, " "); - return parts[1] " - " parts[7]; - }; - - $is_error = (status) -> { - return status >= 400; - }; -} - -{ - if (is_error($9)) { - print parse_log($0); - } -} -``` - -### Data Validation -```rawk -RAWK { - $validate_user = (name, email, age) -> { - if (!is_alpha(name)) return 0; - if (!is_email(email)) return 0; - if (!is_number(age) || age < 0) return 0; - return 1; - }; -} - -{ - if (validate_user($1, $2, $3)) { - print "Valid user: " $1; - } -} -``` - ## Testing -Run the test suite: +Run the test suite, + ```bash cd tests && ./test_runner.sh ``` @@ -175,7 +141,7 @@ cd tests && ./test_runner.sh ## Requirements - Any awk implementation (gawk, mawk, nawk, etc.) -- No additional dependencies, strive to work with any POSIX awk +- No additional dependencies, strives to work with any POSIX awk ## License diff --git a/awk/rawk/rawk.awk b/awk/rawk/rawk.awk index 8180a7b..5ee579b 100644 --- a/awk/rawk/rawk.awk +++ b/awk/rawk/rawk.awk @@ -1,11 +1,22 @@ #!/usr/bin/awk -f + +# rawk.awk + +# Author: @eli_oat +# License: Public Domain +# Version: +RAWK_VERSION = "0.0.1" + +# Lets help awk rawk + # ============================================================================= -# rawk v2.0.0 - Multi-pass block-based compiler +# Multi-pass compiler # ============================================================================= # -# This compiler transforms rawk code into standard awk with smart standard -# library inclusion. It uses a multi-pass approach to overcome AWK's variable -# scoping limitations and ensure deterministic compilation. +# This compiler transforms rawk code into standard awk and smartly includes only +# those standard library functions you've actually used. It uses a multi-pass +# approach to overcome awk's variable scoping limitations and ensure +# deterministic compilation. # # COMPILATION PROCESS: # Pass 1: Collect all input lines into memory @@ -27,7 +38,7 @@ BEGIN { # ============================================================================= # Arrays to store compilation state - delete lines # All input lines (Pass 1) + delete lines # All input lines (Pass 1) delete FUNCTION_NAMES # User-defined function names (Pass 3) delete FUNCTION_ARGS # User-defined function arguments (Pass 3) delete FUNCTION_BODIES # User-defined function bodies (Pass 3) @@ -307,7 +318,7 @@ END { # user-defined functions, and the main script body. # Output header with compilation metadata - print "# Generated by rawk v2.0.0" + print "# Generated with rawk v" RAWK_VERSION print "# Source: " ARGV[1] print "" @@ -334,13 +345,13 @@ END { # This is the "smart inclusion" feature that only includes functions that are called for (func_name in USED_STDLIB_FUNCTIONS) { if (func_name == "assert") { - print "function assert(condition, message) { if (!condition) { print \"❌ Assertion failed: \" message > \"/dev/stderr\"; exit 1 } }" + print "function assert(condition, message) { if (!condition) { print \"Assertion failed: \" message > \"/dev/stderr\"; exit 1 } }" } else if (func_name == "expect_equal") { - print "function expect_equal(actual, expected, message) { if (actual != expected) { print \"❌ Expected \" expected \" but got \" actual \" - \" message > \"/dev/stderr\"; exit 1 } }" + print "function expect_equal(actual, expected, message) { if (actual != expected) { print \"Expected \" expected \" but got \" actual \" - \" message > \"/dev/stderr\"; exit 1 } }" } else if (func_name == "expect_true") { - print "function expect_true(condition, message) { if (!condition) { print \"❌ Expected true but got false - \" message > \"/dev/stderr\"; exit 1 } }" + print "function expect_true(condition, message) { if (!condition) { print \"Expected true but got false - \" message > \"/dev/stderr\"; exit 1 } }" } else if (func_name == "expect_false") { - print "function expect_false(condition, message) { if (condition) { print \"❌ Expected false but got true - \" message > \"/dev/stderr\"; exit 1 } }" + print "function expect_false(condition, message) { if (condition) { print \"Expected false but got true - \" message > \"/dev/stderr\"; exit 1 } }" } else if (func_name == "is_positive") { print "function is_positive(value) { return is_number(value) && value > 0 }" } else if (func_name == "is_negative") { @@ -465,7 +476,7 @@ END { # DISPATCH FUNCTION: Dynamic function calling for functional programming # ============================================================================= # The dispatch_call function enables functional programming utilities (map, reduce, etc.) - # to dynamically call user-defined functions by name. This is only included when needed. + # to dynamically call user-defined functions by name. This is only included when used. if ("map" in USED_STDLIB_FUNCTIONS || "reduce" in USED_STDLIB_FUNCTIONS || "filter" in USED_STDLIB_FUNCTIONS || "find" in USED_STDLIB_FUNCTIONS || "findIndex" in USED_STDLIB_FUNCTIONS || "flatMap" in USED_STDLIB_FUNCTIONS || "pipe" in USED_STDLIB_FUNCTIONS || "pipe_multi" in USED_STDLIB_FUNCTIONS) { print "# Dispatch function for functional programming" @@ -521,7 +532,7 @@ END { # ============================================================================= print "" print "# Rawk compilation summary:" - print "# - Rawk Version: 2.0.0" + print "# - Rawk Version: " RAWK_VERSION print "# - Functions defined: " function_count print "# - Source lines: " line_count print "# - Standard library functions included: " length(USED_STDLIB_FUNCTIONS) |