add input function

This commit is contained in:
Aidan 2020-07-02 16:24:34 -07:00
parent c253dc6375
commit a5f157dbd7
No known key found for this signature in database
GPG key ID: 327711E983899316

View file

@ -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
}