Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ze7111 committed Feb 20, 2024
1 parent 58319a3 commit 07d5788
Show file tree
Hide file tree
Showing 26 changed files with 786 additions and 100 deletions.
Binary file modified __pycache__/globals.cpython-312.pyc
Binary file not shown.
Binary file added a.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion build_rev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
0.0.2-alpha.p
1311
1515
20 changes: 19 additions & 1 deletion classes/Token.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def line_number(self, value: int) -> None:
def indent_level(self, value: int) -> None:
self.__indent_level = value

def set_token(self, value: str) -> 'Token':
self.__processed_line = value
return self

# ---------------------------- Magic Methods ----------------------------- #

def __hash__(self):
Expand Down Expand Up @@ -198,6 +202,17 @@ def splice(self, start: int = 0, end: int = None) -> 'Token_List':
temp.line = temp.line[start:end]
return temp

def append(self, __value: Token | str) -> None:
if isinstance(__value, str):
self.line.append(Token(None, __value, -1, self.indent_level))
else:
self.line.append(__value)

def replace(self, __old: str, __new: str) -> 'Token_List':
self.line = [token if token.token != __old else token.set_token(__new) for token in self.line]
return self


class Processed_Line:
def __init__(self, line: str, non_parsed_line: Token_List):
self.line = line
Expand All @@ -207,4 +222,7 @@ def __str__(self):
return self.line

def __repr__(self):
return repr(self.line.rstrip())
return repr(self.line.rstrip())

def to_code(self) -> str:
return self.line
1 change: 0 additions & 1 deletion classes/Transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,4 @@ def transpile(cls, root_scope: Scope):
[cls.__transpile(child) for child in root_scope.children]
cls.__add_from_queue(root_scope)

print(*cls.transpiled, sep="\n")
return cls.transpiled
Binary file modified classes/__pycache__/Token.cpython-312.pyc
Binary file not shown.
Binary file modified classes/__pycache__/Transpiler.cpython-312.pyc
Binary file not shown.
Binary file modified core/token/__pycache__/tokenize_line.cpython-312.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions core/token/tokenize_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def tokenize_line(code: Token | str) -> list[str]:
back_slash = "\\"

pattern: str = rf"""
("[^"\\]*(?:\\.[^"\\]*)*") | # Double quotes strings
('[^'\\]*(?:\\.[^'\\]*)*') | # Single quotes strings
([fbur]*"[^"\\]*(?:\\.[^"\\]*)*") | # Double quotes strings, including f, b, r, u strings
([fbur]*'[^'\\]*(?:\\.[^'\\]*)*') | # Single quotes strings, including f, b, r, u strings
({back_slash.join(globals.COMMENT.split())}[^\n]*) | # Single line comments (~~)
({back_slash.join(globals.BLOCK_COMMENT.split())}[\s\S]*?{back_slash.join(globals.BLOCK_COMMENT.split())}) | # Multi line comments (~*~ ... ~*~)
(\b\d+\.\d+\b) | # Decimal numbers
Expand Down
Binary file modified functions/__pycache__/_functions.cpython-312.pyc
Binary file not shown.
Binary file modified functions/__pycache__/_include.cpython-312.pyc
Binary file not shown.
17 changes: 16 additions & 1 deletion functions/_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
CLASS_EXTENSION = "::"
SEPARATOR_FOR_CLASSES = "+"

def generate_default_code(indent_chars: str) -> str:
return f"""
{indent_chars} __value__: object = None
{indent_chars} def __set__(self, value: Any) -> None:
{indent_chars} if isinstance(value, self.__class__):
{indent_chars} self.value = value.value
{indent_chars} else:
{indent_chars} try:
{indent_chars} self.value = getattr(value, f"to_{{self.__class__.__name__.lower()}}")()
{indent_chars} except Exception as e:
{indent_chars} panic(TypeError(f"Expected type {{self.__class__}}, got {{type(value)}}, unable to cast"), file=__file__)
"""


def _class(ast_list: Token_List, current_scope, parent_scope, root_scope, modifiers=None) -> str:
data_structure_types = (
parent_scope.get_keyword("INTERFACE"),
Expand Down Expand Up @@ -107,4 +121,5 @@ def _class(ast_list: Token_List, current_scope, parent_scope, root_scope, modifi
if class_decorators:
output = "\n" + "\n".join([f"{INDENT_CHAR*ast_list.indent_level}{i}" for i in class_decorators]) + "\n" + output

return Processed_Line(output, ast_list)

return Processed_Line(output + generate_default_code(INDENT_CHAR*ast_list.indent_level), ast_list)
4 changes: 2 additions & 2 deletions functions/_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def re_process_stack(_name: str, processed_decls: dict[str, dict[str, str]]) ->
# if any vars need to be discarded
if any([declarations[_name]["discard"] for _name in declarations]):
Transpiler.Transpiler.add_to_transpiled_queue.append((
f"{INDENT_CHAR*ast_list.indent_level}del {', '.join([f'{_name}' for _name in declarations if declarations[_name]['discard']])}\n",
Processed_Line(f"{INDENT_CHAR*ast_list.indent_level}del {', '.join([f'{_name}' for _name in declarations if declarations[_name]['discard']])}\n", ast_list),
current_scope
))
elif len(init_statement) > 1:
Expand Down Expand Up @@ -309,7 +309,7 @@ def extract_variables(index: int, token: Token) -> None:
# if any vars need to be discarded
if any([declarations[_name]["discard"] for _name in declarations]):
Transpiler.Transpiler.add_to_transpiled_queue.append((
f"{INDENT_CHAR*ast_list.indent_level}del {', '.join([f'{_name}' for _name in declarations if declarations[_name]['discard']])}\n",
Processed_Line(f"{INDENT_CHAR*ast_list.indent_level}del {', '.join([f'{_name}' for _name in declarations if declarations[_name]['discard']])}\n", ast_list),
current_scope
))

Expand Down
57 changes: 50 additions & 7 deletions functions/_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,47 @@
INDENT_CHAR = load_config().Formatter["indent_char"]
re = __import__(load_config().Transpiler["regex_module"])

from types import MappingProxyType as map

from classes.Scope import Scope
from core.panic import panic
from functions._class import _class
from classes.Scope import Scope

replace_function_name = map({
"==" : "__eq__",
"//" : "__floordiv__",
">=" : "__ge__",
"<=" : "__le__",
"<<" : "__lshift__",
"!=" : "__ne__",
"**" : "__pow__",
"//" : "__rfloordiv__",
"<<" : "__rlshift__",
">>" : "__rshift__",
"**" : "__rpow__",
">>" : "__rrshift__",
"%" : "__rmod__",
"%" : "__mod__",
"*" : "__rmul__",
"&" : "__and__",
"*" : "__mul__",
"+" : "__radd__",
"+" : "__add__",
"-" : "__neg__",
"|" : "__ror__",
"~" : "__invert__",
"+" : "__pos__",
">" : "__gt__",
"&" : "__rand__",
"<" : "__lt__",
"-" : "__rsub__",
"/" : "__rtruediv__",
"|" : "__or__",
"^" : "__rxor__",
"-" : "__sub__",
"/" : "__truediv__",
"^" : "__xor__"
})

def extract_variables(ast_line: Token_List, root_scope: Scope) -> str:
variables = {}
Expand Down Expand Up @@ -58,12 +96,12 @@ def extract_variables(ast_line: Token_List, root_scope: Scope) -> str:
variables[tuple(variables.keys())[-1]] = {"type": variables[tuple(variables.keys())[-1]]["type"] + generic}
line = line[1:]
continue
generic += "["
generic += "[" + " "
line = line[1:]
elif line[0].token == ">":
generic_count -= 1
in_generic = True if generic_count > 0 else False
generic += "]"
generic += " " + "]"
if not in_generic:
in_generic = False
variables[tuple(variables.keys())[-1]] = {"type": variables[tuple(variables.keys())[-1]]["type"] + generic}
Expand Down Expand Up @@ -165,6 +203,11 @@ def function(ast_list: Token_List, current_scope: Scope, parent_scope: Scope, ro
variables = extract_variables(ast_list, root_scope)
name = ast_list.line[1].token

if name in replace_function_name:
if not_allowed_classes not in parent_scope.namespace_header:
panic(SyntaxError(f"<Hex(02.E3)>: Cannot overload a unary operator outside a class"), name, file=ast_list.file, line_no=ast_list.find_line_number(name))
name = replace_function_name[name]

output = f"def {name}("

if not variables["params"]:
Expand All @@ -188,13 +231,13 @@ def function(ast_list: Token_List, current_scope: Scope, parent_scope: Scope, ro
output = f"\n{INDENT_CHAR*ast_list.indent_level}{output}"

if not any([i in not_allowed_classes for i in parent_scope.name]):
output = f"\n{INDENT_CHAR*ast_list.indent_level}@__internal__multi_method" + output
output = f"\n{INDENT_CHAR*ast_list.indent_level}@hx__multi_method" + output
# if the type of parent_sope is an abstract class
if any([i == root_scope.get_keyword("ABSTRACT") for i in parent_scope.name]):
output = f"\n{INDENT_CHAR*ast_list.indent_level}@__internal__abstract_method" + output
output = f"\n{INDENT_CHAR*ast_list.indent_level}@hx__abstract_method" + output
# if the type of parent_sope is an interface
if any([i == root_scope.get_keyword("INTERFACE") for i in parent_scope.name]):
output = f"\n{INDENT_CHAR*ast_list.indent_level}@__internal__abstract_method" + output
output = f"\n{INDENT_CHAR*ast_list.indent_level}@hx__abstract_method" + output

static: bool = False
async_: bool = False
Expand All @@ -209,7 +252,7 @@ def function(ast_list: Token_List, current_scope: Scope, parent_scope: Scope, ro
static = True

if root_scope.get_keyword('ASYNC') in modifiers:
output = f"\n{INDENT_CHAR*ast_list.indent_level}@async{output}"
output = f"\n{INDENT_CHAR*ast_list.indent_level}@hx__async{output}"
async_ = True

# if root_scope.get_keyword('PRIVATE') in modifiers:
Expand Down
32 changes: 16 additions & 16 deletions functions/_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ def include(ast_list: Token_List, current_scope, parent_scope, root_scope) -> st
import_statement = ""
if type == "C":
if not alias and not modules:
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __c_cpp_import__({path})\n", ast_list)
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __c_cpp_import__(\"{path}\")\n", ast_list)
elif alias and modules and len(modules) == 1:
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{alias} = __c_cpp_import__({path}).{modules[0]}\n", ast_list)
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{alias} = __c_cpp_import__(\"{path}\").{modules[0]}\n", ast_list)
elif alias and modules and len(modules) > 1:
panic(SyntaxError(f"Invalid include statement: {combined_line} cannot have multiple modules and an alias"), file=ast_list.file, line_no=ast_list.line[0].line_number)
elif alias and not modules:
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{alias} = __c_cpp_import__({path})\n", ast_list)
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{alias} = __c_cpp_import__(\"{path}\")\n", ast_list)
elif not alias and modules and len(modules) == 1:
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __c_cpp_import__({path}).{modules[0]}\n", ast_list)
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __c_cpp_import__(\"{path}\").{modules[0]}\n", ast_list)
elif not alias and modules and len(modules) > 1:
import_statement = f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __c_cpp_import__({path})\n"
import_statement = f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __c_cpp_import__(\"{path}\")\n"
for module in modules:
import_statement += f"{INDENT_CHAR*ast_list.line[0].indent_level}{module.strip()} = {os_path.splitext(path)[0].strip()}.{module}\n"
import_statement += f"{INDENT_CHAR*ast_list.line[0].indent_level}del {os_path.splitext(path)[0].strip()}\n"
Expand All @@ -81,17 +81,17 @@ def include(ast_list: Token_List, current_scope, parent_scope, root_scope) -> st
panic(SyntaxError(f"Invalid include statement: {combined_line}"), file=ast_list.file, line_no=ast_list.line[0].line_number)
elif type == "CPP":
if not alias and not modules:
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __c_cpp_import__({path})\n", ast_list)
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __c_cpp_import__(\"{path}\")\n", ast_list)
elif alias and modules and len(modules) == 1:
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{alias} = __c_cpp_import__({path}).{modules[0]}\n", ast_list)
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{alias} = __c_cpp_import__(\"{path}\").{modules[0]}\n", ast_list)
elif alias and modules and len(modules) > 1:
panic(SyntaxError(f"Invalid include statement: {combined_line} cannot have multiple modules and an alias"), file=ast_list.file, line_no=ast_list.line[0].line_number)
elif alias and not modules:
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{alias} = __c_cpp_import__({path})\n", ast_list)
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{alias} = __c_cpp_import__(\"{path}\")\n", ast_list)
elif not alias and modules and len(modules) == 1:
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __c_cpp_import__({path}).{modules[0]}\n", ast_list)
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __c_cpp_import__(\"{path}\").{modules[0]}\n", ast_list)
elif not alias and modules and len(modules) > 1:
import_statement = f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __c_cpp_import__({path})\n"
import_statement = f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __c_cpp_import__(\"{path}\")\n"
for module in modules:
import_statement += f"{INDENT_CHAR*ast_list.line[0].indent_level}{module} = {os_path.splitext(path)[0].strip()}.{module}\n"
import_statement += f"{INDENT_CHAR*ast_list.line[0].indent_level}del {os_path.splitext(path)[0].strip()}\n"
Expand All @@ -113,25 +113,25 @@ def include(ast_list: Token_List, current_scope, parent_scope, root_scope) -> st
panic(SyntaxError(f"Invalid include statement: {combined_line}"), file=ast_list.file, line_no=ast_list.line[0].line_number)
elif type == "RS":
if not alias and not modules:
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __rs_import__({path})\n", ast_list)
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __rs_import__(\"{path}\")\n", ast_list)
elif alias and not modules:
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{alias} = __rs_import__({path})\n", ast_list)
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{alias} = __rs_import__(\"{path}\")\n", ast_list)
elif alias and modules and len(modules) == 1:
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{alias} = __rs_import__({path}).{modules[0]}\n", ast_list)
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{alias} = __rs_import__(\"{path}\").{modules[0]}\n", ast_list)
elif alias and modules and len(modules) > 1:
panic(SyntaxError(f"Invalid include statement: {combined_line} cannot have multiple modules and an alias"), file=ast_list.file, line_no=ast_list.line[0].line_number)
elif not alias and modules and len(modules) == 1:
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __rs_import__({path}).{modules[0]}\n", ast_list)
return Processed_Line(f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __rs_import__(\"{path}\").{modules[0]}\n", ast_list)
elif not alias and modules and len(modules) > 1:
import_statement = f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __rs_import__({path})\n"
import_statement = f"{INDENT_CHAR*ast_list.line[0].indent_level}{os_path.splitext(path)[0].strip()} = __rs_import__(\"{path}\")\n"
for module in modules:
import_statement += f"{INDENT_CHAR*ast_list.line[0].indent_level}{module} = {os_path.splitext(path)[0].strip()}.{module}\n"
import_statement += f"{INDENT_CHAR*ast_list.line[0].indent_level}del {os_path.splitext(path)[0].strip()}\n"
return Processed_Line(import_statement, ast_list)
else:
panic(SyntaxError(f"Invalid include statement: {combined_line}"), file=ast_list.file, line_no=ast_list.line[0].line_number)
elif type == "HX":
panic(SyntaxError(f"importing namespace {path} is not supported"), file=ast_list.file, line_no=ast_list.line[0].line_number)
panic(SyntaxError(f"importing namespace \"{path}\" is not supported"), file=ast_list.file, line_no=ast_list.line[0].line_number)

return Processed_Line(import_statement, ast_list)

Expand Down
Loading

0 comments on commit 07d5788

Please sign in to comment.