schala/robo/src/lib.rs

173 lines
3.6 KiB
Rust
Raw Normal View History

2018-03-21 01:46:11 -07:00
#![feature(box_patterns)]
extern crate itertools;
extern crate schala_repl;
2017-09-18 19:09:27 -07:00
use itertools::Itertools;
2018-03-20 21:17:46 -07:00
use schala_repl::{ProgrammingLanguageInterface, EvalOptions, LanguageOutput};
2017-08-29 00:28:19 -07:00
pub struct Robo {
}
impl Robo {
2017-08-31 20:59:43 -07:00
pub fn new() -> Robo {
Robo { }
}
2017-08-29 00:28:19 -07:00
}
2017-11-01 22:41:34 -07:00
#[derive(Debug)]
pub struct TokenError {
pub msg: String,
}
impl TokenError {
pub fn new(msg: &str) -> TokenError {
TokenError { msg: msg.to_string() }
}
}
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-10-02 23:07:05 -07:00
fn get_source_file_suffix(&self) -> String {
format!("robo")
}
2018-03-07 22:07:13 -08:00
fn evaluate_in_repl(&mut self, input: &str, _eval_options: &EvalOptions) -> LanguageOutput {
let mut output = LanguageOutput::default();
2017-08-31 03:19:36 -07:00
let tokens = match tokenize(input) {
Ok(tokens) => tokens,
Err(e) => {
2017-08-31 20:59:43 -07:00
output.add_output(format!("Tokenize error: {:?}", e));
2017-08-31 03:19:36 -07:00
return output;
}
};
2017-08-31 20:59:43 -07:00
output.add_output(format!("{:?}", tokens));
2017-08-31 03:19:36 -07:00
output
}
2017-08-29 00:28:19 -07:00
}