Variables: Added frames with shit performance

This commit is contained in:
Sekulum Forka 2021-05-17 16:58:14 +02:00
parent eff52bf920
commit 2a732eb569
1 changed files with 13 additions and 12 deletions

View File

@ -10,7 +10,8 @@ import os
import strutils import strutils
# globals # globals
var shellVariables = newStringTable(modeCaseSensitive) # shellVariables are an array of vars
var shellVariables = @[newStringTable(modeCaseSensitive)]
# Environment variables get defined in a separate var # Environment variables get defined in a separate var
var envVariables = newStringTable(modeCaseSensitive) var envVariables = newStringTable(modeCaseSensitive)
# populate the table # populate the table
@ -191,9 +192,9 @@ proc readVariable(strm: Stream): string =
# evalVariable finds a value of a variable # evalVariable finds a value of a variable
proc evalVariable(vari: string): string = proc evalVariable(vari: string): string =
if vari == "$": return "$" if vari == "$": return "$"
if shellVariables.hasKey(vari): for frame in shellVariables:
result=shellVariables[vari] if frame.hasKey(vari): return frame[vari]
elif envVariables.hasKey(vari): if envVariables.hasKey(vari):
result = envVariables[vari] result = envVariables[vari]
else: else:
raise newShellVariableException("No such variable", vari) raise newShellVariableException("No such variable", vari)
@ -252,7 +253,7 @@ proc runBuiltin(builtin: string, args: openArray[string]): (string, int) =
of "set": of "set":
if args.len < 2: if args.len < 2:
raise newShellCommandException("Not enough arguments provided", "set") raise newShellCommandException("Not enough arguments provided", "set")
shellVariables[args[0]] = args[1..^1].join(" ") shellVariables[0][args[0]] = args[1..^1].join(" ")
return ("", 0) return ("", 0)
of "setenv": of "setenv":
if args.len < 2: if args.len < 2:
@ -310,16 +311,16 @@ proc runBuiltin(builtin: string, args: openArray[string]): (string, int) =
of "cd": of "cd":
try: try:
if args.len == 0: if args.len == 0:
shellVariables["LASTPWD"] = getCurrentDir() shellVariables[shellVariables.high]["LASTPWD"] = getCurrentDir()
setCurrentDir(getHomeDir()) setCurrentDir(getHomeDir())
return ("", 0) return ("", 0)
else: else:
if args[0] == "-": if args[0] == "-":
let LASTPWD = getCurrentDir() let LASTPWD = getCurrentDir()
setCurrentDir(shellVariables["LASTPWD"]) setCurrentDir(shellVariables[shellVariables.high]["LASTPWD"])
shellVariables["LASTPWD"] = LASTPWD shellVariables[shellVariables.high]["LASTPWD"] = LASTPWD
return ("", 0) return ("", 0)
shellVariables["LASTPWD"] = getCurrentDir() shellVariables[shellVariables.high]["LASTPWD"] = getCurrentDir()
setCurrentDir(args[0]) setCurrentDir(args[0])
return ("", 0) return ("", 0)
except OsError as e: except OsError as e:
@ -374,12 +375,12 @@ when isMainModule:
setControlCHook(ctrlc) setControlCHook(ctrlc)
let stdinstrm = stdin.newFileStream let stdinstrm = stdin.newFileStream
# Set the default prompt # Set the default prompt
shellVariables["PROMPT"] = "$PWD \\$" shellVariables[shellVariables.high]["PROMPT"] = "$PWD \\$"
stdout.write(shellVariables["PROMPT"].newStringStream.substitute) stdout.write(shellVariables[shellVariables.high]["PROMPT"].newStringStream.substitute)
while not stdinstrm.atEnd(): while not stdinstrm.atEnd():
try: try:
discard stdinstrm.readCommand.execute discard stdinstrm.readCommand.execute
stdout.write(shellVariables["PROMPT"].newStringStream.substitute) stdout.write(shellVariables[shellVariables.high]["PROMPT"].newStringStream.substitute)
except ShellError, ShellCommandError, ShellVariableError: except ShellError, ShellCommandError, ShellVariableError:
let e = getCurrentException() let e = getCurrentException()
stderr.write(e.msg, "\n") stderr.write(e.msg, "\n")