added chars

This commit is contained in:
MCorange
2023-03-20 16:33:08 +02:00
parent d536bfcf31
commit 427f904d0b
5 changed files with 37 additions and 14 deletions

View File

@@ -140,7 +140,7 @@ pub enum TokenType {
Word,
Int,
String,
//TODO: Add char
Char
}
impl Token {
@@ -159,6 +159,7 @@ impl TokenType {
TokenType::Word => "Word",
TokenType::Int => "Int",
TokenType::String => "String",
TokenType::Char => "Char"
}.to_string()
}
}

View File

@@ -11,9 +11,12 @@ fn lex_word(s: String, tok_type: TokenType) -> (TokenType, String) {
return (TokenType::Word, s);
},
s if tok_type == TokenType::String => {
return (tok_type, s);
return (TokenType::String, s);
}
_ => panic!()
s if tok_type == TokenType::Char=> {
return (TokenType::Char, s);
}
_ => unreachable!()
}
}
@@ -52,6 +55,22 @@ fn lex_line(text: String) -> Result<Vec<(u32, String, TokenType)>> {
}
col = find_col(text.clone(), col_end + 1, |x, _| !x.is_whitespace())?;
} else if &text[(col as usize)..(col + 1) as usize] == "'"{
col_end = find_col(text.clone(), col + 1, |x, x2| x == '\'' && x2 != '\\')?;
let t = &text[((col + 1) as usize)..(col_end as usize)];
let t = t.replace("\\n", "\n")
.replace("\\t", "\t")
.replace("\\r", "\r")
.replace("\\\'", "\'")
.replace("\\\"", "\"")
.replace("\\0", "\0");
if !t.is_empty() {
tokens.push((col, t.to_string(), TokenType::Char));
}
col = find_col(text.clone(), col_end + 1, |x, _| !x.is_whitespace())?;
} else {
col_end = find_col(text.clone(), col, |x, _| x.is_whitespace())?;

View File

@@ -92,6 +92,15 @@ impl Parser {
TokenType::String => {
tokens.push(Operator::new(OpType::PushStr, 0, token.text.clone(), token.file.clone(), token.line, token.col));
}
TokenType::Char => {
let c = token.text.clone();
if c.len() != 1 {
lerror!(&token.loc(), "Chars can only be of lenght 1, got {}", c.len());
return Err(eyre!(""));
}
tokens.push(Operator::new(OpType::PushInt, token.text.chars().next().unwrap() as i64, String::new(), token.file.clone(), token.line, token.col));
}
};