diff --git a/README.md b/README.md index 1f7676d..2b8362a 100644 --- a/README.md +++ b/README.md @@ -49,9 +49,9 @@ programs/pow.exe // computes and prints 3 ^ (2 + 2) = 81 ./run.sh programs/pow.wb // combines both of the above steps ``` -And a work-in-progress prototype that partially compiles a program, then transpiles it to Python, as a quick way of sanity-checking program logic for more complex programs. +And a work-in-progress prototype that partially compiles a program, then transpiles it to a minimal subset of Python, as a quick way of sanity-checking program logic for more complex programs. ``` -gen_python.sh programs/pow.wb +./gen_python.sh programs/pow.wb ``` diff --git a/compile.sh b/compile.sh index dd13424..674b56d 100755 --- a/compile.sh +++ b/compile.sh @@ -6,7 +6,7 @@ if [ $# -eq 0 ]; then echo "" echo "Usage: $0 (you may also omit the .wb extension)" echo "For example:" - echo " ./compile.sh programs/test_programs/program1.wb" + echo " $0 programs/test_programs/program1.wb" echo " programs/test_programs/program1.exe" echo "" exit 1 diff --git a/gen_python.sh b/gen_python.sh new file mode 100755 index 0000000..07ae4d4 --- /dev/null +++ b/gen_python.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +# compile .wb code -> intermediate representation -> transpile to simple Python +# outputs to the terminal not a file + +if [ $# -eq 0 ]; then + echo "" + echo "Usage: $0 (you may also omit the .wb extension)" + echo "For example:" + echo " $0 programs/pow.wb > programs/pow.py" + echo " python3 programs/pow.py" + echo "" + exit 1 +fi + +FILEPATH="$1" +# remove extension if present (will assume .wb) +FILEPATH=${FILEPATH%.*} +FILE=$(BASENAME $FILEPATH) + +BLUE="\033[1;34m" +RESETCOLOR="\033[0m" + +# echo simplified version of commands run, without all the relative path prefixes +echo "${BLUE}python3 compile_to_python.py $FILE.wb$RESETCOLOR" +python3 compiler/compile_to_python.py $FILEPATH.wb + +# Versions that write to file directly (not used) +#echo "${BLUE}python3 compile_to_python.py $FILE.wb > ${FILE}_gen.py$RESETCOLOR" +#python3 compiler/compile_to_python.py $FILEPATH.wb > ${FILEPATH}_gen.py + diff --git a/programs/pow_gen.py b/programs/pow_gen.py new file mode 100644 index 0000000..321fcd4 --- /dev/null +++ b/programs/pow_gen.py @@ -0,0 +1,17 @@ +# original source: programs/pow.wb +# created by gen_python.py (work in progress) +def pow(x, n): + out = 1 + i = 0 + while (i < n): + out = (out * x) + i = (i + 1) + return out + +def main(): + x = pow(3, 4) + print(x) + return 0 + +if __name__ == "__main__": + main() diff --git a/run.sh b/run.sh index e2a2310..95bcc7c 100755 --- a/run.sh +++ b/run.sh @@ -7,7 +7,7 @@ if [ $# -eq 0 ]; then echo "" echo "Usage: $0 (you may also omit the .wb extension)" echo "For example:" - echo " ./run.sh programs/test_programs/program1.wb" + echo " $0 programs/test_programs/program1.wb" echo "" exit 1 fi