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;
|
|
|
|
|
2015-07-22 03:02:55 -07:00
|
|
|
use tokenizer::Token;
|
|
|
|
use tokenizer::Token::*;
|
|
|
|
|
|
|
|
mod tokenizer;
|
2015-07-22 02:26:46 -07:00
|
|
|
|
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)]
|
2015-07-22 02:54:30 -07:00
|
|
|
enum AST {
|
2015-07-20 01:46:02 -07:00
|
|
|
Name(String),
|
2015-07-22 02:51:49 -07:00
|
|
|
LangString(String),
|
2015-07-20 01:46:02 -07:00
|
|
|
Number(f64),
|
2015-07-22 02:54:30 -07:00
|
|
|
BinOp(Box<AST>, Box<AST>, Box<AST>),
|
|
|
|
Binding(String, Box<AST>)
|
2015-07-16 02:55:03 -07:00
|
|
|
}
|
|
|
|
|
2015-07-22 02:48:27 -07:00
|
|
|
enum ParseResult {
|
2015-07-22 02:54:30 -07:00
|
|
|
Ok(AST),
|
2015-07-22 02:48:27 -07:00
|
|
|
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) {
|
2015-07-22 02:48:27 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-07-19 13:55:34 -07:00
|
|
|
} 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);
|
2015-07-19 13:55:34 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-07-19 13:55:34 -07:00
|
|
|
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-22 02:48:27 -07:00
|
|
|
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
|
|
|
|
2015-07-22 02:48:27 -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
|
|
|
|
2015-07-22 02:48:27 -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;
|
|
|
|
}
|
|
|
|
|
2015-07-22 02:48:27 -07:00
|
|
|
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 {
|
2015-07-22 02:54:30 -07:00
|
|
|
let ast = AST::Binding(name.clone(), Box::new(AST::Name(value.clone())));
|
2015-07-22 02:48:27 -07:00
|
|
|
return ParseResult::Ok(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(&StrLiteral(ref value)) = next {
|
2015-07-22 02:54:30 -07:00
|
|
|
let ast = AST::Binding(name.clone(), Box::new(AST::LangString(value.clone())));
|
2015-07-22 02:48:27 -07:00
|
|
|
return ParseResult::Ok(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(&NumLiteral(n)) = next {
|
2015-07-22 02:54:30 -07:00
|
|
|
let ast = AST::Binding(name.clone(), Box::new(AST::Number(n)));
|
2015-07-22 02:48:27 -07:00
|
|
|
return ParseResult::Ok(ast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-22 02:26:46 -07:00
|
|
|
|
2015-07-22 02:48:27 -07:00
|
|
|
return ParseResult::Err("Bad parse".to_string());
|
2015-07-22 02:26:46 -07:00
|
|
|
}
|
|
|
|
|