Created
September 4, 2018 01:23
-
-
Save tanner00/11b269115124538f0c8fee3b2ec57ff6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "strings" | |
type Id struct { | |
Id string | |
} | |
func (Id) CanParse() {} | |
type List struct { | |
Parent Parsable | |
Children []Parsable | |
} | |
func (List) CanParse() {} | |
type Parsable interface { | |
CanParse() | |
} | |
func ToTokens(code string) []string { | |
return strings.Fields(strings.Replace(strings.Replace(code, "(", " ( ", -1), ")", " ) ", -1)) | |
} | |
func popString(v *[]string) string { | |
e := (*v)[0] | |
*v = (*v)[1:] | |
return e | |
} | |
func ToAst(tokens *[]string) Parsable { | |
t := popString(tokens) | |
if t == "(" { | |
l := make([]Parsable, 0) | |
parent := popString(tokens) | |
for (*tokens)[0] != ")" { | |
l = append(l, ToAst(tokens)) | |
} | |
return List{Id{parent}, l} | |
} | |
return Id{t} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment