schala/src/main.rs

200 lines
5.2 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-22 02:26:46 -07:00
use std::slice::Iter;
use Token::*;
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();
}
2015-07-22 02:26:46 -07:00
#[derive(Debug, Clone)]
2015-07-16 02:55:03 -07:00
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>),
Binding(String, Box<ASTNode>)
2015-07-16 02:55:03 -07:00
}
enum ParseResult {
Ok(ASTNode),
Err(String)
2015-07-20 01:46:02 -07:00
}
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-20 01:46:02 -07:00
}
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
}
fn parse(input: Vec<Token>) -> ParseResult {
2015-07-18 15:00:18 -07:00
2015-07-22 02:26:46 -07:00
let mut tokens = input.iter();
2015-07-18 15:00:18 -07:00
if let ParseResult::Ok(ast) = let_expression(&mut tokens) {
if expect(EOF, &mut tokens) {
return ParseResult::Ok(ast);
}
}
2015-07-18 15:00:18 -07:00
return ParseResult::Err("Bad parse".to_string());
2015-07-18 15:00:18 -07:00
}
2015-07-20 01:46:02 -07:00
2015-07-22 02:26:46 -07:00
fn expect(tok: Token, tokens: &mut Iter<Token>) -> bool {
if let Some(n) = tokens.next() {
let next = (*n).clone();
return match (tok, next) {
(EOF, EOF) => true,
(Separator, Separator) => true,
(LParen, LParen) => true,
(RParen, RParen) => true,
(Comma, Comma) => true,
(NumLiteral(_), NumLiteral(_)) => true,
(StrLiteral(_), StrLiteral(_)) => true,
(Identifier(ref i1), Identifier(ref i2)) => i1 == i2,
_ => false
}
}
return false;
}
fn let_expression<'a>(input: &mut Iter<Token>) -> ParseResult {
if expect(Identifier("let".to_string()), input) {
if let Some(&Identifier(ref name)) = input.next() {
if let Some(&Identifier(ref s)) = input.next() {
if s == "=" {
let next = input.next();
if let Some(&Identifier(ref value)) = next {
let ast = ASTNode::Binding(name.clone(), Box::new(ASTNode::Name(value.clone())));
return ParseResult::Ok(ast);
}
if let Some(&StrLiteral(ref value)) = next {
let ast = ASTNode::Binding(name.clone(), Box::new(ASTNode::Name(value.clone())));
return ParseResult::Ok(ast);
}
if let Some(&NumLiteral(n)) = next {
let ast = ASTNode::Binding(name.clone(), Box::new(ASTNode::Number(n)));
return ParseResult::Ok(ast);
}
}
}
}
}
2015-07-22 02:26:46 -07:00
return ParseResult::Err("Bad parse".to_string());
2015-07-22 02:26:46 -07:00
}