Bashforth icon indicating copy to clipboard operation
Bashforth copied to clipboard

save-system / load-system

Open quaraman-me opened this issue 6 years ago • 3 comments

I am new to github so I do not know another way to contact you.

I like this shell script for test driving forth, without installing an compiled program.

Thanks for this software.

Here is a code that in my copy of the 0.59a bashforth version function well for save-system and load-system:

save_array_print() {
local i
local inext
local -n a=$1
   inext=0
   printf "a0\n"
   for i in "${!a[@]}"; do
   if [ "$inext" != "$i" ]; then
   printf 'ao %s\n' "$i"
   fi
   printf 'a %s\n' "${a[i]}"
   let "inext=++i"
   done
}

# -----------------------------------------------------------------------------
# ------------------------------- save-system ---------------------------------
# -----------------------------------------------------------------------------


# ( a c -- )
code saveas saveas
saveas()  {
local i
local inext
local len
   pack

   (
   printf "version %s\n" "$version"
   printf "wc %s\n" "$wc"
   printf "dp %s\n" "$dp"
   
save_array_print m
save_array_print h
save_array_print hf 
save_array_print x 
  
   ) > $tos
   tos=${s[sp--]}
}


# (  -- ) write image of system to file, file name taken from input stream
revealheader "save-system"
colon savesystem    $bl $stream $saveas


# -----------------------------------------------------------------------------
# ------------------------------- load-system ---------------------------------
# -----------------------------------------------------------------------------


# ( a c -- )
code loadfrom loadfrom
function loadfrom  {
   local load_version
   local cmd
   local prg
   local a
   local ai
   local linenr
   pack
   m=()
   h=()
   hf=()
   x=()   
   ai=0
   a=0
   fname=$tos
   tos=${s[sp--]}
   linenr=0
   while read -r line
   do
     prg=($line)
	 cmd=${prg[0]}
	 p1=${prg[1]}
	 p2=${prg[2]}
	 
   case $cmd in
   version)
		load_version=$p1
		if [ "$load_version" != "$version" ]; then
			echo "Not same Version : $load_version"
		fi
		;;
   wc) 
		wc=$p1
		;;
   dp)
		dp=$p1
		;;
   a0)
		ai=0
		let "a=++a"
		;;		
   ao)
		ai=$p1
		;;
   a) 
   case $a in
   1)
		m[ai++]=$p1
		;;
   2)
		h[ai++]=$p1
		;;
   3)
		hf[ai++]=$p1
		;;
	4)
		x[ai++]=$p1
		;;
	esac
		;;
	hlt) 
	ai=0
	break;
			;;
   esac
   let "linenr=++linenr"
   done 

quaraman-me avatar Aug 29 '19 20:08 quaraman-me

This looks useful and functional. I have added your version to bashforth. Thank you very much for your contribution. One of the slight modifications I did to your code was to allow execution of save-system and restore without giving a file name. In this case will saving/restoring happen with a preset file name. This allows easy restarting and restoring bashforth with a command like "bashforth restore" to the version previously save with "save-system". Currently, this file doesn't default to another location than current directory - it may be practical to let it default to, for example, sourcepath directory. I'll leave this issue open until you reported the addition functional in spite of the modifications which I've applied.

Bushmills avatar Aug 30 '19 08:08 Bushmills

I just found a small problem with restore: * gets expanded - probably merely a matter of a missed double quote somewhere.

Bushmills avatar Aug 30 '19 13:08 Bushmills

found and fixed

Bushmills avatar Aug 30 '19 13:08 Bushmills