2017-08-29 00:28:19 -07:00
|
|
|
extern crate itertools;
|
|
|
|
use self::itertools::Itertools;
|
|
|
|
|
2017-08-31 18:05:10 -07:00
|
|
|
use language::{ProgrammingLanguageInterface, EvalOptions, ParseError, TokenError, LLVMCodeString};
|
2017-08-29 00:28:19 -07:00
|
|
|
|
|
|
|
pub struct Robo {
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Robo {
|
|
|
|
pub fn new() -> Robo {
|
|
|
|
Robo { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 14:57:56 -07:00
|
|
|
#[allow(dead_code)]
|
2017-08-29 00:28:19 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Token {
|
|
|
|
StrLiteral(String),
|
|
|
|
Backtick,
|
|
|
|
Newline,
|
|
|
|
LParen,
|
|
|
|
RParen,
|
|
|
|
LBracket,
|
|
|
|
RBracket,
|
|
|
|
LBrace,
|
|
|
|
RBrace,
|
|
|
|
Period,
|
|
|
|
Comma,
|
|
|
|
Colon,
|
|
|
|
Semicolon,
|
|
|
|
SingleQuote,
|
|
|
|
Identifier(String),
|
|
|
|
Operator(String),
|
|
|
|
NumLiteral(Number),
|
|
|
|
}
|
|
|
|
|
2017-08-31 14:57:56 -07:00
|
|
|
#[allow(dead_code)]
|
2017-08-29 00:28:19 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Number {
|
|
|
|
IntegerRep(String),
|
|
|
|
FloatRep(String)
|
|
|
|
}
|
|
|
|
|
2017-08-31 14:57:56 -07:00
|
|
|
#[allow(dead_code)]
|
2017-08-29 00:28:19 -07:00
|
|
|
pub type AST = Vec<ASTNode>;
|
|
|
|
|
2017-08-31 14:57:56 -07:00
|
|
|
#[allow(dead_code)]
|
2017-08-29 00:28:19 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ASTNode {
|
|
|
|
FunctionDefinition(String, Expression),
|
|
|
|
ImportStatement(String),
|
|
|
|
}
|
|
|
|
|
2017-08-31 14:57:56 -07:00
|
|
|
#[allow(dead_code)]
|
2017-08-29 00:28:19 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Expression {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-31 03:19:36 -07:00
|
|
|
fn tokenize(input: &str) -> Result<Vec<Token>, TokenError> {
|
|
|
|
use self::Token::*;
|
|
|
|
let mut tokens = Vec::new();
|
|
|
|
let mut iter = input.chars().peekable();
|
|
|
|
while let Some(c) = iter.next() {
|
|
|
|
if c == ';' {
|
|
|
|
while let Some(c) = iter.next() {
|
|
|
|
if c == '\n' {
|
|
|
|
break;
|
2017-08-29 00:28:19 -07:00
|
|
|
}
|
2017-08-31 03:19:36 -07:00
|
|
|
}
|
|
|
|
continue;
|
2017-08-29 00:28:19 -07:00
|
|
|
}
|
2017-08-31 03:19:36 -07:00
|
|
|
let cur_tok = match c {
|
|
|
|
c if char::is_whitespace(c) && c != '\n' => continue,
|
|
|
|
'\n' => Newline,
|
|
|
|
'(' => LParen,
|
|
|
|
')' => RParen,
|
|
|
|
'[' => LBracket,
|
|
|
|
']' => RBracket,
|
|
|
|
'{' => LBrace,
|
|
|
|
'}' => RBrace,
|
|
|
|
',' => Comma,
|
|
|
|
':' => Colon,
|
|
|
|
';' => Semicolon,
|
|
|
|
'.' => Period,
|
|
|
|
'`' => Backtick,
|
|
|
|
'\'' => SingleQuote,
|
|
|
|
'"' => {
|
|
|
|
let mut buffer = String::new();
|
|
|
|
loop {
|
|
|
|
match iter.next() {
|
|
|
|
Some(x) if x == '"' => break,
|
|
|
|
Some(x) => buffer.push(x),
|
|
|
|
None => return Err(TokenError::new("Unclosed quote")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StrLiteral(buffer)
|
|
|
|
}
|
|
|
|
c if c.is_digit(10) => {
|
|
|
|
let mut integer = true;
|
|
|
|
let mut buffer = String::new();
|
|
|
|
buffer.push(c);
|
|
|
|
buffer.extend(iter.peeking_take_while(|x| x.is_digit(10)));
|
|
|
|
if let Some(&'.') = iter.peek() {
|
|
|
|
buffer.push(iter.next().unwrap());
|
|
|
|
integer = false;
|
|
|
|
}
|
|
|
|
buffer.extend(iter.peeking_take_while(|x| x.is_digit(10)));
|
|
|
|
let inner = if integer {
|
|
|
|
Number::IntegerRep(buffer)
|
|
|
|
} else {
|
|
|
|
Number::FloatRep(buffer)
|
|
|
|
};
|
|
|
|
NumLiteral(inner)
|
|
|
|
},
|
|
|
|
c if char::is_alphanumeric(c) => {
|
|
|
|
let mut buffer = String::new();
|
|
|
|
buffer.push(c);
|
|
|
|
buffer.extend(iter.peeking_take_while(|x| char::is_alphanumeric(*x)));
|
|
|
|
Identifier(buffer)
|
|
|
|
},
|
|
|
|
c => {
|
|
|
|
let mut buffer = String::new();
|
|
|
|
buffer.push(c);
|
|
|
|
buffer.extend(iter.peeking_take_while(|x| !char::is_whitespace(*x)));
|
|
|
|
Operator(buffer)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
tokens.push(cur_tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(tokens)
|
|
|
|
}
|
2017-08-29 00:28:19 -07:00
|
|
|
|
2017-08-31 03:19:36 -07:00
|
|
|
impl ProgrammingLanguageInterface for Robo {
|
|
|
|
fn get_language_name(&self) -> String {
|
|
|
|
"Robo".to_string()
|
|
|
|
}
|
|
|
|
|
2017-08-31 18:05:10 -07:00
|
|
|
fn evaluate_in_repl(&mut self, input: &str, _eval_options: EvalOptions) -> Vec<String> {
|
2017-08-31 03:19:36 -07:00
|
|
|
let mut output = vec!();
|
|
|
|
let tokens = match tokenize(input) {
|
|
|
|
Ok(tokens) => tokens,
|
|
|
|
Err(e) => {
|
|
|
|
output.push(format!("Tokenize error: {:?}", e));
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
output.push(format!("{:?}", tokens));
|
|
|
|
output
|
|
|
|
}
|
2017-08-29 00:28:19 -07:00
|
|
|
}
|
|
|
|
|