Skip to content

Instantly share code, notes, and snippets.

@KrystianGraba
Created January 28, 2021 22:42
Show Gist options
  • Select an option

  • Save KrystianGraba/69a0453b89120152e6be1839b86522bd to your computer and use it in GitHub Desktop.

Select an option

Save KrystianGraba/69a0453b89120152e6be1839b86522bd to your computer and use it in GitHub Desktop.
#!/bin/bash
echo "program Projekt;" >> b.txt >> c.txt
echo "begin" >> b.txt >> c.txt
wyr1="^echo *" #echo
wyr2="read *" #read
#deklaracje_zmiennych
wyr3="^[a-zA-Z0-9]*\=[0-9]" #deklaracja_integer
wyr4="^[a-zA-Z0-9]*\=\"" #deklaracja_string
#if
wyr5="^if \[" #if_1_start
wyr6="^fi*" #fi_2_koniec
wyr8="^elif*";#if_3_else
wyr10="^then$" #then
wyr7="^$" #nowalinia
wyr9="^exit*" #koniec_programu(pomin_nowa linia)
wyr11="^\#.*$" #komentarz
#case
wyr12="^case *" #start_case
wyr13="^\"[0-9]|[0-9][0-9]\"\)*" #case
wyr14="^esac$" #koniec
wyr15="^\*\).*$" #else
wyr16="^\"[0-9]|[0-9][0-9]\:\).*" #writeln
str="str"
str_scnd="str_scnd"
cas_number="cas_number"
cat a.txt | while read line || [[ -n $line ]];
do
if [[ "$line" =~ $wyr1 ]] #echo
then
str=`echo $line |cut -d \" -f2`
echo "KRIS: Test(echo): $line writeLn '$str'" >> b.txt
echo "writeLn '$str'" >> c.txt
elif [[ "$line" =~ $wyr2 ]] #read
then
str=`echo $line |rev |cut -d" " -f1 |rev`
echo "KRIS: Test(read): $line readLn ($str)" >> b.txt
echo "readLn ($str)" >> c.txt
elif [[ "$line" =~ $wyr3 ]] #deklaracja_integer
then
echo "int"
str=`echo $line |cut -d "=" -f2`
str_scnd=`echo $line |cut -d "=" -f1`
echo "KRIS: Test(deklaracja_int): $line var $str_scnd: integer = $str" >> b.txt
echo "var $str_scnd : integer = $str" >> c.txt
elif [[ "$line" =~ $wyr4 ]] #deklaracja_string
then
str=`echo $line |cut -d "=" -f2`
str_scnd=`echo $line |cut -d "=" -f1`
echo "KRIS: Test(deklaracja_string): $line var $str_scnd : string = "$str"" >> b.txt
echo "var $str_scnd : string = "$str"" >> c.txt
elif [[ "$line" =~ $wyr5 ]] #IF
then
str=`echo $line |cut -d "[" -f2`
str_scnd=`echo $str |cut -d "]" -f1`
echo "KRIS: Test(if): $line IF ($str_scnd) THEN " >> b.txt
echo -e "IF ($str_scnd) THEN\nBEGIN" >> c.txt
echo "if"
elif [[ "$line" =~ $wyr6 ]] #fi
then
echo "KRIS: Test(fi(koniec if)): $line END;" >> b.txt
echo "END;" >> c.txt
elif [[ "$line" =~ $wyr7 ]] #nowa_linia
then
echo "KRIS: Test(nowa_Linia): $line NOWA LINIA" >> b.txt
echo -e "\n" >> c.txt
elif [[ "$line" =~ $wyr8 ]] #else_if_else
then
str=`echo $line|cut -d "[" -f3`
str_scnd=`echo $str|cut -d "]" -f1`
#echo $str_scnd
echo "KRIS: Test(else): $line ELSE" >> b.txt
echo "ELSE {$str_scnd}" >> c.txt
elif [[ "$line" =~ $wyr9 ]] #exit_x_pomin
then
echo -e"KRIS: Test(exit X): $line \n" >> b.txt
echo -e "\n" >> c.txt
elif [[ "$line" =~ $wyr10 ]] #then_rob_nic
then
echo -e"KRIS: Test(then): $line" >> b.txt
#echo -e "\n" >> c.txt
elif [[ "$line" =~ $wyr11 ]] #komentarz
then
str=`echo $line |cut -d "#" -f2`
echo "KRIS: Test(komentarz): $line" >> b.txt
echo -e '\\\\' "$str" >> c.txt
elif [[ "$line" =~ $wyr12 ]] #case(start)
then
echo -e"KRIS: Test(case:start): $line" >> b.txt
str=`echo $line|cut -d"'" -f2`#bad
str_scnd=`echo $str|cut -d "'" -f1`#bad
cas_number=`echo $line|cut -d" " -f2`
cas_number=`echo $cas_number|cut -d "$" -f2`
cas_number=`echo $cas_number|cut -d "\"" -f1`
echo "kejs"
echo "case $cas_number of" >> c.txt
elif [[ "$line" =~ $wyr13 ]] #case(instrukcja)
then
str=`echo $line|cut -d")" -f2`
str_scnd=`echo $str|cut -d ";" -f1`
cas_number=`echo $line|cut -d"\"" -f2`
echo "KRIS: Test(case:warunek): $line" >> b.txt
echo "$cas_number: $str_scnd" >> c.txt
elif [[ "$line" =~ $wyr14 ]] #case(koniec)
then
echo -e"KRIS: Test(case:end): $line" >> b.txt
echo "end; //case end" >> c.txt
elif [[ "$line" =~ $wyr15 ]] #case(else)
then
str=`echo $line|cut -d")" -f2`
echo "KRIS: Test(case:else): $line" >> b.txt
echo "else $str" >> c.txt
else
echo "KRIS: Test(nieobslugiwana): $line" >> b.txt
echo "Nieobslugiwane: $line" >> c.txt
fi
done
echo "end." >> b.txt >> c.txt
#writeln
filename="c.txt"
search='echo '
replace='writeln ('
if [[ $search != "" && $replace != "" ]]; then
sed -i "s/$search/$replace/" $filename
fi
awk -v mytext=");" '/ *writeln*/ {$0=$0mytext} 1' c.txt #writeln
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment