Print debugging statement: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(+C)
Line 10: Line 10:
* Show the print debugging statements in the language.
* Show the print debugging statements in the language.
* Demonstrate their ability to track provenance by displaying information about source code (e.g., code fragment, line and column number).
* Demonstrate their ability to track provenance by displaying information about source code (e.g., code fragment, line and column number).

=={{header|C}}==

<lang C>#include <stdio.h>

#define DEBUG_INT(x) printf( #x " at line %d\nresult: %d\n\n", __LINE__, x)

int add(int x, int y) {
int result = x + y;
DEBUG_INT(x);
DEBUG_INT(y);
DEBUG_INT(result);
DEBUG_INT(result+1);
return result;
}

int main() {
add(2, 7);
return 0;
}</lang>

{{out}}
<pre>
x at line 7
result: 2

y at line 8
result: 7

result at line 9
result: 9

result+1 at line 10
result: 10
</pre>


=={{header|Pyret}}==
=={{header|Pyret}}==

Revision as of 14:34, 27 August 2019

Print debugging statement is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

From Wikipedia:

Print debugging (or tracing) is the act of watching (live or recorded) trace statements, or print statements, that indicate the flow of execution of a process. This is sometimes called printf debugging, due to the use of the printf function in C.

Task
  • Show the print debugging statements in the language.
  • Demonstrate their ability to track provenance by displaying information about source code (e.g., code fragment, line and column number).

C

<lang C>#include <stdio.h>

  1. define DEBUG_INT(x) printf( #x " at line %d\nresult: %d\n\n", __LINE__, x)

int add(int x, int y) {

 int result = x + y;
 DEBUG_INT(x);
 DEBUG_INT(y);
 DEBUG_INT(result);
 DEBUG_INT(result+1);
 return result;

}

int main() {

 add(2, 7);
 return 0;

}</lang>

Output:
x at line 7
result: 2

y at line 8
result: 7

result at line 9
result: 9

result+1 at line 10
result: 10

Pyret

Pyret has the spy expression. The expression can print the value of an identifier, using the identifier itself as a label if it's not already given. It could also print the value of an arbitrary expression, but it needs an explicit label in this case.

<lang pyret>fun add(x, y):

 result = x + y
 spy "in add": 
   x,
   y,
   result,
   result-plus-one: result + 1
 end
 result

end

add(2, 7)</lang>

Output:
Spying "in add" (at file:///spies.arr:3:2-8:5)
  x: 2
  y: 7
  result: 9
  result-plus-one: 10

9

Racket

Racket doesn't have a built-in print debugging statement. However, it can be defined by users as a macro.

<lang racket>#lang racket

(require syntax/parse/define)

(define (debug:core line col code val #:label [label #f])

 ;; if label exists, use it instead of the code fragment
 (printf "~a at line ~a column ~a\n" (or label code) line col)
 (printf "result: ~a\n\n" val)
 ;; return the value itself, so that we can wrap macro around an expression 
 ;; without restructuring any code
 val)

(define-simple-macro (debug <x> option ...)

 #:with line (datum->syntax this-syntax (syntax-line #'<x>))
 #:with col (datum->syntax this-syntax (syntax-column #'<x>))
 (debug:core line col (quote <x>) <x> option ...))

(define (add x y)

 (define result (+ x y))
 (debug x)
 (debug y)
 (debug (if #t (+ x y) (error 'impossible)))
 (debug (add1 result) #:label "result plus one")
 (debug result))

(add 2 7)</lang>

Output:
x at line 20 column 9
result: 2

y at line 21 column 9
result: 7

(if #t (+ x y) (error 'impossible)) at line 22 column 9
result: 9

result plus one at line 23 column 9
result: 10

result at line 24 column 9
result: 9

9