feat: add call stack tracking and enhanced error reporting with file:line context

Implement a full call stack system that tracks function calls during interpretation, including native function boundaries. Add `push_call_frame`/`pop_call_frame` functions, a `CallStack` struct with depth-limited frames, and `print_stacktrace` for formatted error output. Introduce `error_with_context` to replace bare `error()` calls, and modify the tokenizer to track line/column/filename per token. Update the parser and interpreter to push/pop frames on function entry/exit, and add new Makefile targets (`demo-error-*`) plus test scripts (`test_error_stacktrace.rc`, `test_error_native.rc`, `test_error_undefined_var.rc`) and an `examples/error_handling_demo.rc` to demonstrate the new error reporting capabilities.
This commit is contained in:
2025-11-24 00:59:54 +00:00
parent 9a83539e33
commit 655d33d789
14 changed files with 391 additions and 21 deletions
+24
View File
@@ -19,6 +19,7 @@ SOURCES = $(SRC_DIR)/main.c \
$(SRC_DIR)/string_utils.c \
$(SRC_DIR)/preprocessor.c \
$(SRC_DIR)/oop.c \
$(SRC_DIR)/error.c \
$(SRC_DIR)/stdlib/stdlib.c \
$(SRC_DIR)/stdlib/string/string_stdlib.c \
$(SRC_DIR)/stdlib/math/math_stdlib.c \
@@ -136,6 +137,20 @@ test-eval: $(TARGET)
test-validation: $(TARGET)
$(TARGET) $(TEST_DIR)/test_input_validation.rc
demo-error-stacktrace: $(TARGET)
@echo "=== Demonstrating Full Stack Trace ==="
-$(TARGET) $(TEST_DIR)/test_error_stacktrace.rc
demo-error-undefined: $(TARGET)
@echo "=== Demonstrating Undefined Variable Error ==="
-$(TARGET) $(TEST_DIR)/test_error_undefined_var.rc
demo-error-native: $(TARGET)
@echo "=== Demonstrating Error in Native Function Call ==="
-$(TARGET) $(TEST_DIR)/test_error_native.rc
demo-errors: demo-error-stacktrace demo-error-undefined demo-error-native
run-http-simple: $(TARGET)
$(TARGET) $(EXAMPLE_DIR)/http_simple.rc
@@ -165,6 +180,14 @@ help:
@echo " make test-async - Async stdlib (20+ tests)"
@echo " make test-oop - OOP features (31 tests)"
@echo " make test-preprocessor - Preprocessor (2 tests)"
@echo " make test-eval - Eval function (30 tests)"
@echo " make test-validation - Input validation (77 tests)"
@echo ""
@echo "Error Handling Demonstrations:"
@echo " make demo-errors - Run all error handling demos"
@echo " make demo-error-stacktrace - Show full stack trace with file:line"
@echo " make demo-error-undefined - Show undefined variable error with tips"
@echo " make demo-error-native - Show error during native function call"
@echo ""
@echo "Examples:"
@echo " make run-http-simple - Run http_simple.rc (single connection HTTP server)"
@@ -178,6 +201,7 @@ help:
@echo " stdlib/math/ - Math functions"
@echo " stdlib/io/ - I/O functions"
@echo " stdlib/async/ - Async functions"
@echo " stdlib/eval/ - Eval function"
@echo " tests/ - Test programs (unittest framework)"
@echo " examples/ - Example programs (.rc files)"
@echo " build/ - Compiled object files"