diff --git a/sm-test-files/actual-testing/math.sm b/sm-test-files/actual-testing/math.sm new file mode 100644 index 0000000..af225e0 --- /dev/null +++ b/sm-test-files/actual-testing/math.sm @@ -0,0 +1,4 @@ +2+5 +3 + 6 +6 / 4 +2.5 + 1.7 diff --git a/src/main.rs b/src/main.rs index fe0c251..8731b4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,26 @@ use std::env; use std::fs; -mod preprocessor; +mod shell; +mod parsing; + +use parsing::preprocessor::preprocess; fn main() { let args: Vec = env::args().collect(); - let file = fs::read_to_string(&args[1]).expect("Couldnt read file:"); - println!("{:?}", preprocessor::preprocess(file)); + let run_shell = String::from("RUN_SH"); + let path: &String = match args.get(1) { + Some(path) => path, + None => &run_shell, + }; + + if path == "RUN_SH" { + shell::run_shell(); + } else { + let file = fs::read_to_string(path).expect("Couldnt read file:"); + + let code = preprocess(file); + } } -//fn parse_and_interpret(code: String) { - -//} - diff --git a/src/parsing/lexer.rs b/src/parsing/lexer.rs new file mode 100644 index 0000000..260b4ba --- /dev/null +++ b/src/parsing/lexer.rs @@ -0,0 +1,62 @@ +use preprocessor::preprocess; + +struct Lexer { + code: String, + pos: usize, + current: Option, +} + +impl Lexer { + pub fn new(code: String) -> Lexer { + Lexer { + code, + pos: 1, + current: None, + } + } + + pub fn next(&mut self) { + self.pos += 1; + self.current = if self.pos < self.code.len() { + self.code.chars().nth(self.pos) + } else { + None + }; + } + + pub fn tokenize(&mut self) { + let mut tokens: Vec = vec![]; + self.code = preprocess(self.code); + + loop { + //let token = match self.current { + //Some('+') => Token::ADD, + //Some('-') => Token::SUBTRACT, + //Some('*') => Token::MULTIPLY, + //Some('/') => Token::DIVIDE, + //Some('(') => Token::LBRACK, + //Some(')') => Token::RBRACK, + //Some(' ') => continue, + //Some('\n') => continue, + //None => break, + //Some(_) => continue, + //}; + + tokens.push(match self.current { + Some('+') => Token::ADD, + Some('-') => Token::SUBTRACT, + Some('*') => Token::MULTIPLY, + Some('/') => Token::DIVIDE, + Some('(') => Token::LBRACK, + Some(')') => Token::RBRACK, + Some(' ') => continue, + Some('\n') => continue, + None => break, + Some(_) => continue, + }); + self.next(); + } + + println!("{:?}", tokens); + } +} diff --git a/src/parsing/mod.rs b/src/parsing/mod.rs new file mode 100644 index 0000000..20299d9 --- /dev/null +++ b/src/parsing/mod.rs @@ -0,0 +1,3 @@ +pub mod lexer; +pub mod preprocessor; +pub mod token; diff --git a/src/preprocessor.rs b/src/parsing/preprocessor.rs similarity index 100% rename from src/preprocessor.rs rename to src/parsing/preprocessor.rs diff --git a/src/parsing/token.rs b/src/parsing/token.rs new file mode 100644 index 0000000..cd6f344 --- /dev/null +++ b/src/parsing/token.rs @@ -0,0 +1,13 @@ +pub enum Token { + INT, + FLOAT, + ADD, + SUBTRACT, + MULTIPLY, + DIVIDE, + LBRACK, + RBRACK, +} + +impl Token {} + diff --git a/src/shell.rs b/src/shell.rs new file mode 100644 index 0000000..f918835 --- /dev/null +++ b/src/shell.rs @@ -0,0 +1,18 @@ +use std::io; + +pub fn run_shell() { + 'shell: loop { + let string = get_line(); + if string.eq("q\n") || string.eq("quit\n") { + break 'shell; + } + println!("{}", string); + } +} + +fn get_line() -> String { + let mut line = String::new(); + io::stdin().read_line(&mut line).unwrap(); + line +} + diff --git a/src/stdlib/io.rs b/src/stdlib/io.rs new file mode 100644 index 0000000..e69de29