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,
|
2015-07-19 13:55:34 -07:00
|
|
|
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 {
|
|
|
|
GenericNode
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|