Skip to content

Commit

Permalink
Add logical operators
Browse files Browse the repository at this point in the history
LunaStev committed Nov 14, 2024
1 parent be3984e commit 143538e
Showing 3 changed files with 135 additions and 31 deletions.
146 changes: 121 additions & 25 deletions src/lexer.rs
Original file line number Diff line number Diff line change
@@ -14,30 +14,39 @@ pub enum TokenType {
CONTINUE,
PRINT,
PRINTLN,
AND, // &&
OR, // ||
NOT, // !=
IN, // in
LOGICAL_AND, // &&
BITWISE_AND, // &
LOGICAL_OR, // ||
BITWISE_OR, // |
NOT_EQUAL, // !=
XOR, // ^
XNOR, // ~^
NAND, // !&
NOR, // !|
NOT, // !
IN, // in
IS, // is
IDENTIFIER(String),
STRING(String),
NUMBER(i64),
PLUS, // +
MINUS, // -
STAR, // *
DIV, // /
ASSIGN, // =
COMMA, // ,
SEMICOLON, // ;
COLON, // :
LCHEVR, // <
RCHEVR, // >
LPAREN, // (
RPAREN, // )
LBRACE, // {
RBRACE, // }
LBRACK, // [
RBRACK, // ]
EOF, // End of file
PLUS, // +
MINUS, // -
STAR, // *
DIV, // /
EQUAL, // =
EQUAL_TWO, // ==
COMMA, // ,
SEMICOLON, // ;
COLON, // :
LCHEVR, // <
RCHEVR, // >
LPAREN, // (
RPAREN, // )
LBRACE, // {
RBRACE, // }
LBRACK, // [
RBRACK, // ]
EOF, // End of file
}

#[derive(Debug)]
@@ -215,10 +224,95 @@ impl<'a> Lexer<'a> {
lexeme: "]".to_string(),
line: self.line,
},
'=' => Token {
token_type: TokenType::ASSIGN,
lexeme: "=".to_string(),
line: self.line,
'=' => {
if self.match_next('=') {
Token {
token_type: TokenType::EQUAL_TWO,
lexeme: "==".to_string(),
line: self.line,
}
} else {
Token {
token_type: TokenType::EQUAL,
lexeme: "=".to_string(),
line: self.line,
}
}
},
'&' => {
if self.match_next('&') {
Token {
token_type: TokenType::LOGICAL_AND,
lexeme: "&&".to_string(),
line: self.line,
}
} else {
Token {
token_type: TokenType::BITWISE_AND,
lexeme: "&".to_string(),
line: self.line,
}
}
},
'|' => {
if self.match_next('|') {
Token {
token_type: TokenType::LOGICAL_OR,
lexeme: "||".to_string(),
line: self.line,
}
} else {
Token {
token_type: TokenType::BITWISE_OR,
lexeme: "|".to_string(),
line: self.line,
}
}
},
'!' => {
if self.match_next('=') {
Token {
token_type: TokenType::NOT_EQUAL,
lexeme: "!=".to_string(),
line: self.line,
}
} else if self.match_next('&') {
Token {
token_type: TokenType::NAND,
lexeme: "!&".to_string(),
line: self.line,
}
} else if self.match_next('|') {
Token {
token_type: TokenType::NOR,
lexeme: "!|".to_string(),
line: self.line,
}
} else {
Token {
token_type: TokenType::NOT,
lexeme: "!".to_string(),
line: self.line,
}
}
},
'^' => {
Token {
token_type: TokenType::XOR,
lexeme: "^".to_string(),
line: self.line,
}
},
'~' => {
if self.match_next('^') {
Token {
token_type: TokenType::XNOR,
lexeme: "~^".to_string(),
line: self.line,
}
} else {
panic!("Unexpected character: {}", c);
}
},
'"' => {
return Token {
@@ -237,6 +331,8 @@ impl<'a> Lexer<'a> {
"else" => TokenType::ELSE,
"while" => TokenType::WHILE,
"for" => TokenType::FOR,
"in" => TokenType::IN,
"is" => TokenType::IS,
"import" => TokenType::IMPORT,
"return" => TokenType::RETURN,
"continue" => TokenType::CONTINUE,
18 changes: 13 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -8,22 +8,30 @@ use parser::Parser;

fn main() {
// Sample code to parse and run
let code = r#"
let code_a = r#"
fun main() {
var a = 10;
var b = 20;
print("Hello World\n");
println("Hello World");
if (a != b) {
println("Hello");
} else if (a > b || a == b) {
println("a >= b");
}
}
"#;

// Create a Lexer
let mut lexer = Lexer::new(code);
let mut lexer = Lexer::new(code_a);

// Tokenize the source code
let tokens = lexer.tokenize();

// Create a Parser
let mut parser = Parser::new(lexer);
// let mut parser = Parser::new(lexer);

// Start parsing the tokens
parser.parse();
}
// parser.parse();
println!("{:?}", tokens);
}
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
@@ -85,7 +85,7 @@ impl<'a> Parser<'a> {
panic!("Expected variable name after 'var'");
};

if self.current_token.token_type != TokenType::ASSIGN {
if self.current_token.token_type != TokenType::EQUAL {
panic!("Expected '=' after variable name");
}
self.advance();

0 comments on commit 143538e

Please sign in to comment.