From ef7068e027ae018e11594985245e7611f992ccfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Wed, 18 Jan 2023 07:30:00 +0100 Subject: [PATCH] Special class members: structures and a few definitions --- src/main.rs | 1 + src/special.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/special.rs diff --git a/src/main.rs b/src/main.rs index 787fc99..ac99889 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod interpreter; mod parser; mod resolver; mod scanner; +mod special; mod tokens; use std::{ diff --git a/src/special.rs b/src/special.rs new file mode 100644 index 0000000..50858e3 --- /dev/null +++ b/src/special.rs @@ -0,0 +1,58 @@ +use std::collections::HashMap; + +use lazy_static::lazy_static; + +/// The various "special" class members. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum SpecialClassMember { + Init, + ToString, + Compare, +} + +/// The characteristics of a special class member. +#[derive(Debug, Clone)] +pub struct SpecialCharacteristics { + /// The type of the special member + pub which: SpecialClassMember, + /// The identifier for the member + pub identifier: &'static str, + /// The minimal amount of arguments for the member + pub min_args: usize, + /// The maximal amount of arguments for the member + pub max_args: usize, +} + +lazy_static! { + /// A map that can be used to retrieve a special member's characteristics. + pub static ref SPECIAL_MEMBERS: HashMap = { + let characteristics = vec![ + SpecialCharacteristics { + which: SpecialClassMember::Init, + identifier: "init", + min_args: 0, + max_args: usize::MAX, + }, + SpecialCharacteristics { + which: SpecialClassMember::ToString, + identifier: "to_string", + min_args: 0, + max_args: 0, + }, + SpecialCharacteristics { + which: SpecialClassMember::Compare, + identifier: "compare", + min_args: 1, + max_args: 1, + }, + ]; + characteristics.into_iter().map(|c| (c.which, c)).collect() + }; + /// A map associating special member identifiers to types. + pub static ref SPECIAL_MEMBER_IDENTIFIERS: HashMap<&'static str, SpecialClassMember> = { + SPECIAL_MEMBERS + .iter() + .map(|(k, v)| (v.identifier, *k)) + .collect() + }; +}