Add type system and refator for having only epression
This commit is contained in:
Giulio Agostini
2026-07-06 10:43:17 +02:00
parent 9f15a00b98
commit 842216729b
23 changed files with 940 additions and 979 deletions
+29 -1
View File
@@ -16,7 +16,7 @@ pub struct Lexer {
fn get_keyword_token(word: &str) -> Option<TokenType> {
match word {
"and" => Some(TokenType::And),
"class" => Some(TokenType::Class),
"struct" => Some(TokenType::Struct),
"do" => Some(TokenType::StartBlock),
"end" => Some(TokenType::EndBlock),
"false" => Some(TokenType::False),
@@ -514,4 +514,32 @@ mod tests {
assert_eq!(tokens[1].source_slice.start_position.line, 1);
assert_eq!(tokens[1].source_slice.start_position.column, 0);
}
#[test]
fn handle_struct() {
let tokens = lex("Cosa :: struct {field1: Number, field2: String}");
let tokens_tyepe: Vec<TokenType> = tokens.iter().map(|x| x.token_type.clone()).collect();
let tokent_for_check = vec![
TokenType::Identifier,
TokenType::Colon,
TokenType::Colon,
TokenType::Struct,
TokenType::LeftBrace,
TokenType::Identifier,
TokenType::Colon,
TokenType::Identifier,
TokenType::Comma,
TokenType::Identifier,
TokenType::Colon,
TokenType::Identifier,
TokenType::RightBrace,
TokenType::Eof,
];
assert_eq!(tokens_tyepe, tokent_for_check);
assert_eq!(tokens[0].lexeme, "Cosa");
assert_eq!(tokens[5].lexeme, "field1");
assert_eq!(tokens[7].lexeme, "Number");
assert_eq!(tokens[9].lexeme, "field2");
assert_eq!(tokens[11].lexeme, "String");
}
}
+266 -268
View File
File diff suppressed because it is too large Load Diff
+8
View File
@@ -121,6 +121,14 @@ impl SourceSlice {
end_position,
}
}
pub fn from_source_slices(start_point: SourceSlice, end_point: SourceSlice) -> Self {
Self {
source_id: start_point.source_id,
start_position: start_point.start_position,
end_position: end_point.end_position,
}
}
pub fn tree_point(
source_id: SourceId,
column_start: usize,
+4 -2
View File
@@ -35,11 +35,12 @@ pub enum TokenType {
Identifier,
String,
Number,
Atom,
// Keywords
Fn,
And,
Class,
Struct,
StartBlock,
EndBlock,
False,
@@ -80,8 +81,9 @@ impl fmt::Display for TokenType {
TokenType::Identifier => write!(f, "IDENTIFIER"),
TokenType::String => write!(f, "STRING"),
TokenType::Number => write!(f, "NUMBER"),
TokenType::Atom => write!(f, "ATOM"),
TokenType::And => write!(f, "and"),
TokenType::Class => write!(f, "class"),
TokenType::Struct => write!(f, "struct"),
TokenType::Else => write!(f, "else"),
TokenType::False => write!(f, "false"),
TokenType::True => write!(f, "true"),