Skip to content

Commit

Permalink
Add functions to conditionally include forth files.
Browse files Browse the repository at this point in the history
  • Loading branch information
splbio committed Nov 12, 2013
1 parent 384d86e commit 0ca72dc
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions sys/boot/forth/support.4th
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,40 @@ only forth also support-functions definitions
throw
;

\ Does the file exist?
: file-exists? ( c-addr/u -- bool )
O_RDONLY fopen \ open file
dup \ save a copy to not leak

This comment has been minimized.

Copy link
@devinteske

devinteske Nov 13, 2013

the dup is not to prevent a leak, but to save a copy of the stack item for later fclose. "to not leak" is incorrect.

This comment has been minimized.

Copy link
@splbio

splbio Nov 13, 2013

Author Owner

Comment updated.

-1 <> if
fclose true
else
drop false
then
;

\ Source file as code if it exists.
: source-if-exists ( c-addr/u -- )

This comment has been minimized.

Copy link
@devinteske

devinteske Nov 13, 2013

include-if-exists would be a better name (since ultimately we'll use "include").

This comment has been minimized.

Copy link
@splbio

splbio Nov 13, 2013

Author Owner

agreed. changed to include-if-exists.


2dup file-exists? if

This comment has been minimized.

Copy link
@devinteske

devinteske Nov 13, 2013

stack leak? Before the 2dup, we have ( c-addr/u ). After 2dup ( c-addr/u c-addr/u ). Then we call file-exists? ( [before] c-addr/u c-addr/u -- [after] c-addr/u bool ). Then we come to the "if" ( c-addr/u bool -- c-addr/u ). Jump ahead to the end, "then" and we still have a ( c-addr/u ) on the stack. The simple solution is to add "else 2drop" before "then". The elegant solution is to instead change the conditional to "2dup file-exists? false = if 2drop exit then" ... this acting as a short-circuit and then you can break all that stuff that used to be in the larger if-then block out of said block and reduce the indentation level by-one.

This comment has been minimized.

Copy link
@splbio

splbio Nov 13, 2013

Author Owner

nice catch, I've reworked the logic as you suggested to have early exit.

\ If file exists then prepend "include" to it

\ first allocate a string, top of stack is strlen of
\ the filename, so just add a comfortable 15 bytes to it.
dup 15 +

This comment has been minimized.

Copy link
@devinteske

devinteske Nov 13, 2013

Why 15 + ? strlen("include ") == 8; so shouldn't it be 8 + ?

This comment has been minimized.

Copy link
@splbio

splbio Nov 13, 2013

Author Owner

I'm paranoid about 4th strings because I wasn't sure, I trimmed it down to 9, but would be more comfortable with 10, however if you're certain about 8 I have no objections. :)

This comment has been minimized.

Copy link
@devinteske

devinteske via email Nov 15, 2013

allocate if ENOMEM throw then
0

s" include " strcat

This comment has been minimized.

Copy link
@devinteske

devinteske Nov 13, 2013

I think a more efficient method (replacing lines 883 through 891) would be:

883-884: dup 8 + allocate if ENOMEM throw then ( c-addr/u -- c-addr/u addr )
885-885: 8 + 0 ( c-addr/u addr -- c-addr/u addr' 0 )
889-890: 2swap strcat ( c-addr/u addr' 0 -- c-addr'/u )
885-885: swap 8 - 0 ( c-addr'/u -- u addr 0 )
887-887: s" include " strcat ( u addr 0 -- u addr 8 )
891-891: rot + ( u addr 8 -- addr u+8 )

NOTE: I know line 891 is blank ;D

This comment has been minimized.

Copy link
@splbio

splbio Nov 13, 2013

Author Owner

This change I have not incorporated yet in my branch as it uses some verbs I'm not familiar with so I will have to research it later.

\ grab the original string up so we can strcat
3 roll 3 roll

This comment has been minimized.

Copy link
@devinteske

devinteske Nov 13, 2013

This doesn't seem efficient. Couldn't you use 2swap?

This comment has been minimized.

Copy link
@splbio

splbio Nov 13, 2013

Author Owner

done!

strcat

\ evaluate " include file"
2dup evaluate
drop free
then
;

: print_line line_buffer strtype cr ;

: print_syntax_error
Expand Down

0 comments on commit 0ca72dc

Please sign in to comment.