new eval.go

This commit is contained in:
Aidan 2020-06-29 00:06:53 -07:00
parent 89d6a1013b
commit 2a2e5b4527
No known key found for this signature in database
GPG key ID: 327711E983899316
15 changed files with 382 additions and 1215 deletions

View file

@ -17,88 +17,96 @@
package ast
import "gitlab.com/whom/shs/log"
import (
"gitlab.com/whom/shs/log"
)
var CallExecutablesFromUndefFuncCalls = false
var CallExecutableToken = "l"
/* determines whether or not to execute a system call
* when a function cannot be found in the functable
* (use case: shell)
* ExecFunc determines the name of the system call function to fetch
*/
var ExecWhenFuncUndef = false
var ExecFunc = "l"
func (t *Token) Eval(funcs FuncTable, vars VarTable) (*Token, bool) {
if t == nil {
return nil, false
/* Runs through an AST of tokens
* Evaluates the Tokens to determine simplest form
* Returns simplest form
*
* canFunc determines whether a symbol could be a function to call
* (true when first elem of a list)
*/
func (in *Token) Eval(funcs FuncTable, vars VarTable) *Token {
if in == nil {
return nil
}
var reduce func(*Token) *Token
reduce = func(t_ *Token) *Token {
var unwrap bool
var res *Token
if t_.Next != nil {
t_.Next = reduce(t_.Next)
}
switch in.Tag {
case BOOL, NUMBER, STRING:
res = in
switch (t_.Tag) {
case SYMBOL:
maybeToken := GetVar(t_.Inner.(string), vars)
if maybeToken != nil {
tok, _ := maybeToken.Eval(funcs, vars)
tok.Next = t_.Next
return tok
}
case SYMBOL:
res = GetVar(in.Value(), vars)
if res == nil {
res = in
case LIST:
t_.Inner, unwrap = t_.Inner.(*Token).Eval(funcs, vars)
if unwrap {
next := t_.Next
t_ = t_.Inner.(*Token)
if t_ == nil {
log.Log(log.DEBUG, "nil Inner on list unwrap", "eval")
return nil
}
i := &t_
for (*i).Next != nil {
i = &((*i).Next)
}
(*i).Next = next
}
}
return t_
}
ret := reduce(t)
if ret == nil {
log.Log(log.INFO, "reduce returned nil", "eval")
return nil, false
}
//if symbol in front of a list, could be a function call
if ret.Tag == SYMBOL {
f := GetFunction(ret.Inner.(string), funcs)
if f == nil {
if CallExecutablesFromUndefFuncCalls {
f = GetFunction(CallExecutableToken, funcs)
if f == nil {
log.Log(log.DEBUG, "Symbol " + ret.Inner.(string) +
" has no definition in function table. Additionally " +
"the configured LoadExecutableToken is also not defined",
"eval")
return ret, false
}
// see the use of CallFunction below
ret = &Token{Next: ret}
} else {
log.Log(log.DEBUG,
"could not find definition for symbol " + ret.Inner.(string),
if GetFunction(in.Value(), funcs) == nil {
log.Log(log.ERR,
"undefined symbol: "+in.Value(),
"eval")
return ret, false
return nil
}
}
res.Next = in.Next
case LIST:
inner := in.Expand()
if inner == nil {
res = in
break
}
if inner.Tag != SYMBOL {
in.Direct(inner.Eval(funcs, vars))
res = in
break
}
makeHead := false
funct := GetFunction(inner.Value(), funcs)
if funct == nil {
if ExecWhenFuncUndef {
funct = GetFunction(ExecFunc, funcs)
makeHead = true
}
}
return (*f).CallFunction(ret.Next, vars, funcs), true
if funct != nil {
if makeHead {
inner = &Token{inner: inner}
}
res = funct.CallFunction(inner.Next, vars, funcs).Eval(funcs, vars)
res.Append(in.Next)
} else {
log.Log(log.ERR,
"undefined function "+inner.Value()+" called",
"eval")
return nil
}
default:
log.Log(log.ERR,
"Eval hit unknown token type!",
"eval")
return nil
}
return ret, false
if res.Next != nil {
res.Next = res.Next.Eval(funcs, vars)
}
return res
}

185
ast/lex.go Normal file
View file

@ -0,0 +1,185 @@
/* 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 ast
import (
"gitlab.com/whom/shs/log"
"unicode"
)
const string_delims string = "\"'`"
func Lex(input string) *Token {
if len(input) == 0 {
return nil
}
var ret *Token
iter := &ret
is_str := false
is_list := false
tokenBuilder := func (pos int, tok string) {
if len(tok) == 0 && !is_list && !is_str {
return
}
*iter = new(Token)
(*iter).Position = pos
if is_list {
(*iter).inner = Lex(tok)
(*iter).Tag = LIST
is_list = false
} else {
(*iter).inner = tok
if is_str {
(*iter).Tag = STRING
is_str = false
} else if StrIsNumber(tok) {
(*iter).Tag = NUMBER
} else {
(*iter).Tag = SYMBOL
}
}
iter = &(*iter).Next
}
// returns -1 on unmatched string delim
matchStrEnd := func(start int, delim byte) int {
for i := start; i < len(input); i++ {
if input[i] == delim {
return i
}
}
return -1
}
// returns -1 on unmatched string delim
// returns -2 on unmatched list delim
matchListEnd := func(start int) int {
depth := 0
for i := start; i < len(input); i++ {
switch input[i] {
case '"','\'','`':
i = matchStrEnd(i + 1, input[i])
if i == -1 {
return -1
}
case '(':
depth++
case ')':
if depth == 0 {
return i
} else {
depth -= 1
}
}
}
return -2
}
needs_alloc := false
start_pos := 0
for i := 0; i < len(input); i++ {
switch input[i] {
case '(':
start_pos = i + 1
i = matchListEnd(start_pos)
is_list = true
needs_alloc = true
case '"','\'','`':
start_pos = i + 1
i = matchStrEnd(start_pos, input[i])
is_str = true
needs_alloc = true
case ' ':
if i == start_pos {
start_pos += 1
continue
}
needs_alloc = true
}
if needs_alloc {
needs_alloc = false
if (i < 0) {
// TODO: Maybe not overload this.
start_pos = i
goto error
}
tokenBuilder(start_pos, input[start_pos:i])
start_pos = i+1
}
}
if start_pos < len(input) {
tokenBuilder(start_pos, input[start_pos:])
}
return ret
error:
// TODO: Hook into error module
// TODO: Finalize and GC alloced tokens
if start_pos == -1 {
log.Log(log.ERR,
"Unmatched string delimiter in input. discarding.",
"lex")
} else if start_pos == -2 {
log.Log(log.ERR,
"Unmatched list delimiter in input. discarding.",
"lex")
} else {
log.Log(log.ERR,
"Unknown error in input. discarding.",
"lex")
}
return nil
}
func StrIsNumber(arg string) bool {
dotCount := 0
for _, char := range arg {
if !unicode.IsDigit(char) {
if char == '.' && dotCount == 0 {
dotCount++
} else {
return false
}
}
}
return true
}

View file

@ -19,39 +19,8 @@ package ast
import (
"strings"
"fmt"
)
/* Print function which is better suited for repl.
* This one prints the SEXPRs as one would write them.
*/
func (t *Token) String() string {
switch t.Tag {
case STRING:
return "\"" + t.Inner.(string) + "\""
case NUMBER, BOOL:
return t.Inner.(string)
case LIST:
repr := "("
if t.Inner.(*Token) == nil {
return repr + ")"
}
for i := t.Inner.(*Token); i != nil; i = i.Next {
repr = repr + i.String() + " "
}
// remove trailing space
return repr[:len(repr)-1] + ")"
case SYMBOL:
return "<" + t.Inner.(string) + ">"
}
return "[UNKNOWN CELL TYPE]"
}
/* Print function which breaks each embedded list out on individual lines.
* Used in the print_ast debug tool. not too useful for repl applications.
* Very useful for debugging syntax though.
@ -74,42 +43,13 @@ loop:
for iter := i; iter != nil; iter = iter.Next {
if iter.Tag == LIST {
lists.Push(iter.Inner.(*Token))
lists.Push(iter.Expand())
}
constructor.WriteString(FmtToken(iter))
constructor.WriteString(iter.FmtToken())
}
println(constructor.String())
goto loop
}
func FmtToken(arg *Token) string {
suffix := "->"
if arg.Next == nil {
suffix = ""
}
switch arg.Tag {
case LIST:
return fmt.Sprintf("(%s, [List])%s", "LIST", suffix)
default:
return fmt.Sprintf("(%s, %s)%s", GetTagAsStr(arg.Tag), arg.Inner, suffix)
}
}
func GetTagAsStr(tag Token_t) string {
switch tag {
case LIST:
return "LIST"
case STRING:
return "STRING"
case NUMBER:
return "NUMBER"
case SYMBOL:
return "SYMBOL"
}
return "UNKNOWN"
}

View file

@ -17,10 +17,7 @@
package ast
import (
"gitlab.com/whom/shs/log"
"unicode"
)
import "fmt"
type Token_t int
const (
@ -35,166 +32,116 @@ type Token struct {
Next *Token
Tag Token_t
Position int
Inner interface{}
inner interface{}
}
const string_delims string = "\"'`"
/* Appends another token to the end of this token list
*/
func (t *Token) Append(arg *Token) {
if t.Next != nil {
t.Next.Append(arg)
} else {
t.Next = arg
}
}
func Lex(input string) *Token {
if len(input) == 0 {
/* Print function which is better suited for repl.
* This one prints the SEXPRs as one would write them.
* Does not evaluate tokens.
*/
func (t *Token) String() string {
switch t.Tag {
case STRING:
return "\"" + t.inner.(string) + "\""
case NUMBER, BOOL:
return t.inner.(string)
case LIST:
repr := "("
if t.inner.(*Token) == nil {
return repr + ")"
}
for i := t.inner.(*Token); i != nil; i = i.Next {
repr = repr + i.String() + " "
}
// remove trailing space
return repr[:len(repr)-1] + ")"
case SYMBOL:
return "<" + t.inner.(string) + ">"
}
return "[UNKNOWN CELL TYPE]"
}
/* Returns a list held by a token
* returns nil if token holds no list
*/
func (t *Token) Expand() *Token {
if t.Tag != LIST {
return nil
}
var ret *Token
iter := &ret
is_str := false
is_list := false
tokenBuilder := func (pos int, tok string) {
if len(tok) == 0 && !is_list && !is_str {
return
}
*iter = new(Token)
(*iter).Position = pos
if is_list {
(*iter).Inner = Lex(tok)
(*iter).Tag = LIST
is_list = false
} else {
(*iter).Inner = tok
if is_str {
(*iter).Tag = STRING
is_str = false
} else if StrIsNumber(tok) {
(*iter).Tag = NUMBER
} else {
(*iter).Tag = SYMBOL
}
}
iter = &(*iter).Next
}
// returns -1 on unmatched string delim
matchStrEnd := func(start int, delim byte) int {
for i := start; i < len(input); i++ {
if input[i] == delim {
return i
}
}
return -1
}
// returns -1 on unmatched string delim
// returns -2 on unmatched list delim
matchListEnd := func(start int) int {
depth := 0
for i := start; i < len(input); i++ {
switch input[i] {
case '"','\'','`':
i = matchStrEnd(i + 1, input[i])
if i == -1 {
return -1
}
case '(':
depth++
case ')':
if depth == 0 {
return i
} else {
depth -= 1
}
}
}
return -2
}
needs_alloc := false
start_pos := 0
for i := 0; i < len(input); i++ {
switch input[i] {
case '(':
start_pos = i + 1
i = matchListEnd(start_pos)
is_list = true
needs_alloc = true
case '"','\'','`':
start_pos = i + 1
i = matchStrEnd(start_pos, input[i])
is_str = true
needs_alloc = true
case ' ':
if i == start_pos {
start_pos += 1
continue
}
needs_alloc = true
}
if needs_alloc {
needs_alloc = false
if (i < 0) {
// TODO: Maybe not overload this.
start_pos = i
goto error
}
tokenBuilder(start_pos, input[start_pos:i])
start_pos = i+1
}
}
if start_pos < len(input) {
tokenBuilder(start_pos, input[start_pos:])
}
return ret
error:
// TODO: Hook into error module
// TODO: Finalize and GC alloced tokens
if start_pos == -1 {
log.Log(log.ERR,
"Unmatched string delimiter in input. discarding.",
"lex")
} else if start_pos == -2 {
log.Log(log.ERR,
"Unmatched list delimiter in input. discarding.",
"lex")
} else {
log.Log(log.ERR,
"Unknown error in input. discarding.",
"lex")
}
return nil
return t.inner.(*Token)
}
func StrIsNumber(arg string) bool {
dotCount := 0
for _, char := range arg {
if !unicode.IsDigit(char) {
if char == '.' && dotCount == 0 {
dotCount++
} else {
return false
}
}
/* Sets inner to a Token value
* returns false if parent token is not a list
* otherwise returns true
*/
func (t *Token) Direct(head *Token) bool {
if t.Tag != LIST {
return false
}
t.inner = head
return true
}
/* If token holds an atomic value
* (not a symbol or list)
* will return its value as a string
* else returns ""
*/
func (t *Token) Value() string {
if t.Tag == LIST {
return ""
}
return t.inner.(string)
}
/* returns an ascii representation of a token
*/
func (t *Token) FmtToken() string {
suffix := "->"
if t.Next == nil {
suffix = ""
}
switch t.Tag {
case LIST:
return fmt.Sprintf("(%s, [List])%s", "LIST", suffix)
default:
return fmt.Sprintf("(%s, %s)%s", GetTagAsStr(t.Tag), t.inner.(string), suffix)
}
}
/* Returns a tag in text
*/
func GetTagAsStr(tag Token_t) string {
switch tag {
case LIST:
return "LIST"
case STRING:
return "STRING"
case NUMBER:
return "NUMBER"
case SYMBOL:
return "SYMBOL"
}
return "UNKNOWN"
}

View file

@ -40,7 +40,7 @@ func GetVar(arg string, vt VarTable) *Token {
e := os.Getenv(arg)
if e != "" {
t := &Token{Inner: e}
t := &Token{inner: e}
if StrIsNumber(e) {
t.Tag = NUMBER
} else {
@ -56,11 +56,13 @@ func GetVar(arg string, vt VarTable) *Token {
}
// TODO: this could be much more optimal
// TODO: Make sure variables are evaluated before being set
// probably a stdlib thing
func SetVar(variable string, value *Token, vt VarTable) {
(*vt)[variable] = value
if SyncTablesWithOSEnviron &&
(value.Tag == NUMBER || value.Tag == STRING) {
token := value.Inner.(string)
token := value.Value()
if value.Tag == NUMBER {
// make sure its an int
a, err := strconv.ParseFloat(token, 64)
@ -91,7 +93,7 @@ func GetVarFromTables(arg string, library []VarTable) *Token {
func InitVarTable(table VarTable) {
for _, val := range os.Environ() {
variable := strings.Split(val, "=")
t := &Token{Inner: variable[1]}
t := &Token{inner: variable[1]}
if StrIsNumber(variable[1]) {
t.Tag = NUMBER
} else {