add list functions to stdlib
This commit is contained in:
parent
49ea765e40
commit
b01415d786
4 changed files with 146 additions and 12 deletions
67
stdlib/list.go
Normal file
67
stdlib/list.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/* 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 (
|
||||
"git.callpipe.com/aidan/shs/ast"
|
||||
"git.callpipe.com/aidan/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 {
|
||||
if input.Tag != ast.LIST {
|
||||
log.Log(log.DEBUG, "expand called on not a list", "expand")
|
||||
return input
|
||||
}
|
||||
|
||||
return input.Inner.(*ast.Token)
|
||||
}
|
||||
|
||||
/* 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 {
|
||||
src := input
|
||||
|
||||
if input.Tag != ast.LIST {
|
||||
// TODO: Position, if I can figure out what to do with it
|
||||
src = &ast.Token{Tag: ast.LIST, Inner: input}
|
||||
return src
|
||||
}
|
||||
|
||||
// deref inner first
|
||||
i := src.Inner.(*ast.Token)
|
||||
iter := &i
|
||||
if *iter == nil {
|
||||
src.Inner = input.Next
|
||||
return src
|
||||
}
|
||||
|
||||
for (*iter).Next != nil {
|
||||
iter = &((*iter).Next)
|
||||
}
|
||||
(*iter).Next = input.Next
|
||||
|
||||
return src
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue