Skip to content

Commit

Permalink
Bootstrapped
Browse files Browse the repository at this point in the history
  • Loading branch information
Chadderbox committed Nov 28, 2021
1 parent a90f8d8 commit 24ca394
Show file tree
Hide file tree
Showing 5 changed files with 726 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ dmypy.json
.pyre/

#cbLang test files
*.cb
Testing.py
cbLangBootstrap/*

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ So, how does it work?
}
*^This is a basic `Hello World` program.*

To run this, you will need the CBLang interpreter that you can either obtain by running `.\build.bat` or downloading it from the [releases section](https://github.com/Ceebox/cbLang/releases).
To run this, you will need the CBLang interpreter that you can either obtain by downloading it from the [releases section](https://github.com/Ceebox/cbLang/releases) or running `.\build.bat` (this will give you a depricated version of the interpreter based on the old Python code).

Expand Down
8 changes: 8 additions & 0 deletions error.cb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from native reference sys;

class Error()
{
function Start(error):
print(error);
sys.exit();
}
142 changes: 142 additions & 0 deletions interpreter.cb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
from native reference os.path;
from native reference subprocess;
from native reference sys;
from native reference shutil;

include parse;
include error;

class Interpreter()
{
function Interpret(code)
{
subprocess.call(["python", "output.py"]);
}
}
class Main()
{
function GetCode(filePath)
{
if os.path.isfile(filePath)
{
with open(filePath, 'r') as file
{
return file.read();
}
}
else
{
Error("Input file not found");
}
}

function HandleArgs()
{
if sys.argv[1] == "--help" or sys.argv[1] == "-h"
{
Error("Command line arguments: \n--help -h: Prints this message \n--version -b: Prints the version of the interpreter \n--run -r (default) [file]: Runs the interpreter on the file specified \n--transpile -t [file] [address]: Converts the file specified into python code and saves it to the address specified \n--compile -c [file] [address]: Compiles the file specified into an executable and saves it to the address specified");
}
else if sys.argv[1] == "--run" or sys.argv[1] == "-r"
{
if len(sys.argv) < 3
{
Error("Invalid number of arguments");
}
else
{
if os.path.isfile(sys.argv[2])
{
parser = Parser(this.GetCode((sys.argv[2])));
interpreter = Interpreter();
interpreter.Interpret(parser.code);
}
else
{
Error("File not found");
}
}
}
else if os.path.isfile(sys.argv[1])
{
parser = Parser(this.GetCode(sys.argv[1]));
interpreter = Interpreter();
interpreter.Interpret(parser.code);
}
else if sys.argv[1] == "--transpile" or sys.argv[1] == "-t"
{
if len(sys.argv) < 4
{
Error("Invalid number of arguments");
}
else
{
if os.path.isfile(sys.argv[2])
{
parser = Parser(this.GetCode((sys.argv[2])));
with open(sys.argv[3], "w") as f
{
f.write(parser.code);
}
}
else
{
Error("Input file not found");
}
}
}
else if sys.argv[1] == "--compile" or sys.argv[1] == "-c"
{
if len(sys.argv) < 4
{
Error("Invalid number of arguments");
}
else
{
if os.path.isfile(sys.argv[2])
{
parser = Parser(this.GetCode((sys.argv[2])));
fileName = sys.argv[3].split(".")[0];
with open(fileName + ".py", "w") as f
{
f.write(parser.code);
}
if (os.path.isfile(fileName + ".exe"))
{
os.remove(fileName + ".exe");
}
subprocess.call(["PyInstaller", fileName + ".py", "--onefile", "--log-level", "ERROR"]);
os.rename("dist/{}".format(fileName+".exe"),"./"+sys.argv[3]);
os.remove(fileName + ".py");
os.remove(fileName + ".spec");
shutil.rmtree("build");
shutil.rmtree("dist");
}
else
{
Error("File not found");
}
}
}
else
{
Error("Invalid argument");
}

if (os.path.isfile("output.py"))
{
os.remove("output.py");
}
}

function CheckArgs()
{
if len(sys.argv) < 2:
Error("Invalid number of arguments");
this.HandleArgs();
}

function Main()
{
this.CheckArgs();
}
}
Loading

0 comments on commit 24ca394

Please sign in to comment.