Commit e0d5dad3 authored by Laurent Le Brun's avatar Laurent Le Brun
Browse files

Better handling of if/else and empty blocks

Fix a crash due to empy block.
Empty else blocks are removed.

Fixes https://github.com/laurentlb/Shader_Minifier/issues/3
parent 817b7ff4
......@@ -79,13 +79,17 @@ let minify file =
code
let run files =
let fail (exn:exn) s =
printfn "%s" s;
printfn "%s" exn.StackTrace
1
try
let codes = Array.map minify files
CGen.print (Array.zip files codes)
0
with
| Failure s as exn -> printfn "%s" s; 1 //; printfn "%s" exn.StackTrace
| exn -> printfn "Error: %s" exn.Message; 1 //; printfn "%s" exn.StackTrace
| Failure s as exn -> fail exn s
| exn -> fail exn exn.Message
let printHeader () =
printfn "Shader Minifier %s (c) Laurent Le Brun 2012" Ast.version
......
......@@ -176,7 +176,9 @@ let instr = function
if not Ast.noSequence && canOptimize then
let li = List.choose (function Expr e -> Some e | _ -> None) b
match returnExp with
| None -> Expr (List.reduce (fun acc x -> FunCall(Var ",", [acc;x])) li)
| None ->
if li = [] then Block []
else Expr (List.reduce (fun acc x -> FunCall(Var ",", [acc;x])) li)
| Some e ->
let expr = List.reduce (fun acc x -> FunCall(Var ",", [acc;x])) (li@[e])
Keyword("return", Some expr)
......@@ -188,6 +190,7 @@ let instr = function
| If(Var "true", e1, e2) -> e1
| If(Var "false", e1, Some e2) -> e2
| If(Var "false", e1, None) -> Block []
| If(c, b, Some (Block [])) -> If(c, b, None)
| Verbatim s -> Verbatim (stripSpaces s)
| e -> e
......
......@@ -2,5 +2,13 @@ void f() {
if (1) {
return 2;
} else {;}
if (x) {
;
} else {}
if (y); else;
more_semicolons;;;
return 3;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment