Skip to content

Commit

Permalink
Refactor cpp.rs formatting and change lazy_static to OnceLock for Cpp…
Browse files Browse the repository at this point in the history
…_keyword check
  • Loading branch information
Qubi0-0 committed Oct 4, 2024
1 parent 6731a6d commit eb0576d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 110 deletions.
1 change: 0 additions & 1 deletion internal/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ itertools = { workspace = true }
once_cell = "1"
url = "2.2.1"
linked_hash_set = "0.1.4"
lazy_static = "1.4"

# for processing and embedding the rendered image (texture)
image = { workspace = true, optional = true, features = ["default"] }
Expand Down
131 changes: 22 additions & 109 deletions internal/compiler/generator/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

use std::fmt::Write;
use std::io::BufWriter;

use std::collections::HashSet;
use std::sync::OnceLock;
use lyon_path::geom::euclid::approxeq::ApproxEq;

use lazy_static::lazy_static;
use std::collections::HashSet;

/// The configuration for the C++ code generator
#[derive(Clone, Debug, Default, PartialEq)]
Expand All @@ -22,115 +21,29 @@ pub struct Config {
pub header_include: String,
}

// static initialization of C++ keywords Hashset
lazy_static! {
static ref CPP_KEYWORDS: HashSet<&'static str> = {
let keywords: HashSet<&str> = HashSet::from([
"alignas",
"alignof",
"and",
"and_eq",
"asm",
"atomic_cancel",
"atomic_commit",
"atomic_noexcept",
"auto",
"bitand",
"bitor",
"bool",
"break",
"case",
"catch",
"char",
"char8_t",
"char16_t",
"char32_t",
"class",
"compl",
"concept",
"const",
"consteval",
"constexpr",
"constinit",
"const_cast",
"continue",
"co_await",
"co_return",
"co_yield",
"decltype",
"default",
"delete",
"do",
"double",
"dynamic_cast",
"else",
"enum",
"explicit",
"export",
"extern",
"false",
"float",
"for",
"friend",
"goto",
"if",
"inline",
"int",
"long",
"mutable",
"namespace",
"new",
"noexcept",
"not",
"not_eq",
"nullptr",
"operator",
"or",
"or_eq",
"private",
"protected",
"public",
"reflexpr",
"register",
"reinterpret_cast",
"requires",
"return",
"short",
"signed",
"sizeof",
"static",
"static_assert",
"static_cast",
"struct",
"switch",
"synchronized",
"template",
"this",
"thread_local",
"throw",
"true",
"try",
"typedef",
"typeid",
"typename",
"union",
"unsigned",
"using",
"virtual",
"void",
"volatile",
"wchar_t",
"while",
"xor",
"xor_eq",
]);
keywords
};
}

// Check if word is one of C++ keywords
fn is_cpp_keyword(word: &str) -> bool {
CPP_KEYWORDS.contains(word)
static CPP_KEYWORDS: OnceLock<HashSet<&'static str>> = OnceLock::new();
let keywords = CPP_KEYWORDS.get_or_init(|| {
let keywords: HashSet<&str> = HashSet::from([
"alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit",
"atomic_noexcept", "auto", "bitand", "bitor", "bool", "break", "case", "catch",
"char", "char8_t", "char16_t", "char32_t", "class", "compl", "concept", "const",
"consteval", "constexpr", "constinit", "const_cast", "continue", "co_await",
"co_return", "co_yield", "decltype", "default", "delete", "do", "double",
"dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", "float",
"for", "friend", "goto", "if", "inline", "int", "long", "mutable", "namespace",
"new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq", "private",
"protected", "public", "reflexpr", "register", "reinterpret_cast", "requires",
"return", "short", "signed", "sizeof", "static", "static_assert", "static_cast",
"struct", "switch", "synchronized", "template", "this", "thread_local", "throw",
"true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using",
"virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq",
]);
keywords
});
keywords.contains(word)
}

fn ident(ident: &str) -> String {
Expand Down

0 comments on commit eb0576d

Please sign in to comment.