much stdlib docs
This commit is contained in:
parent
b941df68e1
commit
ee39de01fd
9 changed files with 228 additions and 74 deletions
112
stdlib/stdlib.go
112
stdlib/stdlib.go
|
|
@ -29,231 +29,231 @@ func GenFuncTable() ast.FuncTable {
|
|||
var stdlib ast.FuncTable
|
||||
stdlib = &map[string]*ast.Function{
|
||||
"if": &ast.Function{
|
||||
Function: shs_if,
|
||||
Function: ShsIf,
|
||||
Name: "if",
|
||||
TimesCalled: 0,
|
||||
Args: 3,
|
||||
},
|
||||
|
||||
"while": &ast.Function{
|
||||
Function: shs_while,
|
||||
Function: ShsWhile,
|
||||
Name: "while",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"progn": &ast.Function{
|
||||
Function: shs_progn,
|
||||
Function: ShsProgn,
|
||||
Name: "shs_progn",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"eval": &ast.Function{
|
||||
Function: eval,
|
||||
Function: Eval,
|
||||
Name: "eval",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"func": &ast.Function{
|
||||
Function: decl_func,
|
||||
Function: DeclFunc,
|
||||
Name: "decl_func",
|
||||
TimesCalled: 0,
|
||||
Args: 3,
|
||||
},
|
||||
|
||||
"export": &ast.Function{
|
||||
Function: export,
|
||||
Function: Export,
|
||||
Name: "export",
|
||||
TimesCalled: 0,
|
||||
Args: 2,
|
||||
},
|
||||
|
||||
"input": &ast.Function{
|
||||
Function: input,
|
||||
Function: Input,
|
||||
Name: "input",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"load": &ast.Function{
|
||||
Function: load,
|
||||
Function: Load,
|
||||
Name: "load",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"bool": &ast.Function{
|
||||
Function: bool_cast,
|
||||
Function: BoolCast,
|
||||
Name: "bool",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"string": &ast.Function{
|
||||
Function: str_cast,
|
||||
Function: StrCast,
|
||||
Name: "string",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"number": &ast.Function{
|
||||
Function: num_cast,
|
||||
Function: NumCast,
|
||||
Name: "number",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"...": &ast.Function{
|
||||
Function: expand,
|
||||
Function: Expand,
|
||||
Name: "...",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"append": &ast.Function{
|
||||
Function: l_append,
|
||||
Function: L_append,
|
||||
Name: "append",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"exit": &ast.Function{
|
||||
Function: exit_shell,
|
||||
Function: ExitShell,
|
||||
Name: "exit",
|
||||
TimesCalled: 0,
|
||||
Args: 0,
|
||||
},
|
||||
|
||||
"eq": &ast.Function{
|
||||
Function: eq,
|
||||
Function: Eq,
|
||||
Name: "==",
|
||||
TimesCalled: 0,
|
||||
Args: 2,
|
||||
},
|
||||
|
||||
"ne": &ast.Function{
|
||||
Function: ne,
|
||||
Function: Ne,
|
||||
Name: "!=",
|
||||
TimesCalled: 0,
|
||||
Args: 2,
|
||||
},
|
||||
|
||||
"<": &ast.Function{
|
||||
Function: lt,
|
||||
Function: Lt,
|
||||
Name: "<",
|
||||
TimesCalled: 0,
|
||||
Args: 2,
|
||||
},
|
||||
|
||||
">": &ast.Function{
|
||||
Function: gt,
|
||||
Function: Gt,
|
||||
Name: ">",
|
||||
TimesCalled: 0,
|
||||
Args: 2,
|
||||
},
|
||||
|
||||
"<=": &ast.Function{
|
||||
Function: lte,
|
||||
Function: Lte,
|
||||
Name: "<=",
|
||||
TimesCalled: 0,
|
||||
Args: 2,
|
||||
},
|
||||
|
||||
">=": &ast.Function{
|
||||
Function: gte,
|
||||
Function: Gte,
|
||||
Name: ">=",
|
||||
TimesCalled: 0,
|
||||
Args: 2,
|
||||
},
|
||||
|
||||
"!": &ast.Function{
|
||||
Function: not,
|
||||
Function: Not,
|
||||
Name: "!",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"+": &ast.Function{
|
||||
Function: add,
|
||||
Function: Add,
|
||||
Name: "add",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"-": &ast.Function{
|
||||
Function: sub,
|
||||
Function: Sub,
|
||||
Name: "sub",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"*": &ast.Function{
|
||||
Function: mult,
|
||||
Function: Mult,
|
||||
Name: "mult",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"/": &ast.Function{
|
||||
Function: div,
|
||||
Function: Div,
|
||||
Name: "div",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"cd": &ast.Function{
|
||||
Function: cd,
|
||||
Function: Cd,
|
||||
Name: "changedir",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"concat": &ast.Function{
|
||||
Function: concat,
|
||||
Function: Concat,
|
||||
Name:"concatenate",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"print": &ast.Function{
|
||||
Function:print_str,
|
||||
Function: PrintStr,
|
||||
Name: "print",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"l": &ast.Function{
|
||||
Function: call,
|
||||
Function: Call,
|
||||
Name: "call",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"bg": &ast.Function{
|
||||
Function: bgcall,
|
||||
Function: Bgcall,
|
||||
Name: "background call",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"fg": &ast.Function{
|
||||
Function: fg,
|
||||
Function: Fg,
|
||||
Name: "foreground",
|
||||
TimesCalled: 0,
|
||||
Args: 0,
|
||||
},
|
||||
|
||||
"$": &ast.Function{
|
||||
Function: read_cmd,
|
||||
Function: ReadCmd,
|
||||
Name: "read cmd",
|
||||
TimesCalled: 0,
|
||||
Args: -1,
|
||||
},
|
||||
|
||||
"?": &ast.Function{
|
||||
Function: get_exit,
|
||||
Function: GetExit,
|
||||
Name:"get exit code",
|
||||
TimesCalled: 0,
|
||||
Args: 0,
|
||||
|
|
@ -270,42 +270,42 @@ func GenFuncTable() ast.FuncTable {
|
|||
*/
|
||||
|
||||
"jobs": &ast.Function{
|
||||
Function: jobs,
|
||||
Function: Jobs,
|
||||
Name: "list jobs",
|
||||
TimesCalled: 0,
|
||||
Args: 0,
|
||||
},
|
||||
|
||||
"info": &ast.Function{
|
||||
Function: sh_info,
|
||||
Function: ShInfo,
|
||||
Name: "Shell Info",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"fexists": &ast.Function{
|
||||
Function: fexists,
|
||||
Function: Fexists,
|
||||
Name: "file exists",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"fread": &ast.Function{
|
||||
Function: fread,
|
||||
Function: Fread,
|
||||
Name: "read file",
|
||||
TimesCalled: 0,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"fwrite": &ast.Function{
|
||||
Function: fwrite,
|
||||
Function: Fwrite,
|
||||
Name: "write file",
|
||||
TimesCalled: 0,
|
||||
Args: 2,
|
||||
},
|
||||
|
||||
"fappend": &ast.Function{
|
||||
Function: fappend,
|
||||
Function: Fappend,
|
||||
Name:"append to file",
|
||||
TimesCalled: 0,
|
||||
Args: 2,
|
||||
|
|
@ -315,12 +315,22 @@ func GenFuncTable() ast.FuncTable {
|
|||
return stdlib
|
||||
}
|
||||
|
||||
func exit_shell(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
/* takes no args
|
||||
* exits shell when called
|
||||
*
|
||||
* Example: (exit)
|
||||
*/
|
||||
func ExitShell(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
os.Exit(0)
|
||||
return nil // I hope execution doesnt get here
|
||||
}
|
||||
|
||||
func sh_info(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
/* takes one arg, doesnt evaluate it
|
||||
* returns type or metadata
|
||||
*
|
||||
* Example: (info append)
|
||||
*/
|
||||
func ShInfo(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
switch in.Tag {
|
||||
case ast.BOOL:
|
||||
fmt.Printf("BOOL LITERAL\nValue: %s\n", in.Value())
|
||||
|
|
@ -350,11 +360,14 @@ func sh_info(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
|||
return nil
|
||||
}
|
||||
|
||||
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 {
|
||||
/* Takes 1 arg, uses it as a prompt
|
||||
* errs if prompt is not a string or number
|
||||
* gets a line from stdin
|
||||
* returns it as a string
|
||||
*
|
||||
* Example: (print (input))
|
||||
*/
|
||||
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,
|
||||
|
|
@ -374,7 +387,12 @@ func input(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
|||
return ret
|
||||
}
|
||||
|
||||
func load(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
/* Takes 1 arg, returns nil
|
||||
* if arg is a valid existing file than load will execute it as a script
|
||||
*
|
||||
* Example: (load "myscript.shs")
|
||||
*/
|
||||
func Load(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
in = in.Eval(ft, vt, true)
|
||||
if in.Tag != ast.STRING {
|
||||
log.Log(log.ERR,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue