Example #1
0
File: syntax.go Project: ypb/golisp
func ReadFile(port interface{}) interface{} {
	return readExpr(
		peg.Select(peg.And{
			peg.Bind(peg.Repeat(syntax), func(x interface{}) interface{} {
				return vecToLs(Vector(x.([]interface{})))
			}),
			peg.Eof,
		}, 0),
		port,
	)
}
Example #2
0
File: syntax.go Project: ypb/golisp
func listExpr(start, rec, end peg.Expr) peg.Expr {
	tail := peg.Bind(
		peg.Option(peg.Select(peg.And{_DOT, rec}, 1)),
		func(x interface{}) interface{} {
			o := x.([]interface{})
			if len(o) != 0 {
				return o[0]
			}
			return EMPTY_LIST
		},
	)
	inner := peg.Bind(
		peg.Option(peg.And{peg.Multi(rec), tail}),
		func(x interface{}) interface{} {
			o := x.([]interface{})
			if len(o) == 0 {
				return EMPTY_LIST
			}
			expr := o[0].([]interface{})
			ls := expr[0].([]interface{})
			res := expr[1]
			if Failed(res) {
				return res
			}
			for i := len(ls) - 1; i >= 0; i-- {
				x := ls[i]
				if Failed(x) {
					return x
				}
				res = Cons(x, res)
			}
			return res
		},
	)
	return peg.Select(peg.And{start, inner, end}, 1)
}
Example #3
0
File: syntax.go Project: ypb/golisp
			if err != nil {
				SystemError(err)
			}
			return res
		}),
		peg.Bind(_STR, func(x interface{}) interface{} {
			res, err := strconv.Unquote(x.(string))
			if err != nil {
				SystemError(err)
			}
			return res
		}),
		listExpr(_LSTART, expr, _LEND),
		listExpr(_LSTART2, expr, _LEND2),
		peg.Bind(
			peg.Select(peg.And{_VSTART, peg.Repeat(expr), _LEND}, 1),
			func(x interface{}) interface{} {
				return Vector(x.([]interface{}))
			},
		),
		peg.Bind(peg.And{_QUOTE, expr}, func(x interface{}) interface{} {
			qu := x.([]interface{})
			s := ""
			switch qu[0].(string) {
			case "'":
				s = "quote"
			case "`":
				s = "quasiquote"
			case ",":
				s = "unquote"
			case ",@":