Created
March 26, 2024 10:55
-
-
Save dmateos/ee83769ab0ddc87a0177772c2e391c28 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"bufio" | |
"fmt" | |
"os" | |
"strconv" | |
"strings" | |
) | |
const ( | |
TOKEN_LPAREN = iota | |
TOKEN_RPAREN | |
TOKEN_NUMBER | |
TOKEN_SYMBOL | |
) | |
type Token struct { | |
kind int | |
value string | |
} | |
func tokenize(input string) []string { | |
input = strings.Replace(input, "(", " ( ", -1) | |
input = strings.Replace(input, ")", " ) ", -1) | |
tokens := strings.Fields(input) | |
return tokens | |
} | |
func build_tokens(tokens []string) []Token { | |
var result []Token | |
for _, token := range tokens { | |
if token == "(" { | |
result = append(result, Token{TOKEN_LPAREN, token}) | |
} else if token == ")" { | |
result = append(result, Token{TOKEN_RPAREN, token}) | |
} else if _, err := strconv.Atoi(token); err == nil { | |
result = append(result, Token{TOKEN_NUMBER, token}) | |
} else { | |
result = append(result, Token{TOKEN_SYMBOL, token}) | |
} | |
} | |
return result | |
} | |
func main() { | |
scanner := bufio.NewScanner(os.Stdin) | |
for { | |
scanner.Scan() | |
text := scanner.Text() | |
fmt.Println(">", text) | |
tokens := tokenize(text) | |
token_structure := build_tokens(tokens) | |
for _, token := range token_structure { | |
fmt.Println(token) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment