Skip to content
Draft

Docs #64

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/pychart/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
from src.pychart.runner import run_prompt, run_file, run_file_as_bytecode, run_as_bytecode, run

def main():
"""
The default entrypoint for the pychart-lang

usage: pychart [-h] [--version] [file]

positional arguments:
file run a pychart file

optional arguments:
-h, --help show this help message and exit
--version, -V
"""
parser = argparse.ArgumentParser(prog='pychart')
parser.add_argument('file', nargs='?', help='run a pychart file')
parser.add_argument('--version', '-V', action='store_true')
Expand Down
30 changes: 30 additions & 0 deletions src/pychart/_interpreter/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ class Scanner:
}

def __init__(self, program_text: str):
"""
program_text: str - The source text to turn into tokens
"""
self.source = program_text

def __is_at_end(self) -> bool:
"""Checks whether the pointer has reached EOF"""
return self.current >= len(self.source)

def __advance(self) -> str:
"""
advances the pointer
returns the current character
"""
char = self.source[self.current]
self.current += 1
return char
Expand All @@ -52,6 +60,10 @@ def __add_token(self, token_type: TokenType, literal: Any = None):
self.tokens.append(Token(token_type, text, literal, self.line))

def __match(self, char: str) -> bool:
"""
Checks whether the next character is char, and advances the pointer
returns True if the next character is char, False otherwise
"""
if self.__is_at_end():
return False

Expand All @@ -62,17 +74,24 @@ def __match(self, char: str) -> bool:
return True

def __peek(self) -> str:
"""
returns the following character
"""
if self.__is_at_end():
return "\0"
return self.source[self.current]

def __peek_next(self) -> str:
""" Same as __peek but one simulated advance in the future"""
if self.current + 1 >= len(self.source):
return "\0"

return self.source[self.current + 1]

def __string(self):
"""
Scans a string literal which is exactly the characters between two quotation marks (")
"""
while self.__peek() != '"' and not self.__is_at_end():
if self.__peek() == "\n":
self.line += 1
Expand All @@ -87,6 +106,10 @@ def __string(self):
self.__add_token(TokenType.STRING, value)

def __number(self):
"""
Scans a number literal (base 10) defined as any alpha string
(including leading zeros and any number of characters) and is stored as a float
"""
while self.__peek().isdigit():
self.__advance()

Expand All @@ -100,6 +123,10 @@ def __number(self):
)

def __identifier(self):
"""
Scans a identifier which is an alphanumeric string of any lengths starting with
a letter
"""
char = self.__peek()
while char.isalnum() or char == '_':
self.__advance()
Expand Down Expand Up @@ -166,6 +193,7 @@ def __scan_token(self):
while self.__peek() != "\n" and not self.__is_at_end():
self.__advance()
else:
# Division (Slash)
self.__add_token(TokenType.SLASH)

# Whitespace
Expand All @@ -186,6 +214,8 @@ def __scan_token(self):
pass # Throw error -> unrecognised token

def get_tokens(self) -> List[Token]:
"""Turns the source text used into a list of tokens"""

if len(self.tokens) != 0:
self.tokens = []

Expand Down
3 changes: 3 additions & 0 deletions src/pychart/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ def run(source: str):


def run_prompt():
"""
REPL entrypoint, exit with ".exit"
"""
try:
while True:
line = input("$ : ")
Expand Down