more statements

This commit is contained in:
2024-07-29 04:30:00 +02:00
parent ab840b36e4
commit 5b08de7d16
2 changed files with 87 additions and 20 deletions
+62 -7
View File
@@ -79,17 +79,20 @@ impl Transpiler {
Rule::identifier => {
let ident = t.as_str().trim();
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();
let ident_chars: Vec<char> = ident.chars().collect();
for i in 0..ident_chars.len() {
let current = ident_chars[i];
if ident_chars.len() > i + 1 {
let next = ident_chars[i + 1];
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();
if current != ':' && current != '.' {
ident_rs +=
hex::encode(current.to_string()).as_str();
}
}
} else {
if current == '.' {
@@ -108,6 +111,7 @@ impl Transpiler {
"int" => "i32",
"float" => "f64",
"Vec" => "Vec",
"bool" => "bool",
_ => {
if self.mod_manager.map.contains_key(ident) {
self.mod_manager.map.get(ident).unwrap()
@@ -248,7 +252,7 @@ impl Transpiler {
res += self.transpile_pairs(pair.into_inner()).as_str();
}
Rule::expr_ => {
Rule::term => {
res += self.transpile_pairs(pair.into_inner()).as_str();
}
@@ -326,6 +330,57 @@ impl Transpiler {
res += self.transpile(tokens[0].clone()).as_str();
}
Rule::if_stm => {
res += "if ";
res += self.transpile_pairs(pair.into_inner()).as_str();
}
Rule::else_if_stm => {
res += "else if ";
res += self.transpile_pairs(pair.into_inner()).as_str();
}
Rule::else_stm => {
res += "else ";
res += self.transpile_pairs(pair.into_inner()).as_str();
}
Rule::bool => {
res += pair.as_str();
}
Rule::operator => {
res += pair.as_str();
}
Rule::if_operator => {
res += pair.as_str();
}
Rule::if_expr => {
res += self.transpile_pairs(pair.into_inner()).as_str();
}
Rule::namespace_def => {
res += "mod ";
res += self.transpile_pairs(pair.into_inner()).as_str();
}
Rule::top_code => {
res += "{";
res += self.transpile_pairs(pair.into_inner()).as_str();
res += "}";
}
Rule::top_expr => {
res += self.transpile_pairs(pair.into_inner()).as_str();
}
Rule::impl_def => {
res += "impl ";
res += self.transpile_pairs(pair.into_inner()).as_str();
}
_ => {
println!("\x1b[33mUnh: {}\x1b[0m", pair);
res += " ";
+25 -13
View File
@@ -1,5 +1,4 @@
WHITESPACE = _{ ("//" ~ (!"\n" ~ ANY)*) | ("/*" ~ (!"*/" ~ ANY)*) | " " | "\t" | "\n" }
wspc = { " " | "\t" | "\n" }
// [chars]
comma = {","}
@@ -41,26 +40,34 @@ struct_def = { "struct" ~ identifier ~ curly_def }
ecall = { identifier ~ ( "(" ~ ((def_identifier ~ comma)* ~ def_identifier?) ~ ")" )? }
ecurly = { "{" ~ ((ecall ~ comma)* ~ ecall?) ~ "}" }
enum_def = { "enum" ~ identifier ~ ecurly }
top_code = {"{"~ top_expr ~"}"}
namespace_def = {"namespace" ~ identifier ~ top_code}
impl_def = {"impl" ~ identifier ~ top_code}
// [Ptr]
ptr_fn_def = { def_identifier ~ "*" ~ identifier ~ round_def ~ curly }
ptr_set = { "*" ~ identifier ~ "=" ~ expr }
refrence = { "&" ~ identifier }
// [Operators]
if_operator = {"&&"|"||"}
operator = {"=="}
if_expr = {expr ~ (if_operator ~ (("("~if_expr*~")")|expr))*}
// [Statement]
return_stm = { "return" ~ expr }
if_stm = { "if" ~ if_expr ~ curly }
else_if_stm = { "else" ~ "if" ~ if_expr ~ curly }
else_stm = { "else" ~ curly }
rust_macro_lint = { "macro_rules" ~ "!" ~ identifier }
rust_code = { (rust_macro_lint | ANY)* }
plus = {"+"}
call = {identifier ~ round}
expr_ = {
term = {
call |
round |
curly |
char |
hex |
float |
@@ -72,32 +79,37 @@ expr_ = {
}
expr = {
(expr_ ~ plus ~ expr_) |
(expr_ ~ dot ~ identifier ~ round?) |
expr_
(term ~ operator ~ term)+ |
(term ~ dot ~ identifier ~ round?) |
term
}
code_expr = {
(
((
return_stm |
var_def_set|
var_def |
var_set |
ptr_set |
expr
) ~ semicolon
) ~ semicolon) |
if_stm |
else_if_stm |
else_stm
}
top_expr = {
(
func_def |
namespace_def |
include |
include_global |
func_def |
ptr_fn_def |
struct_def |
enum_def |
map
)* ~ !ANY
map |
impl_def
)*
}
header_expr = {