From 178434171e03ca40a290ccb6d615f052ba214928 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 23 Jan 2017 19:45:26 -0800 Subject: [PATCH] Cleaning up some types --- src/language.rs | 6 ++++++ src/main.rs | 2 +- src/schala_lang/compilation.rs | 1 - src/schala_lang/mod.rs | 2 +- src/schala_lang/tokenizer.rs | 27 +++++++++------------------ 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/language.rs b/src/language.rs index fe562a3..9f2cdbc 100644 --- a/src/language.rs +++ b/src/language.rs @@ -4,6 +4,12 @@ pub struct TokenError { pub msg: String, } +impl TokenError { + pub fn new(msg: &str) -> TokenError { + TokenError { msg: msg.to_string() } + } +} + pub struct ParseError { pub msg: String, } diff --git a/src/main.rs b/src/main.rs index 072d2d0..edd1db0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ mod schala_lang; use schala_lang::eval::Evaluator; use schala_lang::Schala; -use language::{ProgrammingLanguage, ParseError, TokenError, LLVMCodeString}; +use language::{ProgrammingLanguage, LLVMCodeString}; mod language; mod llvm_wrap; diff --git a/src/schala_lang/compilation.rs b/src/schala_lang/compilation.rs index 5c36955..2949212 100644 --- a/src/schala_lang/compilation.rs +++ b/src/schala_lang/compilation.rs @@ -1,7 +1,6 @@ extern crate llvm_sys; use std::collections::HashMap; -use std::fs::File; use self::llvm_sys::prelude::*; use self::llvm_sys::{LLVMIntPredicate, LLVMRealPredicate}; diff --git a/src/schala_lang/mod.rs b/src/schala_lang/mod.rs index 1e56691..118945b 100644 --- a/src/schala_lang/mod.rs +++ b/src/schala_lang/mod.rs @@ -12,7 +12,7 @@ impl<'a> ProgrammingLanguage> for Schala { type AST = parser::AST; fn tokenize(input: &str) -> Result, TokenError> { - tokenizer::tokenize(input).map_err(|x| TokenError { msg: x.msg }) + tokenizer::tokenize(input) } fn parse(input: Vec) -> Result { diff --git a/src/schala_lang/tokenizer.rs b/src/schala_lang/tokenizer.rs index e708959..a1a9235 100644 --- a/src/schala_lang/tokenizer.rs +++ b/src/schala_lang/tokenizer.rs @@ -5,6 +5,8 @@ use std::str::Chars; use self::itertools::Itertools; use std::rc::Rc; +use language::TokenError; + #[derive(Debug, Clone, PartialEq)] pub enum Token { Newline, @@ -38,18 +40,7 @@ pub enum Kw { Null, } -pub type TokenizeResult = Result, TokenizeError>; - -#[derive(Debug)] -pub struct TokenizeError { - pub msg: String, -} - -impl TokenizeError { - fn new(msg: &str) -> TokenizeError { - TokenizeError { msg: msg.to_string() } - } -} +pub type TokenizeResult = Result, TokenError>; fn is_digit(c: &char) -> bool { c.is_digit(10) @@ -89,27 +80,27 @@ pub fn tokenize(input: &str) -> TokenizeResult { Ok(tokens) } -fn tokenize_str(iter: &mut Peekable) -> Result { +fn tokenize_str(iter: &mut Peekable) -> Result { let mut buffer = String::new(); loop { // TODO handle string escapes, interpolation match iter.next() { Some(x) if x == '"' => break, Some(x) => buffer.push(x), - None => return Err(TokenizeError::new("Unclosed quote")), + None => return Err(TokenError::new("Unclosed quote")), } } Ok(Token::StrLiteral(Rc::new(buffer))) } -fn tokenize_operator(c: char, iter: &mut Peekable) -> Result { +fn tokenize_operator(c: char, iter: &mut Peekable) -> Result { let mut buffer = String::new(); buffer.push(c); buffer.extend(iter.peeking_take_while(|x| !char::is_alphanumeric(*x) && !char::is_whitespace(*x))); Ok(Token::Operator(OpTok(Rc::new(buffer)))) } -fn tokenize_number_or_period(c: char, iter: &mut Peekable) -> Result { +fn tokenize_number_or_period(c: char, iter: &mut Peekable) -> Result { if c == '.' && !iter.peek().map_or(false, is_digit) { return Ok(Token::Period); } @@ -120,11 +111,11 @@ fn tokenize_number_or_period(c: char, iter: &mut Peekable) -> Result() { Ok(f) => Ok(Token::NumLiteral(f)), - Err(_) => Err(TokenizeError::new("Failed to parse digit")), + Err(_) => Err(TokenError::new("Failed to parse digit")), } } -fn tokenize_identifier(c: char, iter: &mut Peekable) -> Result { +fn tokenize_identifier(c: char, iter: &mut Peekable) -> Result { fn ends_identifier(c: &char) -> bool { let c = *c; char::is_whitespace(c) || is_digit(&c) || c == ';' || c == '(' || c == ')' ||