Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save-system / load-system #2

Open
quaraman-me opened this issue Aug 29, 2019 · 3 comments
Open

save-system / load-system #2

quaraman-me opened this issue Aug 29, 2019 · 3 comments
Assignees

Comments

@quaraman-me
Copy link

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 < $fname
}
@Bushmills
Copy link
Owner

Bushmills commented Aug 30, 2019

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 Bushmills self-assigned this Aug 30, 2019
@Bushmills
Copy link
Owner

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

@Bushmills
Copy link
Owner

found and fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants