Skip to content

About

A programming language whose source file is a PNG image. Interpreter, static analyzer, text-to-image compiler and stepping debugger, in C.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

ColorEngine

ci version language platform license

ColorEngine is a programming language where the program is stored as a PNG image.

The image is split into 32×32 pixel cells. Each cell represents one token. The cell color determines the token, while the row indentation determines the nesting level.

There is also a text format. Text files can be converted to PNG with t2p.

Building

Open ColorEngine.sln in Visual Studio, or run this from a developer command prompt:

msbuild ColorEngine.vcxproj /p:Configuration=Debug /p:Platform=x64

Example:

ColorEngine.exe run examples\AsteroidLike\AsteroidLike.png

Commands

Command Description
run <program.png> Run a program
dbg <program.png> Run a program one statement at a time
analyze <program.png> Check a program without running it
codegen <program.png> [-o out.txt] Print the program as pseudocode
info <program.png> Print the token for each cell
t2p <program.txt> <out.png> Convert text to PNG
palette Print the color table
version Print the version

analyze exits with code 1 when the program contains errors.

The version lives in .github/project.yml, which is what the README badge reads, and in include/version.h, which is what the binary reports. CI fails if the two drift apart.

run and dbg options

Option Description
--no-window Use the current console
--no-pause Do not wait for a key when the program ends
--max-steps <n> Stop after n statements; 0 means unlimited

By default, run opens a new console window and waits for a key at the end.

For scripts, use --no-window.

Ctrl+C stops a running program.

Image format

Programs are stored as PNG images made of 32×32 pixel cells.

The color of a cell is calculated from the average color of its non-transparent pixels.

A cell is considered empty when all pixels are transparent (alpha below 32), or when the average color is near white (all channels are at least 244).

The alpha channel selects the token category. RGB selects the token inside that category.

Alpha Category
255 Commands: print, if, while, func, cls, sleep, ...
230 Names: varref and its index digits
210 Digits 0–9 and the decimal point
190 Operators: == != < > <= >= + - * / % and or not ?
170 true, false
150 , [ ] ; :
130 String marker

Token matching uses the nearest color in the selected category. The squared RGB distance must be below 3000.

Because the category is stored separately, different token categories can use similar RGB colors.

Strings are encoded differently. They use alpha 255, the red channel stores the ASCII value, and green and blue are at most 10. Supported ASCII values are 32–126.

The PNG must have an alpha channel. Plain RGB PNGs do not contain the category information and are rejected.

8-bit and 16-bit PNGs are supported. Interlaced and palette-based PNGs are not.

Names do not have their own color. For example, var127 is stored as:

varref 1 2 7

The number of names is therefore limited by the table size (1024).

Text format

The text format uses one statement per line.

Tokens are separated by dots and leading spaces define indentation. # starts a comment line.

Example:

var.var0.0
while.var0.<.1.0
  assign.var0.var0.+.1
  if.var0.==.5
    break
print.string."done"

Numbers are made from consecutive digit tokens:

1.0      -> 10
3.0.0    -> 300

The decimal point is the dot token:

3.dot.1.4 -> 3.14
dot.5     -> 0.5

Strings use:

string."text"

The quotes are not stored in the PNG.

Variables and functions use the same name format:

var0
var42

include."lib.txt" can be used at indentation level 0 to include a library.

Only functions reachable from the program are copied, based on the call graph.

Included files may contain function declarations only and cannot include other files.

Language

Statements

Form Description
print.<expr>[.,.<expr>…] Print values separated by spaces
var.<name>.<expr> Declare a variable
assign.<name>.<expr> Assign a value
assign.<name>.[.<expr>.].<expr> Assign an array element
push.<name>.<expr> Append to an array
if / elif / else Conditional blocks
while.<cond> While loop
do.<cond> Run the body, then test the condition
for.<init>.;.<cond>.;.<step> For loop
switch.<expr> Switch statement
case.<v>[.,.<v>…] Switch case
case.default Default switch case
break / continue Loop control
func.<name>[.<param>…] Define a function
return.<expr> Return a value
call.<name>[.<arg>…] Call a function
cls Clear the console
loc.<col>.<row> Set the cursor position
color.<n> Set the console color
sleep.<ms> Sleep for a number of milliseconds

Switch cases do not fall through.

Expressions

Supported operators and expressions include:

  • arithmetic: + - * / %
  • comparison: == != < > <= >=
  • logic: and or not
  • ternary: <cond>.?.<a>.:.<b>
  • len.<expr>
  • array literals: [.1.,.2.]
  • array indexing: <name>.[.<expr>.]

key reads the next pending keycode, or 0 when there is none.

key_avail reports whether a key is waiting.

now_ms returns the current millisecond clock value.

These are expressions, not statements.

Values can be numbers, strings, booleans, arrays, or null.

and and or use short-circuit evaluation.

Scope

At the top level, var creates a global variable.

Inside a function, var creates a local variable. A local with the same name hides the global one.

Locals and parameters exist only for the duration of the call.

assign writes to the local variable when one exists. Otherwise it writes to the global variable.

Example:

var.var5.1
func.var0.var1
  assign.var5.var1
  var.var2.var1.*.2
  return.var2

Here assign.var5 writes to the global variable, while var.var2 creates a local variable even if var2 already exists globally.

Function arguments are evaluated in the caller's scope before the new call frame is created.

This allows parameters to be passed to other functions and allows recursive calls.

Limits

The limits are defined in include/colorlang.h and can be overridden at compile time.

Limit Value
Variables 1024
Functions 256
Call depth 32
Indentation depth 64
String length 512
Array length 1024
Parameters 64
Arguments 64
AST nodes 65536

Tests

Run:

powershell -ExecutionPolicy Bypass -File tests\run_tests.ps1

Use -Exe to test another build.

The test script:

  1. runs every program in tests\cases and compares stdout with the matching .out file;
  2. checks the expected error for programs in tests\malformed;
  3. reads the compiled images again with codegen.

Exit code 1 means a test failed.

Project layout

include/    headers
src/        implementation and miniz
tests/      test runner, test programs and malformed images
examples/   AsteroidLike example
assets/     logo and icon

Builds go to bin\<Platform>\<Configuration>, so the 32-bit and 64-bit binaries sit side by side. tests\run_tests.ps1 picks the most recently built one unless you pass -Exe.

The main path is:

png_reader
    -> cell_scanner
    -> parser
    -> analyzer
    -> interpreter

t2p uses:

txt_to_grid
    -> png_writer

License

MIT. See LICENSE.

About

A programming language whose source file is a PNG image. Interpreter, static analyzer, text-to-image compiler and stepping debugger, in C.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages