Skip to content

Commit

Permalink
Started working on adding traits to use with generics
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Oct 28, 2024
1 parent e104876 commit 1e8a2e9
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/ast/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
enumeration::Enum, global_variable::GlobalVar, structure::Structure, type_alias::TypeAlias,
Function, HelperExpr, SettingsId,
Function, HelperExpr, SettingsId, Trait,
};

#[derive(Clone, Debug)]
Expand All @@ -11,6 +11,7 @@ pub struct AstFile {
pub global_variables: Vec<GlobalVar>,
pub enums: Vec<Enum>,
pub helper_exprs: Vec<HelperExpr>,
pub traits: Vec<Trait>,
pub settings: Option<SettingsId>,
}

Expand All @@ -24,6 +25,7 @@ impl AstFile {
enums: vec![],
helper_exprs: vec![],
settings: None,
traits: vec![],
}
}
}
2 changes: 2 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod helper_expr;
mod module;
mod stmt;
mod structure;
mod traits;
mod type_alias;
mod workspace;

Expand All @@ -26,5 +27,6 @@ pub use helper_expr::*;
pub use module::*;
pub use stmt::*;
pub use structure::*;
pub use traits::*;
pub use type_alias::*;
pub use workspace::*;
9 changes: 9 additions & 0 deletions src/ast/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use super::Privacy;
use crate::source_files::Source;

#[derive(Clone, Debug)]
pub struct Trait {
pub name: String,
pub source: Source,
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 @@ -43,6 +43,7 @@ impl IdentifierState {
"zeroed" => TokenKind::ZeroedKeyword,
"pragma" => TokenKind::PragmaKeyword,
"pub" => TokenKind::PubKeyword,
"trait" => TokenKind::TraitKeyword,
_ => TokenKind::Identifier(identifier),
}
.at(self.start_source)
Expand Down
1 change: 1 addition & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod parse_helper_expr;
mod parse_stmt;
mod parse_structure;
mod parse_top_level;
mod parse_trait;
mod parse_type;
mod parse_type_alias;
mod parse_util;
Expand Down
4 changes: 4 additions & 0 deletions src/parser/parse_top_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
let helper_expr = self.parse_helper_expr(annotations)?;
ast_file.helper_exprs.push(helper_expr);
}
TokenKind::TraitKeyword => {
let trait_decl = self.parse_trait(annotations)?;
ast_file.traits.push(trait_decl);
}
TokenKind::EndOfFile => {
// End-of-file is only okay if no preceeding annotations
if !annotations.is_empty() {
Expand Down
44 changes: 44 additions & 0 deletions src/parser/parse_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use super::{
annotation::{Annotation, AnnotationKind},
error::ParseError,
Parser,
};
use crate::{
ast::{Privacy, Trait},
inflow::Inflow,
token::{Token, TokenKind},
};

impl<'a, I: Inflow<Token>> Parser<'a, I> {
pub fn parse_trait(&mut self, annotations: Vec<Annotation>) -> Result<Trait, ParseError> {
let source = self.source_here();
self.input.advance();

let name = self.parse_identifier(Some("for trait name after 'trait' keyword"))?;
self.ignore_newlines();

let mut privacy = Privacy::Private;

for annotation in annotations {
match annotation.kind {
AnnotationKind::Public => privacy = Privacy::Public,
_ => return Err(self.unexpected_annotation(&annotation, Some("for trait"))),
}
}

self.ignore_newlines();
self.parse_token(TokenKind::OpenCurly, Some("to begin trait body"))?;

while !self.input.peek_is_or_eof(TokenKind::CloseCurly) {
todo!("parse trait body");
}

self.parse_token(TokenKind::CloseCurly, Some("to end trait body"))?;

Ok(Trait {
name,
source,
privacy,
})
}
}
3 changes: 3 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub enum TokenKind {
ZeroedKeyword,
PragmaKeyword,
PubKeyword,
TraitKeyword,
Member,
Add,
Subtract,
Expand Down Expand Up @@ -172,6 +173,7 @@ impl Display for TokenKind {
TokenKind::ZeroedKeyword => f.write_str("'zeroed' keyword"),
TokenKind::PragmaKeyword => f.write_str("'pragma' keyword"),
TokenKind::PubKeyword => f.write_str("'pub' keyword"),
TokenKind::TraitKeyword => f.write_str("'trait' keyword"),
TokenKind::Member => f.write_str("'.'"),
TokenKind::Add => f.write_str("'+'"),
TokenKind::Subtract => f.write_str("'-'"),
Expand Down Expand Up @@ -300,6 +302,7 @@ impl TokenKind {
| TokenKind::ZeroedKeyword
| TokenKind::PragmaKeyword
| TokenKind::PubKeyword
| TokenKind::TraitKeyword
| TokenKind::OpenAngle
| TokenKind::Comma
| TokenKind::Colon
Expand Down

0 comments on commit 1e8a2e9

Please sign in to comment.