Add new token: TokenType.BOOL

This commit is contained in:
Bartłomiej Pluta
2019-07-10 13:48:13 +02:00
parent 739610c663
commit 26a2b27def
6 changed files with 27 additions and 13 deletions

View File

@@ -8,7 +8,7 @@ from smnp.program.interpreter import Interpreter
def main():
try:
stdLibraryEnv = loadStandardLibrary()
Interpreter.interpretFile(sys.argv[1], printAst=True, baseEnvironment=stdLibraryEnv)
Interpreter.interpretFile(sys.argv[1], printTokens=True, printAst=False, baseEnvironment=stdLibraryEnv)
except SmnpException as e:
print(e.message())

View File

@@ -8,7 +8,7 @@ class Token:
self.rawValue = rawValue
def __str__(self):
return "Token(" + str(self.type) + ", '" + str(self.value) + "', " + str(self.pos) + ")"
return "{" + str(self.type.name) + ", '" + str(self.value) + "', " + str(self.pos) + "}"
def __repr__(self):
return self.__str__()

View File

@@ -1,5 +1,6 @@
from smnp.error.syntax import SyntaxException
from smnp.token.model import TokenList
from smnp.token.tokenizers.bool import boolTokenizer
from smnp.token.tokenizers.comment import commentTokenizer
from smnp.token.tokenizers.identifier import identifierTokenizer
from smnp.token.tokenizers.keyword import typeTokenizer
@@ -31,8 +32,9 @@ tokenizers = (
# Types
separated(regexPatternTokenizer(TokenType.INTEGER, r'\d')),
stringTokenizer,
typeTokenizer,
noteTokenizer,
boolTokenizer,
typeTokenizer,
# Keywords
separated(defaultTokenizer(TokenType.FUNCTION)),
@@ -50,7 +52,6 @@ tokenizers = (
commentTokenizer,
)
filters = [
lambda token: token.type is not None,
lambda token: token.type != TokenType.COMMENT

View File

@@ -0,0 +1,11 @@
from smnp.token.tools import keywordsTokenizer, separated
from smnp.token.type import TokenType
def boolTokenizer(input, current, line):
consumedChars, token = separated(keywordsTokenizer(TokenType.BOOL, "true", "false"))(input, current, line)
if consumedChars > 0:
token.value = token.value == "true"
return (consumedChars, token)
return (0, None)

View File

@@ -21,6 +21,7 @@ class TokenType(Enum):
INTEGER = 'integer'
STRING = 'string'
NOTE = 'note'
BOOL = 'bool'
TYPE = 'type'
FUNCTION = 'function'
RETURN = 'return'

View File

@@ -13,6 +13,7 @@ class Type(Enum):
MAP = (dict, lambda x: '{' + ', '.join(f"'{k.stringify()}' -> '{v.stringify()}'" for k, v in x.items()) + '}')
PERCENT = (float, lambda x: f"{int(x * 100)}%")
NOTE = (Note, lambda x: x.note.name)
BOOL = (bool, lambda x: str(x).lower())
SOUND = (Sound, lambda x: x.file)
TYPE = (None, lambda x: x.name.lower())
VOID = (type(None), lambda x: _failStringify(Type.VOID))