new eval.go
This commit is contained in:
parent
89d6a1013b
commit
2a2e5b4527
15 changed files with 382 additions and 1215 deletions
255
ast/token.go
255
ast/token.go
|
|
@ -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"
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue