add input function
This commit is contained in:
parent
c253dc6375
commit
a5f157dbd7
1 changed files with 32 additions and 4 deletions
|
|
@ -20,34 +20,42 @@ package stdlib
|
|||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"gitlab.com/whom/shs/log"
|
||||
"gitlab.com/whom/shs/ast"
|
||||
)
|
||||
|
||||
func GenFuncTable() ast.FuncTable {
|
||||
var stdlib ast.FuncTable
|
||||
stdlib = &map[string]*ast.Function{
|
||||
"if": &ast.Function{
|
||||
"if": &ast.Function{
|
||||
Function: shs_if,
|
||||
Name: "if",
|
||||
TimesCalled: 0,
|
||||
Args: 3,
|
||||
},
|
||||
|
||||
"while": &ast.Function{
|
||||
"while": &ast.Function{
|
||||
Function: shs_while,
|
||||
Name: "while",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"eval": &ast.Function{
|
||||
"eval": &ast.Function{
|
||||
Function: eval,
|
||||
Name: "eval",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"...": &ast.Function{
|
||||
"input": &ast.Function{
|
||||
Function: input,
|
||||
Name: "input",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"...": &ast.Function{
|
||||
Function: expand,
|
||||
Name: "...",
|
||||
TimesCalled: 0,
|
||||
|
|
@ -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 {
|
||||
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