From a5f157dbd70d437f76be34f128110ae54b3b0097 Mon Sep 17 00:00:00 2001 From: Aidan Date: Thu, 2 Jul 2020 16:24:34 -0700 Subject: [PATCH] add input function --- stdlib/stdlib.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/stdlib/stdlib.go b/stdlib/stdlib.go index eed2e2b..acb74ee 100644 --- a/stdlib/stdlib.go +++ b/stdlib/stdlib.go @@ -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 +}