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