schala/src/main.rs

144 lines
3.4 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-19 16:53:37 -07:00
NumLiteral(f64),
2015-07-16 02:55:03 -07:00
StrLiteral(String),
Identifier(String)
/* Keyword(Keyword) */ //implement in future
}
#[derive(Debug)]
enum ASTNode {
2015-07-20 01:46:02 -07:00
Name(String),
Number(f64),
BinOp(Box<ASTNode>, Box<ASTNode>, Box<ASTNode>)
2015-07-16 02:55:03 -07:00
}
2015-07-20 01:46:02 -07:00
enum ParseResult<'a> {
Ok(ASTNode, &'a [Token]),
Err(String, &'a [Token])
}
2015-07-16 02:55:03 -07:00
fn repl() {
2015-07-19 16:53:37 -07:00
let stdin = io::stdin();
2015-07-16 02:55:03 -07:00
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 {
2015-07-19 16:53:37 -07:00
Ok(_) => {
2015-07-19 17:11:22 -07:00
if buf.is_empty() {
break;
}
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);
2015-07-20 01:46:02 -07:00
match parse(tokens) {
ParseResult::Ok(ast, _) => println!("AST: {:?}", ast),
ParseResult::Err(err, _) => println!("Error: {}", err)
}
2015-07-18 15:00:18 -07:00
2015-07-20 01:46:02 -07:00
/*
2015-07-18 15:00:18 -07:00
let eval = evaluate(&ast);
println!("{}", eval);
2015-07-20 01:46:02 -07:00
*/
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();
2015-07-19 16:53:37 -07:00
fn ends_identifier(c: char) -> bool {
match c {
c if char::is_whitespace(c) => true,
',' => true,
';' => true,
'(' => true,
')' => true,
_ => false
}
}
2015-07-19 00:12:27 -07:00
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() {
2015-07-19 16:53:37 -07:00
if ends_identifier(x) {
2015-07-19 00:12:27 -07:00
break;
}
buffer.push(iterator.next().unwrap());
2015-07-19 00:12:27 -07:00
}
2015-07-19 16:53:37 -07:00
match buffer.parse::<f64>() {
Ok(f) => tokens.push(Token::NumLiteral(f)),
_ => tokens.push(Token::Identifier(buffer))
}
2015-07-19 00:12:27 -07:00
}
2015-07-16 02:55:03 -07:00
}
tokens.push(Token::EOF);
tokens
}
2015-07-20 01:46:02 -07:00
fn parse<'a>(input: Vec<Token>) -> ParseResult<'a> {
2015-07-18 15:00:18 -07:00
2015-07-20 01:46:02 -07:00
let mut tokens = input.iter().peekable();
2015-07-18 15:00:18 -07:00
2015-07-20 01:46:02 -07:00
return ParseResult::Ok(ASTNode::Name("Hella".to_string()), &[]);
2015-07-18 15:00:18 -07:00
}
2015-07-20 01:46:02 -07:00