From e220900ac3d0ace899a392b99f28ea0eaf5bb5a6 Mon Sep 17 00:00:00 2001 From: sofia Date: Tue, 22 Jul 2025 14:44:28 +0300 Subject: [PATCH] Start adding chars --- reid/src/ast/mod.rs | 1 + reid/src/ast/process.rs | 1 + reid/src/codegen.rs | 1 + reid/src/lexer.rs | 21 +++++++++++++++------ reid/src/mir/fmt.rs | 1 + reid/src/mir/implement.rs | 9 ++++++--- reid/src/mir/mod.rs | 2 ++ reid_src/cast.reid | 3 +++ reid_src/char.reid | 5 +++++ 9 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 reid_src/char.reid diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index afa119c..b14bf49 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -44,6 +44,7 @@ pub enum Literal { Decimal(f64), Bool(bool), String(String), + Char(String), } #[derive(Debug, Clone)] diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index e8f34d9..3d94f58 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -289,6 +289,7 @@ impl ast::Literal { ast::Literal::Bool(v) => mir::Literal::Bool(*v), ast::Literal::String(val) => mir::Literal::String(val.clone()), ast::Literal::Decimal(v) => mir::Literal::Vague(mir::VagueLiteral::Decimal(*v)), + ast::Literal::Char(inner) => mir::Literal::Char(inner.chars().next().unwrap()), } } } diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index bb2a709..4077bcc 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -1199,6 +1199,7 @@ impl mir::Literal { mir::Literal::F80(val) => ConstValue::F80(val), mir::Literal::F128(val) => ConstValue::F128(val), mir::Literal::F128PPC(val) => ConstValue::F128PPC(val), + mir::Literal::Char(c) => todo!(), }) } } diff --git a/reid/src/lexer.rs b/reid/src/lexer.rs index 769aa40..5a2a7db 100644 --- a/reid/src/lexer.rs +++ b/reid/src/lexer.rs @@ -1,4 +1,4 @@ -use std::{fmt::Debug, str::Chars}; +use std::{fmt::Debug, hint::unreachable_unchecked, str::Chars}; static DECIMAL_NUMERICS: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; @@ -8,7 +8,9 @@ pub enum Token { Identifier(String), /// Number with at most one decimal point DecimalValue(u64), - /// Some string literal that was surrounded by "quotes". + /// Some character literal that was surrounded by 'single-quotes'. + CharLit(String), + /// Some string literal that was surrounded by "double-quotes". StringLit(String), // Keywords @@ -112,6 +114,7 @@ impl ToString for Token { match &self { Token::Identifier(ident) => ident.clone(), Token::DecimalValue(val) => val.to_string(), + Token::CharLit(lit) => format!("\'{}\'", lit), Token::StringLit(lit) => format!("\"{}\"", lit), Token::LetKeyword => String::from("let"), Token::MutKeyword => String::from("mut"), @@ -245,10 +248,12 @@ pub fn tokenize>(to_tokenize: T) -> Result, Error } continue; } - '\"' => { + '\"' | '\'' => { let mut value = String::new(); let mut ignore_next = false; - while cursor.first().is_some() && (cursor.first() != Some('\"') || ignore_next) { + while cursor.first().is_some() + && (cursor.first() != Some(*character) || ignore_next) + { if cursor.first() == Some('\\') && !ignore_next { cursor.next(); // Consume backslash anjd always add next character ignore_next = true; @@ -257,12 +262,16 @@ pub fn tokenize>(to_tokenize: T) -> Result, Error value += &cursor.next().unwrap().to_string(); } } - if cursor.first() == Some('\"') { + if cursor.first() == Some(*character) { cursor.next(); } else { return Err(Error::MissingQuotation(position)); } - Token::StringLit(value) + match character { + '\'' => Token::StringLit(value), + '\"' => Token::StringLit(value), + _ => unsafe { unreachable_unchecked() }, + } } // "words" c if c.is_alphabetic() => { diff --git a/reid/src/mir/fmt.rs b/reid/src/mir/fmt.rs index b79ecb1..c0d2bf5 100644 --- a/reid/src/mir/fmt.rs +++ b/reid/src/mir/fmt.rs @@ -269,6 +269,7 @@ impl Display for Literal { Literal::F80(val) => write!(f, "{}f80", val), Literal::F128(val) => write!(f, "{}f128", val), Literal::F128PPC(val) => write!(f, "{}f128ppc", val), + Literal::Char(c) => std::fmt::Debug::fmt(c, f), } } } diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index bc6a25d..5b758b9 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -1,6 +1,4 @@ -use crate::util::try_all; - -use super::{pass::ScopeFunction, typecheck::ErrorKind, typerefs::TypeRefs, VagueType as Vague, *}; +use super::{typecheck::ErrorKind, typerefs::TypeRefs, VagueType as Vague, *}; #[derive(Debug, Clone)] pub enum ReturnTypeOther { @@ -81,8 +79,12 @@ impl TypeKind { let other_cat = other.category(); match (self, other) { (TypeKind::UserPtr(_), TypeKind::UserPtr(_)) => Ok(other.clone()), + (TypeKind::Str, TypeKind::U8) => Ok(other.clone()), + (TypeKind::U8, TypeKind::Str) => Ok(other.clone()), _ => match (&self_cat, &other_cat) { (TypeCategory::Integer, TypeCategory::Integer) => Ok(other.clone()), + (TypeCategory::Integer, TypeCategory::Real) => Ok(other.clone()), + (TypeCategory::Real, TypeCategory::Integer) => Ok(other.clone()), (TypeCategory::Real, TypeCategory::Real) => Ok(other.clone()), _ => Err(ErrorKind::NotCastableTo(self.clone(), other.clone())), }, @@ -610,6 +612,7 @@ impl Literal { Literal::F80(_) => None, Literal::F128(_) => None, Literal::F128PPC(_) => None, + Literal::Char(_) => None, } } } diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index b734c7e..b97a82f 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -162,6 +162,7 @@ pub enum Literal { F128PPC(f64), Bool(bool), String(String), + Char(char), Vague(VagueLiteral), } @@ -195,6 +196,7 @@ impl Literal { Literal::F80(_) => TypeKind::F80, Literal::F128(_) => TypeKind::F128, Literal::F128PPC(_) => TypeKind::F128PPC, + Literal::Char(_) => todo!(), } } } diff --git a/reid_src/cast.reid b/reid_src/cast.reid index 120d0e5..fc7ba7a 100644 --- a/reid_src/cast.reid +++ b/reid_src/cast.reid @@ -1,6 +1,7 @@ // Arithmetic, function calls and imports! import std::allocate; +import std::print; fn other() -> i16 { return 6; @@ -8,6 +9,8 @@ fn other() -> i16 { fn main() -> u32 { let value = other() as u32; + let other_value = other() as f32; + let same_value = other() as i16; let v = (allocate(4) as *u32); diff --git a/reid_src/char.reid b/reid_src/char.reid new file mode 100644 index 0000000..fd74074 --- /dev/null +++ b/reid_src/char.reid @@ -0,0 +1,5 @@ +// Arithmetic, function calls and imports! + +pub fn main() -> char { + return 'b'; +}