mirror of
https://gitlab.com/niansa/pilang.git
synced 2025-03-06 20:48:26 +01:00
Added experimental builtin.getargv
This commit is contained in:
parent
e96778a7c7
commit
12fc3cc125
4 changed files with 59 additions and 4 deletions
9
cli.py
9
cli.py
|
@ -42,6 +42,8 @@ if len(sys.argv) == 1: # CLI loop
|
|||
res = main.get_rstring(main.run_command(commandstr), errorsonly=False)
|
||||
except goto:
|
||||
res = "Goto is not possible in CLI mode"
|
||||
except RuntimeError as error:
|
||||
res = error.args[0]
|
||||
if res != None:
|
||||
print(res)
|
||||
else: # File execution loop
|
||||
|
@ -52,8 +54,10 @@ else: # File execution loop
|
|||
del sys.argv[1]
|
||||
if len(sys.argv[1]) == 1:
|
||||
sys.exit(1)
|
||||
scriptfile = sys.argv[1]
|
||||
del sys.argv[0]
|
||||
# Save file line-by-line into list
|
||||
with open(sys.argv[1], mode="r") as f:
|
||||
with open(scriptfile, mode="r") as f:
|
||||
lines = []
|
||||
for line in f:
|
||||
if line[-1] == "\n":
|
||||
|
@ -66,6 +70,9 @@ else: # File execution loop
|
|||
try:
|
||||
cmdres = main.get_rstring(main.run_command(lines[linenum]), errorsonly=True)
|
||||
linenum += 1
|
||||
except RuntimeError as error:
|
||||
print(f"In line {linenum + 1}:", error.args[0])
|
||||
sys.exit(1)
|
||||
except goto as gotoinstr:
|
||||
if gotoinstr.define:
|
||||
markers[gotoinstr.marker] = linenum + 1
|
||||
|
|
34
examples/gettest2.pil
Normal file
34
examples/gettest2.pil
Normal file
|
@ -0,0 +1,34 @@
|
|||
#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/>.
|
||||
|
||||
catch True
|
||||
# Get passed arguments as concenated string
|
||||
set argslen,$len $getargv
|
||||
set counter,1
|
||||
set userin,
|
||||
marker set,concatloop
|
||||
append userin,$get "$getargv,$~counter"," "
|
||||
inc counter
|
||||
cmp smaller,$~counter,$~argslen,"marker goto,concatloop"
|
||||
|
||||
# Print character for character slowly
|
||||
set charnum,0
|
||||
marker set,printloop
|
||||
sleep 0.1
|
||||
printnnl $get "$~userin,$~charnum"
|
||||
inc charnum
|
||||
cmp bigger,$len $~userin,$~charnum,"marker goto,printloop"
|
||||
|
||||
print
|
|
@ -62,8 +62,7 @@ class main_class:
|
|||
finally:
|
||||
if environment.catch and iserr:
|
||||
environment.catch = False
|
||||
print("An exception was catched:", self.get_rstring(robj))
|
||||
sys.exit(1)
|
||||
raise RuntimeError(self.get_rstring(robj))
|
||||
|
||||
def import_module(self, path):
|
||||
with open(path) as file:
|
||||
|
@ -140,4 +139,5 @@ class main_class:
|
|||
else:
|
||||
pass
|
||||
# Run command
|
||||
#print([command] + [args])
|
||||
return module.processor(command, args)
|
||||
|
|
|
@ -27,11 +27,13 @@ class builtin:
|
|||
"len": self.cmd_len,
|
||||
"catch": self.cmd_catch,
|
||||
"exit": self.cmd_exit,
|
||||
"getargv": self.cmd_getargv,
|
||||
"marker": self.cmd_marker,
|
||||
"printnnl": self.cmd_printnnl,
|
||||
"print": self.cmd_print,
|
||||
"input": self.cmd_input,
|
||||
"set": self.cmd_set,
|
||||
"append": self.cmd_append,
|
||||
"inc": self.cmd_inc,
|
||||
"dec": self.cmd_dec,
|
||||
"del": self.cmd_del,
|
||||
|
@ -83,6 +85,9 @@ class builtin:
|
|||
except IndexError:
|
||||
return errors.badarguments
|
||||
|
||||
def cmd_getargv(self, args):
|
||||
return sys.argv
|
||||
|
||||
def cmd_marker(self, args):
|
||||
if len(args) != 2:
|
||||
return errors.badarguments
|
||||
|
@ -117,6 +122,15 @@ class builtin:
|
|||
self.environ.variables[args[0]] = args[1]
|
||||
return rtypes.success
|
||||
|
||||
def cmd_append(self, args):
|
||||
if len(args) < 2:
|
||||
return errors.badarguments
|
||||
if not str(args[0]) in self.environ.variables:
|
||||
return errors.nosuchvariable
|
||||
for arg in args[1:]:
|
||||
self.environ.variables[str(args[0])] += str(arg)
|
||||
return rtypes.success
|
||||
|
||||
def cmd_inc(self, args):
|
||||
if len(args) == 1:
|
||||
args.append(1)
|
||||
|
@ -125,7 +139,7 @@ class builtin:
|
|||
try:
|
||||
self.environ.variables[args[0]] = str(float(self.environ.variables[args[0]]) + float(args[1]))
|
||||
except KeyError:
|
||||
return errors.badarguments
|
||||
return errors.nosuchvariable
|
||||
return rtypes.success
|
||||
|
||||
def cmd_dec(self, args):
|
||||
|
|
Loading…
Add table
Reference in a new issue