Skip to content

benja2998/batch

benja2998/batch

Batch compiler written in Rust

Important

This compiler is incomplete. Consider contributing!

Usage

Install the compiler:

make # .cargo/bin has to be in PATH
# You can also run "make install", or "cargo install --path ." as they do the same thing

Run the compiler after installing:

make run ARGS="-h" # or "batch-compiler -h"

You can also try some of the tests:

make test<num> # where <num> is the test number. Currently, there are only 3 tests

Limitations

  • If the batch file has no exit commands that are always executed, it may lead to undefined behavior in the compiled binary
  • Many batch features are not supported, such as if statements, for loops, delayed variable expansion, etc
  • Doing this:
set command=calc.exe
%command%

will only work if it is an external command, not a built-in command

Features

  • Compiled output is significantly faster than the Windows cmd.exe due to the executable using native Windows APIs and native CPU instructions

Benchmarking

You can benchmark the compiler by running benchmark.bat.

Currently supported batch features

  • Echo command
  • Exit command
  • Labels
  • Goto command
  • Comments (rem or ::)
  • Set command
  • %VAR% variable syntax
  • Calling external commands

Supported OSes

Compiler itself

  • All OSes that support modern Cargo

Compiled batch files

How it works

The compiler architecture goes like this:

flowchart TD
  A[Batch file is provided as input] --> B[Compiler reads and tokenizes the batch file]
  B --> C[Tokens]
  C --> D[Compiler grows the parse tree]
  D --> E[Parse tree]
  E --> F[Variables in the batch file are expanded]
  F --> G[Updated parse tree]
  G --> H[Compiler does two passes over the parse tree to generate assembly code]
  H --> I[Assembly code]
  I --> J[Compiler calls NASM to assemble the code]
  J --> K[Object file]
  K --> L[Compiler calls a linker to produce the executable]
  L --> M[Executable]
Loading

License

The Apache License 2.0.

FAQ

Q: How can I contact you privately?

A: You can contact me at benja2998@duck.com. Responses won't be guaranteed as I don't look at it often.

Q: Will you add official Linux support?

A: Yes, but currently it is not a priority.

Q: Will you add official macOS support?

A: No.

Q: How can I contribute?

A: See CONTRIBUTING.md.

Q: What is batch?

A: Batch is a Windows scripting language developed by Microsoft. It is used in .cmd files, .bat files and the Windows command prompt.