From 1aab723c3f059610905fc50e2b6d9bcaeacfc4d3 Mon Sep 17 00:00:00 2001 From: Leo Dev Date: Mon, 22 Jul 2024 18:43:46 +0200 Subject: [PATCH] small features --- src/main.rs | 2 +- src/transpiler.rs | 32 +++++++++++++++++++++++++++++++- src/wyst.pest | 7 ++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index cfaccb3..84b27aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use compiler::{compile_rust, transpile_file}; fn main() { let matches = App::new("Wyst") .version("0.51") - .author("Leo dev") + .author("Orus") .about("The wyst compiler") .arg( Arg::with_name("file") diff --git a/src/transpiler.rs b/src/transpiler.rs index ef83d54..0e2959a 100644 --- a/src/transpiler.rs +++ b/src/transpiler.rs @@ -91,7 +91,20 @@ impl Transpiler { .as_str(); } Rule::var_def => { - res += format!("let mut {}: {}", tokens[1].as_str(), tokens[0].as_str()).as_str(); + res += format!( + "let mut {}: {}", + self.transpile(tokens[1].clone()), + self.transpile(tokens[0].clone()) + ) + .as_str(); + } + Rule::var_def_set => { + res += format!( + "{} = {}", + self.transpile(tokens[0].clone()), + self.transpile(tokens[1].clone()), + ) + .as_str(); } Rule::curly => { res += "{"; @@ -128,6 +141,7 @@ impl Transpiler { } else { res += match ident { "void" => "()", + "int" => "i32", _ => ident_rs.as_str(), }; } @@ -153,6 +167,22 @@ impl Transpiler { res += self.transpile_pairs(pair.into_inner()).as_str(); } + Rule::struct_def => { + res += "struct "; + res += self.transpile(tokens[0].clone()).as_str(); + res += "{"; + res += self.transpile(tokens[1].clone()).as_str(); + res += "}"; + } + + Rule::semicolon => { + res += ";\n"; + } + + Rule::string => { + res += pair.as_str(); + } + _ => { res += " "; res += pair.as_str(); diff --git a/src/wyst.pest b/src/wyst.pest index 761a618..aaa9c58 100644 --- a/src/wyst.pest +++ b/src/wyst.pest @@ -1,4 +1,5 @@ WHITESPACE = _{ " " | "\t" | "\n" } +wspc = { " " | "\t" | "\n" } // [chars] comma = {","} @@ -15,6 +16,7 @@ raw_string = { (!("\\" | "\"") ~ ANY)+ } string = { "\"" ~ (raw_string | escape )* ~ "\"" } char = { "'" ~ (escape | ANY) ~ "'" } bool = @{"true" | "false"} +keyword = @{"struct"} // [numbers] hex = @{ "0" ~ ("x"|"X") ~ ('0'..'9' | 'a'..'f' | 'A'..'F')*} @@ -27,10 +29,12 @@ def_identifier = {identifier ~ ("<" ~ ((def_identifier ~ comma)* ~ def_identifie // [definitions] round_def = { "(" ~ ((var_def ~ comma)* ~ var_def?) ~ ")" } +curly_def = { "{" ~ (var_def ~ semicolon)* ~ "}" } var_def = { def_identifier ~ identifier } 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 } rust_macro_lint = { "macro_rules" ~ "!" ~ identifier } rust_code = { (rust_macro_lint | ANY)* } @@ -70,6 +74,7 @@ top_expr = { ( include | include_global | - func_def + func_def | + struct_def )* ~ !ANY }