146 lines
3.6 KiB
Go
146 lines
3.6 KiB
Go
/* 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 (
|
|
"fmt"
|
|
"gitlab.com/whom/shs/ast"
|
|
"gitlab.com/whom/shs/log"
|
|
)
|
|
|
|
/* EXPAND
|
|
* Takes 1 element: a list
|
|
* retuns a sequence of elements (list contents)
|
|
* in event a not-list is passed in, returns the arg.
|
|
*/
|
|
func Expand(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
|
|
input = input.Eval(funcs, vars, false)
|
|
if input.Tag != ast.LIST {
|
|
log.Log(log.INFO, "expand called on not a list", "expand")
|
|
return input
|
|
}
|
|
|
|
return input.Eval(funcs, vars, false).Expand()
|
|
}
|
|
|
|
/* L_APPEND (append from repl)
|
|
* Appends N elements to the end of a list
|
|
* Arg one is a list, next args are appended
|
|
* if no args are a list, a list is made from all args
|
|
*/
|
|
func L_append(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
|
|
input = input.Eval(funcs, vars, false)
|
|
src := input
|
|
|
|
if input.Tag != ast.LIST {
|
|
r := &ast.Token{Tag: ast.LIST}
|
|
r.Direct(input)
|
|
return r
|
|
}
|
|
|
|
// deref inner first
|
|
i := src.Expand()
|
|
iter := &i
|
|
if *iter == nil {
|
|
src.Direct(input.Next)
|
|
src.Next = nil
|
|
|
|
} else {
|
|
for (*iter).Next != nil {
|
|
iter = &((*iter).Next)
|
|
}
|
|
|
|
(*iter).Next = input.Next
|
|
input.Next = nil
|
|
}
|
|
|
|
return src
|
|
}
|
|
|
|
/* Len
|
|
* Returns length of list as a number
|
|
* Returns nil if not a list
|
|
*
|
|
* Example: () -> 0
|
|
* Example: (1 2 3) -> 3
|
|
*/
|
|
func Len(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
|
|
input = input.Eval(funcs, vars, false)
|
|
if input.Tag != ast.LIST {
|
|
log.Log(log.ERR,
|
|
"non-list as parameter to head",
|
|
"head")
|
|
return nil
|
|
}
|
|
|
|
length := 0
|
|
for iter := input.Expand(); iter != nil; iter = iter.Next {
|
|
length += 1
|
|
}
|
|
|
|
ret := &ast.Token{Tag: ast.NUMBER}
|
|
ret.Set(fmt.Sprintf("%d", length))
|
|
return ret
|
|
}
|
|
|
|
/* Head
|
|
* Returns first element in the list
|
|
* Returns nil if input is not a list or if list is empty
|
|
*
|
|
* Example: (head (2 3 4)) -> 2
|
|
*/
|
|
func Head(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
|
|
input = input.Eval(funcs, vars, false)
|
|
if input.Tag != ast.LIST {
|
|
log.Log(log.ERR,
|
|
"non-list as parameter to head",
|
|
"head")
|
|
return nil
|
|
}
|
|
|
|
li := input.Expand().Copy()
|
|
if li != nil {
|
|
li.Next = nil
|
|
}
|
|
|
|
return li
|
|
}
|
|
|
|
/* Tail
|
|
* Returns last element in a list
|
|
* Returns nil if not a list
|
|
*
|
|
* Example: (tail (2 3 4)) -> 4
|
|
*/
|
|
func Tail(input *ast.Token, vars ast.VarTable, funcs ast.FuncTable) *ast.Token {
|
|
input = input.Eval(funcs, vars, false)
|
|
if input.Tag != ast.LIST {
|
|
log.Log(log.ERR,
|
|
"non-list as parameter to head",
|
|
"head")
|
|
return nil
|
|
}
|
|
|
|
|
|
iter := input.Expand()
|
|
for iter != nil && iter.Next != nil {
|
|
iter = iter.Next
|
|
}
|
|
|
|
return iter.Copy()
|
|
}
|