Comments
# CommentsVariable declaration
var1 = "hello"Accessing variable
print var1Updating variable value
var1 = "Hello World"Print statement
print "wth..!"Swapping values
v1 = 1
v2 = 2
v1 = v2| import colorama, re, argparse, sys | |
| class PrintToken: | |
| ValueType: str | |
| Value: str | |
| class VariableToken: | |
| VariableName: str | |
| ValueType: str | |
| Value: any | |
| class Lexer: | |
| def __init__(self, code: str) -> None: | |
| self.code: str = code | |
| self.tokens: list = [] | |
| self.chunk: str = "" | |
| self._lexer() | |
| def _lexer(self) -> None: | |
| i: int = 0 | |
| while i < len(self.code): | |
| if self.code[i] == "\n": | |
| self.chunk = "" | |
| i += 1 | |
| continue | |
| if self.code[i] == " ": | |
| i += 1 | |
| continue | |
| if self.code[i] == "#": | |
| while i < len(self.code) and self.code[i] != "\n": | |
| i += 1 | |
| continue | |
| if self.chunk == "print" and (self.code[i-1] in (" ", "\n") or self.code[i] == "\""): | |
| while i < len(self.code) and self.code[i] in (" ", "\n"): | |
| i += 1 | |
| obj = PrintToken() | |
| if i < len(self.code) and self.code[i] == "\"": | |
| i += 1 | |
| self.chunk = "" | |
| while i < len(self.code) and self.code[i] != "\"": | |
| self.chunk += self.code[i] | |
| i += 1 | |
| if i < len(self.code) and self.code[i] == "\"": | |
| i += 1 | |
| obj.ValueType = str | |
| obj.Value = self.chunk | |
| self.tokens.append(obj) | |
| self.chunk = "" | |
| continue | |
| self.chunk = "" | |
| while i < len(self.code) and self.code[i] not in (" ", "\n"): | |
| self.chunk += self.code[i] | |
| i += 1 | |
| if exists := [t for t in self.tokens if type(t) == VariableToken and t.VariableName == self.chunk]: | |
| obj.ValueType = VariableToken | |
| obj.Value = exists[0].Value | |
| elif _ := bool(re.match(r"^-?\d+$", self.chunk)): | |
| obj.ValueType = int | |
| obj.Value = int(self.chunk) | |
| elif _ := bool(re.match(r"-?(?:\d+\.\d+|\.\d+)", self.chunk)): | |
| obj.ValueType = float | |
| obj.Value = float(self.chunk) | |
| else: | |
| obj.ValueType = NameError | |
| obj.Value = f"'{self.chunk}' not defined" | |
| self.tokens.append(obj) | |
| self.chunk = "" | |
| continue | |
| if self.code[i] == "=": | |
| i += 1 | |
| while i < len(self.code) and self.code[i] in (" ", "\n"): | |
| i += 1 | |
| obj = VariableToken() | |
| while len(self.chunk) and self.chunk[-1] == " ": | |
| self.chunk = self.chunk[:-1] | |
| if exists := [t for t in self.tokens if type(t) == VariableToken and t.VariableName == self.chunk]: | |
| obj = exists[0] | |
| else: | |
| obj.VariableName = self.chunk | |
| while i < len(self.code) and self.code[i] in (" ", "\n"): | |
| i += 1 | |
| if i < len(self.code) and self.code[i] == "\"": | |
| obj.ValueType = str | |
| i += 1 | |
| val = "" | |
| while i < len(self.code) and self.code[i] != "\"": | |
| val += self.code[i] | |
| i += 1 | |
| if i < len(self.code) and self.code[i] == "\"": | |
| i += 1 | |
| obj.Value = val | |
| self.tokens.append(obj) | |
| self.chunk = "" | |
| continue | |
| self.chunk = "" | |
| while i < len(self.code) and self.code[i] not in (" ", "\n"): | |
| self.chunk += self.code[i] | |
| i += 1 | |
| if _ := bool(re.match(r"^-?\d+$", self.chunk)): | |
| obj.ValueType = int | |
| obj.Value = int(self.chunk) | |
| self.tokens.append(obj) | |
| self.chunk = "" | |
| continue | |
| if _ := bool(re.match(r"-?(?:\d+\.\d+|\.\d+)", self.chunk)): | |
| obj.ValueType = float | |
| obj.Value = float(self.chunk) | |
| self.tokens.append(obj) | |
| self.chunk = "" | |
| continue | |
| if exists := [t for t in self.tokens if type(t) == VariableToken and t.VariableName == self.chunk]: | |
| obj.ValueType = exists[0].ValueType | |
| obj.Value = exists[0].Value | |
| self.tokens.append(obj) | |
| self.chunk = "" | |
| continue | |
| obj.ValueType = NameError | |
| obj.Value = f"'{self.chunk}' not defined" | |
| self.tokens.append(obj) | |
| self.chunk = "" | |
| continue | |
| self.chunk += self.code[i] | |
| i += 1 | |
| def main() -> None: | |
| try: | |
| args = argparse.ArgumentParser() | |
| args.add_argument("source_file", help="Input source file") | |
| args = args.parse_args() | |
| with open(args.source_file, "r", encoding="utf-8") as source_file: | |
| code: str = source_file.read() | |
| lexer = Lexer(code) | |
| i: int = 0 | |
| while i < len(lexer.tokens): | |
| token = lexer.tokens[i] | |
| if type(token) == PrintToken: | |
| if token.ValueType in (str, int, float): | |
| print(token.Value) | |
| elif token.ValueType == VariableToken: | |
| print(token.Value) | |
| elif token.ValueType == NameError: | |
| print(colorama.Fore.RED, token.Value, colorama.Style.RESET_ALL, sep="") | |
| i += 1 | |
| except FileNotFoundError: | |
| print(colorama.Fore.RED, "Error: Input file doesn't exists", colorama.Style.RESET_ALL, sep="") | |
| if __name__ == "__main__": | |
| main() |