add input function
This commit is contained in:
parent
c253dc6375
commit
a5f157dbd7
1 changed files with 32 additions and 4 deletions
|
|
@ -20,6 +20,7 @@ package stdlib
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gitlab.com/whom/shs/log"
|
||||||
"gitlab.com/whom/shs/ast"
|
"gitlab.com/whom/shs/ast"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -47,6 +48,13 @@ func GenFuncTable() ast.FuncTable {
|
||||||
Args: -1,
|
Args: -1,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"input": &ast.Function{
|
||||||
|
Function: input,
|
||||||
|
Name: "input",
|
||||||
|
TimesCalled: 0,
|
||||||
|
Args: 1,
|
||||||
|
},
|
||||||
|
|
||||||
"...": &ast.Function{
|
"...": &ast.Function{
|
||||||
Function: expand,
|
Function: expand,
|
||||||
Name: "...",
|
Name: "...",
|
||||||
|
|
@ -267,3 +275,23 @@ func sh_info(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||||
func eval(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
func eval(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||||
return in.Eval(ft, vt, false)
|
return in.Eval(ft, vt, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func input(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||||
|
in = in.Eval(ft, vt, false)
|
||||||
|
if in.Tag != ast.STRING && in.Tag != ast.NUMBER {
|
||||||
|
log.Log(log.ERR,
|
||||||
|
"argument to input must be a string or number",
|
||||||
|
"input")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
prompt := in.Value()
|
||||||
|
var output string
|
||||||
|
|
||||||
|
fmt.Printf(prompt)
|
||||||
|
fmt.Scanln(&output)
|
||||||
|
|
||||||
|
ret := &ast.Token{Tag: ast.STRING}
|
||||||
|
ret.Set(output)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue