schala/src/main.rs

122 lines
2.8 KiB
Rust
Raw Normal View History

2015-07-16 02:55:03 -07:00
use std::io;
use std::io::Write;
use std::io::BufRead;
2015-07-19 00:12:27 -07:00
use std::char;
2015-07-16 02:55:03 -07:00
2015-07-16 01:40:37 -07:00
fn main() {
2015-07-16 02:55:03 -07:00
println!("Unnamed language 0.01");
repl();
}
#[derive(Debug)]
enum Token {
EOF,
Separator,
LParen,
RParen,
2015-07-19 14:24:42 -07:00
Comma,
2015-07-16 02:55:03 -07:00
NumLiteral(i32),
StrLiteral(String),
Identifier(String)
/* Keyword(Keyword) */ //implement in future
}
#[derive(Debug)]
enum ASTNode {
GenericNode
}
fn repl() {
let mut stdin = io::stdin();
let mut stdout = io::stdout();
let mut buf = String::with_capacity(20);
loop {
print!(">> ");
stdout.flush().ok();
let line = stdin.lock().read_line(&mut buf);
match line {
Ok(n) => {
2015-07-18 14:50:26 -07:00
let tokens = tokenize(&buf);
2015-07-19 00:12:27 -07:00
buf.clear();
2015-07-18 14:50:26 -07:00
println!("Tokens: {:?}", tokens);
let ast = parse(tokens);
println!("AST: {:?}", ast);
2015-07-18 15:00:18 -07:00
let eval = evaluate(&ast);
println!("{}", eval);
2015-07-16 02:55:03 -07:00
},
Err(err) => {
println!("Error: {}", err);
}
}
}
}
fn tokenize(input: &str) -> Vec<Token> {
let mut tokens = Vec::new();
2015-07-19 00:12:27 -07:00
let mut iterator = input.chars().peekable();
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;
}
buffer.push(x);
}
tokens.push(Token::StrLiteral(buffer));
2015-07-16 02:55:03 -07:00
2015-07-19 00:12:27 -07:00
} else if c == '#' {
while let Some(x) = iterator.next() {
if x == '\n' {
break;
}
}
} else if c == ';' || c == '\n' {
tokens.push(Token::Separator);
} else if c == '(' {
tokens.push(Token::LParen);
} else if c == ')' {
tokens.push(Token::RParen);
2015-07-19 14:24:42 -07:00
} else if c == ',' {
tokens.push(Token::Comma);
2015-07-19 00:12:27 -07:00
} else {
let mut buffer = String::with_capacity(20);
buffer.push(c);
while let Some(x) = iterator.peek().cloned() {
if !char::is_alphanumeric(x) {
2015-07-19 00:12:27 -07:00
break;
}
buffer.push(iterator.next().unwrap());
2015-07-19 00:12:27 -07:00
}
tokens.push(Token::Identifier(buffer));
}
2015-07-16 02:55:03 -07:00
}
tokens.push(Token::EOF);
tokens
}
fn parse(_input: Vec<Token>) -> ASTNode {
ASTNode::GenericNode
2015-07-16 01:40:37 -07:00
}
2015-07-18 15:00:18 -07:00
fn evaluate(input: &ASTNode) -> String {
return match eval_ast(input) {
ASTNode::GenericNode => "Not implemented".to_string()
};
fn eval_ast(_input: &ASTNode) -> ASTNode {
return ASTNode::GenericNode;
}
}