# Basic.Py

unknown
python
2 years ago
6.3 kB
1
Indexable
Never
```import basic
while True:

# lexer will go through the input character by character and break up the text into a list of Tokens.
# A Token is a simpe object that has a type and optionly a value each Token comes from a small set of the code.

# Basic.Py

# This is creating a class called Token and we are using a function and init and then assigining it to
#self = to it's self. we are then setting the self to it's value.

# Assigning Intergers This includes Numbers,decimals, +,-,*,/,(,)
# A float is a whole number or a decimal
# TT stands for Token type.

# Defining the Digits

DIGITS = '0123456789'

# Tokens
TT_INTERGER         = 'TT_INTERGER' # Number
TT_FLOAT            = 'TT_FLOAT' # Decimal Number
TT_PLUS             = 'TT_PLUS' # Additon +
TT_MINUS            = 'TT_MINUS' # Minus -
TT_MULTIPLY         = 'TT_MULTIPLY' # Muliplication *
TT_DIVIDE           = 'TT_DIVIDE' # Divison /
TT_Left_parenthese  = 'TT_Left_parenthese' # (
TT_Right_parenthese = 'TT_Right_parenthese' # )

class Token:
def __init__(self,type, value = None):
self.type = type_
self.value = value

# If the token has the value this will print the type and the value
# and if does not have a value it will just print the type.
def __repr__(self):
if self.value: return f'{self.type}:{self.value}'
return f'{self.type}'

# Error class

class Error:
def __init__(self, positon_start,positon_End,error_name,details):
self.positon_End = positon_start
self.positon_start = positon_End
self.error_name = error_name
self.details = details

def as_string(self):
result = f'{self.error_name}: {self.details}'
result += f'File {self.positon_start.filename}, line {self.positon_start.index +1}'
return result

class InvaildCharacterError(Error):
def __init__(self, positon_start, positon_End, details):
super().__init__('Invaild Character', details)

# Lexer class
# Creating the class lexer creating a function for the lexer and the text, setting the text
# equal to it's self, setting the positon equal to -1, and setting the current_character to None

class Lexer:
def __init__ (self,filename, text):
self.filename = filename
self.text = text
self.positon = positon(-1, 0, -1, text)
self.current_character = None
# Defining a advance method which will advance to the next character in the text

self.current_character = self.text[self.positon.index] if self.positon.index < length(self.text) else None

# creating a make tokens method and making a empty list of tokens

def make_tokens(self):
token = []
# Assigning the variables created to characters
while self.current_character != None:
if self.current_character in ' \t':
elif self.current_character in DIGITS:
tokens.append(self.make_number())
elif self.current_character == '+':
tokens.append(token(TT_PLUS))
elif self.current_character == '-':
tokens.append(token(TT_MINUS))
elif self.current_character == '*':
tokens.append(token(TT_MULTIPLY))
elif self.current_character == '/':
tokens.append(token(TT_PLUS))
elif self.current_character == '(':
tokens.append(token(TT_Left_parenthese))
elif self.current_character == ')':
tokens.append(token(TT_Right_parenthese))
else:
positon_start = self.positon.copy
character = self.current_character
return [], InvaildCharacterError(positon_start_start, self.positon, "'" + character + "'")

# positon class

class Positon:
def __init__(self, index, line, colum, filename, filetext):
self.index = index
self.line = line
self.colum = colum
self.filename = filename
self.filetext = filetext

self.index +=1
self.colum +=1

if current_character == '\n':
self.line +=1
self.colum = 0

return self

def copy(self):
return Positon(self.index, self.line, self.colum, self.filename, self.filetext)

# if there are no dots then the number is a interger else it is a decimal

def make_number(self):
number_String = ''
dot_count = 0

while self.current_character != None and self.current_character in DIGITS + '.':
if self.current_character == '.':
if dot_count == 1: break
dot_count += 1
number_String += '.'
else:
number_String += self.current_character

if dot_count == 0:
else: