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