enums and identifier encoder

This commit is contained in:
2024-07-27 00:44:16 +02:00
parent c48cad6817
commit 607e023ca4
4 changed files with 62 additions and 2 deletions
View File
+1 -1
View File
@@ -2,6 +2,6 @@
macro_rules! _0x7072696e7466 { macro_rules! _0x7072696e7466 {
($($arg:tt)*) => { ($($arg:tt)*) => {
print!($($arg)*); print!($($arg)*)
}; };
} }
+55 -1
View File
@@ -78,7 +78,27 @@ impl Transpiler {
match t.as_rule() { match t.as_rule() {
Rule::identifier => { Rule::identifier => {
let ident = t.as_str().trim(); let ident = t.as_str().trim();
let ident_rs = format!("_0x{}", hex::encode(ident.as_bytes())); let mut ident_rs = String::from("_0x");
let mut iter = ident.chars().peekable();
while let Some(current) = iter.next() {
if iter.peek().is_some() {
let next = iter.next().unwrap();
if current == '.' {
ident_rs += "._0x";
} else if current == ':' && next == ':' {
ident_rs += "::_0x";
} else {
ident_rs += hex::encode(current.to_string()).as_str();
ident_rs += hex::encode(next.to_string()).as_str();
}
} else {
if current == '.' {
ident_rs += "._0x";
} else {
ident_rs += hex::encode(current.to_string()).as_str();
}
}
}
if self.mod_manager.macros.contains(&ident_rs) { if self.mod_manager.macros.contains(&ident_rs) {
output += ident_rs.as_str(); output += ident_rs.as_str();
output += "!"; output += "!";
@@ -119,6 +139,13 @@ impl Transpiler {
} }
res res
} }
pub fn transpile_vec(&mut self, pairs: Vec<Pair<Rule>>) -> String {
let mut res = String::new();
for pair in pairs {
res += self.transpile(pair).as_str();
}
res
}
pub fn transpile(&mut self, pair: Pair<Rule>) -> String { pub fn transpile(&mut self, pair: Pair<Rule>) -> String {
let mut res = String::new(); let mut res = String::new();
let tokens = to_indexable(pair.clone().into_inner()); let tokens = to_indexable(pair.clone().into_inner());
@@ -223,7 +250,34 @@ impl Transpiler {
); );
} }
Rule::enum_def => {
res += format!(
"enum {} {}{}{}",
self.transpile(tokens[0].clone()),
"{",
self.transpile_pairs(tokens[1].clone().into_inner()),
"}\n"
)
.as_str();
}
Rule::ecall => {
res += self.transpile(tokens[0].clone()).as_str();
if tokens.len() > 1 {
let mut params = tokens.clone();
params.remove(0);
res += "(";
res += self.transpile_vec(params).as_str();
res += ")";
}
}
Rule::comma => {
res += ",";
}
_ => { _ => {
println!("\x1b[33mUnh: {}\x1b[0m", pair);
res += " "; res += " ";
res += pair.as_str(); res += pair.as_str();
res += " "; res += " ";
+6
View File
@@ -4,6 +4,7 @@ wspc = { " " | "\t" | "\n" }
// [chars] // [chars]
comma = {","} comma = {","}
semicolon = {";"} semicolon = {";"}
dot = {"."}
// [literals] // [literals]
filepath = { ("/"|('a'..'z' | 'a'..'z' | '0'..'9' | "_" | "-" | "."))+ } filepath = { ("/"|('a'..'z' | 'a'..'z' | '0'..'9' | "_" | "-" | "."))+ }
@@ -36,6 +37,9 @@ var_def_set = { var_def ~ "=" ~ expr }
var_set = { def_identifier ~ "=" ~ expr } var_set = { def_identifier ~ "=" ~ expr }
func_def = { def_identifier ~ identifier ~ round_def ~ curly } func_def = { def_identifier ~ identifier ~ round_def ~ curly }
struct_def = { "struct" ~ identifier ~ curly_def } struct_def = { "struct" ~ identifier ~ curly_def }
ecall = { identifier ~ ( "(" ~ ((def_identifier ~ comma)* ~ def_identifier?) ~ ")" )? }
ecurly = { "{" ~ ((ecall ~ comma)* ~ ecall?) ~ "}" }
enum_def = { "enum" ~ identifier ~ ecurly }
rust_macro_lint = { "macro_rules" ~ "!" ~ identifier } rust_macro_lint = { "macro_rules" ~ "!" ~ identifier }
rust_code = { (rust_macro_lint | ANY)* } rust_code = { (rust_macro_lint | ANY)* }
@@ -59,6 +63,7 @@ expr_ = {
expr = { expr = {
(expr_ ~ plus ~ expr_) | (expr_ ~ plus ~ expr_) |
(expr_ ~ dot ~ identifier ~ round?) |
expr_ expr_
} }
@@ -77,6 +82,7 @@ top_expr = {
include_global | include_global |
func_def | func_def |
struct_def | struct_def |
enum_def |
map map
)* ~ !ANY )* ~ !ANY
} }