Skip to content

Commit

Permalink
adding brun keyword & fixing if bug
Browse files Browse the repository at this point in the history
  • Loading branch information
decrovert committed Aug 8, 2022
1 parent be9caae commit 926e6d8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pobrelanglib/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def lex_line(line: str) -> list[str]:
case "if":
tokens.append("IFS")

case "burn":
tokens.append("BRN")

case "note":
tokens.append("NTE")

Expand Down
15 changes: 12 additions & 3 deletions pobrelanglib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def parse_math(expr: str) -> str:
for var, val in variables.items():
expr = expr.replace(var, val)

return str(eval(expr))
return str(float(eval(expr)))

def create_variable(type: str, name: str, value: str) -> None:
global variables, money
Expand Down Expand Up @@ -179,15 +179,24 @@ def parse_line(line: list[str]) -> None:
condition_result = 0

try:
condition_result = parse_math(condition)
condition_result = bool(float(parse_math(condition)))
except (NameError, SyntaxError):
lt_panic("pass an invalid expression to an if statement")

del condition

if condition_result == True or not condition_result == 0:
if condition_result == True:
line_number = stamps[stamp]

case "BRN":
if not len(line) == 2 or not is_token(line[1], "EXP"):
lt_panic("try to delete a variable without knowing how to use the burn keyword")

try:
variables.pop(extract_expression(line[1]))
except KeyError:
lt_panic("try to delete a variable that does not exist")

case "NTE":
pass

Expand Down
4 changes: 3 additions & 1 deletion test.pbr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ item PobreLang/shit 3

stamp PobreLang/shit
scream Hello world!
if 1 PobreLang/shit
if 1>3 PobreLang/shit

burn PobreLang/shit

work PobreLang/shit+3

0 comments on commit 926e6d8

Please sign in to comment.