Variables: Added a readVariable and evalVariable procedures
This commit is contained in:
parent
c00678603b
commit
8a5e6c3cea
39
src/fsh.nim
39
src/fsh.nim
|
@ -5,9 +5,20 @@
|
||||||
# imports
|
# imports
|
||||||
import streams
|
import streams
|
||||||
import osproc
|
import osproc
|
||||||
|
import strtabs
|
||||||
|
import os
|
||||||
|
|
||||||
|
# globals
|
||||||
|
var shellVariables = newStringTable(modeCaseSensitive)
|
||||||
|
# Environment variables get defined in a separate var
|
||||||
|
var envVariables = newStringTable(modeCaseSensitive)
|
||||||
|
# populate the table
|
||||||
|
for k, v in envPairs():
|
||||||
|
envVariables[k]=v
|
||||||
|
|
||||||
# forward declarations
|
# forward declarations
|
||||||
proc eval(cmd: string): string
|
proc eval(cmd: string): string
|
||||||
|
proc substitute(strm: Stream, delim = ";\p"): string
|
||||||
|
|
||||||
# skipSpaces reads the stream until a non-space character is found
|
# skipSpaces reads the stream until a non-space character is found
|
||||||
proc skipSpaces(strm: Stream): int =
|
proc skipSpaces(strm: Stream): int =
|
||||||
|
@ -103,6 +114,34 @@ proc readCommand(strm: Stream): string =
|
||||||
of '\n', '\r', ';': break
|
of '\n', '\r', ';': break
|
||||||
else: result.add(c)
|
else: result.add(c)
|
||||||
|
|
||||||
|
# readVariable reads a variable
|
||||||
|
proc readVariable(strm: Stream): string =
|
||||||
|
let c = strm.readChar()
|
||||||
|
case c:
|
||||||
|
of '{':
|
||||||
|
result = strm.readLiteral()
|
||||||
|
of '[':
|
||||||
|
result.add(strm.readInterpelation)
|
||||||
|
of '"':
|
||||||
|
result.add(strm.readSubstitution())
|
||||||
|
of ']', '}':
|
||||||
|
stderr.write("Unexpected " & c & ".\n")
|
||||||
|
else:
|
||||||
|
result.add(c)
|
||||||
|
while not strm.atEnd():
|
||||||
|
let d = strm.readChar()
|
||||||
|
case d:
|
||||||
|
of 'a'..'z', 'A'..'Z', '0'..'9', '_', ':': result.add(d)
|
||||||
|
else: break
|
||||||
|
|
||||||
|
# evalVariable finds a value of a variable
|
||||||
|
proc evalVariable(vari: string): string =
|
||||||
|
if shellVariables.hasKey(vari):
|
||||||
|
result=shellVariables[vari]
|
||||||
|
elif envVariables.hasKey(vari):
|
||||||
|
result = envVariables[vari]
|
||||||
|
else: raise newException(Exception, "No such variable: " & vari & ".")
|
||||||
|
|
||||||
# substitute does a substitution on the stream passed in
|
# substitute does a substitution on the stream passed in
|
||||||
proc substitute(strm: Stream, delim=";\p"): string =
|
proc substitute(strm: Stream, delim=";\p"): string =
|
||||||
while not strm.atEnd():
|
while not strm.atEnd():
|
||||||
|
|
Loading…
Reference in New Issue