Add lexing support for structs

This commit is contained in:
Sofia 2025-07-15 20:37:41 +03:00
parent 0ec427252f
commit 5fca72a3f9
6 changed files with 51 additions and 1 deletions

View File

@ -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: ""$?"

View File

@ -26,6 +26,7 @@ pub enum TypeKind {
U128,
String,
Array(Box<TypeKind>, 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<StructField>),
}
#[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)]

View File

@ -476,6 +476,12 @@ impl Parse for SetStatement {
}
}
impl Parse for TypeDefinition {
fn parse(stream: TokenStream) -> Result<Self, Error> {
todo!()
}
}
impl Parse for TopLevelStatement {
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
use TopLevelStatement as Stmt;

View File

@ -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<ast::TypeKind> 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"),
}
}
}

View File

@ -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<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
"false" => Token::False,
"extern" => Token::Extern,
"pub" => Token::PubKeyword,
"struct" => Token::Struct,
_ => Token::Identifier(value),
};
variant
@ -249,6 +254,7 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
'{' => Token::BraceOpen,
'}' => Token::BraceClose,
',' => Token::Comma,
'.' => Token::Dot,
// Invalid token
_ => Err(Error::InvalidToken(*character, cursor.position))?,
};

15
reid_src/struct.reid Normal file
View File

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