Start adding chars

This commit is contained in:
Sofia 2025-07-22 14:44:28 +03:00
parent 9c0d373f9d
commit e220900ac3
9 changed files with 35 additions and 9 deletions

View File

@ -44,6 +44,7 @@ pub enum Literal {
Decimal(f64),
Bool(bool),
String(String),
Char(String),
}
#[derive(Debug, Clone)]

View File

@ -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()),
}
}
}

View File

@ -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!(),
})
}
}

View File

@ -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<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, 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<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, 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() => {

View File

@ -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),
}
}
}

View File

@ -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,
}
}
}

View File

@ -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!(),
}
}
}

View File

@ -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);

5
reid_src/char.reid Normal file
View File

@ -0,0 +1,5 @@
// Arithmetic, function calls and imports!
pub fn main() -> char {
return 'b';
}