diff --git a/README.md b/README.md index b727bfe..bcbbabd 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,3 @@ -

- -

- # Plasma-Compiler ⚡ C compiler written in Python. It does not comply with any standards for the C @@ -10,8 +6,28 @@ assembly, then leveraging third party tools to assemble and link the program. The code is fully custom, everything from the lexer to the code generation (no libraries used). -NOTE: This project is complete and further development has been moved to -https://github.com/BluBloos/Portable-Programming-Language +> NOTE: This project was developed when I was in high school and therefore does not reflect my current expertise. I have made minor updates to the project since then, but the majority of the codebase remains unchanged from the original version. + +

+ +

+ +# Features + +- Single line comments +- Function declaration +- Variable declaration and assignment +- Variable Scoping +- If, else if, and else +- For loops +- While loops +- Break / Continue +- Recursion +- Precedence in expressions +- Ternary operator +- Supported binary operations: + - * / == != > < >= <= || && +- Supported unary operations: - ! + # Steps for Using @@ -47,37 +63,17 @@ shell.bat This will setup the MSVC environment, so that link.exe is accessible. -## Normal Usage - -To run the compiler for a novel program, run the following, - -``` -python compiler.py -``` - -Plasma Compiler will compile the file, run it, and display the result of the -program to the console window. +**The compiler will not log an error if link.exe is unavailable.** -## Running Tests +## Normal Usage -To run the pre-made tests: +usage: Plasma Compiler [-h] [-d] [-r] [filename] -``` -python test.py -``` +positional arguments: + filename The file to process. -# Features +options: + -h, --help show this help message and exit + -d, --debug Enable debug mode. + -r, --run Run code after compile. -- Single line comments -- Function declaration -- Variable declaration and assignment -- Variable Scoping -- If, else if, and else -- For loops -- While loops -- Break / Continue -- Recursion -- Precedence in expressions -- Ternary operator -- Supported binary operations: + - * / == != > < >= <= || && -- Supported unary operations: - ! diff --git a/compiler.py b/compiler.py index 14e139e..41f86a0 100644 --- a/compiler.py +++ b/compiler.py @@ -7,6 +7,9 @@ import semantics import codegen import optimization +import argparse +from colorama import init +from colorama import Fore, Back, Style import sys import os @@ -75,32 +78,62 @@ def Run(fileName, DEBUG, TEST, platform): return return_val - -# usage: python compiler.py ? ? +def colored(string, color): + if color == "green": + return Fore.GREEN + string + elif color == "red": + return Fore.RED + string + +def SingleTest(fileName, desired_result): + global PLATFORM + debug = False + test = True + result = Run(fileName, debug, test, PLATFORM) + if desired_result == result: + print(colored("{} works.".format(fileName), "green")) + print(Style.RESET_ALL) + else: + print(colored("Error: {} does not work.".format(fileName), "red")) + print(Style.RESET_ALL) + +def RunAllTests(): + + global PLATFORM + init() + PLATFORM = "WINDOWS" + + SingleTest("tests/variable_scoping.c", 3) + SingleTest("tests/variables.c", 5) + SingleTest("tests/expression.c", 1) + SingleTest("tests/comments.c", 0) + SingleTest("tests/function.c", 65) + SingleTest("tests/function2.c", 128) + SingleTest("tests/factorial.c", 6) + SingleTest("tests/if.c", 100) + SingleTest("tests/if2.c", 80) + SingleTest("tests/if3.c", 40) + SingleTest("tests/conditional.c", 12) + SingleTest("tests/fib.c", 13) + SingleTest("tests/for.c", 5) + SingleTest("tests/while.c", 10) + + if __name__ == "__main__": - if len(sys.argv) > 1: - - platform = "WINDOWS" - DEBUG = True - - # Determine if they specified a specific platform - if len(sys.argv) > 2: - user_pla = sys.argv[2] - if user_pla == "LINUX" or user_pla == "WINDOWS": - platform = user_pla - else: - print("Platform not recognized, defaulting to Windows.") - else: - print("No platform specified, defaulting to Windows.") - # did they specify debug? - if len(sys.argv) > 3: - user_debug = sys.argv[3] - if user_debug == "DEBUG=true": - DEBUG = True - elif user_debug == "DEBUG=false": - DEBUG = False + parser = argparse.ArgumentParser(prog='Plasma Compiler', + description='Compile C programs.') + parser.add_argument('filename', nargs='?', help="The file to process.", default=None) + parser.add_argument('-d', '--debug', action='store_true', help="Enable debug mode.") + parser.add_argument('-r', '--run', action='store_true', help="Run code after compile.") + parser.add_argument('-t', '--test', action='store_true', help="Run all tests (ignores positional argument).") + args = parser.parse_args() - Run(sys.argv[1], DEBUG, True, platform) + if args.test: + RunAllTests() else: - logger.Error("No source file.") + if args.filename is not None: + platform = "WINDOWS" + DEBUG = args.debug + Run(args.filename, DEBUG, args.run, platform) + else: + logger.Error("No source file.") \ No newline at end of file diff --git a/test.py b/test.py deleted file mode 100644 index 190f5fb..0000000 --- a/test.py +++ /dev/null @@ -1,42 +0,0 @@ -# TEST DRIVEN DEVELOPMENT - -import os -import subprocess -import compiler -from colorama import init -from colorama import Fore, Back, Style -init() - -def colored(string, color): - if color == "green": - return Fore.GREEN + string - elif color == "red": - return Fore.RED + string - -PLATFORM = "WINDOWS" - -def SingleTest(fileName, desired_result): - debug = False - test = True - result = compiler.Run(fileName, debug, test, PLATFORM) - if desired_result == result: - print(colored("{} works.".format(fileName), "green")) - print(Style.RESET_ALL) - else: - print(colored("Error: {} does not work.".format(fileName), "red")) - print(Style.RESET_ALL) - -SingleTest("tests/variable_scoping.c", 3) -SingleTest("tests/variables.c", 5) -SingleTest("tests/expression.c", 1) -SingleTest("tests/comments.c", 0) -SingleTest("tests/function.c", 65) -SingleTest("tests/function2.c", 128) -SingleTest("tests/factorial.c", 6) -SingleTest("tests/if.c", 100) -SingleTest("tests/if2.c", 80) -SingleTest("tests/if3.c", 40) -SingleTest("tests/conditional.c", 12) -SingleTest("tests/fib.c", 13) -SingleTest("tests/for.c", 5) -SingleTest("tests/while.c", 10)