more import preparation

This commit is contained in:
2025-04-25 13:37:26 -04:00
parent 4e7c201690
commit 5adca32dd4
13 changed files with 203 additions and 26 deletions

View File

@@ -13,6 +13,7 @@ impl FnLowerable for PBlock {
let mut statements = Vec::new();
let mut fn_nodes = Vec::new();
let mut struct_nodes = Vec::new();
let mut imports = Vec::new();
// first sort statements
for s in &self.statements {
let Some(s) = s.as_ref() else {
@@ -23,6 +24,7 @@ impl FnLowerable for PBlock {
PStatementLike::Const(pconst_statement) => match pconst_statement {
PConstStatement::Fn(f) => fn_nodes.push(f),
PConstStatement::Struct(s) => struct_nodes.push(s),
PConstStatement::Import(i) => imports.push(i),
},
}
}

View File

@@ -23,6 +23,8 @@ impl PFunction {
Some(id)
}
pub fn lower(&self, id: FnID, p: &mut UProgram, output: &mut CompilerOutput) {
let name = p.names.name(id).to_string();
p.push_name(&name);
let (args, ret) = if let Some(header) = self.header.as_ref() {
(
header
@@ -56,7 +58,7 @@ impl PFunction {
ret,
instructions,
};
p.expect_mut(p.fn_var.var(id)).ty = f.ty(p);
p.pop_name();
p.write(id, f)
}
}

View File

@@ -26,10 +26,8 @@ impl PModule {
instructions: fctx.instructions,
ret: Type::Unit,
};
let ty = f.ty(p);
p.write(id, f);
p.pop_name();
p.expect_mut(p.fn_var.var(id)).ty = ty;
}
}

View File

@@ -1,6 +1,6 @@
use super::{
Keyword, Node, PExpr, PFunction, PStruct, PVarDef, Parsable, ParseResult, ParserCtx, Symbol,
Token,
Keyword, Node, PExpr, PFunction, PIdent, PStruct, PVarDef, Parsable, ParseResult, ParserCtx,
Symbol, Token,
};
pub enum PStatement {
@@ -12,6 +12,7 @@ pub enum PStatement {
pub enum PConstStatement {
Fn(Node<PFunction>),
Struct(Node<PStruct>),
Import(Node<PIdent>),
}
pub enum PStatementLike {
@@ -47,6 +48,10 @@ impl Parsable for PStatementLike {
ctx.next();
ParseResult::Ok(Self::Const(PConstStatement::Struct(ctx.parse()?)))
}
Token::Keyword(Keyword::Import) => {
ctx.next();
ParseResult::Ok(Self::Const(PConstStatement::Import(ctx.parse()?)))
}
_ => ctx.parse().map(|n| Self::Statement(PStatement::Expr(n))),
}
}
@@ -81,6 +86,9 @@ impl std::fmt::Debug for PConstStatement {
Self::Struct(s) => {
s.fmt(f)?;
}
Self::Import(s) => {
writeln!(f, "import {:?}", s);
}
}
Ok(())
}

View File

@@ -12,6 +12,7 @@ pub enum Keyword {
Impl,
For,
Asm,
Import,
Funne,
}
@@ -30,6 +31,7 @@ impl Keyword {
"trait" => Self::Trait,
"impl" => Self::Impl,
"asm" => Self::Asm,
"import" => Self::Import,
"funne" => Self::Funne,
_ => return None,
})