Skip to content

Commit

Permalink
done did the general token right
Browse files Browse the repository at this point in the history
- also, variables
- compressed stuff
- TokenType.NAME is dead code
- ACE exploits are yesn't
  • Loading branch information
lyxal committed Aug 13, 2021
1 parent c250bf4 commit 5bc7e7d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
6 changes: 6 additions & 0 deletions vyxal/helpers.py
Expand Up @@ -6,6 +6,8 @@
"""

import textwrap
from typing import Union
import lexer


def indent_str(string: str, indent: int, end="\n") -> str:
Expand Down Expand Up @@ -44,3 +46,7 @@ def indent_code(*code, indent: int = 1) -> str:
`code`, all indented by `(4 * indent)` spaces, then joined on `\n`.
"""
return "\n".join(indent_str(line, indent, end="") for line in code) + "\n"


def uncompress(token: lexer.Token) -> Union[int, str]:
return token.value
1 change: 0 additions & 1 deletion vyxal/lexer.py
Expand Up @@ -44,7 +44,6 @@ class TokenType(Enum):

STRING = "string"
NUMBER = "number"
NAME = "name"
GENERAL = "general"
COMPRESSED_NUMBER = "compressed_number"
COMPRESSED_STRING = "compressed_string"
Expand Down
26 changes: 16 additions & 10 deletions vyxal/transpile.py
Expand Up @@ -55,29 +55,35 @@ def transpile_single(
elif isinstance(token_or_struct, structure.Structure):
return transpile_structure(token_or_struct, indent)
raise ValueError(
f"Input must be a Token or Structure, was {type(token_or_struct).__name__}: {token_or_struct}"
f"Input must be a Token or Structure,"
" was {type(token_or_struct).__name__}: {token_or_struct}"
)


def transpile_token(token: Token, indent: int) -> str:
from helpers import indent_str
from helpers import indent_str, uncompress

print(token.name, TokenType.GENERAL)

if token.name == TokenType.STRING:
return indent_str(f"stack.append('{token.value}')", indent)
# Make sure we avoid any ACE exploits
string = uncompress(token) # TODO: Account for -D flag
value = string.replace("\\", "\\\\").replace('"', '\\"')
return indent_str(f'stack.append("{value}")', indent)
elif token.name == TokenType.NUMBER:
return indent_str(f"stack.append({token.value})", indent)
elif token.name == TokenType.NAME:
pass
elif token.name == TokenType.GENERAL:
pass # return elements.elements.get(token.value) @lyxal is this right?
return elements.elements.get(token.value, ["pass", -1])[0]
elif token.name == TokenType.COMPRESSED_NUMBER:
pass
return indent_str(f"stack.append({uncompress(token)})")
elif token.name == TokenType.COMPRESSED_STRING:
pass
return indent_str(f"stack.append('{uncompress(token)}')")
# No need to check for ACE exploits here because this string
# type will only ever contain lower alpha + space.
elif token.name == TokenType.VARIABLE_GET:
pass
return indent_str(f"stack.append(VAR_{token.value})")
elif token.name == TokenType.VARIABLE_SET:
pass
return indent_str(f"VAR_{token.value} = pop(stack, ctx=ctx)")
raise ValueError(f"Bad token: {token}")


Expand Down

0 comments on commit 5bc7e7d

Please sign in to comment.