Last active
June 24, 2019 17:09
-
-
Save drezap/f773ab96908d68597c86ca7bb277347f to your computer and use it in GitHub Desktop.
This flattens the meta programs of the C++ stan math library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script should do the following: | |
# 1) Move scal to prim/meta | |
# 2) Move mat to the prim/meta | |
# a) if a file exists, just cat it to the end | |
# b) properly handle the #ifndefs, and the brackets and stuff | |
# 3) Move arr | |
# a) same logic as a) above | |
# b) same logic as b) above | |
# we also need to creat the meta.hpp file properly | |
old_dir="/home/andre/cmdstan/stan/lib/stan_math/stan/math/prim" | |
new_dir="./stan/math/prim/" | |
#dims=("scal" "mat" "scal") | |
test="/home/andre/" | |
#ls -ltr "${old_dir}scal/meta/*" | |
#echo "$bew_" | |
#ls "$old_dir/scal/meta/"* | |
cp "$old_dir/scal/meta/"* "$new_dir/meta/" | |
# for file in "$new_dir/meta/"*; | |
# do | |
# sed -i '/#endif/d' $file | |
# head -n --lines=-2 $file > "$file.tmp" | |
# mv "$file.tmp" $file | |
# done | |
for file in "$old_dir/mat/meta/"*; | |
do | |
file_nm=${file##*/} | |
if [ ! -f "$new_dir/meta/$file_nm" ]; ## check to see if that works | |
then | |
echo $file_nm | |
# do we really need the git history for everything? | |
cp "$file" "$new_dir/meta/" | |
else | |
# remove }*namespace and end if | |
sed -i '/#endif/d' "$new_dir/meta/$file_nm" | |
sed -i '/\/\/ namespace/d' "$new_dir/meta/$file_nm" | |
sed '1,2d' $file > "$new_dir/meta/$file_nm.tmp1" | |
cat "$new_dir/meta/$file_nm" "$new_dir/meta/$file_nm.tmp1" > "$new_dir/meta/$file_nm.tmp" | |
mv "$new_dir/meta/$file_nm.tmp" "$new_dir/meta/$file_nm" | |
rm "$new_dir/meta/$file_nm.tmp1" | |
echo "catted" | |
## move includes to the top | |
sed '/#include/!H;//p;$!d;g;s/\n//' "$new_dir/meta/$file_nm" > "$new_dir/meta/$file_nm.tmp2" | |
mv "$new_dir/meta/$file_nm.tmp2" "$new_dir/meta/$file_nm" | |
## move the #ifndef statements back to the top (lazy way to do it) | |
sed '/#define/!H;//p;$!d;g;s/\n//' "$new_dir/meta/$file_nm" > "$new_dir/meta/$file_nm.tmp3" | |
mv "$new_dir/meta/$file_nm.tmp3" "$new_dir/meta/$file_nm" | |
sed '/#ifndef/!H;//p;$!d;g;s/\n//' "$new_dir/meta/$file_nm" > "$new_dir/meta/$file_nm.tmp4" | |
mv "$new_dir/meta/$file_nm.tmp4" "$new_dir/meta/$file_nm" | |
## remove the second occurances of namespace stan and namespace math | |
## if line exists, remove all then re insert insert after includes | |
if grep -q "namespace stan" "$new_dir/meta/$file_nm" ## if the file contains namespace stan | |
then | |
echo "asdfadsfa" | |
sed -i '/namespace stan/d' "$new_dir/meta/$file_nm" ## delete lines containing namespace stan | |
## below: place after includes | |
sed -i '1h;1!H;$!d;x;s/.*#include[^\n]*/&\nnamespace stan {/' "$new_dir/meta/$file_nm" | |
fi | |
## now we do the same thing for math | |
if grep -q "namespace math" "$new_dir/meta/$file_nm" ## if the file contains namespace math | |
then | |
echo "asdfadsfa" | |
sed -i '/namespace math/d' "$new_dir/meta/$file_nm" ## delete lines containing namespace math | |
## below: place after includes | |
sed -i '1h;1!H;$!d;x;s/.*#include[^\n]*/&\nnamespace math {/' "$new_dir/meta/$file_nm" | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of creating temp files, Andre, just use
sed -i