add list functions to stdlib
This commit is contained in:
parent
49ea765e40
commit
b01415d786
4 changed files with 146 additions and 12 deletions
|
|
@ -22,22 +22,22 @@ import "git.callpipe.com/aidan/shs/log"
|
||||||
type Operation func(*Token, VarTable, FuncTable) *Token
|
type Operation func(*Token, VarTable, FuncTable) *Token
|
||||||
|
|
||||||
type Function struct {
|
type Function struct {
|
||||||
function Operation
|
Function Operation
|
||||||
name string
|
Name string
|
||||||
timesCalled int
|
TimesCalled int
|
||||||
args int // TODO: Make this a list of expected types (TAGs)
|
Args int // TODO: Make this a list of expected types (TAGs)
|
||||||
}
|
}
|
||||||
|
|
||||||
type FuncTable map[string]*Function
|
type FuncTable map[string]*Function
|
||||||
|
|
||||||
// TODO: Currently only checks arg list length
|
// TODO: Currently only checks arg list length
|
||||||
func (f Function) ParseFunction(args *Token) bool {
|
func (f Function) ParseFunction(args *Token) bool {
|
||||||
// HANDLE EXEC
|
// handle infinite args
|
||||||
if f.args < 0 {
|
if f.Args < 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
i := f.args
|
i := f.Args
|
||||||
for iter := args; iter != nil; iter = iter.Next {
|
for iter := args; iter != nil; iter = iter.Next {
|
||||||
i -= 1
|
i -= 1
|
||||||
}
|
}
|
||||||
|
|
@ -55,13 +55,13 @@ func (f Function) ParseFunction(args *Token) bool {
|
||||||
func (f Function) CallFunction(args *Token, vt VarTable, ft FuncTable) *Token {
|
func (f Function) CallFunction(args *Token, vt VarTable, ft FuncTable) *Token {
|
||||||
if !f.ParseFunction(args) {
|
if !f.ParseFunction(args) {
|
||||||
log.Log(log.ERR,
|
log.Log(log.ERR,
|
||||||
"Couldnt call " + f.name,
|
"Couldnt call " + f.Name,
|
||||||
"eval")
|
"eval")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
f.timesCalled += 1
|
f.TimesCalled += 1
|
||||||
return f.function(args, vt, ft)
|
return f.Function(args, vt, ft)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (table FuncTable) GetFunction(arg string) *Function {
|
func (table FuncTable) GetFunction(arg string) *Function {
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ type Function struct {
|
||||||
function Operation
|
function Operation
|
||||||
name string
|
name string
|
||||||
timesCalled int
|
timesCalled int
|
||||||
args int // TODO: Make this a list of expected types (TAGs)
|
args int
|
||||||
}
|
}
|
||||||
|
|
||||||
type FuncTable map[string]*Function
|
type FuncTable map[string]*Function
|
||||||
|
|
@ -31,6 +31,12 @@ The standard library is loaded during the init step of the repl (or interpreter
|
||||||
2. *Create a `Function` to encapsulate your `Operation`.* Make sure to set the `args` and `name` fields. Args will be used to validate function calls and Name will be used in debug/log output.
|
2. *Create a `Function` to encapsulate your `Operation`.* Make sure to set the `args` and `name` fields. Args will be used to validate function calls and Name will be used in debug/log output.
|
||||||
3. *Add your `Function` to the `FuncTable`.* Make sure your `Operations`s get added to the table generated in `GenFuncTable`.
|
3. *Add your `Function` to the `FuncTable`.* Make sure your `Operations`s get added to the table generated in `GenFuncTable`.
|
||||||
|
|
||||||
|
## Tips
|
||||||
|
- You can use the Log module to add helpful output to your functions.
|
||||||
|
- Try not to clutter the output with unnessesary ERR level logging.
|
||||||
|
- Make sure you properly set the Next element of any Token you swap into a list
|
||||||
|
- Make sure you properly set the Next element of the previous Token as you swap a token into a list
|
||||||
|
|
||||||
## License
|
## License
|
||||||
Copyright (C) 2019 Aidan Hahn.
|
Copyright (C) 2019 Aidan Hahn.
|
||||||
|
|
||||||
|
|
|
||||||
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
|
||||||
|
}
|
||||||
|
|
@ -23,6 +23,67 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenFuncTable() ast.FuncTable {
|
func GenFuncTable() ast.FuncTable {
|
||||||
var stdlib FuncTable
|
var stdlib = ast.FuncTable{
|
||||||
|
"...": &ast.Function{
|
||||||
|
Function: expand,
|
||||||
|
Name: "...",
|
||||||
|
TimesCalled: 0,
|
||||||
|
Args: 1,
|
||||||
|
},
|
||||||
|
|
||||||
|
"append": &ast.Function{
|
||||||
|
Function: l_append,
|
||||||
|
Name: "append",
|
||||||
|
TimesCalled: 0,
|
||||||
|
Args: -1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
return stdlib
|
return stdlib
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: put list funcs in their own file
|
||||||
|
|
||||||
|
/* 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