2019-11-29 12:57:03 -08:00
|
|
|
/* SHS: Syntactically Homogeneous Shell
|
|
|
|
|
* Copyright (C) 2019 Aidan Hahn
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-21 11:11:57 -07:00
|
|
|
package stdlib
|
2020-06-17 00:28:16 -07:00
|
|
|
|
|
|
|
|
import (
|
2020-06-27 21:54:14 -07:00
|
|
|
"os"
|
2020-06-29 21:21:30 -07:00
|
|
|
"fmt"
|
2020-07-02 16:24:34 -07:00
|
|
|
"gitlab.com/whom/shs/log"
|
2020-06-28 13:47:44 -07:00
|
|
|
"gitlab.com/whom/shs/ast"
|
2020-07-15 18:41:54 -07:00
|
|
|
"gitlab.com/whom/shs/util"
|
2020-06-17 00:28:16 -07:00
|
|
|
)
|
2019-11-28 08:57:12 -08:00
|
|
|
|
2020-07-19 14:53:27 -07:00
|
|
|
/* Makes a FuncTable from all functions in the stdlib
|
|
|
|
|
* add your function here if you are extending the standard library
|
|
|
|
|
*/
|
2020-06-21 11:11:57 -07:00
|
|
|
func GenFuncTable() ast.FuncTable {
|
2020-06-21 12:46:25 -07:00
|
|
|
var stdlib ast.FuncTable
|
|
|
|
|
stdlib = &map[string]*ast.Function{
|
2020-07-02 16:24:34 -07:00
|
|
|
"if": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: ShsIf,
|
2020-06-30 20:27:12 -07:00
|
|
|
Name: "if",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 3,
|
|
|
|
|
},
|
|
|
|
|
|
2020-07-02 16:24:34 -07:00
|
|
|
"while": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: ShsWhile,
|
2020-06-30 20:27:12 -07:00
|
|
|
Name: "while",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: -1,
|
|
|
|
|
},
|
|
|
|
|
|
2020-07-18 10:44:18 -07:00
|
|
|
"progn": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: ShsProgn,
|
2020-07-18 10:44:18 -07:00
|
|
|
Name: "shs_progn",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: -1,
|
|
|
|
|
},
|
|
|
|
|
|
2020-07-02 16:24:34 -07:00
|
|
|
"eval": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Eval,
|
2020-06-30 20:27:12 -07:00
|
|
|
Name: "eval",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: -1,
|
|
|
|
|
},
|
|
|
|
|
|
2020-07-04 20:41:10 -07:00
|
|
|
"func": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: DeclFunc,
|
2020-07-04 20:41:10 -07:00
|
|
|
Name: "decl_func",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 3,
|
|
|
|
|
},
|
|
|
|
|
|
2020-07-03 16:27:02 -07:00
|
|
|
"export": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Export,
|
2020-07-03 16:27:02 -07:00
|
|
|
Name: "export",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 2,
|
|
|
|
|
},
|
|
|
|
|
|
2020-07-15 18:41:54 -07:00
|
|
|
"input": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Input,
|
2020-07-02 16:24:34 -07:00
|
|
|
Name: "input",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 1,
|
|
|
|
|
},
|
|
|
|
|
|
2020-07-15 18:41:54 -07:00
|
|
|
"load": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Load,
|
2020-07-15 18:41:54 -07:00
|
|
|
Name: "load",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 1,
|
|
|
|
|
},
|
|
|
|
|
|
2020-07-18 00:22:43 -07:00
|
|
|
"bool": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: BoolCast,
|
2020-07-18 00:22:43 -07:00
|
|
|
Name: "bool",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 1,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"string": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: StrCast,
|
2020-07-18 00:22:43 -07:00
|
|
|
Name: "string",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 1,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"number": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: NumCast,
|
2020-07-18 00:22:43 -07:00
|
|
|
Name: "number",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 1,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"...": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Expand,
|
2020-06-21 12:29:20 -07:00
|
|
|
Name: "...",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 1,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"append": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: L_append,
|
2020-06-21 12:29:20 -07:00
|
|
|
Name: "append",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: -1,
|
|
|
|
|
},
|
2020-06-26 20:17:45 -07:00
|
|
|
|
2020-06-27 21:27:33 -07:00
|
|
|
"exit": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: ExitShell,
|
2020-06-27 21:54:14 -07:00
|
|
|
Name: "exit",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 0,
|
2020-06-27 21:27:33 -07:00
|
|
|
},
|
2020-06-29 12:26:01 -07:00
|
|
|
|
|
|
|
|
"eq": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Eq,
|
2020-06-29 12:26:01 -07:00
|
|
|
Name: "==",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 2,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"ne": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Ne,
|
2020-06-29 12:26:01 -07:00
|
|
|
Name: "!=",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 2,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"<": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Lt,
|
2020-06-29 12:26:01 -07:00
|
|
|
Name: "<",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 2,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
">": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Gt,
|
2020-06-29 12:26:01 -07:00
|
|
|
Name: ">",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 2,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"<=": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Lte,
|
2020-06-29 12:26:01 -07:00
|
|
|
Name: "<=",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 2,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
">=": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Gte,
|
2020-06-29 12:26:01 -07:00
|
|
|
Name: ">=",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 2,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"!": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Not,
|
2020-06-29 12:26:01 -07:00
|
|
|
Name: "!",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 1,
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-29 12:51:41 -07:00
|
|
|
"+": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Add,
|
2020-06-29 12:51:41 -07:00
|
|
|
Name: "add",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: -1,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"-": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Sub,
|
2020-06-29 12:51:41 -07:00
|
|
|
Name: "sub",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: -1,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"*": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Mult,
|
2020-06-29 12:51:41 -07:00
|
|
|
Name: "mult",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: -1,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"/": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Div,
|
2020-06-29 12:51:41 -07:00
|
|
|
Name: "div",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: -1,
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-29 16:58:43 -07:00
|
|
|
"cd": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Cd,
|
2020-06-29 16:58:43 -07:00
|
|
|
Name: "changedir",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 1,
|
|
|
|
|
},
|
2020-06-29 17:06:27 -07:00
|
|
|
|
|
|
|
|
"concat": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Concat,
|
2020-06-29 17:06:27 -07:00
|
|
|
Name:"concatenate",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: -1,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"print": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: PrintStr,
|
2020-06-29 17:06:27 -07:00
|
|
|
Name: "print",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 1,
|
|
|
|
|
},
|
2020-06-29 19:15:00 -07:00
|
|
|
|
|
|
|
|
"l": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Call,
|
2020-06-29 19:15:00 -07:00
|
|
|
Name: "call",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: -1,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"bg": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Bgcall,
|
2020-06-29 19:15:00 -07:00
|
|
|
Name: "background call",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: -1,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"fg": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Fg,
|
2020-06-29 19:15:00 -07:00
|
|
|
Name: "foreground",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 0,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"$": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: ReadCmd,
|
2020-06-29 19:15:00 -07:00
|
|
|
Name: "read cmd",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: -1,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"?": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: GetExit,
|
2020-06-29 19:15:00 -07:00
|
|
|
Name:"get exit code",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 0,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
USE NATIVE KILL COMMAND.
|
|
|
|
|
"kill": &ast.Function{
|
|
|
|
|
Function: kill,
|
|
|
|
|
Name: "kill job",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 1,
|
|
|
|
|
},
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
"jobs": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Jobs,
|
2020-06-29 19:15:00 -07:00
|
|
|
Name: "list jobs",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 0,
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-29 21:21:30 -07:00
|
|
|
"info": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: ShInfo,
|
2020-06-29 21:21:30 -07:00
|
|
|
Name: "Shell Info",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 1,
|
|
|
|
|
},
|
2020-07-02 19:35:22 -07:00
|
|
|
|
|
|
|
|
"fexists": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Fexists,
|
2020-07-02 19:35:22 -07:00
|
|
|
Name: "file exists",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 1,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"fread": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Fread,
|
2020-07-02 19:35:22 -07:00
|
|
|
Name: "read file",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 1,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"fwrite": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Fwrite,
|
2020-07-02 19:35:22 -07:00
|
|
|
Name: "write file",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 2,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"fappend": &ast.Function{
|
2020-07-19 14:37:20 -07:00
|
|
|
Function: Fappend,
|
2020-07-02 19:35:22 -07:00
|
|
|
Name:"append to file",
|
|
|
|
|
TimesCalled: 0,
|
|
|
|
|
Args: 2,
|
|
|
|
|
},
|
2020-06-21 12:29:20 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-21 11:11:57 -07:00
|
|
|
return stdlib
|
2019-11-28 08:57:12 -08:00
|
|
|
}
|
2020-06-27 21:27:33 -07:00
|
|
|
|
2020-07-19 14:37:20 -07:00
|
|
|
/* takes no args
|
|
|
|
|
* exits shell when called
|
|
|
|
|
*
|
|
|
|
|
* Example: (exit)
|
|
|
|
|
*/
|
|
|
|
|
func ExitShell(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
2020-06-27 21:27:33 -07:00
|
|
|
os.Exit(0)
|
|
|
|
|
return nil // I hope execution doesnt get here
|
|
|
|
|
}
|
2020-06-29 21:21:30 -07:00
|
|
|
|
2020-07-19 14:37:20 -07:00
|
|
|
/* 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 {
|
2020-06-29 21:21:30 -07:00
|
|
|
switch in.Tag {
|
|
|
|
|
case ast.BOOL:
|
|
|
|
|
fmt.Printf("BOOL LITERAL\nValue: %s\n", in.Value())
|
|
|
|
|
case ast.STRING:
|
|
|
|
|
fmt.Printf("STRING LITERAL \nValue: %s\n", in.Value())
|
|
|
|
|
case ast.NUMBER:
|
|
|
|
|
fmt.Printf("NUMBER LITERAL \nValue: %s\n", in.Value())
|
|
|
|
|
case ast.LIST:
|
|
|
|
|
fmt.Printf("LIST \nString Value: %s, AST:\n", in.String())
|
|
|
|
|
ast.PrintSExprsIndividually(in)
|
|
|
|
|
case ast.SYMBOL:
|
|
|
|
|
repr := ast.GetVar(in.Value(), vt)
|
|
|
|
|
if repr != nil {
|
|
|
|
|
fmt.Printf("VARIABLE\nTYPE: %s\nVALUE: %s\n", ast.GetTagAsStr(repr.Tag), repr.Value())
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
funct := ast.GetFunction(in.Value(), ft)
|
|
|
|
|
if funct != nil {
|
|
|
|
|
fmt.Printf("FUNCTION\nNAME: %s\nTIMES CALLED: %s\nNUM ARGS: %d\n", funct.Name, funct.TimesCalled, funct.Args)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Printf("UNKNOWN SYMBOL\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-06-30 20:27:12 -07:00
|
|
|
|
2020-07-19 14:37:20 -07:00
|
|
|
/* 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 {
|
2020-07-02 16:24:34 -07:00
|
|
|
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
|
|
|
|
|
}
|
2020-07-15 18:41:54 -07:00
|
|
|
|
2020-07-19 14:37:20 -07:00
|
|
|
/* 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 {
|
2020-07-15 18:41:54 -07:00
|
|
|
in = in.Eval(ft, vt, true)
|
|
|
|
|
if in.Tag != ast.STRING {
|
|
|
|
|
log.Log(log.ERR,
|
|
|
|
|
"argument to load must be a string",
|
|
|
|
|
"load")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bp := in.Value()
|
|
|
|
|
bp = AbsPath(bp)
|
|
|
|
|
util.LoadScript(bp, vt, ft)
|
|
|
|
|
return nil
|
|
|
|
|
}
|