Skip to content

Commit 8903127

Browse files
committed
Use lines WIP
1 parent 60e9f7b commit 8903127

28 files changed

Lines changed: 1153 additions & 846 deletions

Cargo.lock

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

compiler_v4/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ name = "candy_v4"
99
path = "src/main.rs"
1010

1111
[dependencies]
12+
arcstr = "1.2.0"
1213
# candy_compiler_v4_derive = { path = "../compiler_v4_derive" }
1314
clap = { version = "4.1.8", features = ["derive"] }
1415
derive_more = "0.99.17"

compiler_v4/src/ast.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,41 @@ impl<T> AstResult<T> {
4747
errors: self.errors,
4848
}
4949
}
50+
51+
pub fn add_error(&mut self, error: AstError) {
52+
self.errors.push(error);
53+
}
5054
}
5155
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
5256
pub struct AstError {
5357
pub unparsable_input: AstString,
5458
pub error: String,
5559
}
5660

57-
pub type Ast = Vec<AstDeclaration>;
61+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
62+
pub struct Ast {
63+
pub use_lines: Vec<AstUseLine>,
64+
pub declarations: Vec<AstDeclaration>,
65+
}
66+
67+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
68+
pub enum AstUseLine {
69+
Local(AstLocalUseLine),
70+
Package(AstPackageUseLine),
71+
}
72+
73+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
74+
pub struct AstLocalUseLine {
75+
pub use_keyword_span: Range<Offset>,
76+
pub dot_count: usize,
77+
pub path: AstResult<AstString>,
78+
}
79+
80+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
81+
pub struct AstPackageUseLine {
82+
pub use_keyword_span: Range<Offset>,
83+
pub package_name: AstResult<AstString>,
84+
}
5885

5986
// Declarations
6087

@@ -70,6 +97,7 @@ pub enum AstDeclaration {
7097

7198
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
7299
pub struct AstStruct {
100+
pub is_pub: bool,
73101
pub name: AstResult<AstString>,
74102
pub type_parameters: Option<AstTypeParameters>,
75103
pub kind: AstStructKind,
@@ -95,6 +123,7 @@ pub struct AstStructField {
95123

96124
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
97125
pub struct AstEnum {
126+
pub is_pub: bool,
98127
pub name: AstResult<AstString>,
99128
pub type_parameters: Option<AstTypeParameters>,
100129
pub opening_curly_brace_error: Option<AstError>,
@@ -110,6 +139,7 @@ pub struct AstEnumVariant {
110139

111140
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
112141
pub struct AstTrait {
142+
pub is_pub: bool,
113143
pub name: AstResult<AstString>,
114144
pub type_parameters: Option<AstTypeParameters>,
115145
pub opening_curly_brace_error: Option<AstError>,
@@ -131,6 +161,7 @@ pub struct AstImpl {
131161

132162
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
133163
pub struct AstAssignment {
164+
pub is_pub: bool,
134165
pub display_span: Range<Offset>,
135166
pub name: AstResult<AstString>,
136167
pub type_: Option<AstResult<AstType>>,
@@ -140,6 +171,7 @@ pub struct AstAssignment {
140171

141172
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
142173
pub struct AstFunction {
174+
pub is_pub: bool,
143175
pub display_span: Range<Offset>,
144176
pub name: AstResult<AstString>,
145177
pub type_parameters: Option<AstTypeParameters>,
@@ -389,6 +421,30 @@ impl CollectAstErrors for i64 {
389421
fn collect_errors_to(&self, _errors: &mut Vec<CompilerError>) {}
390422
}
391423

424+
impl CollectAstErrors for Ast {
425+
fn collect_errors_to(&self, errors: &mut Vec<CompilerError>) {
426+
self.use_lines.collect_errors_to(errors);
427+
self.declarations.collect_errors_to(errors);
428+
}
429+
}
430+
impl CollectAstErrors for AstUseLine {
431+
fn collect_errors_to(&self, errors: &mut Vec<CompilerError>) {
432+
match &self {
433+
Self::Local(local) => local.collect_errors_to(errors),
434+
Self::Package(package) => package.collect_errors_to(errors),
435+
}
436+
}
437+
}
438+
impl CollectAstErrors for AstLocalUseLine {
439+
fn collect_errors_to(&self, errors: &mut Vec<CompilerError>) {
440+
self.path.collect_errors_to(errors);
441+
}
442+
}
443+
impl CollectAstErrors for AstPackageUseLine {
444+
fn collect_errors_to(&self, errors: &mut Vec<CompilerError>) {
445+
self.package_name.collect_errors_to(errors);
446+
}
447+
}
392448
impl CollectAstErrors for AstDeclaration {
393449
fn collect_errors_to(&self, errors: &mut Vec<CompilerError>) {
394450
match &self {

compiler_v4/src/ast_to_hir.rs

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use crate::{
99
hir::{
1010
self, Assignment, Body, BodyOrBuiltin, BuiltinFunction, BuiltinType, ContainsError,
1111
Expression, ExpressionKind, Function, FunctionSignature, FunctionType, Hir, Id, Impl,
12-
NamedType, Parameter, ParameterType, SliceOfTypeParameter, StructField, SwitchCase, Trait,
13-
TraitDefinition, TraitFunction, Type, TypeDeclaration, TypeDeclarationKind, TypeParameter,
12+
Module, NamedType, Parameter, ParameterType, SliceOfTypeParameter, StructField, SwitchCase,
13+
Trait, TraitDefinition, TraitFunction, Type, TypeDeclaration, TypeDeclarationKind,
14+
TypeParameter,
1415
},
1516
id::IdGenerator,
1617
position::Offset,
@@ -35,8 +36,8 @@ use std::{
3536
};
3637
use strum::VariantArray;
3738

38-
pub fn ast_to_hir(path: &Path, ast: &Ast) -> (Hir, Vec<CompilerError>) {
39-
let mut context = Context::new(path, ast);
39+
pub fn ast_to_hir(path: &Path, module: Module, ast: &Ast) -> (Hir, Vec<CompilerError>) {
40+
let mut context = Context::new(path, module, ast);
4041
context.add_builtin_functions();
4142
context.lower_declarations();
4243
context.into_hir()
@@ -45,6 +46,7 @@ pub fn ast_to_hir(path: &Path, ast: &Ast) -> (Hir, Vec<CompilerError>) {
4546
#[derive(Debug)]
4647
struct Context<'a> {
4748
path: &'a Path,
49+
module: Module,
4850
ast: &'a Ast,
4951
id_generator: IdGenerator<Id>,
5052
traits: FxHashMap<Box<str>, TraitDeclaration<'a>>,
@@ -238,9 +240,10 @@ impl Display for Signature {
238240
}
239241

240242
impl<'a> Context<'a> {
241-
fn new(path: &'a Path, ast: &'a Ast) -> Self {
243+
fn new(path: &'a Path, module: Module, ast: &'a Ast) -> Self {
242244
Self {
243245
path,
246+
module,
244247
ast,
245248
id_generator: IdGenerator::start_at(BuiltinFunction::VARIANTS.len()),
246249
traits: FxHashMap::default(),
@@ -425,13 +428,13 @@ impl<'a> Context<'a> {
425428
// TODO: remove these vecs?
426429
let mut assignments_to_lower = vec![];
427430
let mut functions_to_lower = vec![];
428-
for declaration in self.ast {
431+
for declaration in &self.ast.declarations {
429432
let AstDeclaration::Trait(trait_) = declaration else {
430433
continue;
431434
};
432435
self.lower_trait_definition(trait_);
433436
}
434-
for declaration in self.ast {
437+
for declaration in &self.ast.declarations {
435438
match declaration {
436439
AstDeclaration::Struct(struct_) => self.lower_struct(struct_),
437440
AstDeclaration::Enum(enum_) => self.lower_enum(enum_),
@@ -699,7 +702,7 @@ impl<'a> Context<'a> {
699702
.collect::<Box<_>>()
700703
});
701704

702-
let type_parameters = match self.ast.iter().find_map(|it| match it {
705+
let type_parameters = match self.ast.declarations.iter().find_map(|it| match it {
703706
AstDeclaration::Trait(AstTrait {
704707
name: it_name,
705708
type_parameters,
@@ -947,29 +950,33 @@ impl<'a> Context<'a> {
947950
.collect::<Box<_>>()
948951
});
949952

950-
let type_parameters = match self.ast.iter().find_map(|it| match it {
951-
AstDeclaration::Struct(AstStruct {
952-
name: it_name,
953-
type_parameters,
954-
..
955-
})
956-
| AstDeclaration::Enum(AstEnum {
957-
name: it_name,
958-
type_parameters,
959-
..
960-
}) if it_name.value().map(|it| &it.string) == Some(&name.string) => {
961-
Some(Ok(type_parameters
962-
.as_ref()
963-
.map_or::<&[AstTypeParameter], _>(&[], |it| &it.parameters)))
964-
}
965-
AstDeclaration::Trait(AstTrait { name: it_name, .. })
966-
if it_name.value().map(|it| &it.string) == Some(&name.string) =>
967-
{
968-
self.add_error(name.span.clone(), "Traits can't be used as types");
969-
Some(Err(()))
970-
}
971-
_ => None,
972-
}) {
953+
let type_parameters = match self
954+
.ast
955+
.declarations
956+
.iter()
957+
.find_map(|it: &AstDeclaration| match it {
958+
AstDeclaration::Struct(AstStruct {
959+
name: it_name,
960+
type_parameters,
961+
..
962+
})
963+
| AstDeclaration::Enum(AstEnum {
964+
name: it_name,
965+
type_parameters,
966+
..
967+
}) if it_name.value().map(|it| &it.string) == Some(&name.string) => {
968+
Some(Ok(type_parameters
969+
.as_ref()
970+
.map_or::<&[AstTypeParameter], _>(&[], |it| &it.parameters)))
971+
}
972+
AstDeclaration::Trait(AstTrait { name: it_name, .. })
973+
if it_name.value().map(|it| &it.string) == Some(&name.string) =>
974+
{
975+
self.add_error(name.span.clone(), "Traits can't be used as types");
976+
Some(Err(()))
977+
}
978+
_ => None,
979+
}) {
973980
Some(Ok(type_parameters)) => type_parameters,
974981
Some(Err(())) => return Type::Error,
975982
None => {

0 commit comments

Comments
 (0)