• Stars
    star
    467
  • Rank 93,935 (Top 2 %)
  • Language
    C
  • License
    MIT License
  • Created over 8 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ParTcl - a micro Tcl implementation

Partcl - a minimal Tcl interpreter

Build Status

Features

  • ~600 lines of "pedantic" C99 code
  • No external dependencies
  • Good test coverage
  • Can be extended with custom Tcl commands
  • Runs well on bare metal embedded MCUs (~10k of flash is required)

Built-in commands:

  • subst arg
  • set var ?val?
  • while cond loop
  • if cond branch ?cond? ?branch? ?other?
  • proc name args body
  • return
  • break
  • continue
  • arithmetic operations: +, -, *, /, <, >, <=, >=, ==, !=

Usage

struct tcl tcl;
const char *s = "set x 4; puts [+ [* $x 10] 2]";

tcl_init(&tcl);
if (tcl_eval(&tcl, s, strlen(s)) != FERROR) {
    printf("%.*s\n", tcl_length(tcl.result), tcl_string(tcl.result));
}
tcl_destroy(&tcl);

Language syntax

Tcl script is made up of commands separated by semicolons or newline symbols. Commnads in their turn are made up of words separated by whitespace. To make whitespace a part of the word one may use double quotes or braces.

An important part of the language is command substitution, when the result of a command inside square braces is returned as a part of the outer command, e.g. puts [+ 1 2].

The only data type of the language is a string. Although it may complicate mathematical operations, it opens a broad way for building your own DSLs to enhance the language.

Lexer

Any symbol can be part of the word, except for the following special symbols:

  • whitespace, tab - used to delimit words
  • \r, \n, semicolon or EOF - used to delimit commands
  • Braces, square brackets, dollar sign - used for substitution and grouping

Partcl has special helper functions for these char classes:

static int tcl_is_space(char c);
static int tcl_is_end(char c);
static int tcl_is_special(char c, int q);

tcl_is_special behaves differently depending on the quoting mode (q parameter). Inside a quoted string braces, semicolon and end-of-line symbols lose their special meaning and become regular printable characters.

Partcl lexer is implemented in one function:

int tcl_next(const char *s, size_t n, const char **from, const char **to, int *q);

tcl_next function finds the next token in the string s. from and to are set to point to the token start/end, q denotes the quoting mode and is changed if " is met.

A special macro tcl_each(s, len, skip_error) can used to iterate over all the tokens in the string. If skip_error is false - loop ends when string ends, otherwise loop can end earlier if a syntax error is found. It allows to "validate" input string without evaluating it and detect when a full command has been read.

Data types

Tcl uses strings as a primary data type. When Tcl script is evaluated, many of the strings are created, disposed or modified. In embedded systems memory management can be complex, so all operations with Tcl values are moved into isolated functions that can be easily rewritten to optimize certain parts (e.g. to use a pool of strings, a custom memory allocator, cache numerical or list values to increase performance etc).

/* Raw string values */
tcl_value_t *tcl_alloc(const char *s, size_t len);
tcl_value_t *tcl_dup(tcl_value_t *v);
tcl_value_t *tcl_append(tcl_value_t *v, tcl_value_t *tail);
int tcl_length(tcl_value_t *v);
void tcl_free(tcl_value_t *v);

/* Helpers to access raw string or numeric value */
int tcl_int(tcl_value_t *v);
const char *tcl_string(tcl_value_t *v);

/* List values */
tcl_value_t *tcl_list_alloc();
tcl_value_t *tcl_list_append(tcl_value_t *v, tcl_value_t *tail);
tcl_value_t *tcl_list_at(tcl_value_t *v, int index);
int tcl_list_length(tcl_value_t *v);
void tcl_list_free(tcl_value_t *v);

Keep in mind, that ..._append() functions must free the tail argument. Also, the string returned by tcl_string() it not meant to be mutated or cached.

In the default implementation lists are implemented as raw strings that add some escaping (braces) around each iterm. It's a simple solution that also reduces the code, but in some exotic cases the escaping can become wrong and invalid results will be returned.

Environments

A special type, struct tcl_env is used to keep the evaluation environment (a set of functions). The interpreter creates a new environment for each user-defined procedure, also there is one global environment per interpreter.

There are only 3 functions related to the environment. One creates a new environment, another seeks for a variable (or creates a new one), the last one destroys the environment and all its variables.

These functions use malloc/free, but can easily be rewritten to use memory pools instead.

static struct tcl_env *tcl_env_alloc(struct tcl_env *parent);
static struct tcl_var *tcl_env_var(struct tcl_env *env, tcl_value_t *name);
static struct tcl_env *tcl_env_free(struct tcl_env *env);

Variables are implemented as a single-linked list, each variable is a pair of values (name + value) and a pointer to the next variable.

Interpreter

Partcl interpreter is a simple structure struct tcl which keeps the current environment, array of available commands and a last result value.

Interpreter logic is wrapped around two functions - evaluation and substitution.

Substitution:

  • If argument starts with $ - create a temporary command [set name] and evaluate it. In Tcl $foo is just a shortcut to [set foo], which returns the value of "foo" variable in the current environment.
  • If argument starts with [ - evaluate what's inside the square brackets and return the result.
  • If argument is a quoted string (e.g. {foo bar}) - return it as is, just without braces.
  • Otherwise return the argument as is.

Evaluation:

  • Iterates over each token in a list
  • Appends words into a list
  • If the command end is met (semicolor, or newline, or end-of-file - our lexer has a special token type TCMD for them) - then find a suitable command (the first word in the list) and call it.

Where the commands are taken from? Initially, a Partcl interpeter starts with no commands, but one may add the commands by calling tcl_register().

Each command has a name, arity (how many arguments is shall take - interpreter checks it before calling the command, use zero arity for varargs) and a C function pointer that actually implements the command.

Builtin commands

"set" - tcl_cmd_set, assigns value to the variable (if any) and returns the current variable value.

"subst" - tcl_cmd_subst, does command substitution in the argument string.

"puts" - tcl_cmd_puts, prints argument to the stdout, followed by a newline. This command can be disabled using #define TCL_DISABLE_PUTS, which is handy for embedded systems that don't have "stdout".

"proc" - tcl_cmd_proc, creates a new command appending it to the list of current interpreter commands. That's how user-defined commands are built.

"if" - tcl_cmd_if, does a simple if {cond} {then} {cond2} {then2} {else}.

"while" - tcl_cmd_while, runs a while loop while {cond} {body}. One may use "break", "continue" or "return" inside the loop to contol the flow.

Various math operations are implemented as tcl_cmd_math, but can be disabled, too if your script doesn't need them (if you want to use Partcl as a command shell, not as a programming language).

Building and testing

All sources are in one file, tcl.c. It can be used as a standalone interpreter, or included as a single-file library (you may want to rename it into tcl.h then).

Tests are run with clang and coverage is calculated. Just run "make test" and you're done.

Code is formatted using clang-format to keep the clean and readable coding style. Please run it for pull requests, too.

License

Code is distributed under MIT license, feel free to use it in your proprietary projects as well.

More Repositories

1

lorca

Build cross-platform modern desktop apps in Go + HTML5
Go
7,954
star
2

jsmn

Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket
C
3,633
star
3

awfice

The world smallest office suite
HTML
3,462
star
4

fenster

The most minimal cross-platform GUI library
C++
513
star
5

tray

Cross-platform, super tiny C99 implementation of a system tray icon with a popup menu.
C
484
star
6

metric

Minimal metrics for Go (counter/gauge/histogram). No dependencies. Compatible with expvar. Web UI included.
Go
353
star
7

luash

Tiny lua module to write shell scripts with lua (inspired by Python's sh module)
Lua
302
star
8

pt

Protothreads (coroutines) in C99. Highly portable, but work best in low-end embedded systems.
C
267
star
9

o

Tiny and simple React clone
JavaScript
249
star
10

lua-promises

A+ promises in Lua
Lua
216
star
11

log

Ultimately minimal (yet very convenient) logger for Android and Java
Java
157
star
12

tojvm

A toy JVM in Go
Go
156
star
13

webview-python

Python bindings to webview
Objective-C
151
star
14

bfapi

Resilient, scalable Brainf*ck, in the spirit of modern systems design
Go
144
star
15

nokia-composer

Nokia Composer in 512 bytes
HTML
125
star
16

expr

Fast and lightweight math expression evaluator in C99
C
119
star
17

hid

Simple HID driver for Go (pure golang, no dependencies, no cgo)
Go
119
star
18

zs

Absolutely minimal static site generator in Go (powers https://zserge.com)
Go
91
star
19

tinysh

Tiny UNIX shell, de-obfuscated, modernized, and "rewritten in Rust".
C
88
star
20

nanonn

A nano-framework for neural networks
Rust
83
star
21

dotfiles

git clone --bare https://github.com/zserge/dotfiles $HOME/.dotfiles && git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME checkout
Vim Script
79
star
22

lc3-forth

Post-Apocalyptic Computing: bootstrapping Forth environment for LC-3 CPU
C
79
star
23

1bitr

Minimalistic text-based 1-bit music tracker
C
73
star
24

carnatus

A tiny chess engine in Go (sunfish port)
Go
65
star
25

odetoj

Rewrite of Arthur Whitney's one-page J interpreter in Rust
Rust
61
star
26

headline

Ascetic RSS reader in JavaScript, no server required
JavaScript
57
star
27

glob-grep

A little experiment: compare the languages aimed to replace C
Zig
52
star
28

buckbone

A simple android project generator for the Buck build system
Shell
52
star
29

q

Tiny and simple VueJS clone
JavaScript
46
star
30

beep

Cross-platform beep() function
C
43
star
31

slide

An attempt to implement Trikita Slide for desktop
C++
39
star
32

figma-simplify-path

Figma plugin to simplify vector paths
JavaScript
26
star
33

mucks

A tiny terminal session manager for Tmux, Screen and DVTM
Shell
20
star
34

anvil-kotlin-demos

Minimal tutorial/demos for Anvil+Kotlin
Kotlin
18
star
35

zserge.github.io

My static site
HTML
14
star
36

kv

An ultimately minimal persistent key-value store + LRU cache
Go
12
star
37

jsmn.lua

The world fastest JSON parser ported to Lua
Lua
11
star
38

aint

Code for the "AI or AIN'T" blog posts
Go
11
star
39

yu

Yu is a tee-like tool, but with rotation feature like logrotate
C
10
star
40

tab

🎼 A tiny CLI tool to render tabs for music instruments (🎹🎷🎺🎸🪕🪈 and many others!)
C
9
star
41

bsoz

One of the most minimal MOS6502 and retro computer emulators!
C
8
star
42

mdns

Very pragmatic mDNS implementation in Go
Go
8
star
43

kveer

A tiny in-memory key-value storage in Go with optional persistence (atomic backup file, or append-only)
Go
7
star
44

bf

Well, everyone has to write a brainf*ck interpreter at some point
C
7
star
45

covered

Trello Cover Card Generator
JavaScript
6
star
46

toy-java-agent

Toy Java agent
Java
6
star
47

atomicwriter

Atomic file writes in Go (using a unique temporary file and atomic rename)
Go
5
star
48

lex

A library for writing lexers in Go
Go
4
star
49

textizer

Minimal android widgets in Scheme
Java
4
star
50

chess

JavaScript
4
star
51

ping

An ultimately minimal social network, messaging, pub/sub and home automation app
4
star
52

tinylangs

Real programming langauges in 50 lines of code
Python
4
star
53

photo

Minimalistic private photo booth
HTML
3
star
54

incr

incr.it backend
JavaScript
3
star
55

zine

Tiny CSS template to produce micro-zines (folded 8-page magazines)
Python
3
star
56

one-click-hugo-cms

CSS
2
star
57

grafana-zero

Python
2
star
58

gif

Simple GIF recorder
HTML
2
star
59

protoc-gen-micro

Protobuf code generation for micro
Go
2
star
60

scaffold

Templates for quick project start
Java
1
star
61

r

Something that rhymes. Or not.
1
star