stdlib Call, fg, bg, $, and cd functions. wow

This commit is contained in:
Aidan 2020-06-27 20:31:49 -07:00
parent 94c9b2beba
commit af184d9176
No known key found for this signature in database
GPG key ID: 327711E983899316
6 changed files with 272 additions and 33 deletions

View file

@ -24,8 +24,6 @@ func (t *Token) Eval(funcs FuncTable, vars VarTable) (*Token, bool) {
return nil, false
}
eligibleForSystemCall := true
var reduce func(*Token) *Token
reduce = func(t_ *Token) *Token {
var unwrap bool
@ -39,21 +37,11 @@ func (t *Token) Eval(funcs FuncTable, vars VarTable) (*Token, bool) {
maybeToken := GetVar(t_.Inner.(string), vars)
if maybeToken != nil {
tok, _ := maybeToken.Eval(funcs, vars)
// Should already be unwrapped
//if unwrap {
// tok = tok.Inner.(*Token)
//}
if tok.Tag == LIST {
eligibleForSystemCall = false
}
tok.Next = t_.Next
return tok
}
case LIST:
eligibleForSystemCall = false
t_.Inner, unwrap = t_.Inner.(*Token).Eval(funcs, vars)
if unwrap {
next := t_.Next
@ -84,15 +72,10 @@ func (t *Token) Eval(funcs FuncTable, vars VarTable) (*Token, bool) {
if ret.Tag == SYMBOL {
f := GetFunction(ret.Inner.(string), funcs)
if f == nil {
if !eligibleForSystemCall {
log.Log(log.DEBUG,
"could not find definition for symbol " + ret.Inner.(string),
"eval")
return nil, false
}
// TODO: hook into stdlib exec
return nil, false // TODO: Thats gotta change
log.Log(log.DEBUG,
"could not find definition for symbol " + ret.Inner.(string),
"eval")
return nil, false
}
return (*f).CallFunction(ret.Next, vars, funcs), true

View file

@ -109,12 +109,16 @@ func main() {
}
result, unwrap := userInput.Eval(funcs, vars)
if unwrap {
result = result.Inner.(*ast.Token)
}
for i := result; i != nil; i = i.Next {
fmt.Printf(i.String() + " ")
if result != nil {
if result.Tag == ast.LIST && unwrap {
result = result.Inner.(*ast.Token)
}
for i := result; i != nil; i = i.Next {
fmt.Printf(i.String() + " ")
}
}
fmt.Printf("\n")
}
}

171
stdlib/call.go Normal file
View file

@ -0,0 +1,171 @@
/* 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/>.
*/
package stdlib
import (
"os"
"fmt"
"bytes"
"os/exec"
"git.callpipe.com/aidan/shs/ast"
"git.callpipe.com/aidan/shs/log"
)
var bgProcs = make([]*exec.Cmd, 0)
func call(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
if in == nil {
return nil
}
path, err := exec.LookPath(in.Inner.(string))
if err != nil {
log.Log(log.ERR, "Couldnt exec " + in.Inner.(string) + ", file not found", "call")
return nil
}
args := []string{}
for i := in.Next; i != nil; i = i.Next {
if i.Tag == ast.LIST {
log.Log(log.ERR, "Couldnt exec " + path + ", element in arguments is a list", "call")
return nil
}
args = append(args, i.Inner.(string))
}
var cmd *exec.Cmd
if len(args) > 0 {
cmd = exec.Command(path, args...)
} else {
cmd = exec.Command(path)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
exit := 0
err = cmd.Run()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
exit = exitError.ExitCode()
} else {
log.Log(log.ERR, "Execution step returned unparsable error: " + err.Error(), "call")
}
}
return &ast.Token{Tag: ast.NUMBER, Inner: fmt.Sprintf("%d", exit)}
}
func bgcall(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
if in == nil {
return nil
}
path, err := exec.LookPath(in.Inner.(string))
if err != nil {
log.Log(log.ERR, "Couldnt exec " + in.Inner.(string) + ", file not found", "call")
return nil
}
args := []string{}
for i := in.Next; i != nil; i = i.Next {
if i.Tag == ast.LIST {
log.Log(log.ERR, "Couldnt exec " + path + ", element in arguments is a list", "call")
return nil
}
args = append(args, i.Inner.(string))
}
var cmd *exec.Cmd
if len(args) > 0 {
cmd = exec.Command(path, args...)
} else {
cmd = exec.Command(path)
}
cmd.Stderr = os.Stderr
bgProcs = append(bgProcs, cmd)
cmd.Start()
return nil
}
func fg(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
if len(bgProcs) < 1 {
return nil
}
cmd := bgProcs[0]
bgProcs = bgProcs[1:]
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
exit := 0
err := cmd.Wait()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
exit = exitError.ExitCode()
} else {
log.Log(log.ERR, "Execution step returned unparsable error: " + err.Error(), "call")
}
}
return &ast.Token{Tag: ast.NUMBER, Inner: fmt.Sprintf("%d", exit)}
}
func read_cmd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
if in == nil {
return nil
}
var out bytes.Buffer
path, err := exec.LookPath(in.Inner.(string))
if err != nil {
log.Log(log.ERR, "Couldnt exec " + in.Inner.(string) + ", file not found", "call")
return nil
}
args := []string{}
for i := in.Next; i != nil; i = i.Next {
if i.Tag == ast.LIST {
log.Log(log.ERR, "Couldnt exec " + path + ", element in arguments is a list", "call")
return nil
}
args = append(args, i.Inner.(string))
}
var cmd *exec.Cmd
if len(args) > 0 {
cmd = exec.Command(path, args...)
} else {
cmd = exec.Command(path)
}
cmd.Stdout = &out
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
if err != nil {
log.Log(log.ERR, err.Error(), "$")
}
return &ast.Token{Tag: ast.STRING, Inner: out.String()}
}

37
stdlib/filesys.go Normal file
View file

@ -0,0 +1,37 @@
/* 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/>.
*/
package stdlib
import (
"os"
"git.callpipe.com/aidan/shs/ast"
"git.callpipe.com/aidan/shs/log"
)
func cd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
if in.Tag == ast.LIST {
log.Log(log.ERR, "Couldnt change dir to a list", "cd")
return nil
}
err := os.Chdir(in.Inner.(string))
if err != nil {
log.Log(log.ERR, err.Error(), "cd")
}
return nil
}

View file

@ -54,14 +54,16 @@ func l_append(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Tok
iter := &i
if *iter == nil {
src.Inner = input.Next
return src
}
src.Next = nil
for (*iter).Next != nil {
iter = &((*iter).Next)
} else {
for (*iter).Next != nil {
iter = &((*iter).Next)
}
(*iter).Next = input.Next
input.Next = nil
}
(*iter).Next = input.Next
input.Next = nil
return src
}

View file

@ -59,12 +59,54 @@ func GenFuncTable() ast.FuncTable {
Args: -1,
},
"div": &ast.Function{
"/": &ast.Function{
Function: div,
Name: "div",
TimesCalled: 0,
Args: -1,
},
"l": &ast.Function{
Function: call,
Name: "call",
TimesCalled: 0,
Args: -1,
},
"bg": &ast.Function{
Function: bgcall,
Name: "background call",
TimesCalled: 0,
Args: -1,
},
"fg": &ast.Function{
Function: fg,
Name: "foreground",
TimesCalled: 0,
Args: 0,
},
"cd": &ast.Function{
Function: cd,
Name: "changedir",
TimesCalled: 0,
Args: 1,
},
"$": &ast.Function{
Function: read_cmd,
Name: "read cmd",
TimesCalled: 0,
Args: -1,
},
"concat": &ast.Function{
Function: concat,
Name:"concatenate",
TimesCalled: 0,
Args: -1,
},
}
return stdlib