source.fs 2.55 KB
Newer Older
1 2
\ source location handling

3
\ Authors: Anton Ertl, Bernd Paysan, Gerald Wodni
Anton Ertl's avatar
Anton Ertl committed
4
\ Copyright (C) 1995,1997,2003,2004,2007,2009,2011,2014,2016,2017,2018 Free Software Foundation, Inc.
5 6 7 8 9

\ This file is part of Gforth.

\ Gforth is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public License
10
\ as published by the Free Software Foundation, either version 3
11 12 13 14 15 16 17 18
\ of the License, or (at your option) any later version.

\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
\ GNU General Public License for more details.

\ You should have received a copy of the GNU General Public License
19
\ along with this program. If not, see http://www.gnu.org/licenses/.
20

pazsan's avatar
pazsan committed
21
\ related stuff can be found in kernel.fs
22

23
\ this stuff is used by (at least) assert.fs and debugs.fs
24

25 26
\ 1-cell encoded position: filenameno9b:lineno15b:charno8b

27 28
require string.fs

29 30
-1 #23 rshift Constant *terminal*#

31
: loadfilename#>str ( n -- addr u )
32
    dup *terminal*# and *terminal*# = IF  drop s" *terminal*"  EXIT  THEN
33
    included-files $[]@ ;
34

35
\ we encode line and character in one cell to keep the interface the same
anton's avatar
anton committed
36

37 38 39
: decode-pos ( npos -- nline nchar )
    dup 8 rshift swap $ff and ;

40
: decode-view ( view -- nfile nline nchar )
41
    dup 23 rshift swap $7fffff and decode-pos ;
42

43
: view>char ( view -- u )
Anton Ertl's avatar
Anton Ertl committed
44 45
    $ff and ;

46 47 48
: .sourcepos3 (  nfile nline nchar -- )
    rot loadfilename#>str type ': emit
    base @ decimal
Anton Ertl's avatar
Anton Ertl committed
49
    rot 0 .r ': emit swap 1+ 0 .r
50 51
    base ! ;

52 53
: .sourceview ( view -- )
    decode-view .sourcepos3 ;
54
    
55
: compile-sourcepos ( compile-time: -- ; run-time: -- view )
56 57
    \ compile the current source position as literals: nfile is the
    \ source file index, nline the line number within the file.
58
    current-sourceview
59
    postpone literal ;
60

61
: .sourcepos ( nfile npos -- )
62
    \ print source position
63
    decode-pos .sourcepos3 ;
64

65
: save-source-filename# ( c-addr1 u1 -- index )
66 67
    \ adds a permanent copy of c-addr1 u1 to the included file names,
    \ returning the index into the included-files
anton's avatar
anton committed
68
    2dup str>loadfilename# dup 0< if
69
	drop add-included-file included-files $[]# 1-
anton's avatar
anton committed
70
    else
71
	nip nip
anton's avatar
anton committed
72 73 74 75 76 77 78
    then ;

: #line ( "u" "["file"]" -- )
    \g Set the line number to @i{u} and (if present) the file name to @i{file}.  Consumes the rest of the line.
    \g 
    parse-name ['] evaluate 10 base-execute 1- loadline !
    '"' parse 2drop '"' parse dup if
79
	save-source-filename# loadfilename# !
anton's avatar
anton committed
80 81 82 83
    else
	2drop
    then
    postpone \ ;