Skip to content

Commit

Permalink
implemented a basic module system, still half-baked
Browse files Browse the repository at this point in the history
  • Loading branch information
xarkenz committed Dec 31, 2024
1 parent 186f70a commit 21c341d
Show file tree
Hide file tree
Showing 18 changed files with 1,249 additions and 1,037 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ path = "src/compiler_driver/main.rs"
[dependencies]
clap = { version = "4.4.6", features = ["derive"] }
utf8-chars = "3.0.1"
indoc = "2.0.4"
9 changes: 4 additions & 5 deletions src/compiler/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,8 @@ impl BinaryOperation {

#[derive(Clone, Debug)]
pub enum TypeNode {
Named {
// TODO: scoped names (e.g. thinga::thingb::Type)
name: String,
Path {
names: Vec<String>,
},
Pointer {
pointee_type: Box<TypeNode>,
Expand All @@ -287,8 +286,8 @@ pub enum TypeNode {
impl fmt::Display for TypeNode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Named { name } => {
write!(f, "{name}")
Self::Path { names } => {
write!(f, "{}", names.join("::"))
},
Self::Pointer { pointee_type, semantics } => match semantics {
PointerSemantics::Immutable => write!(f, "*{pointee_type}"),
Expand Down
20 changes: 13 additions & 7 deletions src/compiler/ast/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,21 @@ impl<'a, T: BufRead> Parser<'a, T> {
pub fn parse_type(&mut self, allowed_ends: Option<&[Token]>) -> crate::Result<TypeNode> {
match self.get_token()? {
Token::Literal(Literal::Identifier(name)) => {
let name = name.clone();
let mut names = vec![name.clone()];
self.scan_token()?;

while let Some(Token::Colon2) = self.current_token() {
self.scan_token()?;
names.push(self.expect_identifier()?);
self.scan_token()?;
}

if let Some(allowed_ends) = allowed_ends {
self.expect_token(allowed_ends)?;
}

Ok(TypeNode::Named {
name: name,
Ok(TypeNode::Path {
names,
})
},
Token::Star => {
Expand Down Expand Up @@ -346,8 +352,8 @@ impl<'a, T: BufRead> Parser<'a, T> {
return_type = Box::new(self.parse_type(allowed_ends)?);
}
else {
return_type = Box::new(TypeNode::Named {
name: "void".into(),
return_type = Box::new(TypeNode::Path {
names: vec!["void".into()],
});

if let Some(allowed_ends) = allowed_ends {
Expand Down Expand Up @@ -483,8 +489,8 @@ impl<'a, T: BufRead> Parser<'a, T> {
self.scan_token()?;
self.parse_type(Some(&[Token::CurlyLeft, Token::Semicolon]))?
} else {
TypeNode::Named {
name: "void".into(),
TypeNode::Path {
names: vec!["void".into()],
}
};
let body = if let Some(Token::CurlyLeft) = self.current_token() {
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ pub enum Error {
UndefinedSymbol {
name: String,
},
UndefinedModule {
name: String,
},
PartialType {
type_name: String,
},
Expand Down Expand Up @@ -241,6 +244,9 @@ pub enum Error {
GlobalSymbolConflict {
name: String,
},
TypeSymbolConflict {
name: String,
},
FunctionSignatureConflict {
function_name: String,
old_type: String,
Expand Down Expand Up @@ -338,6 +344,7 @@ impl fmt::Display for Error {
Self::CannotMutateValue { type_name } => write!(f, "cannot mutate value of type '{type_name}' as it is not 'mut'"),
Self::ExpectedLValue {} => write!(f, "expected an lvalue"),
Self::UndefinedSymbol { name } => write!(f, "undefined symbol '{name}'"),
Self::UndefinedModule { name } => write!(f, "undefined module '{name}'"),
Self::PartialType { type_name } => write!(f, "type '{type_name}' is declared but not defined"),
Self::UnknownType { type_name } => write!(f, "unrecognized type name '{type_name}'"),
Self::NonConstantArrayLength {} => write!(f, "array length must be constant"),
Expand Down Expand Up @@ -379,6 +386,7 @@ impl fmt::Display for Error {
Self::MissingFunctionArguments { expected_count, got_count } => write!(f, "too few arguments (expected {expected_count}, got {got_count})"),
Self::ExtraFunctionArguments { expected_count, got_count } => write!(f, "too many arguments (expected {expected_count}, got {got_count})"),
Self::GlobalSymbolConflict { name } => write!(f, "global name '{name}' is already in use"),
Self::TypeSymbolConflict { name } => write!(f, "module or type name '{name}' is already in use"),
Self::FunctionSignatureConflict { function_name, old_type, new_type } => {
writeln!(f, "conflicting signatures for function '{function_name}':")?;
writeln!(f, "old: {old_type}")?;
Expand Down
Loading

0 comments on commit 21c341d

Please sign in to comment.