Source information
The compiler knows the file, line and procedure of every statement, and a few predefined names hand that knowledge to the program. They are what makes a diagnostic message say where.
'' The compiler knows where every line came from.
sub Report( )
print "in "; __FUNCTION__; ", line"; __LINE__
end sub
print "file: "; __FILE__
print "line:"; __LINE__
Report( )
'' In a macro they describe the place the macro is USED, which is what
'' makes a CHECK macro useful.
#define CHECK( cond ) _
if not ( cond ) then _
print __FILE__ & "(" & __LINE__ & "): failed: " & #cond
function Average( byval total as int32, byval n as int32 ) as int32
CHECK( n > 0 )
return iif( n > 0, total \ n, 0 )
end function
var a = Average( 10, 4 )
var b = Average( 10, 0 )
print "averages:"; a; b
'' The build date changes every day, so print only its shape.
print "an ISO date has"; len( __DATE_ISO__ ); " characters"
'' #line renumbers the lines that follow, and can rename the file.
#line 1000 "generated.tiko"
print "now: "; __FILE__; " line"; __LINE__
Output
file: sourceinfo.tiko
line: 7
in REPORT, line 3
sourceinfo.tiko(17): failed: n > 0
averages: 2 0
an ISO date has 10 characters
now: generated.tiko line 1001
| Name | Gives |
|---|---|
__FILE__ | the source file's name, as a string, spelled as it was given to the compiler |
__LINE__ | the line it is written on, as a number |
__FUNCTION__ | the enclosing procedure's name, as a string, in capitals |
__DATE_ISO__ | the build date as "yyyy-mm-dd" |
- Inside a macro they expand where the macro is used.
CHECKis defined on line 12, but the message names line 17, the line insideAveragethat used it. That is how assertion and logging macros report the caller's position. #condturns the macro's argument into text, so the message shows the condition as it was written.#line 1000 "generated.tiko"renumbers the source: the directive's own line becomes 1000, so the line after it is 1001, and__FILE__changes too. A program that writes tiko source for another uses it, so that errors point at the file a person edits. Compiler diagnostics follow#lineas well.
__FILE__ is the path exactly as it was passed to the compiler: built as
tiko src/sourceinfo.tiko, the program prints src/sourceinfo.tiko. Print
the date or time of a build only where a changing value is wanted; a program's
output that changes every day is hard to test.
Activities
- Add
CHECK( total >= 0 )toAverageand call it with a negative total. - Move the
#linedirective to the top of the file and predict every line number the program prints.