✨
This commit is contained in:
@ -7,7 +7,10 @@ BUILTIN_ARGS = {
|
||||
"print": "*",
|
||||
"input" : "*",
|
||||
"random": 2,
|
||||
"len": 1
|
||||
"len": 1,
|
||||
"int": 1,
|
||||
"str": 1,
|
||||
"flo": 1
|
||||
}
|
||||
|
||||
def rep_join(l):
|
||||
@ -36,6 +39,26 @@ class ExpInt(Exp):
|
||||
def eval(self, vtable, ftable):
|
||||
return vtable, ftable, self.value
|
||||
|
||||
class ExpFloat(Exp):
|
||||
def __init__(self, value: int):
|
||||
self.value = value
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"exp_float({self.value})"
|
||||
|
||||
def eval(self, vtable, ftable):
|
||||
return vtable, ftable, self.value
|
||||
|
||||
class ExpBool(Exp):
|
||||
def __init__(self, value: int):
|
||||
self.value = value
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"exp_bool({self.value})"
|
||||
|
||||
def eval(self, vtable, ftable):
|
||||
return vtable, ftable, self.value
|
||||
|
||||
class ExpString(Exp):
|
||||
def __init__(self, value: str):
|
||||
self.value = value
|
||||
@ -67,23 +90,11 @@ class ExpABinop(Exp):
|
||||
|
||||
def eval(self, vtable, ftable):
|
||||
vtable, ftable, r1 = self.exp1.eval(vtable, ftable)
|
||||
if isinstance(r1,str):
|
||||
if r1.isdigit():
|
||||
r1 = int(r1)
|
||||
else:
|
||||
try:
|
||||
r1 = float(r1)
|
||||
except ValueError:
|
||||
pass
|
||||
vtable, ftable, r2 = self.exp2.eval(vtable, ftable)
|
||||
if isinstance(r2,str):
|
||||
if r2.isdigit():
|
||||
r2 = int(r2)
|
||||
else:
|
||||
try:
|
||||
r2 = float(r2)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if not (type(r1) is type(r2) or (type(r1) is float and type(r2) is int) or (type(r1) is int and type(r2) is float)):
|
||||
print(f"C003: Incorrect type pairing {type(r1)} and {type(r2)} for {self.op}")
|
||||
|
||||
if self.op == "+":
|
||||
return vtable, ftable, r1+r2
|
||||
elif self.op == "-":
|
||||
@ -175,7 +186,14 @@ class Builtin(Command):
|
||||
prints = []
|
||||
for arg in self.args:
|
||||
vtable, ftable, result = arg.eval(vtable, ftable)
|
||||
prints.append(str(result))
|
||||
if isinstance(result, str) or isinstance(result, int) or isinstance(result, float):
|
||||
prints.append(str(result))
|
||||
elif isinstance(result, list):
|
||||
result = f"{'{'}{''.join([str(i)+";" for i in result])}{'}'}"
|
||||
prints.append(str(result))
|
||||
else:
|
||||
print(f"C003: Incorrect type {type(result)} for builtin print")
|
||||
exit()
|
||||
|
||||
print(" ".join(prints))
|
||||
|
||||
@ -204,6 +222,10 @@ class Builtin(Command):
|
||||
vtable, ftable, result = self.args[0].eval(vtable,ftable)
|
||||
|
||||
return vtable, ftable, len(result)
|
||||
elif self.builtin == "int":
|
||||
vtable, ftable, result = self.args[0].eval(vtable,ftable)
|
||||
|
||||
return vtable, ftable, int(result)
|
||||
else:
|
||||
raise Exception(f"Unknown builtin {self.builtin}")
|
||||
|
||||
@ -281,7 +303,6 @@ class Scope(Statement):
|
||||
vtable, ftable, _ = statement.eval(vtable, ftable)
|
||||
if "#return" in vtable:
|
||||
result = vtable["#return"]
|
||||
del vtable["#return"]
|
||||
break
|
||||
|
||||
return vtable, ftable, result
|
||||
@ -417,6 +438,12 @@ class Program(BaseBox):
|
||||
ftable = {}
|
||||
|
||||
for statement in self.statements:
|
||||
vtable, ftable, _ = statement.eval(vtable, ftable)
|
||||
try:
|
||||
vtable, ftable, _ = statement.eval(vtable, ftable)
|
||||
except Exception as e:
|
||||
print(f"C002: Unknown error in statement {statement}")
|
||||
raise e
|
||||
if "#return" in vtable:
|
||||
break
|
||||
del vtable["#return"]
|
||||
if not isinstance(vtable,Scope):
|
||||
break
|
||||
|
Reference in New Issue
Block a user