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/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:
+
+
+ + + + + + diff --git a/picaxepreprocess.py b/picaxepreprocess.py index f86382b..b202a5d 100755 --- a/picaxepreprocess.py +++ b/picaxepreprocess.py @@ -10,7 +10,9 @@ # 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, base64 +has_requests = False + inputfilename = 'main.bas' outputfilename = 'compiled.bas' outputpath = "" @@ -38,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 @@ -47,36 +51,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 preprocessor 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 +94,8 @@ def main(argv): global outputfilename global outputpath global send_to_compiler + global online_compiler + global syntax_check_only global port global command global tidy @@ -105,7 +113,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 +131,19 @@ 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 + 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") elif opt in ("-c", "--comport"): # Serial port given @@ -203,8 +219,32 @@ 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""") + with open (outputfilename, 'r') as processed_file: + #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) + if "status" in compile_result.keys(): + print(f"\u001b[1m\u001b[32mSYNTAX CHECK SUCCESS: {compile_result['status']}\u001b[0m") + elif "errors" in compile_result.keys(): + 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: + #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"\u001b[1monline compile sucessful, saved to {outputfilename}.axe\u001b[0m") print() print("Done.") @@ -537,5 +577,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 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.