all: getting closer to being able to read the stdlib

This commit is contained in:
2018-05-15 04:05:25 +02:00
parent a136959423
commit 0f19b677a2
2 changed files with 43 additions and 22 deletions

15
main.go
View File

@@ -70,26 +70,24 @@ func runRepl() {
func runFile(path string) {
e := eval.RootEnv()
m := macro.NewMacroEnv()
input, err := ioutil.ReadFile(path)
inp, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println(err)
return
}
parsed, err, unconsumed := parser.Parse(string(input))
input := string(inp)
parsed, err := parser.ParseAll(input)
if err != nil {
fmt.Println(err)
return
}
if len(unconsumed) != 0 {
fmt.Println("Unconsumed input:", strings.Join(unconsumed, " "))
return
}
expanded, err := macro.Expand(parsed, m)
for _, statement := range parsed {
expanded, err := macro.Expand(statement, m)
if err != nil {
fmt.Println(err)
@@ -104,6 +102,7 @@ func runFile(path string) {
}
fmt.Println(evald.Pretty())
}
}
func main() {

View File

@@ -30,6 +30,7 @@ func tokenize(input string) []string {
withoutComments := string(comments.ReplaceAll([]byte(input), []byte("")))
explodedParens := explode(withoutComments, "(", ")")
explodedBrackets := explode(explodedParens, "[", "]")
// TODO this consumes multiple spaces
return withoutEmpty(whitespace.Split(explodedBrackets, -1))
}
@@ -75,7 +76,8 @@ func parseValue(input []string) (*ast.AST, error, []string) {
}
if input[0][0] == '"' {
var agg []string
agg := []string{input[0]}
input = input[1:]
for {
if len(input) == 0 {
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) {
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
}