Skip to content

Commit 62b9100

Browse files
committed
frontend: Prepare for separate stdlib sem check
1 parent 45d983e commit 62b9100

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed

dora-frontend/src/program_parser.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ impl<'a> ProgramParser<'a> {
6969
}
7070

7171
fn add_all_packages(&mut self) {
72-
self.add_stdlib_package();
73-
self.add_boots_package();
72+
if !self.sa.flags.is_standard_library {
73+
self.add_stdlib_package();
74+
self.add_boots_package();
75+
}
76+
7477
self.add_program_package();
7578
self.add_dependency_packages();
7679
}
@@ -93,7 +96,7 @@ impl<'a> ProgramParser<'a> {
9396
}
9497

9598
fn get_stdlib_path(&self) -> Option<PathBuf> {
96-
if let Some(file_content) = self.packages.get("stdlib") {
99+
if let Some(file_content) = self.packages.get("std") {
97100
Some(file_content.to_path().cloned().expect("path expected"))
98101
} else {
99102
let path = std::env::current_exe().expect("illegal path");

dora-frontend/src/sema.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub struct SemaFlags {
9090
pub packages: Vec<(String, FileContent)>,
9191
pub program_file: Option<FileContent>,
9292
pub boots: bool,
93+
pub is_standard_library: bool,
9394
}
9495

9596
impl SemaFlags {
@@ -103,6 +104,7 @@ impl SemaFlags {
103104
packages,
104105
program_file: Some(FileContent::Content(input.to_string())),
105106
boots: false,
107+
is_standard_library: false,
106108
}
107109
}
108110

@@ -116,6 +118,7 @@ impl SemaFlags {
116118
packages,
117119
program_file: Some(FileContent::Content(input.to_string())),
118120
boots: false,
121+
is_standard_library: false,
119122
}
120123
}
121124
}

dora-language-server/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ fn compile_project(project: ProjectConfig, sender: Sender<MainLoopTask>) {
315315
program_file: Some(FileContent::Path(project.main.clone())),
316316
packages: Vec::new(),
317317
boots: false,
318+
is_standard_library: false,
318319
};
319320

320321
let mut sa = Sema::new(sem_args);

dora/src/driver/flags.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ pub struct DriverFlags {
113113
pub packages: Vec<(String, PathBuf)>,
114114

115115
pub command: Command,
116+
117+
pub separate_stdlib_check: bool,
116118
}
117119

118120
impl DriverFlags {
@@ -177,6 +179,8 @@ impl Default for DriverFlags {
177179
snapshot_on_oom: None,
178180

179181
command: Command::Run,
182+
183+
separate_stdlib_check: false,
180184
}
181185
}
182186
}
@@ -349,6 +353,8 @@ pub fn parse_arguments() -> Result<DriverFlags, String> {
349353
idx += 2;
350354
} else if arg.starts_with("--snapshot-on-oom=") {
351355
flags.snapshot_on_oom = Some(argument_value(arg).into());
356+
} else if arg == "--separate-stdlib-check" {
357+
flags.separate_stdlib_check = true;
352358
} else if arg.starts_with("-") {
353359
return Err(format!("unknown flag {}", arg));
354360
} else if flags.arg_file.is_none() {
@@ -441,6 +447,7 @@ pub fn create_sema_flags(flags: &DriverFlags, program_file: PathBuf) -> SemaFlag
441447
program_file: Some(FileContent::Path(program_file)),
442448
packages,
443449
boots: flags.include_boots(),
450+
is_standard_library: false,
444451
}
445452
}
446453

dora/src/driver/start.rs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::path::PathBuf;
55
use crate::driver::flags::{self, create_sema_flags, DriverFlags};
66
use dora_bytecode::{display_fct, FunctionId, PackageId, Program};
77
use dora_frontend as language;
8-
use dora_frontend::sema::Sema;
8+
use dora_frontend::sema::{FileContent, Sema, SemaFlags};
99
use dora_runtime::{clear_vm, execute_on_main, set_vm, VM};
1010

1111
pub fn start() -> i32 {
@@ -45,6 +45,12 @@ pub fn start() -> i32 {
4545
}
4646
}
4747
} else {
48+
if flags.separate_stdlib_check {
49+
if let Err(_) = compile_std_library(&flags) {
50+
return 1;
51+
}
52+
}
53+
4854
match compile_into_program(&flags, file.clone()) {
4955
Ok(result) => result,
5056
Err(_) => {
@@ -112,6 +118,63 @@ pub fn start() -> i32 {
112118
exit_code
113119
}
114120

121+
fn compile_std_library(flags: &DriverFlags) -> Result<Program, ()> {
122+
let path = get_stdlib_path(flags).expect("standard library not found");
123+
124+
let sema_flags = SemaFlags {
125+
program_file: Some(FileContent::Path(path)),
126+
packages: Vec::new(),
127+
boots: false,
128+
is_standard_library: true,
129+
};
130+
131+
let mut sa = Sema::new(sema_flags);
132+
133+
let success = language::check_program(&mut sa);
134+
assert_eq!(success, !sa.diag.borrow().has_errors());
135+
136+
if report_errors(&sa, flags.report_all_warnings) {
137+
return Err(());
138+
}
139+
140+
if let Some(ref filter) = flags.emit_ast {
141+
language::emit_ast(&sa, filter);
142+
}
143+
144+
language::generate_bytecode(&sa);
145+
146+
// Create a serializable data structure from bytecode and metadata.
147+
// Here we drop the generated AST.
148+
let prog = language::emit_program(sa);
149+
150+
if let Some(ref filter) = flags.emit_bytecode {
151+
language::emit_bytecode(&prog, filter);
152+
}
153+
154+
Ok(prog)
155+
}
156+
157+
fn get_stdlib_path(flags: &DriverFlags) -> Option<PathBuf> {
158+
for (name, path) in &flags.packages {
159+
if name == "std" {
160+
return Some(path.into());
161+
}
162+
}
163+
164+
let path = std::env::current_exe().expect("illegal path");
165+
let path = path.as_path();
166+
167+
for ancestor in path.ancestors() {
168+
let stdlib_path = ancestor.join("pkgs/std/std.dora");
169+
170+
if stdlib_path.exists() {
171+
return Some(stdlib_path);
172+
}
173+
}
174+
175+
None
176+
}
177+
115178
fn compile_into_program(flags: &DriverFlags, file: String) -> Result<Program, ()> {
116179
let sema_flags = create_sema_flags(flags, PathBuf::from(file));
117180

0 commit comments

Comments
 (0)