8000 New output format: Rust by Kwarf · Pull Request #227 · laurentlb/shader-minifier · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

New output format: Rust #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/formatter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,25 @@ let private printNasmHeader out (shaders: Ast.Shader[]) exportedNames =
fprintfn out "_%s:%s\tdb '%s', 0" name Environment.NewLine (Printer.print shader.code)
fprintfn out ""

let private printRustHeader out (shaders: Ast.Shader[]) exportedNames =
fprintfn out "// Generated with Shader Minifier %s (https://github.com/laurentlb/Shader_Minifier/)" Options.version

for value: Ast.ExportedName in List.sort exportedNames do
if value.ty = "" then
fprintfn out "pub const VAR_%s: &'static [u8] = b\"%s\\0\";" (value.name.ToUpper()) value.newName
else
fprintfn out "pub const %s_%s: &'static [u8] = b\"%s\\0\":" value.ty (value.name.ToUpper()) value.newName

for shader in shaders do
fprintfn out ""
let name = (Path.GetFileName shader.filename).Replace(".", "_")
fprintfn out "pub const %s: &'static [u8] = b\"\\%s %s\\0\";" (name.ToUpper()) Environment.NewLine (Printer.print shader.code)

let print out shaders exportedNames = function
| Options.IndentedText -> printIndented out shaders
| Options.Text -> printNoHeader out shaders
| Options.CHeader -> printHeader out shaders false exportedNames
| Options.CList -> printHeader out shaders true exportedNames
| Options.JS -> printJSHeader out shaders exportedNames
| Options.Nasm -> printNasmHeader out shaders exportedNames
| Options.Rust -> printRustHeader out shaders exportedNames
1 change: 1 addition & 0 deletions src/options.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type OutputFormat =
| [<CustomCommandLine("c-array")>] CList
| [<CustomCommandLine("js")>] JS
| [<CustomCommandLine("nasm")>] Nasm
| [<CustomCommandLine("rust")>] Rust

let text() = Text

Expand Down
5 changes: 3 additions & 2 deletions src/printer.fs
A124
Original file line number Diff line numberDiff line change
Expand Up @@ -70,6 +70,7 @@ module private PrinterImpl =
| Options.Text | Options.JS -> ""
| Options.CHeader | Options.CList -> out "\"%s%s\"" Environment.NewLine spaces
| Options.Nasm -> out "'%s\tdb%s'" Environment.NewLine spaces
| Options.Rust -> out "\\%s%s" Environment.NewLine spaces

let rec exprToS indent exp = exprToSLevel indent 0 exp

Expand Down Expand Up @@ -135,7 +136,7 @@ module private PrinterImpl =
match outputFormat with
| Options.Text | Options.JS | Options.IndentedText -> "\n"
| Options.Nasm -> "', 10, '"
| Options.CHeader | Options.CList -> "\\n"
| Options.CHeader | Options.CList | Options.Rust -> "\\n"

// Print HLSL semantics
let semToS sem =
Expand Down Expand Up @@ -180,7 +181,7 @@ module private PrinterImpl =
| Options.IndentedText -> s
| Options.Text -> s
| Options.JS -> s
| Options.CHeader | Options.CList | Options.JS -> s.Replace("\"", "\\\"").Replace("\n", "\\n")
| Options.CHeader | Options.CList | Options.JS | Options.Rust -> s.Replace("\"", "\\\"").Replace("\n", "\\n")
| Options.Nasm -> s.Replace("'", "\'").Replace("\n", "', 10, '")

/// Detect if the current statement might accept a dangling else.
Expand Down
0