Skip to content

Commit

Permalink
Added first-class support for static functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Chadderbox committed Dec 23, 2021
1 parent 30970ed commit 1d21492
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ dmypy.json
.pyre/

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

Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,21 @@ To compile code, use the command `cbLang.exe -c filename.cb output.exe`. *(This
}
}

*^This is a basic example of importing external code, along with demonstrating other concepts in the programming language.*
*^This is a basic example of importing external code, along with demonstrating other concepts in the programming language.*

class Main
{
function Main()
{
Other.Test();
}
}

class Other
{
static function Test()
{
print("This code was printed from a static m,ethod in Other");
}
}
*^This is a basic example of static methods.*
2 changes: 1 addition & 1 deletion interpreter.cb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Main()
{
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");
Error("Command line arguments: \n--help -h: Prints help 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"
{
Expand Down
28 changes: 27 additions & 1 deletion parse.cb
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,24 @@ class Parser
{
if (line.partition("def")[0].strip() == "")
{
code = code.replace(line, line.replace("(", "(self,"));
code = code.replace(line, line.replace("(", "(this,"));
}
}
}
newCode = [];
for count, line in enumerate(code.splitlines())
{
if "static " in line
{
if not this.IsInString("static ", line, false)
{
newCode.insert(count - 1, " @staticmethod");
line = line.replace(line, line.replace("static ", ""));
}
}
newCode.append(line);
}
code = "\n".join(newCode);

return code;
}
Expand Down Expand Up @@ -487,6 +501,18 @@ class Parser
}
code = newCode;

//this
for line in code.splitlines()
{
if "this" in line
{
if not this.IsInString("this", line, true)
{
code = code.replace(line, line.replace("this", "self"));
}
}
}

//Remove indent helpers
newCode = "";
for line in code.splitlines()
Expand Down

0 comments on commit 1d21492

Please sign in to comment.