From 5fca72a3f99747670a2480fc0df9e02e8bf2c5c7 Mon Sep 17 00:00:00 2001 From: sofia Date: Tue, 15 Jul 2025 20:37:41 +0300 Subject: [PATCH] Add lexing support for structs --- libtest.sh | 2 +- reid/src/ast/mod.rs | 21 +++++++++++++++++++++ reid/src/ast/parse.rs | 6 ++++++ reid/src/ast/process.rs | 2 ++ reid/src/lexer.rs | 6 ++++++ reid_src/struct.reid | 15 +++++++++++++++ 6 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 reid_src/struct.reid diff --git a/libtest.sh b/libtest.sh index ff8adbc..2ef698f 100755 --- a/libtest.sh +++ b/libtest.sh @@ -14,7 +14,7 @@ BINARY="$(echo $1 | cut -d'.' -f1)"".out" -make clean SRC=$1 && make SRC=$1 && echo "" +make clean SRC=$1 ; make SRC=$1 && echo "" $BINARY ; echo "Return value: ""$?" diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index c40a272..6800d73 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -26,6 +26,7 @@ pub enum TypeKind { U128, String, Array(Box, u64), + Custom(String), } #[derive(Debug, Clone)] @@ -157,11 +158,31 @@ pub enum BlockLevelStatement { Return(ReturnType, Expression), } +#[derive(Debug)] +pub struct TypeDefinition { + range: TokenRange, + name: String, + kind: TypeDefinitionKind, +} + +#[derive(Debug)] +pub enum TypeDefinitionKind { + Struct(Vec), +} + +#[derive(Debug)] +pub struct StructField { + name: String, + ty: Type, + range: TokenRange, +} + #[derive(Debug)] pub enum TopLevelStatement { Import(ImportStatement), ExternFunction(FunctionSignature), FunctionDefinition(FunctionDefinition), + TypeDefinition(TypeDefinition), } #[derive(Debug)] diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index 6f9a96c..3236db9 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -476,6 +476,12 @@ impl Parse for SetStatement { } } +impl Parse for TypeDefinition { + fn parse(stream: TokenStream) -> Result { + todo!() + } +} + impl Parse for TopLevelStatement { fn parse(mut stream: TokenStream) -> Result { use TopLevelStatement as Stmt; diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index c5bed72..aaf7043 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -62,6 +62,7 @@ impl ast::Module { }; functions.push(def); } + TypeDefinition(type_definition) => todo!("Add process for type definition"), } } @@ -249,6 +250,7 @@ impl From for mir::TypeKind { mir::TypeKind::Array(Box::new(mir::TypeKind::from(*type_kind.clone())), *length) } ast::TypeKind::String => mir::TypeKind::StringPtr, + ast::TypeKind::Custom(_) => todo!("Add processing for custom types"), } } } diff --git a/reid/src/lexer.rs b/reid/src/lexer.rs index ce7aee7..e72cba8 100644 --- a/reid/src/lexer.rs +++ b/reid/src/lexer.rs @@ -36,6 +36,8 @@ pub enum Token { False, /// `extern` Extern, + /// `struct` + Struct, // Symbols /// `;` @@ -74,6 +76,8 @@ pub enum Token { BracketClose, /// `,` Comma, + /// `.` + Dot, Eof, } @@ -211,6 +215,7 @@ pub fn tokenize>(to_tokenize: T) -> Result, Error "false" => Token::False, "extern" => Token::Extern, "pub" => Token::PubKeyword, + "struct" => Token::Struct, _ => Token::Identifier(value), }; variant @@ -249,6 +254,7 @@ pub fn tokenize>(to_tokenize: T) -> Result, Error '{' => Token::BraceOpen, '}' => Token::BraceClose, ',' => Token::Comma, + '.' => Token::Dot, // Invalid token _ => Err(Error::InvalidToken(*character, cursor.position))?, }; diff --git a/reid_src/struct.reid b/reid_src/struct.reid new file mode 100644 index 0000000..febcaf0 --- /dev/null +++ b/reid_src/struct.reid @@ -0,0 +1,15 @@ +// Arithmetic, function calls and imports! + +struct Test { + field: i32, + second: u32 +} + +fn main() -> u32 { + let value = Test { + field: 5, + second: 3, + }; + + return Test.second; +}