Skip to content
This repository was archived by the owner on Apr 21, 2026. It is now read-only.

Commit a4801b9

Browse files
committed
feat(hir): hir definitions and lowerer implement
1 parent f9d03e7 commit a4801b9

26 files changed

Lines changed: 3432 additions & 13 deletions

File tree

Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

autofront/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ locale = { path = "../locale" }
1212
diag = { path = "../diag" }
1313
lexer = { path = "../lexer" }
1414
parser = { path = "../parser" }
15+
lowerer = { path = "../lowerer" }
1516
anstream = "0.6.21"
1617
unicode-width = "0.2.2"

autofront/src/cli/cmd.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub enum Command {
1717
Lex(DebugSubcommand),
1818
Tt(DebugSubcommand),
1919
Parse(DebugSubcommand),
20+
Hir(DebugSubcommand),
2021
}
2122

2223
#[derive(Debug, Clone)]

autofront/src/cli/help.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub fn print_help(topic: Option<&str>) -> Result<(), CliError> {
99
Some("lex") => print_lex_help(),
1010
Some("tt") => print_tt_help(),
1111
Some("parse") => print_parse_help(),
12+
Some("codegen") => print_codegen_help(),
1213
Some(cmd) => {
1314
return Err(CliError::UnknownCommand(cmd.to_string()));
1415
}
@@ -39,3 +40,7 @@ fn print_tt_help() {
3940
fn print_parse_help() {
4041
anstream::println!("{}", tr!(cli_help_parse));
4142
}
43+
44+
fn print_codegen_help() {
45+
anstream::println!("{}", tr!(cli_help_codegen));
46+
}

autofront/src/cli/parser.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn parse_args(args: &[String]) -> Result<Command> {
5757
Some(topic) => {
5858
let topic = topic.clone();
5959
match topic.as_str() {
60-
"help" | "version" | "lex" | "tt" | "parse" => Some(topic),
60+
"help" | "version" | "lex" | "tt" | "parse" | "codegen" => Some(topic),
6161
_ => Err(CliError::UnknownCommand(topic))?,
6262
}
6363
}
@@ -71,6 +71,7 @@ pub fn parse_args(args: &[String]) -> Result<Command> {
7171
"lex" => Ok(Command::Lex(DebugSubcommand::parse(args)?)),
7272
"tt" => Ok(Command::Tt(DebugSubcommand::parse(args)?)),
7373
"parse" => Ok(Command::Parse(DebugSubcommand::parse(args)?)),
74+
"hir" => Ok(Command::Hir(DebugSubcommand::parse(args)?)),
7475

7576
other => Err(CliError::UnknownCommand(other.into())),
7677
}

autofront/src/driver.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anstream::StripStream;
2+
use lowerer::lower;
23
use std::{
34
env::args,
45
error::Error,
@@ -12,6 +13,8 @@ use unicode_width::UnicodeWidthStr;
1213
use crate::cli::{Command, DebugSubcommand, parse_args, print_help};
1314
use common::source::Source;
1415
use diag::{DiagPrinter, DiagSink};
16+
// use hir::*;
17+
// use ast2hir::lower_ast;
1518
use lexer::{lex, parse_token_tree};
1619
use locale::tr;
1720
use parser::parse;
@@ -124,6 +127,31 @@ impl Driver {
124127
Ok(())
125128
}
126129

130+
fn hir(&mut self, args: DebugSubcommand) -> Result<(), Box<dyn Error>> {
131+
let DebugSubcommand {
132+
file,
133+
output,
134+
show_recovery,
135+
} = args;
136+
137+
let mut sink = DiagSink::default();
138+
let src = self.load(file)?;
139+
140+
let tokens = lex(&src, &mut sink);
141+
let ts = parse_token_tree(&tokens, &mut sink);
142+
let ast = parse(&ts, &mut sink);
143+
let hir = lower(&ast, &mut sink);
144+
145+
let printer = DiagPrinter::new(&src);
146+
printer.print(sink, show_recovery)?;
147+
148+
let output = output.unwrap_or_else(|| format!("{}.out", src.file));
149+
let out = get_out!(Some(output));
150+
write!(out, "{hir:#?}")?;
151+
152+
Ok(())
153+
}
154+
127155
fn drive(mut self) -> Result<(), Box<dyn Error>> {
128156
use Command::*;
129157

@@ -137,6 +165,7 @@ impl Driver {
137165
Lex(args) => self.lex(args)?,
138166
Tt(args) => self.tt(args)?,
139167
Parse(args) => self.parse(args)?,
168+
Hir(args) => self.hir(args)?,
140169
}
141170

142171
Ok(())

hir/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ edition.workspace = true
55
license.workspace = true
66

77
[dependencies]
8+
common = { path = "../common" }
9+
bimap = "0.6.3"
10+
num-bigint = "0.4.6"

hir/src/id.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::ops::{Add, AddAssign};
2+
3+
macro_rules! define_id {
4+
($id:ident) => {
5+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
6+
pub struct $id(usize);
7+
8+
impl $id {
9+
pub fn new(value: usize) -> Self {
10+
Self(value)
11+
}
12+
13+
pub fn as_usize(self) -> usize {
14+
self.0
15+
}
16+
}
17+
18+
impl Add<usize> for $id {
19+
type Output = Self;
20+
fn add(self, rhs: usize) -> Self::Output {
21+
Self(self.0 + rhs)
22+
}
23+
}
24+
25+
impl AddAssign<usize> for $id {
26+
fn add_assign(&mut self, rhs: usize) {
27+
self.0 += rhs
28+
}
29+
}
30+
};
31+
}
32+
33+
define_id!(SymId);
34+
35+
define_id!(DestId);

hir/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pub mod id;
2+
pub mod node;
3+
pub mod print;
4+
5+
use common::span::Span;
6+
pub use {id::*, node::*, print::*};
7+
8+
#[derive(Debug, Clone)]
9+
pub struct Ident {
10+
pub name: String,
11+
pub span: Span,
12+
}
13+
14+
#[derive(Clone)]
15+
pub struct Hir {
16+
pub items: Vec<Item>,
17+
}

hir/src/node.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
pub mod constant;
2+
pub mod expr;
3+
pub mod ty;
4+
5+
pub use {constant::*, expr::*, ty::*};
6+
7+
use crate::{Ident, SymId};
8+
use common::span::Span;
9+
10+
#[derive(Debug, Clone)]
11+
pub struct Item {
12+
pub kind: ItemKind,
13+
pub span: Span,
14+
}
15+
16+
#[derive(Debug, Clone)]
17+
pub enum ItemKind {
18+
FnDef(FnDef),
19+
TyDef(TyDef),
20+
Error,
21+
}
22+
23+
#[derive(Debug, Clone)]
24+
pub struct FnDef {
25+
pub sym_id: SymId,
26+
pub name: Option<Ident>,
27+
pub side_effect: bool,
28+
pub params: Vec<Param>,
29+
pub ret: Box<Ty>,
30+
pub body: Body,
31+
}
32+
33+
#[derive(Debug, Clone)]
34+
pub struct Body {
35+
pub kind: BodyKind,
36+
pub span: Span,
37+
}
38+
39+
#[derive(Debug, Clone)]
40+
pub enum BodyKind {
41+
Expr(Box<Expr>),
42+
CFfi(String),
43+
Error,
44+
}
45+
46+
#[derive(Debug, Clone)]
47+
pub struct TyDef {
48+
pub sym_id: SymId,
49+
pub name: Option<Ident>,
50+
pub ty: Box<Ty>,
51+
}
52+
53+
#[derive(Debug, Clone)]
54+
pub struct Param {
55+
pub sym_id: SymId,
56+
pub mutable: bool,
57+
pub name: Ident,
58+
pub ty: Box<Ty>,
59+
pub span: Span,
60+
}
61+
62+
#[derive(Debug, Clone)]
63+
pub struct Stmt {
64+
pub kind: StmtKind,
65+
pub span: Span,
66+
}
67+
68+
#[derive(Debug, Clone)]
69+
pub enum StmtKind {
70+
LocalDef(LocalDef),
71+
Item(Item),
72+
Expr(Box<Expr>),
73+
Error,
74+
}
75+
76+
#[derive(Debug, Clone)]
77+
pub struct LocalDef {
78+
pub sym_id: SymId,
79+
pub mutable: bool,
80+
pub name: Option<Ident>,
81+
pub ty: Box<Ty>,
82+
pub init: Option<Box<Expr>>,
83+
pub span: Span,
84+
}

0 commit comments

Comments
 (0)