added quitting to shell and started lexer
This commit is contained in:
parent
0eed22213f
commit
d5da7b0f26
8 changed files with 117 additions and 7 deletions
4
sm-test-files/actual-testing/math.sm
Normal file
4
sm-test-files/actual-testing/math.sm
Normal file
|
@ -0,0 +1,4 @@
|
|||
2+5
|
||||
3 + 6
|
||||
6 / 4
|
||||
2.5 + 1.7
|
24
src/main.rs
24
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<String> = 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) {
|
||||
|
||||
//}
|
||||
|
||||
|
|
62
src/parsing/lexer.rs
Normal file
62
src/parsing/lexer.rs
Normal file
|
@ -0,0 +1,62 @@
|
|||
use preprocessor::preprocess;
|
||||
|
||||
struct Lexer {
|
||||
code: String,
|
||||
pos: usize,
|
||||
current: Option<char>,
|
||||
}
|
||||
|
||||
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<Token> = 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);
|
||||
}
|
||||
}
|
3
src/parsing/mod.rs
Normal file
3
src/parsing/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod lexer;
|
||||
pub mod preprocessor;
|
||||
pub mod token;
|
13
src/parsing/token.rs
Normal file
13
src/parsing/token.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
pub enum Token {
|
||||
INT,
|
||||
FLOAT,
|
||||
ADD,
|
||||
SUBTRACT,
|
||||
MULTIPLY,
|
||||
DIVIDE,
|
||||
LBRACK,
|
||||
RBRACK,
|
||||
}
|
||||
|
||||
impl Token {}
|
||||
|
18
src/shell.rs
Normal file
18
src/shell.rs
Normal file
|
@ -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
|
||||
}
|
||||
|
0
src/stdlib/io.rs
Normal file
0
src/stdlib/io.rs
Normal file
Loading…
Reference in a new issue