incomplete docs for stdlib

This commit is contained in:
Aidan 2020-07-19 14:53:27 -07:00
parent b941df68e1
commit e1913fea2c
No known key found for this signature in database
GPG key ID: 327711E983899316
2 changed files with 21 additions and 0 deletions

View file

@ -29,6 +29,10 @@ import (
// perhaps we simply write out arithmetic routines that operate on the strings // perhaps we simply write out arithmetic routines that operate on the strings
// then we need not worry about storage length. // then we need not worry about storage length.
/* Cast a string to a number
* Takes 1 argument
* returns nil if fails
*/
func num_cast(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { func num_cast(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
in = in.Eval(f, a, false) in = in.Eval(f, a, false)
if in.Tag != ast.STRING { if in.Tag != ast.STRING {
@ -50,6 +54,10 @@ func num_cast(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
return out return out
} }
/* adds N number arguments
* takes N arguments
* returns the sum, or nil if improper arguments were given
*/
func add(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { func add(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
var res float64 var res float64
@ -92,6 +100,9 @@ func add(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
return t return t
} }
/* subtract N args from the final arg
* takes N args, returns nil if improper args given
*/
func sub(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { func sub(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
var res float64 var res float64
var sub float64 var sub float64
@ -142,6 +153,9 @@ func sub(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
return t return t
} }
/* multiplies N arguments
* returns nil if an improper argument is given
*/
func mult(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { func mult(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
res := 1.0 res := 1.0
@ -184,6 +198,10 @@ func mult(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
return t return t
} }
/* divide N arguments
* the first argument is divided by each subsequent argument in order
* returns nil if an improper argument is given
*/
func div(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token { func div(in *ast.Token, a ast.VarTable, f ast.FuncTable) *ast.Token {
var res float64 var res float64

View file

@ -25,6 +25,9 @@ import (
"gitlab.com/whom/shs/util" "gitlab.com/whom/shs/util"
) )
/* Makes a FuncTable from all functions in the stdlib
* add your function here if you are extending the standard library
*/
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{