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(); let own_function = *module.functions.get(&self.value).unwrap();
if self.data.flags.is_extern { if self.data.flags.is_extern {
LLVMSetLinkage(own_function.value_ref, LLVMLinkage::LLVMExternalLinkage); LLVMSetLinkage(
own_function.value_ref,
LLVMLinkage::LLVMAvailableExternallyLinkage,
);
return; 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 { for block in &self.blocks {
if block.data.deleted { if block.data.deleted {

View File

@ -89,11 +89,15 @@ pub struct FunctionData {
#[derive(Debug, Clone, Copy, Hash)] #[derive(Debug, Clone, Copy, Hash)]
pub struct FunctionFlags { pub struct FunctionFlags {
pub is_extern: bool, pub is_extern: bool,
pub is_pub: bool,
} }
impl Default for FunctionFlags { impl Default for FunctionFlags {
fn default() -> 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 { let fibonacci = FunctionDefinition {
name: fibonacci_name.clone(), name: fibonacci_name.clone(),
is_pub: false,
return_type: TypeKind::I32, return_type: TypeKind::I32,
parameters: vec![(fibonacci_n.clone(), TypeKind::I32)], parameters: vec![(fibonacci_n.clone(), TypeKind::I32)],
kind: FunctionDefinitionKind::Local( kind: FunctionDefinitionKind::Local(
@ -127,6 +128,7 @@ fn main() {
let main = FunctionDefinition { let main = FunctionDefinition {
name: "main".to_owned(), name: "main".to_owned(),
is_pub: false,
return_type: TypeKind::I32, return_type: TypeKind::I32,
parameters: vec![], parameters: vec![],
kind: FunctionDefinitionKind::Local( kind: FunctionDefinitionKind::Local(

View File

@ -110,7 +110,7 @@ pub struct LetStatement(
pub struct ImportStatement(pub Vec<String>, pub TokenRange); pub struct ImportStatement(pub Vec<String>, pub TokenRange);
#[derive(Debug)] #[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)] #[derive(Debug, Clone)]
pub struct FunctionSignature { pub struct FunctionSignature {

View File

@ -308,9 +308,17 @@ impl Parse for ImportStatement {
impl Parse for FunctionDefinition { impl Parse for FunctionDefinition {
fn parse(mut stream: TokenStream) -> Result<Self, Error> { 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)?; stream.expect(Token::FnKeyword)?;
Ok(FunctionDefinition( Ok(FunctionDefinition(
stream.parse()?, stream.parse()?,
is_pub,
stream.parse()?, stream.parse()?,
stream.get_range().unwrap(), stream.get_range().unwrap(),
)) ))
@ -482,7 +490,9 @@ impl Parse for TopLevelStatement {
stream.expect(Token::Semi)?; stream.expect(Token::Semi)?;
extern_fn 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")?)?, _ => 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())); 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 { let def = mir::FunctionDefinition {
name: signature.name.clone(), name: signature.name.clone(),
is_pub: *is_pub,
return_type: signature return_type: signature
.return_type .return_type
.clone() .clone()
@ -45,6 +46,7 @@ impl ast::Module {
ExternFunction(signature) => { ExternFunction(signature) => {
let def = mir::FunctionDefinition { let def = mir::FunctionDefinition {
name: signature.name.clone(), name: signature.name.clone(),
is_pub: false,
return_type: signature return_type: signature
.return_type .return_type
.clone() .clone()

View File

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

View File

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

View File

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