lexer work

This commit is contained in:
TerraMaster85 2024-01-15 20:51:12 -05:00
parent 8d4226b185
commit b75812aa59
8 changed files with 149 additions and 40 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
kabel-rs/target

0
kabel-rs/src/errors.rs Normal file
View File

View File

@ -1,40 +1,45 @@
pub mod variables;
use crate::lexutil;
use lexutil::{Token, StatementToken};
use std::error::Error;
enum ArithmeticOperatorToken {
Add
Subtract
Multiply
Divide
Exponentiate
Modulus
}
pub fn lexer(text_source: &str) -> Result<Vec<Token>, Box<dyn Error>> {
println!("Lexing!");
let mut lexed: Vec<Token> = Vec::new();
enum BooleanOperatorToken {
And
Or
}
let mut current_token = String::new();
enum LiteralToken {
Str(String)
Num(f64)
}
for (i, c) in text_source.chars().enumerate() {
current_token.push(c);
// Known meaningful tokens
match current_token.as_str() {
";" => {
lexed.push(Token::Statement(StatementToken::Terminator));
current_token = String::new();
}
"if " => {
lexed.push(Token::Statement(StatementToken::Conditional));
current_token = String::new();
continue;
}
"to " => {
lexed.push(Token::Statement(StatementToken::FunctionDef));
current_token = String::new();
continue;
}
"for " => {
lexed.push(Token::Statement(StatementToken::ForLoop));
current_token = String::new();
continue;
}
"while " => {
lexed.push(Token::Statement(StatementToken::WhileLoop));
current_token = String::new();
continue;
}
&_ => {}
}
}
enum StatementToken {
If
Foreach
While
To
}
enum BracketToken {
Open
Close
}
enum Token {
Literal(LiteralToken)
ArithmeticOperator(ArithmeticOperatorToken)
Statement(StatementToken)
Bracket(BracketToken)
Variable(variables::VariableToken)
Ok(lexed)
}

46
kabel-rs/src/lexutil.rs Normal file
View File

@ -0,0 +1,46 @@
use crate::variables;
// parts of Token
pub enum ArithmeticOperatorToken {
Add,
Subtract,
Multiply,
Divide,
Exponentiate,
Modulus,
}
pub enum BooleanOperatorToken {
And,
Or,
}
pub enum LiteralToken {
Str(String),
Num(f64),
}
pub enum StatementToken {
Conditional,
ForLoop,
WhileLoop,
FunctionDef,
Terminator,
}
pub enum BracketToken {
Open,
Close,
}
pub enum Token {
Literal(LiteralToken),
ArithmeticOperator(ArithmeticOperatorToken),
Statement(StatementToken),
Bracket(BracketToken),
Variable(variables::VariableToken),
}

View File

@ -1,9 +1,51 @@
use std::env;
use std::fs;
use std::error::Error;
use std::fs::File;
use std::io::{self, Read};
use lexutil::Token;
pub mod lexer;
mod lexer;
mod lexutil;
mod modules;
mod variables;
fn main() {
let args: Vec<String> = env::args().collect();
dbg!(&args[1]);
fn main() -> Result<(), Box<dyn Error>> {
let argv: Vec<String> = env::args().collect();
if argv.len() != 2 {
println!("Usage: {} <file.kab>", &argv[0]);
std::process::exit(1);
}
let mut source_fd = match File::open(&argv[1]) {
Err(e) if e.kind() == io::ErrorKind::NotFound => {
std::process::exit(1);
}
Err(e) => {
eprintln!(
"Error: Tried opening `{}' and got unexpected error: {}",
argv[1],
e.kind()
);
std::process::exit(1);
}
Ok(fd) => fd,
};
let mut text_source = String::new();
source_fd.read_to_string(&mut text_source)?;
dbg!(&text_source);
// Lex!
let lexed: Vec<Token> = match lexer::lexer(&text_source) {
Err(e) => {
eprintln!(
"Lexer fail with {}",
e
);
std::process::exit(1);
}
Ok(lexed) => lexed
};
Ok(())
}

5
kabel-rs/src/modules.rs Normal file
View File

@ -0,0 +1,5 @@
struct Thruster {}
pub enum ModuleVar {
Thruster(Thruster),
}

3
kabel-rs/src/test.kab Normal file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env kabel
declare p = 5;
debug: print using (3 + p) * 2;

View File

@ -0,0 +1,7 @@
use crate::modules;
pub enum VariableToken {
Module(modules::ModuleVar),
Num(f64),
Str(String),
}