all: getting closer to being able to read the stdlib
This commit is contained in:
41
main.go
41
main.go
@@ -70,40 +70,39 @@ func runRepl() {
|
|||||||
func runFile(path string) {
|
func runFile(path string) {
|
||||||
e := eval.RootEnv()
|
e := eval.RootEnv()
|
||||||
m := macro.NewMacroEnv()
|
m := macro.NewMacroEnv()
|
||||||
input, err := ioutil.ReadFile(path)
|
inp, err := ioutil.ReadFile(path)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
parsed, err, unconsumed := parser.Parse(string(input))
|
input := string(inp)
|
||||||
|
|
||||||
|
parsed, err := parser.ParseAll(input)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(unconsumed) != 0 {
|
for _, statement := range parsed {
|
||||||
fmt.Println("Unconsumed input:", strings.Join(unconsumed, " "))
|
expanded, err := macro.Expand(statement, m)
|
||||||
return
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
evald, err := eval.Eval(expanded, e)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(evald.Pretty())
|
||||||
}
|
}
|
||||||
|
|
||||||
expanded, err := macro.Expand(parsed, m)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
evald, err := eval.Eval(expanded, e)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(evald.Pretty())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@@ -30,6 +30,7 @@ func tokenize(input string) []string {
|
|||||||
withoutComments := string(comments.ReplaceAll([]byte(input), []byte("")))
|
withoutComments := string(comments.ReplaceAll([]byte(input), []byte("")))
|
||||||
explodedParens := explode(withoutComments, "(", ")")
|
explodedParens := explode(withoutComments, "(", ")")
|
||||||
explodedBrackets := explode(explodedParens, "[", "]")
|
explodedBrackets := explode(explodedParens, "[", "]")
|
||||||
|
// TODO this consumes multiple spaces
|
||||||
return withoutEmpty(whitespace.Split(explodedBrackets, -1))
|
return withoutEmpty(whitespace.Split(explodedBrackets, -1))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +76,8 @@ func parseValue(input []string) (*ast.AST, error, []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if input[0][0] == '"' {
|
if input[0][0] == '"' {
|
||||||
var agg []string
|
agg := []string{input[0]}
|
||||||
|
input = input[1:]
|
||||||
for {
|
for {
|
||||||
if len(input) == 0 {
|
if len(input) == 0 {
|
||||||
return nil, errors.New("Unmatched \""), input
|
return nil, errors.New("Unmatched \""), input
|
||||||
@@ -233,3 +235,23 @@ func parseToken(input []string) (*ast.AST, error, []string) {
|
|||||||
func Parse(input string) (*ast.AST, error, []string) {
|
func Parse(input string) (*ast.AST, error, []string) {
|
||||||
return parseToken(tokenize(input))
|
return parseToken(tokenize(input))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParseAll(input string) ([]*ast.AST, error) {
|
||||||
|
tokens := tokenize(input)
|
||||||
|
var res []*ast.AST
|
||||||
|
for {
|
||||||
|
if len(tokens) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed, err, unconsumed := parseToken(tokens)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
res = append(res, parsed)
|
||||||
|
tokens = unconsumed
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user