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

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

\ 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
9
\ as published by the Free Software Foundation, either version 3
10 11 12 13 14 15 16 17
\ 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
18
\ along with this program. If not, see http://www.gnu.org/licenses/.
19

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

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

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

26 27
require string.fs

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

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

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

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

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

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

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

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

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

64
: save-source-filename# ( c-addr1 u1 -- index )
65 66
    \ 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
67
    2dup str>loadfilename# dup 0< if
68
	drop add-included-file included-files $[]# 1-
anton's avatar
anton committed
69
    else
70
	nip nip
anton's avatar
anton committed
71 72 73 74 75 76 77
    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
78
	save-source-filename# loadfilename# !
anton's avatar
anton committed
79 80 81 82
    else
	2drop
    then
    postpone \ ;