added type casts

This commit is contained in:
Aidan 2020-07-18 00:22:43 -07:00
parent 77ce00970f
commit 8278430882
No known key found for this signature in database
GPG key ID: 327711E983899316
4 changed files with 72 additions and 1 deletions

View file

@ -29,6 +29,27 @@ import (
// perhaps we simply write out arithmetic routines that operate on the strings
// then we need not worry about storage length.
func num_cast(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
in = in.Eval(f, a, false)
if in.Tag != ast.STRING {
log.Log(log.ERR,
"only a string can successfully be cast to a number",
"number_cast")
return nil
}
if !ast.StrIsNumber(in.Value()) {
log.Log(log.ERR,
"string failed number cast",
"number_cast")
return nil
}
out := in.Copy()
out.Tag = ast.NUMBER
return out
}
func add(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
var res float64

View file

@ -23,6 +23,28 @@ import (
"gitlab.com/whom/shs/ast"
)
func bool_cast(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
in = in.Eval(ft, vt, false)
if in.Tag == ast.LIST || in.Tag == ast.NUMBER {
log.Log(log.ERR,
"only strings successfully cast to bool",
"bool cast")
return nil
}
body := in.Value()
if body != ast.TRUE && body != ast.FALSE {
log.Log(log.ERR,
"cast to bool failed",
"bool cast")
return nil
}
res := &ast.Token{ Tag: ast.BOOL }
res.Set(body)
return res
}
func not(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
in = in.Eval(ft, vt, false)

View file

@ -77,6 +77,27 @@ func GenFuncTable() ast.FuncTable {
Args: 1,
},
"bool": &ast.Function{
Function: bool_cast,
Name: "bool",
TimesCalled: 0,
Args: 1,
},
"string": &ast.Function{
Function: str_cast,
Name: "string",
TimesCalled: 0,
Args: 1,
},
"number": &ast.Function{
Function: num_cast,
Name: "number",
TimesCalled: 0,
Args: 1,
},
"...": &ast.Function{
Function: expand,
Name: "...",

View file

@ -43,6 +43,13 @@ func concat(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
return t
}
func str_cast(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
body := in.Eval(ft, vt, false).String()
res := &ast.Token{ Tag: ast.STRING }
res.Set(body)
return res
}
func print_str(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
body := in.Eval(ft, vt, false).String()
if body[0] != body[len(body)-1] && body[0] != '"' {