Skip to content

Commit

Permalink
Started working on visibility modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Sep 28, 2024
1 parent 3acd1af commit f402ca0
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/ast/function/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod parameters;

use super::{Stmt, Type};
use super::{Privacy, Stmt, Type};
use crate::{name::Name, source_files::Source, tag::Tag};
pub use parameters::{Parameter, Parameters};

Expand All @@ -14,4 +14,5 @@ pub struct Function {
pub source: Source,
pub abide_abi: bool,
pub tag: Option<Tag>,
pub privacy: Privacy,
}
1 change: 1 addition & 0 deletions src/lexer/identifier_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl IdentifierState {
"define" => TokenKind::DefineKeyword,
"zeroed" => TokenKind::ZeroedKeyword,
"pragma" => TokenKind::PragmaKeyword,
"pub" => TokenKind::PubKeyword,
_ => TokenKind::Identifier(identifier),
}
.at(self.start_source)
Expand Down
2 changes: 2 additions & 0 deletions src/parser/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum AnnotationKind {
Packed,
AbideAbi,
Namespace(String),
Pub,
}

impl AnnotationKind {
Expand All @@ -36,6 +37,7 @@ impl Display for AnnotationKind {
Self::Packed => "packed",
Self::AbideAbi => "abide_abi",
Self::Namespace(_) => "namespace",
Self::Pub => "pub",
})
}
}
1 change: 1 addition & 0 deletions src/parser/parse_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
let namespace = self.parse_identifier(Some("for namespace name"))?;
AnnotationKind::Namespace(namespace)
}
"pub" => AnnotationKind::Pub,
_ => {
return Err(ParseErrorKind::UnrecognizedAnnotation {
name: annotation_name,
Expand Down
5 changes: 4 additions & 1 deletion src/parser/parse_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
Parser,
};
use crate::{
ast::{Function, Parameters, TypeKind},
ast::{Function, Parameters, Privacy, TypeKind},
inflow::Inflow,
name::Name,
token::{Token, TokenKind},
Expand All @@ -18,12 +18,14 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
let mut is_foreign = false;
let mut abide_abi = false;
let mut namespace = None;
let mut privacy = Privacy::Private;

for annotation in annotations {
match annotation.kind {
AnnotationKind::Foreign => is_foreign = true,
AnnotationKind::AbideAbi => abide_abi = true,
AnnotationKind::Namespace(new_namespace) => namespace = Some(new_namespace),
AnnotationKind::Pub => privacy = Privacy::Public,
_ => return Err(self.unexpected_annotation(&annotation, Some("for function"))),
}
}
Expand Down Expand Up @@ -66,6 +68,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
source,
abide_abi,
tag: None,
privacy,
})
}
}
7 changes: 6 additions & 1 deletion src/parser/parse_top_level.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
annotation::Annotation,
annotation::{Annotation, AnnotationKind},
error::{ParseError, ParseErrorKind},
Parser,
};
Expand Down Expand Up @@ -27,6 +27,11 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
self.ignore_newlines();
}

// Parse pub keyword
if self.input.peek().is_pub_keyword() {
annotations.push(AnnotationKind::Pub.at(self.input.advance().source));
}

// Ignore newlines after annotations
self.ignore_newlines();

Expand Down
15 changes: 8 additions & 7 deletions src/resolve/function_search_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,22 @@ impl FunctionSearchCtx {
}

if name.namespace.is_empty() {
let mut matches = self.imported_namespaces.iter().filter_map(|namespace| {
self.available
.get(&ResolvedName::new(&Name::new(
let mut matches = self
.imported_namespaces
.iter()
.filter_map(|namespace| {
self.available.get(&ResolvedName::new(&Name::new(
Some(namespace.to_string()),
name.basename.clone(),
)))
.and_then(|list| list.first())
.copied()
});
})
.flatten();

if let Some(found) = matches.next() {
if matches.next().is_some() {
return Err(FindFunctionError::Ambiguous);
} else {
return Ok(found);
return Ok(*found);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub enum TokenKind {
DefineKeyword,
ZeroedKeyword,
PragmaKeyword,
PubKeyword,
Member,
Add,
Subtract,
Expand Down Expand Up @@ -168,6 +169,7 @@ impl Display for TokenKind {
TokenKind::DefineKeyword => f.write_str("'define' keyword"),
TokenKind::ZeroedKeyword => f.write_str("'zeroed' keyword"),
TokenKind::PragmaKeyword => f.write_str("'pragma' keyword"),
TokenKind::PubKeyword => f.write_str("'pub' keyword"),
TokenKind::Member => f.write_str("'.'"),
TokenKind::Add => f.write_str("'+'"),
TokenKind::Subtract => f.write_str("'-'"),
Expand Down Expand Up @@ -294,6 +296,7 @@ impl TokenKind {
| TokenKind::DefineKeyword
| TokenKind::ZeroedKeyword
| TokenKind::PragmaKeyword
| TokenKind::PubKeyword
| TokenKind::OpenAngle
| TokenKind::Comma
| TokenKind::Colon
Expand Down

0 comments on commit f402ca0

Please sign in to comment.