add export keyword

This commit is contained in:
2025-12-18 16:15:56 +01:00
parent daf9079ca4
commit fbf28748c7
5 changed files with 20 additions and 3 deletions

View File

@@ -22,6 +22,7 @@ impl Analyzer {
params,
return_type: _,
body: _,
exported: _,
} = stmt
{
if self.functions.contains_key(&name.lexeme) {
@@ -65,6 +66,7 @@ impl Analyzer {
params: _,
return_type,
body,
exported: _,
} => {
if name.lexeme == "main" && return_type.lexeme != "I64" {
return error!(&name.loc, "main must return I64");

View File

@@ -232,8 +232,9 @@ _builtin_environ:
params,
return_type: _,
body,
exported,
} => {
if name.lexeme == "main" {
if exported || name.lexeme == "main" {
emit!(&mut self.output, "global {}", name.lexeme);
}
emit!(&mut self.output, "section .text.{}", name.lexeme);

View File

@@ -35,6 +35,7 @@ pub enum Stmt {
params: Vec<Param>,
return_type: Token,
body: Box<Stmt>,
exported: bool,
},
Return(Expr),
Break,
@@ -101,7 +102,11 @@ impl Parser {
fn declaration(&mut self) -> Result<Stmt, ZernError> {
if !self.is_inside_function {
if self.match_token(&[TokenType::KeywordFunc]) {
return self.func_declaration();
return self.func_declaration(false);
}
if self.match_token(&[TokenType::KeywordExport]) {
self.consume(TokenType::KeywordFunc, "expected 'func' after 'export'")?;
return self.func_declaration(true);
}
if self.match_token(&[TokenType::KeywordExtern]) {
return self.extern_declaration();
@@ -119,7 +124,7 @@ impl Parser {
}
}
fn func_declaration(&mut self) -> Result<Stmt, ZernError> {
fn func_declaration(&mut self, exported: bool) -> Result<Stmt, ZernError> {
let name = self.consume(TokenType::Identifier, "expected function name")?;
self.consume(TokenType::LeftBracket, "expected '[' after function name")?;
@@ -157,6 +162,7 @@ impl Parser {
params,
return_type,
body,
exported,
})
}

View File

@@ -229,6 +229,12 @@ func str.from_i64[n: I64] : String
mem.free(buf)
return s
func str.from_char[c: U8] : String
let s: String = mem.alloc(2)
str.set(s, 0, c)
str.set(s, 1, 0)
return s
func str.parse_i64[s: String] : I64
let len: I64 = str.len(s)
let i: I64 = 0

View File

@@ -48,6 +48,7 @@ pub enum TokenType {
KeywordBreak,
KeywordContinue,
KeywordExtern,
KeywordExport,
Indent,
Dedent,
@@ -340,6 +341,7 @@ impl Tokenizer {
"break" => TokenType::KeywordBreak,
"continue" => TokenType::KeywordContinue,
"extern" => TokenType::KeywordExtern,
"export" => TokenType::KeywordExport,
"true" => TokenType::True,
"false" => TokenType::False,
_ => TokenType::Identifier,