Add pub keyword

This commit is contained in:
Sofia 2025-07-14 18:47:02 +03:00
parent 067c84e93e
commit 735c4370aa
10 changed files with 49 additions and 6 deletions

View File

@ -205,9 +205,17 @@ impl FunctionHolder {
let own_function = *module.functions.get(&self.value).unwrap();
if self.data.flags.is_extern {
LLVMSetLinkage(own_function.value_ref, LLVMLinkage::LLVMExternalLinkage);
LLVMSetLinkage(
own_function.value_ref,
LLVMLinkage::LLVMAvailableExternallyLinkage,
);
return;
}
if self.data.flags.is_pub {
LLVMSetLinkage(own_function.value_ref, LLVMLinkage::LLVMCommonLinkage);
} else {
LLVMSetLinkage(own_function.value_ref, LLVMLinkage::LLVMPrivateLinkage);
}
for block in &self.blocks {
if block.data.deleted {

View File

@ -89,11 +89,15 @@ pub struct FunctionData {
#[derive(Debug, Clone, Copy, Hash)]
pub struct FunctionFlags {
pub is_extern: bool,
pub is_pub: bool,
}
impl Default for FunctionFlags {
fn default() -> FunctionFlags {
FunctionFlags { is_extern: false }
FunctionFlags {
is_extern: false,
is_pub: false,
}
}
}

View File

@ -7,6 +7,7 @@ fn main() {
let fibonacci = FunctionDefinition {
name: fibonacci_name.clone(),
is_pub: false,
return_type: TypeKind::I32,
parameters: vec![(fibonacci_n.clone(), TypeKind::I32)],
kind: FunctionDefinitionKind::Local(
@ -127,6 +128,7 @@ fn main() {
let main = FunctionDefinition {
name: "main".to_owned(),
is_pub: false,
return_type: TypeKind::I32,
parameters: vec![],
kind: FunctionDefinitionKind::Local(

View File

@ -110,7 +110,7 @@ pub struct LetStatement(
pub struct ImportStatement(pub Vec<String>, pub TokenRange);
#[derive(Debug)]
pub struct FunctionDefinition(pub FunctionSignature, pub Block, pub TokenRange);
pub struct FunctionDefinition(pub FunctionSignature, pub bool, pub Block, pub TokenRange);
#[derive(Debug, Clone)]
pub struct FunctionSignature {

View File

@ -308,9 +308,17 @@ impl Parse for ImportStatement {
impl Parse for FunctionDefinition {
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
let is_pub = if let Some(Token::PubKeyword) = stream.peek() {
stream.next(); // Consume pub
true
} else {
false
};
stream.expect(Token::FnKeyword)?;
Ok(FunctionDefinition(
stream.parse()?,
is_pub,
stream.parse()?,
stream.get_range().unwrap(),
))
@ -482,7 +490,9 @@ impl Parse for TopLevelStatement {
stream.expect(Token::Semi)?;
extern_fn
}
Some(Token::FnKeyword) => Stmt::FunctionDefinition(stream.parse()?),
Some(Token::FnKeyword) | Some(Token::PubKeyword) => {
Stmt::FunctionDefinition(stream.parse()?)
}
_ => Err(stream.expected_err("import or fn")?)?,
})
}

View File

@ -24,9 +24,10 @@ impl ast::Module {
imports.push(mir::Import(name.clone(), import.1.into()));
}
}
FunctionDefinition(ast::FunctionDefinition(signature, block, range)) => {
FunctionDefinition(ast::FunctionDefinition(signature, is_pub, block, range)) => {
let def = mir::FunctionDefinition {
name: signature.name.clone(),
is_pub: *is_pub,
return_type: signature
.return_type
.clone()
@ -45,6 +46,7 @@ impl ast::Module {
ExternFunction(signature) => {
let def = mir::FunctionDefinition {
name: signature.name.clone(),
is_pub: false,
return_type: signature
.return_type
.clone()

View File

@ -64,7 +64,10 @@ impl mir::Module {
&function.name,
function.return_type.get_type(),
param_types,
FunctionFlags::default(),
FunctionFlags {
is_pub: function.is_pub,
..FunctionFlags::default()
},
),
mir::FunctionDefinitionKind::Extern => module.function(
&function.name,

View File

@ -22,6 +22,8 @@ pub enum Token {
ReturnKeyword,
/// `fn`
FnKeyword,
/// `pub`
PubKeyword,
/// `->`
Arrow,
/// `if`
@ -208,6 +210,7 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
"true" => Token::True,
"false" => Token::False,
"extern" => Token::Extern,
"pub" => Token::PubKeyword,
_ => Token::Identifier(value),
};
variant

View File

@ -240,6 +240,7 @@ pub struct FunctionCall {
#[derive(Debug)]
pub struct FunctionDefinition {
pub name: String,
pub is_pub: bool,
pub return_type: TypeKind,
pub parameters: Vec<(String, TypeKind)>,
pub kind: FunctionDefinitionKind,

10
reid_src/std.reid Normal file
View File

@ -0,0 +1,10 @@
extern fn puts(message: string) -> i32;
pub fn print(message: string) {
puts(message);
}
fn main() -> u16 {
return 0;
}