2020-11-14 10:51:25 +00:00
|
|
|
/*
|
|
|
|
Serene --- Yet an other Lisp
|
|
|
|
|
|
|
|
Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
|
|
|
|
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 2 of the License.
|
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-11-15 22:16:48 +00:00
|
|
|
package core
|
2020-11-14 10:51:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
|
|
|
var validChars = []rune{'!', '$', '%', '&', '*', '+', '-', '.', '~', '/', ':', '<', '=', '>', '?', '@', '^', '_'}
|
|
|
|
|
2020-11-15 22:16:48 +00:00
|
|
|
type IParsable interface {
|
|
|
|
next(skipWhitespace bool) *string
|
|
|
|
peek(skipWhitespace bool) *string
|
|
|
|
back()
|
|
|
|
GetLocation() int
|
2020-11-23 12:30:02 +00:00
|
|
|
Buffer() *[]string
|
2020-11-15 22:16:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 10:51:25 +00:00
|
|
|
type StringParser struct {
|
|
|
|
buffer []string
|
|
|
|
pos int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implementing IParsable for StringParser ---
|
2020-11-20 16:43:07 +00:00
|
|
|
|
|
|
|
// Returns the next character in the buffer
|
2020-11-14 10:51:25 +00:00
|
|
|
func (sp *StringParser) next(skipWhitespace bool) *string {
|
|
|
|
if sp.pos >= len(sp.buffer) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
char := sp.buffer[sp.pos]
|
|
|
|
sp.pos = sp.pos + 1
|
|
|
|
|
|
|
|
if skipWhitespace && char == " " {
|
|
|
|
return sp.next(skipWhitespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &char
|
|
|
|
}
|
|
|
|
|
2020-11-20 16:43:07 +00:00
|
|
|
// Return the character of the buffer without consuming it
|
2020-11-14 10:51:25 +00:00
|
|
|
func (sp *StringParser) peek(skipWhitespace bool) *string {
|
|
|
|
if sp.pos >= len(sp.buffer) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := sp.buffer[sp.pos]
|
|
|
|
if c == " " && skipWhitespace {
|
|
|
|
sp.pos = sp.pos + 1
|
|
|
|
return sp.peek(skipWhitespace)
|
|
|
|
}
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
2020-11-20 16:43:07 +00:00
|
|
|
// Move the char pointer back by one character
|
2020-11-14 10:51:25 +00:00
|
|
|
func (sp *StringParser) back() {
|
|
|
|
if sp.pos > 0 {
|
|
|
|
sp.pos = sp.pos - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *StringParser) GetLocation() int {
|
|
|
|
return sp.pos
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:30:02 +00:00
|
|
|
func (sp *StringParser) Buffer() *[]string {
|
|
|
|
return &sp.buffer
|
|
|
|
}
|
|
|
|
|
2020-11-14 10:51:25 +00:00
|
|
|
// END: IParsable ---
|
2020-11-20 16:43:07 +00:00
|
|
|
|
2020-11-24 18:27:48 +00:00
|
|
|
func makeErrorAtPoint(p IParsable, msg string, a ...interface{}) IError {
|
|
|
|
n := MakeSinglePointNode(p.Buffer(), p.GetLocation())
|
|
|
|
return MakeParsetimeErrorf(n, msg, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeErrorFromError(parser IParsable, e error) IError {
|
|
|
|
return makeErrorAtPoint(parser, "%w", e)
|
|
|
|
}
|
|
|
|
|
2020-11-14 10:51:25 +00:00
|
|
|
func contains(s []rune, c rune) bool {
|
|
|
|
for _, v := range s {
|
|
|
|
if v == c {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isValidForSymbol(char string) bool {
|
|
|
|
c := rune(char[0])
|
|
|
|
return contains(validChars, c) || unicode.IsLetter(c) || unicode.IsDigit(c)
|
|
|
|
}
|
|
|
|
|
2020-11-24 18:27:48 +00:00
|
|
|
func readRawSymbol(parser IParsable) (IExpr, IError) {
|
2020-11-14 10:51:25 +00:00
|
|
|
c := parser.peek(false)
|
|
|
|
var symbol string
|
|
|
|
|
|
|
|
if c == nil {
|
2020-11-24 18:27:48 +00:00
|
|
|
return nil, makeErrorAtPoint(parser, "unexpected enf of file while parsing a symbol")
|
2020-11-14 10:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if isValidForSymbol(*c) {
|
2020-11-14 21:09:54 +00:00
|
|
|
parser.next(false)
|
2020-11-14 10:51:25 +00:00
|
|
|
symbol = *c
|
|
|
|
} else {
|
2020-11-24 18:27:48 +00:00
|
|
|
return nil, makeErrorAtPoint(parser,
|
|
|
|
"unexpected character: got '%s', expected a symbol at %d",
|
2020-11-14 10:51:25 +00:00
|
|
|
*c,
|
|
|
|
parser.GetLocation(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
c := parser.next(false)
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if isValidForSymbol(*c) {
|
|
|
|
symbol = symbol + *c
|
|
|
|
} else {
|
|
|
|
parser.back()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add support for ns qualified symbols
|
2020-11-23 12:30:02 +00:00
|
|
|
node := MakeNode(parser.Buffer(), parser.GetLocation()-len(symbol), parser.GetLocation())
|
|
|
|
return MakeSymbol(node, symbol), nil
|
2020-11-14 10:51:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 18:27:48 +00:00
|
|
|
func readNumber(parser IParsable, neg bool) (IExpr, IError) {
|
2020-11-14 12:22:13 +00:00
|
|
|
isDouble := false
|
|
|
|
result := ""
|
|
|
|
|
|
|
|
if neg {
|
|
|
|
result = "-"
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
c := parser.next(false)
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if *c == "." && isDouble {
|
2020-11-24 18:27:48 +00:00
|
|
|
return nil, makeErrorAtPoint(parser, "a double with more that one '.' ???")
|
2020-11-14 12:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if *c == "." {
|
|
|
|
isDouble = true
|
|
|
|
result = result + *c
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weird, But go won't stop complaining without this swap
|
|
|
|
char := *c
|
|
|
|
r := rune(char[0])
|
|
|
|
if unicode.IsDigit(r) {
|
|
|
|
result = result + *c
|
|
|
|
} else {
|
|
|
|
parser.back()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-11-14 21:09:54 +00:00
|
|
|
|
2020-11-24 18:27:48 +00:00
|
|
|
value, err := MakeNumberFromStr(result, isDouble)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, makeErrorFromError(parser, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
2020-11-14 12:22:13 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 18:27:48 +00:00
|
|
|
func readSymbol(parser IParsable) (IExpr, IError) {
|
2020-11-14 10:51:25 +00:00
|
|
|
c := parser.peek(false)
|
|
|
|
|
|
|
|
if c == nil {
|
2020-11-24 18:27:48 +00:00
|
|
|
return nil, makeErrorAtPoint(parser, "unexpected end of file while scanning a symbol")
|
2020-11-14 10:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if c == "\"" {
|
|
|
|
// return readString(parser)
|
|
|
|
// }
|
|
|
|
|
2020-11-14 12:22:13 +00:00
|
|
|
// Weird, But go won't stop complaining without this swap
|
|
|
|
char := *c
|
|
|
|
r := rune(char[0])
|
|
|
|
if unicode.IsDigit(r) {
|
|
|
|
return readNumber(parser, false)
|
|
|
|
}
|
2020-11-14 10:51:25 +00:00
|
|
|
|
2020-11-14 12:22:13 +00:00
|
|
|
if *c == "-" {
|
|
|
|
parser.next(true)
|
|
|
|
c := parser.peek(false)
|
2020-11-14 10:51:25 +00:00
|
|
|
|
2020-11-14 12:22:13 +00:00
|
|
|
// Weird, But go won't stop complaining without this swap
|
|
|
|
char := *c
|
|
|
|
r := rune(char[0])
|
|
|
|
|
|
|
|
if unicode.IsDigit(r) {
|
|
|
|
return readNumber(parser, true)
|
|
|
|
} else {
|
|
|
|
// Unread '-'
|
|
|
|
parser.back()
|
|
|
|
return readRawSymbol(parser)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-11-14 10:51:25 +00:00
|
|
|
return readRawSymbol(parser)
|
|
|
|
}
|
|
|
|
|
2020-11-24 18:27:48 +00:00
|
|
|
func readList(parser IParsable) (IExpr, IError) {
|
2020-11-15 22:16:48 +00:00
|
|
|
list := []IExpr{}
|
2020-11-14 10:51:25 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
c := parser.peek(true)
|
|
|
|
if c == nil {
|
2020-11-24 18:27:48 +00:00
|
|
|
return nil, makeErrorAtPoint(parser, "reaching the end of file while reading a list")
|
2020-11-14 10:51:25 +00:00
|
|
|
}
|
|
|
|
if *c == ")" {
|
|
|
|
parser.next(true)
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
val, err := readExpr(parser)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
list = append(list, val)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 22:16:48 +00:00
|
|
|
return MakeList(list), nil
|
2020-11-14 10:51:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 18:27:48 +00:00
|
|
|
func readComment(parser IParsable) (IExpr, IError) {
|
2020-11-14 10:51:25 +00:00
|
|
|
for {
|
|
|
|
c := parser.next(false)
|
|
|
|
if c == nil || *c == "\n" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 18:27:48 +00:00
|
|
|
func readQuotedExpr(parser IParsable) (IExpr, IError) {
|
2020-11-14 10:51:25 +00:00
|
|
|
expr, err := readExpr(parser)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:30:02 +00:00
|
|
|
symNode := MakeNode(parser.Buffer(), parser.GetLocation(), parser.GetLocation())
|
2020-11-15 22:16:48 +00:00
|
|
|
return MakeList([]IExpr{
|
2020-11-23 12:30:02 +00:00
|
|
|
MakeSymbol(symNode, "quote"),
|
2020-11-14 10:51:25 +00:00
|
|
|
expr,
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 18:27:48 +00:00
|
|
|
func readUnquotedExpr(parser IParsable) (IExpr, IError) {
|
2020-11-14 10:51:25 +00:00
|
|
|
c := parser.peek(true)
|
|
|
|
|
|
|
|
if c == nil {
|
2020-11-24 18:27:48 +00:00
|
|
|
return nil, makeErrorAtPoint(parser, "end of file while reading an unquoted expression")
|
2020-11-14 10:51:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 22:16:48 +00:00
|
|
|
var sym IExpr
|
2020-11-24 18:27:48 +00:00
|
|
|
var err IError
|
2020-11-15 22:16:48 +00:00
|
|
|
var expr IExpr
|
2020-11-14 10:51:25 +00:00
|
|
|
|
2020-11-23 12:30:02 +00:00
|
|
|
node := MakeNode(parser.Buffer(), parser.GetLocation(), parser.GetLocation())
|
|
|
|
|
2020-11-14 10:51:25 +00:00
|
|
|
if *c == "@" {
|
2020-11-14 11:02:09 +00:00
|
|
|
parser.next(true)
|
2020-11-23 12:30:02 +00:00
|
|
|
sym = MakeSymbol(node, "unquote-splicing")
|
2020-11-14 11:02:09 +00:00
|
|
|
expr, err = readExpr(parser)
|
2020-11-14 10:51:25 +00:00
|
|
|
|
|
|
|
} else {
|
2020-11-23 12:30:02 +00:00
|
|
|
sym = MakeSymbol(node, "unquote")
|
2020-11-14 11:02:09 +00:00
|
|
|
expr, err = readExpr(parser)
|
2020-11-14 10:51:25 +00:00
|
|
|
}
|
2020-11-14 11:02:09 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-15 22:16:48 +00:00
|
|
|
return MakeList([]IExpr{sym, expr}), nil
|
2020-11-14 10:51:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 18:27:48 +00:00
|
|
|
func readQuasiquotedExpr(parser IParsable) (IExpr, IError) {
|
2020-11-14 10:51:25 +00:00
|
|
|
expr, err := readExpr(parser)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:30:02 +00:00
|
|
|
node := MakeNode(parser.Buffer(), parser.GetLocation(), parser.GetLocation())
|
2020-11-15 22:16:48 +00:00
|
|
|
return MakeList([]IExpr{
|
2020-11-23 12:30:02 +00:00
|
|
|
MakeSymbol(node, "quasiquote"),
|
2020-11-14 10:51:25 +00:00
|
|
|
expr,
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 18:27:48 +00:00
|
|
|
func readExpr(parser IParsable) (IExpr, IError) {
|
2020-11-14 10:51:25 +00:00
|
|
|
|
|
|
|
loop:
|
|
|
|
c := parser.next(true)
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
// We're done reading
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if *c == "'" {
|
|
|
|
return readQuotedExpr(parser)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *c == "~" {
|
|
|
|
return readUnquotedExpr(parser)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *c == "`" {
|
|
|
|
return readQuasiquotedExpr(parser)
|
|
|
|
}
|
|
|
|
if *c == "(" {
|
|
|
|
return readList(parser)
|
|
|
|
}
|
|
|
|
if *c == ";" {
|
|
|
|
readComment(parser)
|
|
|
|
goto loop
|
|
|
|
}
|
|
|
|
// case '[':
|
|
|
|
// readVector(parser)
|
|
|
|
|
|
|
|
// case '{':
|
|
|
|
// readMap(parser)
|
|
|
|
|
|
|
|
parser.back()
|
|
|
|
return readSymbol(parser)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-24 18:27:48 +00:00
|
|
|
func ParseToAST(input string) (*Block, IError) {
|
2020-11-14 10:51:25 +00:00
|
|
|
|
2020-11-19 22:17:50 +00:00
|
|
|
var ast Block
|
2020-11-14 10:51:25 +00:00
|
|
|
parser := StringParser{
|
|
|
|
buffer: strings.Split(input, ""),
|
|
|
|
pos: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
expr, err := readExpr(&parser)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if expr == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-11-19 22:17:50 +00:00
|
|
|
ast.Append(expr)
|
2020-11-14 10:51:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 22:17:50 +00:00
|
|
|
return &ast, nil
|
2020-11-14 10:51:25 +00:00
|
|
|
}
|