From d94342ad44545768d49b2455e0c867f293690429 Mon Sep 17 00:00:00 2001 From: Patrick Leiser Date: Tue, 23 Feb 2021 22:03:15 -0800 Subject: [PATCH 1/7] initial proof of concept online compiler hook-in --- haxepad.html | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 haxepad.html diff --git a/haxepad.html b/haxepad.html new file mode 100644 index 0000000..c1d3266 --- /dev/null +++ b/haxepad.html @@ -0,0 +1,74 @@ + + + + +

hAXEPad

+ +
+
+

+ +

+ + +
+

Click on the submit button, and the input will compiled.

+

+raw output:
+
+
+ + + + + + From cc4c37bbe4f1ee4bf3883dacf119db262d4ca07a Mon Sep 17 00:00:00 2001 From: Patrick Leiser Date: Thu, 30 Mar 2023 03:31:57 -0700 Subject: [PATCH 2/7] start integrating online compiler to the preprocessor --- picaxepreprocess.py | 81 ++++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/picaxepreprocess.py b/picaxepreprocess.py index f86382b..a18178b 100755 --- a/picaxepreprocess.py +++ b/picaxepreprocess.py @@ -11,6 +11,8 @@ # TODO: Remove extra comment ; added for ifs import sys, getopt, os, datetime, re, os.path, subprocess +has_requests = False + inputfilename = 'main.bas' outputfilename = 'compiled.bas' outputpath = "" @@ -47,36 +49,38 @@ def print_help(): picaxepreprocess.py [OPTIONS] [INPUTFILE] Optional switches - -i, --ifile= Input file (default main.bas). Flag not required if it is - the last argument given. - -o, --ofile= Output file (default compiled.bas) - -u, --upload Send the file to the compiler if this option is included. - -s, --syntax Send the file to the compiler for a syntax check only (no download) - --nocolor Disable terminal colour for systems that do not support it (Windows). - --noifs Disable evaluation of #if and #ifdef - this will be left to the compiler if present. - --verbose Print preproccessor debugging info - -h, --help Display this help + -i, --ifile= Input file (default main.bas). Flag not required if it is + the last argument given. + -o, --ofile= Output file (default compiled.bas) + -u, --upload Send the file to the compiler if this option is included. + --online-compile Use the online compiler and output a compiled .axe file + -s, --syntax Send the file to the compiler for a syntax check only (no download) + --online-syntax Use the online compiler for a syntax check only (no download) + --nocolor Disable terminal colour for systems that do not support it (Windows). + --noifs Disable evaluation of #if and #ifdef - this will be left to the compiler if present. + --verbose Print preproccessor debugging info + -h, --help Display this help Optional switches only used if sending to the compiler - -v, --variant= Variant (default 08m2) - (alternatively use #PICAXE directive within the program. - This option will be ignored if #PICAXE is used) - -s, --syntax Syntax check only (no download) - -f, --firmware Firmware check only (no download) - -c, --comport= Assign COM/USB port device (default /dev/ttyUSB0) - (alternately use #COM directive within program. This option - will be ignored if #COM is used). There should be a space - between the -c and the port, unlike the compilers. - -d, --debug Leave port open for debug display (b0-13) - --debughex Leave port open for debug display (hex mode) - -e --edebug Leave port open for debug display (b14-b27) - --edebughex Leave port open for debug display (hex mode) - -t, --term Leave port open for sertxd display - --termhex Leave port open for sertxd display (hex mode) - --termint Leave port open for sertxd display (int mode) - -p, --pass Add pass message to error report file - --tidy Remove the output file on completion if in upload mode. - -P --compilepath= specify the path to the compilers directory (defaults to /usr/local/lib/picaxe/) + -v, --variant= Variant (default 08m2) + (alternatively use #PICAXE directive within the program. + This option will be ignored if #PICAXE is used) + -s, --syntax Syntax check only (no download) + -f, --firmware Firmware check only (no download) + -c, --comport= Assign COM/USB port device (default /dev/ttyUSB0) + (alternately use #COM directive within program. This option + will be ignored if #COM is used). There should be a space + between the -c and the port, unlike the compilers. + -d, --debug Leave port open for debug display (b0-13) + --debughex Leave port open for debug display (hex mode) + -e --edebug Leave port open for debug display (b14-b27) + --edebughex Leave port open for debug display (hex mode) + -t, --term Leave port open for sertxd display + --termhex Leave port open for sertxd display (hex mode) + --termint Leave port open for sertxd display (int mode) + -p, --pass Add pass message to error report file + --tidy Remove the output file on completion if in upload mode. + -P --compilepath= specify the path to the compilers directory (defaults to /usr/local/lib/picaxe/) Preprocessor for PICAXE microcontrollers. See https://github.com/Patronics/PicaxePreprocess for more info. @@ -88,6 +92,7 @@ def main(argv): global outputfilename global outputpath global send_to_compiler + global online_compiler global port global command global tidy @@ -105,7 +110,7 @@ def main(argv): argv.pop() # Remove the -i option as it has been parsed here. try: - opts, _ = getopt.getopt(argv,"hi:o:uv:sfc:detpP:",["help", "ifile=","ofile=","upload","variant=","syntax","firmware","comport=","debug","debughex","edebug","edebughex","term","termhex","termint", "pass", "tidy", "compilepath=", "nocolor", "noifs", "verbose"]) + opts, _ = getopt.getopt(argv,"hi:o:uv:sfc:detpP:",["help", "ifile=","ofile=","upload","variant=","syntax","firmware","comport=","debug","debughex","edebug","edebughex","term","termhex","termint", "pass", "tidy", "compilepath=", "nocolor", "noifs", "verbose", "online-syntax", "online-compile"]) except getopt.GetoptError: print_help() sys.exit(2) @@ -123,11 +128,17 @@ def main(argv): outputfilename = arg elif opt in ("-u", "--upload"): send_to_compiler = True + elif opt in ("--online-compile"): + online_compiler = True + compiler_path = "https://picaxecloud.com/compiler/compile.json" elif opt in ("-v", "--variant"): # Picaxe variant set_chip(arg) elif opt in ("-s", "--syntax"): # Syntax only send_to_compiler = True command.append("-s") + elif opt in ("--online-syntax"): + online_compiler = True + compiler_path = "https://picaxecloud.com/compiler/check.json" elif opt in ("-f", "--firmware"): # Firmware check command.append("-f") elif opt in ("-c", "--comport"): # Serial port given @@ -203,8 +214,16 @@ def main(argv): os.remove(outputfilename) err_file = outputfilename.replace("."+outputfilename.split(".")[-1],"") + ".err" # Calculate the name of the error file os.remove(err_file) - - + if online_compiler: + try: + import requests + except ImportError: + preprocessor_error("""Using the online compiler requires the python 'requests' module +Install this module with the command +python3 -m pip install requests +and try again, or use the offline compiler""") + preprocessor_warning("online compiler not fully implemented yet, please check back later") + #TODO FINISH ONLINE COMPILER IMPLEMENTATION BASED ON haxepad.html print() print("Done.") From bee1e1df33ec0a84d3a8301c55b499953b0da3b6 Mon Sep 17 00:00:00 2001 From: Patrick Leiser Date: Fri, 31 Mar 2023 21:10:50 -0700 Subject: [PATCH 3/7] sucessfully use online compilers to create .axe file and syntax check --- picaxepreprocess.py | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/picaxepreprocess.py b/picaxepreprocess.py index a18178b..5c3271a 100755 --- a/picaxepreprocess.py +++ b/picaxepreprocess.py @@ -10,7 +10,7 @@ # TODO: Debug levels # TODO: Remove extra comment ; added for ifs -import sys, getopt, os, datetime, re, os.path, subprocess +import sys, getopt, os, datetime, re, os.path, subprocess, json has_requests = False inputfilename = 'main.bas' @@ -40,6 +40,8 @@ compiler_extension = "" # File extension (.exe...). For linux anyway, there is none, but including # just in case it is different for other platforms. send_to_compiler = False +online_compiler = False +syntax_check_only = False command = [""] # Empty string at the first position will be replaced by the compiler name and path. tidy = False @@ -58,7 +60,7 @@ def print_help(): --online-syntax Use the online compiler for a syntax check only (no download) --nocolor Disable terminal colour for systems that do not support it (Windows). --noifs Disable evaluation of #if and #ifdef - this will be left to the compiler if present. - --verbose Print preproccessor debugging info + --verbose Print preprocessor debugging info -h, --help Display this help Optional switches only used if sending to the compiler @@ -93,6 +95,7 @@ def main(argv): global outputpath global send_to_compiler global online_compiler + global syntax_check_only global port global command global tidy @@ -135,9 +138,11 @@ def main(argv): set_chip(arg) elif opt in ("-s", "--syntax"): # Syntax only send_to_compiler = True + syntax_check_only = True #currently unused in this path command.append("-s") elif opt in ("--online-syntax"): online_compiler = True + syntax_check_only = True compiler_path = "https://picaxecloud.com/compiler/check.json" elif opt in ("-f", "--firmware"): # Firmware check command.append("-f") @@ -222,8 +227,22 @@ def main(argv): Install this module with the command python3 -m pip install requests and try again, or use the offline compiler""") - preprocessor_warning("online compiler not fully implemented yet, please check back later") - #TODO FINISH ONLINE COMPILER IMPLEMENTATION BASED ON haxepad.html + with open (outputfilename, 'r') as processed_file: + #preprocessor_warning("online compiler not fully implemented yet, please check back later") + # TODO: FINISH ONLINE COMPILER IMPLEMENTATION BASED ON haxepad.html + compileFormData = {'platform':chip, 'code':processed_file.read()} + compile_request = requests.post(compiler_path, json=compileFormData) + compile_result = json.loads(compile_request.text) + if "status" in compile_result.keys(): + preprocessor_success("syntax check result: " + compile_result['status']) + elif "errors" in compile_result.keys(): + # TODO: show caret positioned properly. + preprocessor_error(f"\nsyntax check failed on line: \n{compile_result['errors'][0]}\n{compile_result['errors'][2]}") + elif "axe" in compile_result: + print(compile_result) + with open (f'{outputfilename}.axe', 'w') as compiled_file: + compiled_file.write(compile_result['axe']) + preprocessor_success(f"online compile sucessful, saved to {outputfilename}.axe") print() print("Done.") @@ -556,5 +575,16 @@ def preprocessor_info(*values, **kwargs): if verbose: print(*values, **kwargs) +def preprocessor_success(msg): + """ Prints a success status message. Similar to warning. """ + if use_colour: + print("\u001b[1m\u001b[32m", end="") # Bold Green + print("Successs") + if use_colour: + print("\u001b[0m", end="") # Reset + print(msg) + + if __name__ == "__main__": main(sys.argv[1:]) + \ No newline at end of file From bff66032abc6bab0778b2296ba110d2da9e7c02a Mon Sep 17 00:00:00 2001 From: Patrick Leiser Date: Fri, 31 Mar 2023 21:26:18 -0700 Subject: [PATCH 4/7] show location of syntax errors with caret --- Examples/syntaxError.bas | 1 + picaxepreprocess.py | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 Examples/syntaxError.bas diff --git a/Examples/syntaxError.bas b/Examples/syntaxError.bas new file mode 100644 index 0000000..e2b50f9 --- /dev/null +++ b/Examples/syntaxError.bas @@ -0,0 +1 @@ +pause 50000000000 \ No newline at end of file diff --git a/picaxepreprocess.py b/picaxepreprocess.py index 5c3271a..05aa7e4 100755 --- a/picaxepreprocess.py +++ b/picaxepreprocess.py @@ -236,8 +236,7 @@ def main(argv): if "status" in compile_result.keys(): preprocessor_success("syntax check result: " + compile_result['status']) elif "errors" in compile_result.keys(): - # TODO: show caret positioned properly. - preprocessor_error(f"\nsyntax check failed on line: \n{compile_result['errors'][0]}\n{compile_result['errors'][2]}") + preprocessor_error(f"\nsyntax check failed on line: \n{compile_result['errors'][0]}\n{compile_result['errors'][1]}\n{compile_result['errors'][2]}") elif "axe" in compile_result: print(compile_result) with open (f'{outputfilename}.axe', 'w') as compiled_file: From 4af7146c9a88d7ab962387021b4a1e03e1ccf07a Mon Sep 17 00:00:00 2001 From: Patrick Leiser Date: Fri, 31 Mar 2023 22:08:41 -0700 Subject: [PATCH 5/7] properly base64 decode files, now verified identical to online export version --- picaxepreprocess.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/picaxepreprocess.py b/picaxepreprocess.py index 05aa7e4..9e7cd04 100755 --- a/picaxepreprocess.py +++ b/picaxepreprocess.py @@ -10,7 +10,7 @@ # TODO: Debug levels # TODO: Remove extra comment ; added for ifs -import sys, getopt, os, datetime, re, os.path, subprocess, json +import sys, getopt, os, datetime, re, os.path, subprocess, json, base64 has_requests = False inputfilename = 'main.bas' @@ -228,8 +228,7 @@ def main(argv): python3 -m pip install requests and try again, or use the offline compiler""") with open (outputfilename, 'r') as processed_file: - #preprocessor_warning("online compiler not fully implemented yet, please check back later") - # TODO: FINISH ONLINE COMPILER IMPLEMENTATION BASED ON haxepad.html + #produce 'form' layout online compiler expects compileFormData = {'platform':chip, 'code':processed_file.read()} compile_request = requests.post(compiler_path, json=compileFormData) compile_result = json.loads(compile_request.text) @@ -238,9 +237,9 @@ def main(argv): elif "errors" in compile_result.keys(): preprocessor_error(f"\nsyntax check failed on line: \n{compile_result['errors'][0]}\n{compile_result['errors'][1]}\n{compile_result['errors'][2]}") elif "axe" in compile_result: - print(compile_result) - with open (f'{outputfilename}.axe', 'w') as compiled_file: - compiled_file.write(compile_result['axe']) + #print(compile_result) + with open (f'{outputfilename}.axe', 'wb') as compiled_file: + compiled_file.write(base64.b64decode(compile_result['axe'])) preprocessor_success(f"online compile sucessful, saved to {outputfilename}.axe") print() print("Done.") From 1a99ff63b1a9ad757adf119a87a04032e68e74f1 Mon Sep 17 00:00:00 2001 From: Patrick Leiser Date: Fri, 31 Mar 2023 22:20:26 -0700 Subject: [PATCH 6/7] improve error highlighting --- picaxepreprocess.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/picaxepreprocess.py b/picaxepreprocess.py index 9e7cd04..b202a5d 100755 --- a/picaxepreprocess.py +++ b/picaxepreprocess.py @@ -233,14 +233,18 @@ def main(argv): compile_request = requests.post(compiler_path, json=compileFormData) compile_result = json.loads(compile_request.text) if "status" in compile_result.keys(): - preprocessor_success("syntax check result: " + compile_result['status']) + print(f"\u001b[1m\u001b[32mSYNTAX CHECK SUCCESS: {compile_result['status']}\u001b[0m") elif "errors" in compile_result.keys(): - preprocessor_error(f"\nsyntax check failed on line: \n{compile_result['errors'][0]}\n{compile_result['errors'][1]}\n{compile_result['errors'][2]}") + print(f""" +\u001b[1m\u001b[31mSYNTAX CHECK FAILED\u001b[0m\u001b[1m on line: +{compile_result['errors'][0]} +{compile_result['errors'][1]} +{compile_result['errors'][2]}\u001b[0m""") elif "axe" in compile_result: - #print(compile_result) + #base64 decode the result into the .axe file expected with open (f'{outputfilename}.axe', 'wb') as compiled_file: compiled_file.write(base64.b64decode(compile_result['axe'])) - preprocessor_success(f"online compile sucessful, saved to {outputfilename}.axe") + preprocessor_success(f"\u001b[1monline compile sucessful, saved to {outputfilename}.axe\u001b[0m") print() print("Done.") From eb167822bcf1e51b7d05b183bc50e01731bc5e45 Mon Sep 17 00:00:00 2001 From: Patrick Leiser Date: Thu, 30 Nov 2023 02:00:07 -0800 Subject: [PATCH 7/7] add online compiler flags to readme --- readme.md | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/readme.md b/readme.md index ba0a6bf..d4ba3b0 100644 --- a/readme.md +++ b/readme.md @@ -38,25 +38,28 @@ Optional switches -h, --help Display this help Optional switches only used if sending to the compiler - -v, --variant= Variant (default 08m2) - (alternatively use #PICAXE directive within the program. - This option will be ignored if #PICAXE is used) - -s, --syntax Syntax check only (no download) - -f, --firmware Firmware check only (no download) - -c, --comport= Assign COM/USB port device (default /dev/ttyUSB0) - (alternately use #COM directive within program. This option - will be ignored if #COM is used). There should be a space - between the -c and the port, unlike the compilers. - -d, --debug Leave port open for debug display (b0-13) - --debughex Leave port open for debug display (hex mode) - -e --edebug Leave port open for debug display (b14-b27) - --edebughex Leave port open for debug display (hex mode) - -t, --term Leave port open for sertxd display - --termhex Leave port open for sertxd display (hex mode) - --termint Leave port open for sertxd display (int mode) - -p, --pass Add pass message to error report file - --tidy Remove the output file on completion if in upload mode. - -P --compilepath= specify the path to the compilers directory (defaults to /usr/local/lib/picaxe/) + -v, --variant= Variant (default 08m2) + (alternatively use #PICAXE directive within the program. + This option will be ignored if #PICAXE is used) + -s, --syntax Syntax check only (no download) + -f, --firmware Firmware check only (no download) + -c, --comport= Assign COM/USB port device (default /dev/ttyUSB0) + (alternately use #COM directive within program. This option + will be ignored if #COM is used). There should be a space + between the -c and the port, unlike the compilers. + -d, --debug Leave port open for debug display (b0-13) + --debughex Leave port open for debug display (hex mode) + -e --edebug Leave port open for debug display (b14-b27) + --edebughex Leave port open for debug display (hex mode) + -t, --term Leave port open for sertxd display + --termhex Leave port open for sertxd display (hex mode) + --termint Leave port open for sertxd display (int mode) + -p, --pass Add pass message to error report file + --tidy Remove the output file on completion if in upload mode. + -P --compilepath= specify the path to the compilers directory (defaults to /usr/local/lib/picaxe/) + --online-compile Use the online compiler and output a compiled .axe file (instead of the local compiler) + --online-syntax Use the online compiler for a syntax check only (no download) + ``` See the Makefile for an example of advanced usage. When properly configured, the makefile can automatically handle preprocessing the code, compiling it, and uploading to a picaxe chip by simply invoking `make compile` and run a syntax check with `make syntax`. The makefile also demonstrates usage with multiple picaxe chips with separate programs in the same project directory.