Giter Site home page Giter Site logo

7hommay / t2b Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thosakwe/t2b

0.0 1.0 0.0 27 KB

A wicked-powerful text macro language for building binary files.

Home Page: https://thosakwe.github.io/t2b/index.html

License: GNU General Public License v3.0

CMake 1.73% C++ 98.27%

t2b's Introduction

t2b

A wicked-powerful text macro language for building binary files. Supports comments, looping, Unicode, variables, conditionals, macros and recursion.

TLDR; Check out example/.

Pre-built binaries are available for Win64 and MacOS64.

Usage

$ t2b <filename> here
$ t2b # Read directly from stdin

t2b always writes to stdout. To output to a file, simply use a greater-than sign (>).

Language

Newlines are solely for the sake of readability; all whitespace is the same.

# This is a comment!
#
# Comments must be on their own line.

# Emit a byte. In DECIMAL.
u8 10

# Toggle hex mode ON.
# In hex mode, numbers will be interpreted as hexadecimal.
hex
u8 10

# Toggle hex mode OFF.
hex

# Spit out a signed integer.
i64 25677

# Print a string (no line break)
str hello

# Print with a line break.
strl hello

# Wrap in quotes to capture whitespace.
strl "hello world!"

# Escapes are supported.
str "hello, world!\n"

# Unicode?
str "\u{1234}"

# Print a newline.
endl

# Do something 5 times.
# Indentation is purely for readability.
times 5
    u8 23
    u32 24
    times 10
        # We can nest loops
        str "50 times!!!"
    endtimes
endtimes

# Capture the output of another command.
# Oh, and store it into a variable.
set foo (u8 33)

# Access its value.
set bar (get foo)

# Emit its value 3 times.
times 3 get foo

# Create a simple macro.
macro emit_twice x
begin
    times 2 (get x)
endmacro

# Call it!
emit_twice 24

Why?

The need for such a program arose when I was working on writing a simple VM. Manually hex-editing files for an ever changing bytecode spec is tedious, error-prone, and most of all - sucky.

Now there's a lightweight way to do just that.

Supported Commands

  • u8...u64 - Emit unsigned integer
  • i8...i64 - Emit signed integer
  • hex - Toggle hex mode on/off (defaults to OFF)
  • str <expr> - Write a string
  • strl <expr> - Write a string AND newline
  • endl - Write a newline
  • not <expr> Boolean NOT a char
  • if <cond> <pred> endif Execute <pred> if <cond> == 1
  • get <expr> - Fetch the global variable named expr
  • set <expr1> <expr2> - Assign the global variable named expr1 to expr2
  • = - Compare two values, return 0 or 1
  • times <count> <pred> endtimes - Execute <pred> <count> times. i is always set to the current iteration's index.
  • macro <name> <param-names...> begin <pred> endmacro - Declare a custom macro named <name>.

What's next?

It's now feasible to write a machine code compiler in shell. Hooray. Not sure why you would ever do that to yourself, though.

t2b's People

Contributors

thosakwe avatar fadedbee avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.