More tokenizer stuff

This commit is contained in:
greg 2015-07-19 00:12:27 -07:00
parent c6a92728ee
commit 2aaa600d53

View File

@ -1,6 +1,7 @@
use std::io; use std::io;
use std::io::Write; use std::io::Write;
use std::io::BufRead; use std::io::BufRead;
use std::char;
fn main() { fn main() {
println!("Unnamed language 0.01"); println!("Unnamed language 0.01");
@ -34,6 +35,7 @@ fn repl() {
match line { match line {
Ok(n) => { Ok(n) => {
let tokens = tokenize(&buf); let tokens = tokenize(&buf);
buf.clear();
println!("Tokens: {:?}", tokens); println!("Tokens: {:?}", tokens);
let ast = parse(tokens); let ast = parse(tokens);
@ -52,12 +54,40 @@ fn repl() {
fn tokenize(input: &str) -> Vec<Token> { fn tokenize(input: &str) -> Vec<Token> {
let mut tokens = Vec::new(); let mut tokens = Vec::new();
let mut buffer = String::with_capacity(20); let mut iterator = input.chars().peekable();
let mut iterator = input.chars();
loop {
while let Some(c) = iterator.next() {
if char::is_whitespace(c) {
continue;
} else if c == '"' {
let mut buffer = String::with_capacity(20);
while let Some(x) = iterator.next() {
if x == '"' {
break; break;
} }
buffer.push(x);
}
tokens.push(Token::StrLiteral(buffer));
} else if c == '#' {
while let Some(x) = iterator.next() {
if x == '\n' {
break;
}
}
} else {
let mut buffer = String::with_capacity(20);
buffer.push(c);
while let Some(x) = iterator.next() {
if char::is_whitespace(x) {
break;
}
buffer.push(x);
}
tokens.push(Token::Identifier(buffer));
}
}
tokens.push(Token::EOF); tokens.push(Token::EOF);
tokens tokens