Example #1
0
File: syntax.go Project: ypb/golisp
func Read(port interface{}) interface{} {
	return readExpr(
		peg.Or{
			syntax,
			peg.Bind(peg.Eof, func(x interface{}) interface{} { return EOF_OBJECT }),
		},
		port,
	)
}
Example #2
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 #3
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 #4
0
File: syntax.go Project: ypb/golisp
			return res
		},
	)
	return peg.Select(peg.And{start, inner, end}, 1)
}

var syntax = func() *peg.ExtensibleExpr {
	expr := peg.Extensible()
	expr.Add(peg.Or{
		peg.Bind(_INT, func(x interface{}) interface{} {
			s := x.(string)
			res, err := strconv.Atoi(s)
			if err != nil {
				num := big.NewInt(0)
				_, ok := num.SetString(s, 10)
				if !ok {
					SystemError(err)
				}
				return num
			}
			return res
		}),
		peg.Bind(_FLOAT, func(x interface{}) interface{} {
			res, err := strconv.Atof(x.(string))
			if err != nil {
				SystemError(err)
			}
			return res
		}),
		peg.Bind(_STR, func(x interface{}) interface{} {
			res, err := strconv.Unquote(x.(string))