-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
96 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use indexmap::IndexMap; | ||
use rue_parser::{StructField, StructItem}; | ||
|
||
use crate::{ | ||
compiler::Compiler, | ||
ty::{StructType, Type}, | ||
TypeId, | ||
}; | ||
|
||
impl Compiler<'_> { | ||
/// Define a type for a struct in the current scope, but leave it as unknown for now. | ||
pub fn declare_struct_item(&mut self, struct_item: &StructItem) -> TypeId { | ||
let type_id = self.db.alloc_type(Type::Unknown); | ||
if let Some(name) = struct_item.name() { | ||
self.scope_mut().define_type(name.to_string(), type_id); | ||
self.db.insert_type_token(type_id, name); | ||
} | ||
type_id | ||
} | ||
|
||
/// Compile and resolve a struct type. | ||
pub fn compile_struct_item(&mut self, struct_item: &StructItem, type_id: TypeId) { | ||
self.type_definition_stack.push(type_id); | ||
let fields = self.compile_struct_fields(struct_item.fields()); | ||
*self.db.ty_mut(type_id) = Type::Struct(StructType { fields }); | ||
self.type_definition_stack.pop().unwrap(); | ||
} | ||
|
||
/// Compile and resolve the fields of a struct. | ||
pub fn compile_struct_fields(&mut self, fields: Vec<StructField>) -> IndexMap<String, TypeId> { | ||
let mut named_fields = IndexMap::new(); | ||
|
||
for field in fields { | ||
let type_id = field | ||
.ty() | ||
.map_or(self.builtins.unknown, |ty| self.compile_type(ty)); | ||
|
||
if let Some(name) = field.name() { | ||
named_fields.insert(name.to_string(), type_id); | ||
}; | ||
} | ||
|
||
named_fields | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use rue_parser::{AstNode, TypeAliasItem}; | ||
|
||
use crate::{compiler::Compiler, ty::Type, TypeId}; | ||
|
||
impl Compiler<'_> { | ||
/// Define a type for an alias in the current scope, but leave it as unknown for now. | ||
pub fn declare_type_alias_item(&mut self, type_alias: &TypeAliasItem) -> TypeId { | ||
let type_id = self.db.alloc_type(Type::Unknown); | ||
if let Some(name) = type_alias.name() { | ||
self.scope_mut().define_type(name.to_string(), type_id); | ||
self.db.insert_type_token(type_id, name); | ||
} | ||
type_id | ||
} | ||
|
||
/// Compile and resolve the type that the alias points to. | ||
pub fn compile_type_alias_item(&mut self, ty: &TypeAliasItem, alias_type_id: TypeId) { | ||
self.type_definition_stack.push(alias_type_id); | ||
|
||
let type_id = ty | ||
.ty() | ||
.map_or(self.builtins.unknown, |ty| self.compile_type(ty)); | ||
|
||
// Set the alias type to the resolved type. | ||
*self.db.ty_mut(alias_type_id) = Type::Alias(type_id); | ||
|
||
// A cycle between type aliases has been detected. | ||
// We set it to unknown to prevent stack overflow issues later. | ||
if self.detect_cycle(alias_type_id, ty.syntax().text_range()) { | ||
*self.db.ty_mut(alias_type_id) = Type::Unknown; | ||
} | ||
|
||
self.type_definition_stack.pop().unwrap(); | ||
} | ||
} |