Improved codegen

This commit is contained in:
2026-05-22 02:34:01 +02:00
parent c9a3c4693c
commit 960cc40886
5 changed files with 38 additions and 20 deletions
+5 -2
View File
@@ -166,9 +166,12 @@ impl GenRust for Postfix {
Postfix::Call(args) => { Postfix::Call(args) => {
cg.add("("); cg.add("(");
for arg in args { for (i, arg) in args.iter().enumerate() {
if i > 0 {
cg.add(", ");
}
arg.gen_rust(ctx, cg); arg.gen_rust(ctx, cg);
cg.add(", ");
} }
cg.add(")"); cg.add(")");
+13
View File
@@ -81,6 +81,19 @@ impl RustCodegen {
} }
} }
} }
pub fn ensure_brackets_expr(&mut self, ctx: &mut Context, expr: &Expression) {
match expr {
Expression::Statement(stmt) => self.ensure_brackets(ctx, stmt),
_ => {
self.add("{");
self.indent += 1;
expr.gen_rust(ctx, self);
self.indent -= 1;
self.add("}");
}
}
}
} }
impl GenRust for Attribute { impl GenRust for Attribute {
+12 -10
View File
@@ -18,10 +18,11 @@ impl GenRust for Block {
if let Some(soft_return) = &self.1 { if let Some(soft_return) = &self.1 {
ctx.expr_ensure_semicolon = false; ctx.expr_ensure_semicolon = false;
soft_return.gen_rust(ctx, cg); soft_return.gen_rust(ctx, cg);
cg.addln("");
} }
cg.indent -= 1; cg.indent -= 1;
cg.add_indentedln("}"); cg.add_indented("}");
} }
} }
@@ -57,17 +58,14 @@ impl GenRust for Statement {
p.gen_rust(ctx, cg); p.gen_rust(ctx, cg);
} }
cg.add(" => {"); cg.add(" => ");
cg.indent += 1;
body.gen_rust(ctx, cg); cg.ensure_brackets_expr(ctx, body);
cg.indent -= 1;
cg.add_indentedln("}");
} }
cg.indent -= 1; cg.indent -= 1;
cg.add_indentedln("}"); cg.addln("");
cg.add_indented("}");
} }
Statement::If { Statement::If {
@@ -77,16 +75,18 @@ impl GenRust for Statement {
} => { } => {
cg.add("if "); cg.add("if ");
initial.condition.gen_rust(ctx, cg); initial.condition.gen_rust(ctx, cg);
cg.add(" ");
cg.ensure_brackets(ctx, &initial.body); cg.ensure_brackets(ctx, &initial.body);
for else_if_branch in else_if { for else_if_branch in else_if {
cg.add("else if"); cg.add("else if");
else_if_branch.condition.gen_rust(ctx, cg); else_if_branch.condition.gen_rust(ctx, cg);
cg.add(" ");
cg.ensure_brackets(ctx, &else_if_branch.body); cg.ensure_brackets(ctx, &else_if_branch.body);
} }
if let Some(else_br) = else_branch { if let Some(else_br) = else_branch {
cg.add("else"); cg.add(" else ");
cg.ensure_brackets(ctx, else_br); cg.ensure_brackets(ctx, else_br);
} }
} }
@@ -116,7 +116,9 @@ impl GenRust for Statement {
cg.indent += 1; cg.indent += 1;
cg.ensure_brackets(ctx, body); ctx.expr_ensure_semicolon = true;
body.gen_rust(ctx, cg);
update.gen_rust(ctx, cg); update.gen_rust(ctx, cg);
+2 -2
View File
@@ -15,7 +15,7 @@ void main() {
match (args[1].as_str()) { match (args[1].as_str()) {
"run" | "build" | "check" | "r" | "b" | "c" => { "run" | "build" | "check" | "r" | "b" | "c" => {
var root = transpiler::build(); var root = transpiler::build(false);
println!(""); println!("");
@@ -26,7 +26,7 @@ void main() {
} }
} }
"transpile" | "t" => { "transpile" | "t" => {
transpiler::build(); transpiler::build(args.get(2).is_some());
} }
"init" => { "init" => {
init_project(args); init_project(args);
+6 -6
View File
@@ -7,7 +7,7 @@ use std::{
use mist_parser::error::ParseError; use mist_parser::error::ParseError;
pub fn build() -> PathBuf { pub fn build(force: bool) -> PathBuf {
let start = Instant::now(); let start = Instant::now();
let root = std::env::current_dir() let root = std::env::current_dir()
@@ -17,7 +17,7 @@ pub fn build() -> PathBuf {
let src_dir = root.join("src"); let src_dir = root.join("src");
let out_dir = root.join(".mist/src"); let out_dir = root.join(".mist/src");
build_dir(&root, &src_dir, &src_dir, &out_dir); build_dir(&root, &src_dir, &src_dir, &out_dir, force);
let elapsed = start.elapsed(); let elapsed = start.elapsed();
@@ -29,7 +29,7 @@ pub fn build() -> PathBuf {
root root
} }
fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path) { fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path, force: bool) {
let entries = match fs::read_dir(current_dir) { let entries = match fs::read_dir(current_dir) {
Ok(entries) => entries, Ok(entries) => entries,
Err(e) => { Err(e) => {
@@ -57,7 +57,7 @@ fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path) {
// recurse into nested directories // recurse into nested directories
if path.is_dir() { if path.is_dir() {
build_dir(root, base_src, &path, out_dir); build_dir(root, base_src, &path, out_dir, force);
continue; continue;
} }
@@ -72,7 +72,7 @@ fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path) {
let _ = fs::create_dir_all(parent); let _ = fs::create_dir_all(parent);
} }
if should_skip(&path, &dest_path) { if !force && should_skip(&path, &dest_path) {
continue; continue;
} }
@@ -83,7 +83,7 @@ fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path) {
let output_path = out_dir.join(relative).with_extension("rs"); let output_path = out_dir.join(relative).with_extension("rs");
// Cache layer: Skip if the generated .rs file is newer than the .mist source // Cache layer: Skip if the generated .rs file is newer than the .mist source
if should_skip(&path, &output_path) { if !force && should_skip(&path, &output_path) {
continue; continue;
} }