diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c0d979a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +kabel-rs/target diff --git a/kabel-rs/src/errors.rs b/kabel-rs/src/errors.rs new file mode 100644 index 0000000..e69de29 diff --git a/kabel-rs/src/lexer.rs b/kabel-rs/src/lexer.rs index b41734a..87eb9a9 100644 --- a/kabel-rs/src/lexer.rs +++ b/kabel-rs/src/lexer.rs @@ -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, Box> { + println!("Lexing!"); + let mut lexed: Vec = 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) } diff --git a/kabel-rs/src/lexutil.rs b/kabel-rs/src/lexutil.rs new file mode 100644 index 0000000..7b0e0ab --- /dev/null +++ b/kabel-rs/src/lexutil.rs @@ -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), +} + + diff --git a/kabel-rs/src/main.rs b/kabel-rs/src/main.rs index d73aafb..ac3a9e0 100644 --- a/kabel-rs/src/main.rs +++ b/kabel-rs/src/main.rs @@ -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 = env::args().collect(); - dbg!(&args[1]); +fn main() -> Result<(), Box> { + let argv: Vec = env::args().collect(); + if argv.len() != 2 { + println!("Usage: {} ", &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 = match lexer::lexer(&text_source) { + Err(e) => { + eprintln!( + "Lexer fail with {}", + e + ); + std::process::exit(1); + } + Ok(lexed) => lexed + }; + + Ok(()) } diff --git a/kabel-rs/src/modules.rs b/kabel-rs/src/modules.rs new file mode 100644 index 0000000..20dfdc9 --- /dev/null +++ b/kabel-rs/src/modules.rs @@ -0,0 +1,5 @@ +struct Thruster {} + +pub enum ModuleVar { + Thruster(Thruster), +} diff --git a/kabel-rs/src/test.kab b/kabel-rs/src/test.kab new file mode 100644 index 0000000..c88670a --- /dev/null +++ b/kabel-rs/src/test.kab @@ -0,0 +1,3 @@ +#!/usr/bin/env kabel +declare p = 5; +debug: print using (3 + p) * 2; diff --git a/kabel-rs/src/variables.rs b/kabel-rs/src/variables.rs new file mode 100644 index 0000000..601b61a --- /dev/null +++ b/kabel-rs/src/variables.rs @@ -0,0 +1,7 @@ +use crate::modules; + +pub enum VariableToken { + Module(modules::ModuleVar), + Num(f64), + Str(String), +}