Skip to content

Commit

Permalink
improved example main program for rpn.icn and its example invocation …
Browse files Browse the repository at this point in the history
…via tests/run_rpm
  • Loading branch information
eschen42 committed Sep 21, 2022
1 parent 5dad22b commit d7f09db
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -723,4 +723,9 @@ TSWLatexianTemp*
# standalone packages
*.sta

# other version control
.fslckout
.fossil-settings/*

# convenience executables
runt
67 changes: 63 additions & 4 deletions rpn.icn
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
#
# hints:
# * See "Standard Words" and "Regression-test words" in tests.rpn for examples
# * See Unicon\ipl\incl\invkdefs.icn for operators you can use
# * See $IPL\incl\invkdefs.icn for operators you can use
# * However, you generally must specify the number of arguments
# before the operator or procedure name. For example,
# + Use "1-" to indicate the prefix minus
Expand Down Expand Up @@ -127,9 +127,59 @@ $else
$endif

$ifdef RPN_MAIN
procedure main( )

# From the invoking program that defines RPN_MAIN,
# include fileDirIo.icn for:
# procedure directory_seq(name) : s1, ...
# Produce name(s) that name a directory
# procedure path_separator() : s
# return platform-specific path separator
# See tests/run_rpn for an example.

procedure main(args)
local arg, d, f, home, dot_rpn
init_globals( )
interp( [ create token( hear ) ], [ ], "\nOK " )
$ifdef DEBUG
echo := write
$endif
# import ~/.rpn/*.rpn if present
if home := getenv("HOME") then {
dot_rpn := home || path_separator() || ".rpn"
# open the directory to list the *.rpn files
if dot_rpn == directory_seq(dot_rpn)
then {
d := open(dot_rpn, "r")
every f := !d do {
f ?
if tab(find(".rpn")) & pos(-4)
then {
# import a .rpn file
f := dot_rpn || path_separator() || f
if f := open(f, "r")
then {
interp( [ create token( create ! f ) ], [ ], &null )
close(f)
}
}
}
close(d)
}
}
# If no arguments are present, take input from stdin
if *args = 0
then interp( [ create token( hear ) ], [ ], "\nOK " )
else every arg := !args do {
# Import any files provided as arguments.
# When one is named "-", switch to taking input from stdin.
if arg == '-'
then interp( [ create token( hear ) ], [ ], "\nOK " )
else if f := open(arg, "r")
then {
interp( [ create token( create ! f ) ], [ ], &null )
close(f)
}
else stop(&progname, ": Cannot read file: ", arg)
}
end
$endif

Expand Down Expand Up @@ -444,13 +494,17 @@ procedure interp(
&null # nothing to worry about ... yet
} |
case tok of {
###############################################################
# special-case primitives
###############################################################
":" : # begin word definition
( *stk>0, type(stk[1])=="string"
, def := [ ( words[map(rpop(stk))] ) := [ ] ]
)
# handlers for conditional code

###############################################################
# handlers for conditional code
###############################################################
# regression-test: echo "tests.rpn" import testevery | rpn
"&everyif" : # beginning of an every...do implicit secondary
if *LCtoken < 2
Expand Down Expand Up @@ -587,7 +641,9 @@ procedure interp(
"&endrepeat" : # end of a repeat...endrepeat implicit secondary
LCtoken[1] := ^LCtoken[1] # refresh secondary's co-expression

###############################################################
# ordinary primitives
###############################################################
# TODO Decide out how to expose primitives so they can be
# produced by "words".
"&cr" :
Expand Down Expand Up @@ -836,7 +892,10 @@ procedure interp(
if *stk=0
then "var ( s -- ): empty stack" @ gripe
else var[map(rpop(stk))]:=[ ]

###############################################################
# definition-only words
###############################################################
"if" |
"else" |
"endif" |
Expand Down
6 changes: 6 additions & 0 deletions tests/run_rpn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/env icon
# run rpn.icn
#$define DEBUG 1
$define RPN_MAIN 1
$include "../fileDirIo.icn"
$include "../rpn.icn"

0 comments on commit d7f09db

Please sign in to comment.