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

View File

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

View File

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

View File

@@ -229,6 +229,12 @@ func str.from_i64[n: I64] : String
mem.free(buf) mem.free(buf)
return s 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 func str.parse_i64[s: String] : I64
let len: I64 = str.len(s) let len: I64 = str.len(s)
let i: I64 = 0 let i: I64 = 0

View File

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