aboutsummaryrefslogtreecommitdiffstats
path: root/awk/rawk/example.rawk
diff options
context:
space:
mode:
Diffstat (limited to 'awk/rawk/example.rawk')
-rw-r--r--awk/rawk/example.rawk100
1 files changed, 100 insertions, 0 deletions
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