The State-C project is a simple utility to embed various types of data into an applications data section. It is written in Go, but is primarily intended to help in the development of C and C++ applications.
The basic idea is the following:
We write a file foo
detailing data known at your software’s compile time.
We pass this file to State-C, which will produce foo.asm
, the equivalent of this file in assembly code (NASM).
A header file foo.h
is also produced.
This assembly file can be compiled to an object file foo.o
.
The object file can be linked into a larger project. Other components of the project can access the data in this file
via labels defined in foo.h
.
The type of data representable via State-C includes:
Most of learning State-C is learning the syntax of State-C source files.
Scalar values are declared quite simply with:
int a = 10
float b = 8.67
Arrays are declared by specifying the type and a list of its elements.
array int fib 0 1 1 2 3 5 8 13 21
array float pos 5.88 1.131 9.111
Tables are declared by giving the names and types of their fields, followed by a complete list of rows.
table bruh int a float b
69 3.14
420 2.718
Enums in State-C are simply a collection of integer scalars with related names.
Their values are assigned sequentially starting from 0
.
enum category
A B C D
Arbitary data can embedded as so:
dump message "message.txt"
Here message.txt
might contain:
This is a text file, but this could be any arbitrary data.
The header file will look something like:
#pragma once
extern void* message;
extern int message_LEN;
The extra variable message_LEN
details how many bytes message.txt
contained.
In order to avoid namespace contamination, it is recommended to give all data labels a prefix.
prefix res_
int a = 10
In this example, the resulting header file will look something like this:
#pragma once
extern int res_a;
Source files are parsed top-to-bottom, and any scalars defined earlier in a source file can be reused in later defintions.
float pi = 3.14
float e = 2.718
array float cool_nums e pi