diff options
| author | elioat <{ID}+{username}@users.noreply.github.com> | 2025-07-21 07:57:45 -0400 |
|---|---|---|
| committer | elioat <{ID}+{username}@users.noreply.github.com> | 2025-07-21 07:57:45 -0400 |
| commit | e1662a5405920b6338e342c4e04b48e555dbf6bc (patch) | |
| tree | 7de27b7e00bbb8a3e94e335328bce43a7ade11da /awk | |
| parent | d242546a4a618a97307e74c066080395d1333e7b (diff) | |
| download | tour-e1662a5405920b6338e342c4e04b48e555dbf6bc.tar.gz | |
*
Diffstat (limited to 'awk')
| -rwxr-xr-x | awk/scheme/scheme/bin/compiler.awk | 47 | ||||
| -rwxr-xr-x | awk/scheme/scheme/test/run_tests.sh | 7 |
2 files changed, 33 insertions, 21 deletions
diff --git a/awk/scheme/scheme/bin/compiler.awk b/awk/scheme/scheme/bin/compiler.awk index 681610e..f36f497 100755 --- a/awk/scheme/scheme/bin/compiler.awk +++ b/awk/scheme/scheme/bin/compiler.awk @@ -32,6 +32,7 @@ BEGIN { # AWK FEATURE: ENVIRON is a built-in array containing environment variables # Unlike JS process.env, this is automatically available in awk DEBUG = (ENVIRON["DEBUG"] == "1") ? 1 : 0 + error_flag = 0 # Set to 1 if any error occurs } # Debug logging helper function @@ -57,6 +58,9 @@ END { # Parse and compile each expression in the program split_expressions(program) + debug("END block: error_flag=" error_flag) + # If error_flag was set, exit 1 + if (error_flag) exit 1 } # Splits input into individual Scheme expressions @@ -68,27 +72,31 @@ END { # # The function processes the entire program text and identifies complete # expressions that can be compiled independently -function split_expressions(prog, current, paren_count, i, c, expr, cleaned) { +function split_expressions(prog, current, paren_count, i, c, expr, cleaned, lines, n, line, in_string, out, j) { current = "" paren_count = 0 - - # Clean up the input: - # 1. Remove comments (lines starting with ;; or ;) - # 2. Remove comments within lines (text between ; and end of line) - # 3. Normalize whitespace - # 4. Trim leading/trailing whitespace - cleaned = prog - # AWK FEATURE: gsub() is a built-in function for global substitution (like replaceAll in JS) - # gsub(pattern, replacement, target) - modifies target in place and returns count - gsub(/^[ \t]*;;.*$/, "", cleaned) # Remove lines starting with ;; - gsub(/^[ \t]*;.*$/, "", cleaned) # Remove lines starting with ; - gsub(/;[^"]*$/, "", cleaned) # Remove comments at end of lines (but not in strings) - gsub(/[ \t\n]+/, " ", cleaned) # Normalize whitespace to single spaces - # AWK FEATURE: sub() is like gsub() but only replaces the first occurrence - sub(/^[ \t\n]+/, "", cleaned) # Trim leading whitespace - sub(/[ \t\n]+$/, "", cleaned) # Trim trailing whitespace - + # Improved comment removal: process line by line + n = split(prog, lines, "\n") + out = "" + for (j = 1; j <= n; j++) { + line = lines[j] + # Skip lines that start with ';' (comments) + if (line ~ /^[ \t]*;/) continue + # Remove inline comments, but not inside strings + in_string = 0 + cleaned_line = "" + for (i = 1; i <= length(line); i++) { + c = substr(line, i, 1) + if (c == "\"") in_string = !in_string + if (!in_string && c == ";") break + cleaned_line = cleaned_line c + } + # Append cleaned line + out = out cleaned_line "\n" + } + cleaned = out debug("Cleaned program: [" cleaned "]") + if (cleaned == "") return if (cleaned == "") return @@ -172,7 +180,9 @@ function split_expressions(prog, current, paren_count, i, c, expr, cleaned) { # Check for incomplete expressions if (paren_count > 0) { + debug("paren_count at end of split_expressions: " paren_count) error("Unmatched opening parentheses - incomplete expression") + exit 1 } # Add final HALT instruction @@ -1377,5 +1387,6 @@ function compile_expr(expr, split_result, op, args) { # Error reporting helper function error(msg) { print "Error: " msg > "/dev/stderr" + error_flag = 1 exit 1 }
\ No newline at end of file diff --git a/awk/scheme/scheme/test/run_tests.sh b/awk/scheme/scheme/test/run_tests.sh index aac834d..4e7d181 100755 --- a/awk/scheme/scheme/test/run_tests.sh +++ b/awk/scheme/scheme/test/run_tests.sh @@ -49,6 +49,7 @@ print_status() { run_test() { local test_file=$1 local test_name=$(basename "$test_file" .scm) + local rel_test_file=${test_file#$PROJECT_ROOT/} TOTAL_TESTS=$((TOTAL_TESTS + 1)) @@ -88,7 +89,7 @@ run_test() { else print_status "FAIL" "$test_name (exit code: $exit_code)" FAILED_TESTS=$((FAILED_TESTS + 1)) - FAILED_TEST_NAMES+=("$test_name") + FAILED_TEST_NAMES+=("$rel_test_file") echo "Error output:" echo "$output" | sed 's/^/ /' @@ -140,8 +141,8 @@ print_summary() { echo -e "${RED}Some tests failed!${NC}" echo echo "Failed tests:" - for test_name in "${FAILED_TEST_NAMES[@]}"; do - echo -e " ${RED}✗${NC} $test_name" + for test_path in "${FAILED_TEST_NAMES[@]}"; do + echo -e " ${RED}✗${NC} $test_path" done exit 1 fi |