2018-10-09 10:37:56 +02:00
|
|
|
//
|
2020-09-28 05:11:26 +02:00
|
|
|
// Copyright (c) 2018, Přemysl Eric Janouch <p@janouch.name>
|
2018-10-09 10:37:56 +02:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
// Package ell implements a simple scripting language.
|
|
|
|
package ell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// --- Values ------------------------------------------------------------------
|
|
|
|
|
|
|
|
// VType denotes the type of a value.
|
|
|
|
type VType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// VTypeString denotes a string value.
|
|
|
|
VTypeString VType = iota
|
|
|
|
// VTypeList denotes a list value.
|
|
|
|
VTypeList
|
|
|
|
)
|
|
|
|
|
|
|
|
// V is a value in the ell language.
|
|
|
|
type V struct {
|
|
|
|
Type VType // the type of this value
|
2018-10-10 19:39:29 +02:00
|
|
|
List []V // the contents of a VTypeList
|
2018-10-09 18:21:56 +02:00
|
|
|
String string // the immutable contents of a VTypeString
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-10 19:39:29 +02:00
|
|
|
// Clone clones a value including its sublists.
|
2018-10-09 10:37:56 +02:00
|
|
|
func (v *V) Clone() *V {
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &V{
|
2018-10-09 18:21:56 +02:00
|
|
|
Type: v.Type,
|
2018-10-10 19:39:29 +02:00
|
|
|
List: CloneSeq(v.List),
|
2018-10-09 10:37:56 +02:00
|
|
|
String: v.String,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloneSeq clones a value including the rest of its chain.
|
2018-10-10 19:39:29 +02:00
|
|
|
func CloneSeq(v []V) (result []V) {
|
|
|
|
for _, v := range v {
|
|
|
|
result = append(result, *v.Clone())
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
2018-10-10 19:39:29 +02:00
|
|
|
return
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewString creates a new value containing a string.
|
2018-10-09 18:21:56 +02:00
|
|
|
func NewString(string string) *V {
|
2018-10-09 10:37:56 +02:00
|
|
|
return &V{
|
|
|
|
Type: VTypeString,
|
|
|
|
String: string,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewList creates a new list value containing the given sequence.
|
2018-10-10 19:39:29 +02:00
|
|
|
func NewList(list []V) *V {
|
2018-10-09 10:37:56 +02:00
|
|
|
return &V{
|
|
|
|
Type: VTypeList,
|
2018-10-10 19:39:29 +02:00
|
|
|
List: list,
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Lexer -------------------------------------------------------------------
|
|
|
|
|
|
|
|
type token int
|
|
|
|
|
|
|
|
const (
|
|
|
|
tAbort token = iota
|
|
|
|
tLParen
|
|
|
|
tRParen
|
|
|
|
tLBracket
|
|
|
|
tRBracket
|
|
|
|
tLBrace
|
|
|
|
tRBrace
|
|
|
|
tString
|
|
|
|
tNewline
|
|
|
|
tAt
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t token) String() string {
|
|
|
|
switch t {
|
|
|
|
case tAbort:
|
|
|
|
return "end of input"
|
|
|
|
case tLParen:
|
|
|
|
return "left parenthesis"
|
|
|
|
case tRParen:
|
|
|
|
return "right parenthesis"
|
|
|
|
case tLBracket:
|
|
|
|
return "left bracket"
|
|
|
|
case tRBracket:
|
|
|
|
return "right bracket"
|
|
|
|
case tLBrace:
|
|
|
|
return "left brace"
|
|
|
|
case tRBrace:
|
|
|
|
return "right brace"
|
|
|
|
case tString:
|
|
|
|
return "string"
|
|
|
|
case tNewline:
|
|
|
|
return "newline"
|
|
|
|
case tAt:
|
|
|
|
return "at symbol"
|
|
|
|
}
|
|
|
|
panic("unknown token")
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
type lexer struct {
|
|
|
|
p []byte // unread input
|
|
|
|
line, column int // current line and column
|
|
|
|
buf []byte // parsed string value
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLexer(p []byte) *lexer {
|
|
|
|
return &lexer{p: p}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lex *lexer) advance() byte {
|
|
|
|
ch := lex.p[0]
|
|
|
|
lex.p = lex.p[1:]
|
|
|
|
|
|
|
|
if ch == '\n' {
|
|
|
|
lex.column = 0
|
|
|
|
lex.line++
|
|
|
|
} else {
|
|
|
|
lex.column++
|
|
|
|
}
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
var lexerHexAlphabet = "0123456789abcdef"
|
|
|
|
|
|
|
|
// fromHex converts a nibble from hexadecimal. Avoiding dependencies.
|
|
|
|
func lexerFromHex(ch byte) int {
|
|
|
|
if ch >= 'A' && ch <= 'Z' {
|
|
|
|
ch += 32
|
|
|
|
}
|
|
|
|
for i := 0; i < len(lexerHexAlphabet); i++ {
|
|
|
|
if lexerHexAlphabet[i] == ch {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lex *lexer) hexaEscape() bool {
|
|
|
|
if len(lex.p) < 2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
h := lexerFromHex(lex.advance())
|
|
|
|
if h < 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
l := lexerFromHex(lex.advance())
|
|
|
|
if l < 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
lex.buf = append(lex.buf, byte(h<<4|l))
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
lexerStringQuote = '\''
|
|
|
|
lexerEscape = '\\'
|
|
|
|
lexerComment = '#'
|
|
|
|
)
|
|
|
|
|
|
|
|
func lexerIsWhitespace(ch byte) bool {
|
|
|
|
return ch == 0 || ch == ' ' || ch == '\t' || ch == '\r'
|
|
|
|
}
|
|
|
|
|
|
|
|
var lexerEscapes = map[byte]byte{
|
|
|
|
lexerStringQuote: lexerStringQuote,
|
|
|
|
lexerEscape: lexerEscape,
|
|
|
|
'a': '\a',
|
|
|
|
'b': '\b',
|
|
|
|
'n': '\n',
|
|
|
|
'r': '\r',
|
|
|
|
't': '\t',
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lex *lexer) escapeSequence() error {
|
|
|
|
if len(lex.p) == 0 {
|
|
|
|
return errors.New("premature end of escape sequence")
|
|
|
|
}
|
|
|
|
ch := lex.advance()
|
|
|
|
if ch == 'x' {
|
|
|
|
if lex.hexaEscape() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("invalid hexadecimal escape")
|
|
|
|
}
|
|
|
|
ch, ok := lexerEscapes[ch]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("unknown escape sequence")
|
|
|
|
}
|
|
|
|
lex.buf = append(lex.buf, ch)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lex *lexer) string() error {
|
|
|
|
for len(lex.p) > 0 {
|
|
|
|
ch := lex.advance()
|
|
|
|
if ch == lexerStringQuote {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ch != lexerEscape {
|
|
|
|
lex.buf = append(lex.buf, ch)
|
|
|
|
} else if err := lex.escapeSequence(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("premature end of string")
|
|
|
|
}
|
|
|
|
|
|
|
|
var lexerTokens = map[byte]token{
|
|
|
|
'(': tLParen,
|
|
|
|
')': tRParen,
|
|
|
|
'[': tLBracket,
|
|
|
|
']': tRBracket,
|
|
|
|
'{': tLBrace,
|
|
|
|
'}': tRBrace,
|
|
|
|
';': tNewline,
|
|
|
|
'\n': tNewline,
|
|
|
|
'@': tAt,
|
|
|
|
lexerStringQuote: tString,
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lex *lexer) next() (token, error) {
|
|
|
|
for len(lex.p) > 0 && lexerIsWhitespace(lex.p[0]) {
|
|
|
|
lex.advance()
|
|
|
|
}
|
|
|
|
if len(lex.p) == 0 {
|
|
|
|
return tAbort, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lex.buf = nil
|
|
|
|
|
|
|
|
ch := lex.advance()
|
|
|
|
if ch == lexerComment {
|
|
|
|
for len(lex.p) > 0 {
|
|
|
|
if ch := lex.advance(); ch == '\n' {
|
|
|
|
return tNewline, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tAbort, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
token, ok := lexerTokens[ch]
|
|
|
|
if !ok {
|
|
|
|
lex.buf = append(lex.buf, ch)
|
|
|
|
for len(lex.p) > 0 && !lexerIsWhitespace(lex.p[0]) &&
|
|
|
|
lexerTokens[lex.p[0]] == 0 /* ugly but short */ {
|
|
|
|
lex.buf = append(lex.buf, lex.advance())
|
|
|
|
}
|
|
|
|
return tString, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if token == tString {
|
|
|
|
if err := lex.string(); err != nil {
|
|
|
|
return tAbort, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lex *lexer) errorf(format string, a ...interface{}) error {
|
|
|
|
return fmt.Errorf("at or before line %d, column %d: %s",
|
|
|
|
lex.line+1, lex.column+1, fmt.Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Printing ----------------------------------------------------------------
|
|
|
|
|
|
|
|
func printStringNeedsQuoting(s *V) bool {
|
|
|
|
for i := 0; i < len(s.String); i++ {
|
|
|
|
ch := s.String[i]
|
|
|
|
if lexerIsWhitespace(ch) || lexerTokens[ch] != 0 ||
|
|
|
|
ch == lexerEscape || ch < 32 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(s.String) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func printString(w io.Writer, s *V) bool {
|
|
|
|
if s.Type != VTypeString {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !printStringNeedsQuoting(s) {
|
2018-10-09 18:21:56 +02:00
|
|
|
_, _ = w.Write([]byte(s.String))
|
2018-10-09 10:37:56 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = w.Write([]byte{lexerStringQuote})
|
|
|
|
for i := 0; i < len(s.String); i++ {
|
|
|
|
ch := s.String[i]
|
|
|
|
if ch < 32 {
|
|
|
|
_, _ = fmt.Fprintf(w, "\\x%02x", ch)
|
|
|
|
} else if ch == lexerEscape || ch == lexerStringQuote {
|
|
|
|
_, _ = fmt.Fprintf(w, "\\%c", ch)
|
|
|
|
} else {
|
|
|
|
_, _ = w.Write([]byte{ch})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, _ = w.Write([]byte{lexerStringQuote})
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func printBlock(w io.Writer, list *V) bool {
|
2018-10-10 19:39:29 +02:00
|
|
|
if len(list.List) < 1 || list.List[0].String != "block" {
|
2018-10-09 10:37:56 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-10-10 19:39:29 +02:00
|
|
|
sublist := list.List[1:]
|
|
|
|
for _, subsub := range sublist {
|
|
|
|
if subsub.Type != VTypeList {
|
2018-10-09 10:37:56 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = w.Write([]byte{'{'})
|
2018-10-10 19:39:29 +02:00
|
|
|
if len(sublist) > 0 {
|
2018-10-09 10:37:56 +02:00
|
|
|
_, _ = w.Write([]byte{' '})
|
2018-10-10 19:39:29 +02:00
|
|
|
PrintSeq(w, sublist[0].List)
|
|
|
|
for _, subsub := range sublist[1:] {
|
2018-10-10 20:59:35 +02:00
|
|
|
_, _ = w.Write([]byte("; "))
|
2018-10-10 19:39:29 +02:00
|
|
|
PrintSeq(w, subsub.List)
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
2018-10-10 19:39:29 +02:00
|
|
|
_, _ = w.Write([]byte{' '})
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
_, _ = w.Write([]byte{'}'})
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func printSet(w io.Writer, list *V) bool {
|
2018-10-10 19:39:29 +02:00
|
|
|
if len(list.List) != 2 || list.List[0].String != "set" {
|
2018-10-09 10:37:56 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = w.Write([]byte{'@'})
|
2018-10-10 19:39:29 +02:00
|
|
|
PrintSeq(w, list.List[1:])
|
2018-10-09 10:37:56 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func printList(w io.Writer, list *V) bool {
|
2018-10-10 19:39:29 +02:00
|
|
|
if len(list.List) < 1 || list.List[0].String != "list" {
|
2018-10-09 10:37:56 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, _ = w.Write([]byte{'['})
|
2018-10-10 19:39:29 +02:00
|
|
|
PrintSeq(w, list.List[1:])
|
2018-10-09 10:37:56 +02:00
|
|
|
_, _ = w.Write([]byte{']'})
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintV serializes a value to the given writer, ignoring I/O errors.
|
|
|
|
func PrintV(w io.Writer, v *V) {
|
|
|
|
if printString(w, v) ||
|
|
|
|
printBlock(w, v) ||
|
|
|
|
printSet(w, v) ||
|
|
|
|
printList(w, v) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = w.Write([]byte{'('})
|
2018-10-10 19:39:29 +02:00
|
|
|
PrintSeq(w, v.List)
|
2018-10-09 10:37:56 +02:00
|
|
|
_, _ = w.Write([]byte{')'})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintSeq serializes a sequence of values to the given writer.
|
2018-10-10 19:39:29 +02:00
|
|
|
func PrintSeq(w io.Writer, seq []V) {
|
2018-10-10 20:59:35 +02:00
|
|
|
if len(seq) > 0 {
|
|
|
|
PrintV(w, &seq[0])
|
|
|
|
for _, v := range seq[1:] {
|
|
|
|
_, _ = w.Write([]byte{' '})
|
|
|
|
PrintV(w, &v)
|
|
|
|
}
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Parsing -----------------------------------------------------------------
|
|
|
|
|
|
|
|
// Parser is a context for parsing.
|
|
|
|
type Parser struct {
|
|
|
|
lexer *lexer // tokenizer
|
|
|
|
token token // current token in the lexer
|
|
|
|
replaceToken bool // replace the token
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewParser returns a new parser for the give byte slice.
|
|
|
|
func NewParser(script []byte) *Parser {
|
|
|
|
// As reading in tokens may cause exceptions, we wait for the first peek
|
|
|
|
// to replace the initial ELLT_ABORT.
|
|
|
|
return &Parser{
|
|
|
|
lexer: newLexer(script),
|
|
|
|
replaceToken: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) peek() token {
|
|
|
|
if p.replaceToken {
|
|
|
|
token, err := p.lexer.next()
|
|
|
|
if err != nil {
|
|
|
|
panic(p.lexer.errorf("%s", err))
|
|
|
|
}
|
|
|
|
p.token = token
|
|
|
|
p.replaceToken = false
|
|
|
|
}
|
|
|
|
return p.token
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) accept(token token) bool {
|
|
|
|
p.replaceToken = p.peek() == token
|
|
|
|
return p.replaceToken
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) expect(token token) {
|
|
|
|
if !p.accept(token) {
|
|
|
|
panic(p.lexer.errorf("unexpected `%s', expected `%s'", p.token, token))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
func (p *Parser) skipNL() {
|
|
|
|
for p.accept(tNewline) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 19:39:29 +02:00
|
|
|
func parsePrefixList(seq []V, name string) *V {
|
2018-10-10 20:59:35 +02:00
|
|
|
return NewList(append([]V{*NewString(name)}, seq...))
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
func (p *Parser) parseV() *V {
|
|
|
|
p.skipNL()
|
2018-10-10 20:59:35 +02:00
|
|
|
|
|
|
|
var seq []V
|
2018-10-09 10:37:56 +02:00
|
|
|
switch {
|
|
|
|
case p.accept(tString):
|
2018-10-09 18:21:56 +02:00
|
|
|
return NewString(string(p.lexer.buf))
|
2018-10-09 10:37:56 +02:00
|
|
|
case p.accept(tAt):
|
2018-10-10 19:39:29 +02:00
|
|
|
seq = []V{*p.parseV()}
|
|
|
|
return parsePrefixList(seq, "set")
|
2018-10-09 10:37:56 +02:00
|
|
|
case p.accept(tLParen):
|
|
|
|
for !p.accept(tRParen) {
|
2018-10-10 19:39:29 +02:00
|
|
|
seq = append(seq, *p.parseV())
|
2018-10-09 10:37:56 +02:00
|
|
|
p.skipNL()
|
|
|
|
}
|
2018-10-10 19:39:29 +02:00
|
|
|
return NewList(seq)
|
2018-10-09 10:37:56 +02:00
|
|
|
case p.accept(tLBracket):
|
|
|
|
for !p.accept(tRBracket) {
|
2018-10-10 19:39:29 +02:00
|
|
|
seq = append(seq, *p.parseV())
|
2018-10-09 10:37:56 +02:00
|
|
|
p.skipNL()
|
|
|
|
}
|
2018-10-10 19:39:29 +02:00
|
|
|
return parsePrefixList(seq, "list")
|
2018-10-09 10:37:56 +02:00
|
|
|
case p.accept(tLBrace):
|
|
|
|
for {
|
2018-10-10 19:39:29 +02:00
|
|
|
result := p.parseLine()
|
|
|
|
if result == nil {
|
2018-10-09 10:37:56 +02:00
|
|
|
break
|
|
|
|
}
|
2018-10-10 19:39:29 +02:00
|
|
|
seq = append(seq, *result)
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
p.expect(tRBrace)
|
2018-10-10 19:39:29 +02:00
|
|
|
return parsePrefixList(seq, "block")
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
panic(p.lexer.errorf("unexpected `%s', expected a value", p.token))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) parseLine() *V {
|
2018-10-10 19:39:29 +02:00
|
|
|
var seq []V
|
2018-10-09 10:37:56 +02:00
|
|
|
for p.peek() != tRBrace && p.peek() != tAbort {
|
|
|
|
if !p.accept(tNewline) {
|
2018-10-10 19:39:29 +02:00
|
|
|
seq = append(seq, *p.parseV())
|
|
|
|
} else if len(seq) > 0 {
|
|
|
|
return NewList(seq)
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 19:39:29 +02:00
|
|
|
if len(seq) > 0 {
|
|
|
|
return NewList(seq)
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the parser and returns a value to be interpreted or an error.
|
2018-10-10 19:39:29 +02:00
|
|
|
func (p *Parser) Run() (seq []V, err error) {
|
2018-10-09 10:37:56 +02:00
|
|
|
// "The convention in the Go libraries is that even when a package
|
|
|
|
// uses panic internally, its external API still presents explicit
|
|
|
|
// error return values." We're good.
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2018-10-10 19:39:29 +02:00
|
|
|
seq, err = nil, r.(error)
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
2018-10-10 19:39:29 +02:00
|
|
|
result := p.parseLine()
|
|
|
|
if result == nil {
|
2018-10-09 10:37:56 +02:00
|
|
|
break
|
|
|
|
}
|
2018-10-10 19:39:29 +02:00
|
|
|
seq = append(seq, *result)
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
p.expect(tAbort)
|
2018-10-10 19:39:29 +02:00
|
|
|
return seq, nil
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Runtime -----------------------------------------------------------------
|
|
|
|
|
|
|
|
// Handler is a Go handler for an Ell function.
|
2018-10-10 20:59:35 +02:00
|
|
|
type Handler func(ell *Ell, args []V) (result []V, ok bool)
|
2018-10-09 10:37:56 +02:00
|
|
|
|
|
|
|
// Ell is an interpreter context.
|
|
|
|
type Ell struct {
|
2018-10-10 19:49:17 +02:00
|
|
|
Globals map[string]V // list of global variables
|
2018-10-10 19:53:22 +02:00
|
|
|
scopes []map[string]V // dynamic scopes from the oldest
|
2018-10-09 10:37:56 +02:00
|
|
|
Native map[string]Handler // maps strings to Go functions
|
|
|
|
|
|
|
|
Error string // error information
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new interpreter context ready for program execution.
|
|
|
|
func New() *Ell {
|
|
|
|
return &Ell{
|
2018-10-10 19:49:17 +02:00
|
|
|
Globals: make(map[string]V),
|
|
|
|
Native: make(map[string]Handler),
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 19:39:29 +02:00
|
|
|
func scopeFind(scope []*V, name string) int {
|
|
|
|
for i, scope := range scope {
|
|
|
|
if scope.List[0].String == name {
|
|
|
|
return i
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 19:39:29 +02:00
|
|
|
return -1
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves a value by name from the scope or from global variables.
|
|
|
|
func (ell *Ell) Get(name string) *V {
|
2018-10-10 19:53:22 +02:00
|
|
|
for i := len(ell.scopes) - 1; i >= 0; i-- {
|
|
|
|
if v, ok := ell.scopes[i][name]; ok {
|
2018-10-10 19:49:17 +02:00
|
|
|
return &v
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 19:49:17 +02:00
|
|
|
if v, ok := ell.Globals[name]; ok {
|
|
|
|
return &v
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets a value by name in the scope or in global variables.
|
|
|
|
func (ell *Ell) Set(name string, v *V) {
|
2018-10-10 19:53:22 +02:00
|
|
|
for i := len(ell.scopes) - 1; i >= 0; i-- {
|
|
|
|
if _, ok := ell.scopes[i][name]; ok {
|
|
|
|
ell.scopes[i][name] = *v
|
2018-10-09 10:37:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variables only get deleted by "arg" or from the global scope.
|
2018-10-10 19:49:17 +02:00
|
|
|
ell.Globals[name] = *v
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2018-10-10 20:59:35 +02:00
|
|
|
// Errorf sets an error message in the interpreter context and returns an empty
|
|
|
|
// sequence and false.
|
|
|
|
func (ell *Ell) Errorf(format string, args ...interface{}) ([]V, bool) {
|
2018-10-09 10:37:56 +02:00
|
|
|
ell.Error = fmt.Sprintf(format, args...)
|
2018-10-10 20:59:35 +02:00
|
|
|
return nil, false
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ell *Ell) canModifyError() bool {
|
|
|
|
// Errors starting with an underscore are exceptions and would not work
|
|
|
|
// with stack traces generated this way.
|
|
|
|
return ell.Error == "" || ell.Error[0] != '_'
|
|
|
|
}
|
|
|
|
|
2018-10-10 20:59:35 +02:00
|
|
|
func (ell *Ell) evalArgs(args []V) (result []V, ok bool) {
|
2018-10-10 19:39:29 +02:00
|
|
|
for i, arg := range args {
|
2018-10-10 20:59:35 +02:00
|
|
|
evaluated, ok := ell.evalStatement(&arg)
|
|
|
|
if !ok {
|
2018-10-10 19:39:29 +02:00
|
|
|
// Once the code flows like this, at least make some use of it.
|
|
|
|
if ell.canModifyError() {
|
|
|
|
ell.Errorf("(argument %d) -> %s", i, ell.Error)
|
|
|
|
}
|
2018-10-10 20:59:35 +02:00
|
|
|
return nil, false
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
2018-10-10 20:59:35 +02:00
|
|
|
// Arguments should not evaporate, default to a nil value.
|
2018-10-10 19:39:29 +02:00
|
|
|
if len(evaluated) < 1 {
|
|
|
|
evaluated = []V{*NewList(nil)}
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
2018-10-10 20:59:35 +02:00
|
|
|
result = append(result, evaluated[0])
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
2018-10-10 20:59:35 +02:00
|
|
|
return result, true
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-10 20:59:35 +02:00
|
|
|
func (ell *Ell) evalNative(name string, args []V) (result []V, ok bool) {
|
2018-10-09 18:16:19 +02:00
|
|
|
fn := ell.Native[name]
|
2018-10-09 10:37:56 +02:00
|
|
|
if fn == nil {
|
|
|
|
return ell.Errorf("unknown function")
|
|
|
|
}
|
2018-10-10 20:59:35 +02:00
|
|
|
if arguments, ok := ell.evalArgs(args); ok {
|
|
|
|
return fn(ell, arguments)
|
|
|
|
}
|
|
|
|
return nil, false
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-10 20:59:35 +02:00
|
|
|
func (ell *Ell) evalResolved(body *V, args []V) (result []V, ok bool) {
|
2018-10-09 10:37:56 +02:00
|
|
|
// Resolving names recursively could be pretty fatal, let's not do that.
|
|
|
|
if body.Type == VTypeString {
|
2018-10-10 20:59:35 +02:00
|
|
|
return []V{*body}, true
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
2018-10-10 20:59:35 +02:00
|
|
|
if arguments, ok := ell.evalArgs(args); ok {
|
|
|
|
return ell.EvalBlock(body.List, arguments)
|
|
|
|
}
|
|
|
|
return nil, false
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-10 20:59:35 +02:00
|
|
|
func (ell *Ell) evalValue(body []V) (result []V, ok bool) {
|
2018-10-10 19:39:29 +02:00
|
|
|
args := body[1:]
|
|
|
|
if body[0].Type == VTypeString {
|
|
|
|
name := body[0].String
|
2018-10-09 10:37:56 +02:00
|
|
|
if name == "block" {
|
2018-10-10 19:39:29 +02:00
|
|
|
if len(args) > 0 {
|
2018-10-10 20:59:35 +02:00
|
|
|
result = []V{*NewList(CloneSeq(args))}
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
2018-10-10 20:59:35 +02:00
|
|
|
return result, true
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
if body := ell.Get(name); body != nil {
|
2018-10-10 20:59:35 +02:00
|
|
|
return ell.evalResolved(body, args)
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
2018-10-10 20:59:35 +02:00
|
|
|
return ell.evalNative(name, args)
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// When someone tries to call a block directly, we must evaluate it;
|
|
|
|
// e.g. something like `{ choose [@f1 @f2 @f3] } arg1 arg2 arg3`.
|
2018-10-10 20:59:35 +02:00
|
|
|
if evaluated, ok := ell.evalStatement(&body[0]); !ok {
|
|
|
|
return nil, false
|
|
|
|
} else if len(evaluated) > 0 {
|
|
|
|
return ell.evalResolved(&evaluated[0], args)
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// It might a bit confusing that this doesn't evaluate arguments
|
|
|
|
// but neither does "block" and there's nothing to do here.
|
2018-10-10 20:59:35 +02:00
|
|
|
return nil, true
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-10 20:59:35 +02:00
|
|
|
func (ell *Ell) evalStatement(statement *V) (result []V, ok bool) {
|
2018-10-09 10:37:56 +02:00
|
|
|
if statement.Type == VTypeString {
|
2018-10-10 20:59:35 +02:00
|
|
|
return []V{*statement}, true
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Executing a nil value results in no value. It's not very different from
|
|
|
|
// calling a block that returns no value--it's for our callers to resolve.
|
2018-10-10 20:59:35 +02:00
|
|
|
if len(statement.List) < 1 {
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
if result, ok = ell.evalValue(statement.List); ok {
|
|
|
|
return
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
name := "(block)"
|
2018-10-10 19:39:29 +02:00
|
|
|
if statement.List[0].Type == VTypeString {
|
|
|
|
name = statement.List[0].String
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ell.canModifyError() {
|
|
|
|
ell.Errorf("%s -> %s", name, ell.Error)
|
|
|
|
}
|
2018-10-10 20:59:35 +02:00
|
|
|
return nil, false
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-10 19:49:17 +02:00
|
|
|
func argsToScope(args []V) map[string]V {
|
|
|
|
scope := map[string]V{"args": *NewList(args)}
|
2018-10-10 19:39:29 +02:00
|
|
|
for i, arg := range args {
|
2018-10-10 19:49:17 +02:00
|
|
|
scope[fmt.Sprintf("%d", i+1)] = *arg.Clone()
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
2018-10-10 19:39:29 +02:00
|
|
|
return scope
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// EvalBlock executes a block and returns whatever the last statement returned,
|
|
|
|
// eats args.
|
2018-10-10 20:59:35 +02:00
|
|
|
func (ell *Ell) EvalBlock(body []V, args []V) (result []V, ok bool) {
|
2018-10-10 19:53:22 +02:00
|
|
|
ell.scopes = append(ell.scopes, argsToScope(args))
|
2018-10-09 10:37:56 +02:00
|
|
|
|
2018-10-10 20:59:35 +02:00
|
|
|
ok = true
|
2018-10-10 19:39:29 +02:00
|
|
|
for _, stmt := range body {
|
2018-10-10 20:59:35 +02:00
|
|
|
if result, ok = ell.evalStatement(&stmt); !ok {
|
2018-10-09 10:37:56 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 19:53:22 +02:00
|
|
|
ell.scopes = ell.scopes[:len(ell.scopes)-1]
|
2018-10-10 20:59:35 +02:00
|
|
|
return result, ok
|
2018-10-09 10:37:56 +02:00
|
|
|
}
|