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,34 +20,42 @@ package stdlib
import ( import (
"os" "os"
"fmt" "fmt"
"gitlab.com/whom/shs/log"
"gitlab.com/whom/shs/ast" "gitlab.com/whom/shs/ast"
) )
func GenFuncTable() ast.FuncTable { func GenFuncTable() ast.FuncTable {
var stdlib ast.FuncTable var stdlib ast.FuncTable
stdlib = &map[string]*ast.Function{ stdlib = &map[string]*ast.Function{
"if": &ast.Function{ "if": &ast.Function{
Function: shs_if, Function: shs_if,
Name: "if", Name: "if",
TimesCalled: 0, TimesCalled: 0,
Args: 3, Args: 3,
}, },
"while": &ast.Function{ "while": &ast.Function{
Function: shs_while, Function: shs_while,
Name: "while", Name: "while",
TimesCalled: 0, TimesCalled: 0,
Args: -1, Args: -1,
}, },
"eval": &ast.Function{ "eval": &ast.Function{
Function: eval, Function: eval,
Name: "eval", Name: "eval",
TimesCalled: 0, TimesCalled: 0,
Args: -1, Args: -1,
}, },
"...": &ast.Function{ "input": &ast.Function{
Function: input,
Name: "input",
TimesCalled: 0,
Args: 1,
},
"...": &ast.Function{
Function: expand, Function: expand,
Name: "...", Name: "...",
TimesCalled: 0, 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 { 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
}