compiler working for some mathematical expressions, function calls and
simple conditionals
This commit is contained in:
@@ -10,10 +10,13 @@ pub enum TokenType {
|
||||
Else,
|
||||
While,
|
||||
Return,
|
||||
Include,
|
||||
|
||||
// Identifiers and literals
|
||||
Identifier(String),
|
||||
Number(i32),
|
||||
String(String),
|
||||
Char(char),
|
||||
|
||||
// Operators
|
||||
Plus,
|
||||
@@ -35,10 +38,22 @@ pub enum TokenType {
|
||||
RBrace,
|
||||
Semicolon,
|
||||
Comma,
|
||||
Colon,
|
||||
Namespace,
|
||||
|
||||
Eof,
|
||||
}
|
||||
|
||||
pub enum Type {
|
||||
Int32,
|
||||
Int16,
|
||||
Int8,
|
||||
Uint32,
|
||||
Uint16,
|
||||
Uint8,
|
||||
Char,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Token {
|
||||
pub token_type: TokenType,
|
||||
@@ -150,6 +165,45 @@ impl Lexer {
|
||||
ident
|
||||
}
|
||||
|
||||
fn read_string(&mut self) -> Result<String, String> {
|
||||
let mut string = String::new();
|
||||
self.advance(); // Consume the opening quote
|
||||
|
||||
while let Some(ch) = self.peek(0) {
|
||||
if ch == '"' {
|
||||
self.advance(); // Consume the closing quote
|
||||
return Ok(string);
|
||||
} else if ch == '\\' {
|
||||
self.advance(); // Consume the backslash
|
||||
if let Some(escaped_char) = self.peek(0) {
|
||||
string.push(escaped_char);
|
||||
self.advance();
|
||||
}
|
||||
} else {
|
||||
string.push(ch);
|
||||
self.advance();
|
||||
}
|
||||
}
|
||||
|
||||
Err(String::from("Unexpected EOF"))
|
||||
}
|
||||
|
||||
fn read_char(&mut self) -> Result<char, String> {
|
||||
self.advance(); // Consume the opening quote
|
||||
|
||||
if let Some(ch) = self.peek(0) {
|
||||
self.advance();
|
||||
if self.peek(0) == Some('\'') {
|
||||
self.advance();
|
||||
return Ok(ch);
|
||||
} else {
|
||||
Err(String::from("expected closing quote"))
|
||||
}
|
||||
} else {
|
||||
Err(String::from("expected character"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tokenize(&mut self) -> Result<Vec<Token>, String> {
|
||||
let mut tokens = Vec::new();
|
||||
|
||||
@@ -168,6 +222,12 @@ impl Lexer {
|
||||
let token_type = if ch.is_ascii_digit() {
|
||||
let num = self.read_number();
|
||||
TokenType::Number(num)
|
||||
} else if ch == '"' {
|
||||
let string = self.read_string()?;
|
||||
TokenType::String(string)
|
||||
} else if ch == '\'' {
|
||||
let char = self.read_char()?;
|
||||
TokenType::Char(char)
|
||||
} else if ch.is_alphabetic() || ch == '_' {
|
||||
let ident = self.read_identifier();
|
||||
match ident.as_str() {
|
||||
@@ -176,10 +236,20 @@ impl Lexer {
|
||||
"else" => TokenType::Else,
|
||||
"while" => TokenType::While,
|
||||
"return" => TokenType::Return,
|
||||
"include" => TokenType::Include,
|
||||
_ => TokenType::Identifier(ident),
|
||||
}
|
||||
} else {
|
||||
match ch {
|
||||
':' if self.peek(1) == Some(':') => {
|
||||
self.advance();
|
||||
self.advance();
|
||||
TokenType::Namespace
|
||||
}
|
||||
':' => {
|
||||
self.advance();
|
||||
TokenType::Colon
|
||||
}
|
||||
'=' if self.peek(1) == Some('=') => {
|
||||
self.advance();
|
||||
self.advance();
|
||||
|
||||
Reference in New Issue
Block a user