1
0
Fork 0
mirror of https://gitlab.com/niansa/pilang.git synced 2025-03-06 20:48:26 +01:00
pilang/modules/builtin.py

138 lines
4.1 KiB
Python

"""
This file is part of pilang.
pilang is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pilang is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pilang. If not, see <https://www.gnu.org/licenses/>.
"""
import sys
import errors
import rtypes
class builtin:
def __init__(self, environ):
self.commands = {
"ret": self.cmd_ret,
"catch": self.cmd_catch,
"exit": self.cmd_exit,
"print": self.cmd_print,
"input": self.cmd_input,
"set": self.cmd_set,
"del": self.cmd_del,
"eval": self.cmd_eval,
"cmp": self.cmd_cmp,
"lower": self.cmd_lower
}
self.environ = environ
def processor(self, command, args):
try:
return self.commands[command](args)
except KeyError:
return errors.nochsuchcmd
def cmd_ret(self, args):
return " ".join(args)
def cmd_catch(self, args):
if args == []:
self.environ.catch = not self.environ.catch
else:
if args[0].lower() == "true":
self.environ.catch = True
elif args[0].lower() == "false":
self.environ.catch = False
else:
return errors.badarguments
def cmd_exit(self, args):
try:
sys.exit(int(args[0]))
except ValueError:
return errors.badarguments
except IndexError:
return errors.badarguments
def cmd_print(self, args, nonline=False):
for arg in args:
print(arg, end="")
if not nonline:
print()
return rtypes.success
def cmd_input(self, args):
self.cmd_print(args, nonline=True)
return input()
def cmd_set(self, args):
if len(args) != 2:
return errors.badarguments
self.environ.variables[args[0]] = args[1]
return rtypes.success
def cmd_del(self, args):
# Check if variable exists
for arg in args:
if not arg in self.environ.variables.keys():
return errors.nosuchvariable
# Actually do that stuff
for arg in args:
del self.environ.variables[arg]
return rtypes.success
def cmd_lower(self, args):
return str(args[0].lower())
def cmd_cmp(self, args):
# Check
try:
action, var1, var2, *commands = args
if action == "equal":
matches = str(var1) == str(var2)
elif action == "nonequal":
matches = str(var1) != str(var2)
elif action == "bigger":
matches = float(var1) > float(var2)
elif action == "smaller":
matches = float(var1) < float(var2)
elif action in ["isnum", "isnotnum"]:
try:
float(var1)
float(var2)
matches = True
except ValueError:
matches = False
if action == "isnotnum":
matches = not matches
else:
return errors.badarguments
if commands == []:
return matches
except ValueError:
return errors.badarguments
# Evaluate list of commands
if matches:
for command in commands:
lastres = self.environ.evaluate(command)
return lastres
else:
return rtypes.fail
def cmd_eval(self, args):
for command in args:
lastres = self.environ.evaluate(command)
return lastres
def adder(registerer):
registerer(builtin)