enums and identifier encoder
This commit is contained in:
+55
-1
@@ -78,7 +78,27 @@ impl Transpiler {
|
||||
match t.as_rule() {
|
||||
Rule::identifier => {
|
||||
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) {
|
||||
output += ident_rs.as_str();
|
||||
output += "!";
|
||||
@@ -119,6 +139,13 @@ impl Transpiler {
|
||||
}
|
||||
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 {
|
||||
let mut res = String::new();
|
||||
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 += pair.as_str();
|
||||
res += " ";
|
||||
|
||||
@@ -4,6 +4,7 @@ wspc = { " " | "\t" | "\n" }
|
||||
// [chars]
|
||||
comma = {","}
|
||||
semicolon = {";"}
|
||||
dot = {"."}
|
||||
|
||||
// [literals]
|
||||
filepath = { ("/"|('a'..'z' | 'a'..'z' | '0'..'9' | "_" | "-" | "."))+ }
|
||||
@@ -36,6 +37,9 @@ var_def_set = { var_def ~ "=" ~ expr }
|
||||
var_set = { def_identifier ~ "=" ~ expr }
|
||||
func_def = { def_identifier ~ identifier ~ round_def ~ curly }
|
||||
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_code = { (rust_macro_lint | ANY)* }
|
||||
@@ -59,6 +63,7 @@ expr_ = {
|
||||
|
||||
expr = {
|
||||
(expr_ ~ plus ~ expr_) |
|
||||
(expr_ ~ dot ~ identifier ~ round?) |
|
||||
expr_
|
||||
}
|
||||
|
||||
@@ -77,6 +82,7 @@ top_expr = {
|
||||
include_global |
|
||||
func_def |
|
||||
struct_def |
|
||||
enum_def |
|
||||
map
|
||||
)* ~ !ANY
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user