Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
Refactor script for argparse and update README.
Browse files Browse the repository at this point in the history
  • Loading branch information
BluBloos committed May 22, 2024
1 parent 91a3661 commit 142b3d9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 102 deletions.
66 changes: 31 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
<p align="center">
<img src="https://i.gyazo.com/66cada5c7538e5597443c1e467c862aa.gif" />
</p>

# Plasma-Compiler ⚡

C compiler written in Python. It does not comply with any standards for the C
Expand All @@ -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.
<p align="center">
<img src="https://i.gyazo.com/66cada5c7538e5597443c1e467c862aa.gif" />
</p>

# 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

Expand Down Expand Up @@ -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 <fileName>
```

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: - !
83 changes: 58 additions & 25 deletions compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -75,32 +78,62 @@ def Run(fileName, DEBUG, TEST, platform):

return return_val


# usage: python compiler.py <inFile> ?<platform> ?<debug>
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.")
42 changes: 0 additions & 42 deletions test.py

This file was deleted.

0 comments on commit 142b3d9

Please sign in to comment.