KITE: A Handcrafted Programming Language in C for Termux
A developer has implemented the KITE programming language in pure C, compiling it to run in Termux on Android. The interpreter uses a classic architecture: lexer, parser, and tree-walking interpreter. The total codebase is about 3200 lines, with a focus on performance through reference counting instead of garbage collection.
The syntax is designed for readability: no semicolons, blocks using colons and end. Variables are declared with set, conditional constructs are when/orwhen/else, and return is give.
Example basic code:
set name = "world"
say("Hello, ${name}!")
def factorial(n):
when n <= 1: give 1 end
give n * factorial(n - 1)
end
loop for i in range(10):
say("${i}! = ${factorial(i)}")
end
Interpreter Architecture
Lexer
The lexer (lexer.c, ~200 lines) tokenizes source code. It supports string interpolation ${}: "Hello, ${name}!" becomes TK_FMTSTR with subexpressions. Tokens: TK_SET, TK_IDENT, TK_NUM, TK_PLUS, etc.
Parser
A recursive descent parser (parser.c, ~700 lines) builds the AST. Nodes are enum NKind:
typedef enum {
ND_NUM, ND_STR, ND_FMTSTR, ND_BOOL, ND_NIL,
ND_SET, ND_ASSIGN, ND_BINOP, ND_UNOP,
ND_CALL, ND_INDEX, ND_PROP,
ND_DEF, ND_WHEN, ND_LOOP_WHILE, ND_LOOP_FOR,
ND_DO, ND_OBJ, ND_GIVE, ND_BREAK, ND_NEXT,
/* ... */
} NKind;
Expression precedence: assign → or → and → not → cmp → add → mul → pow → unary → postfix → primary.
Interpreter
The tree-walking interpreter (interp.c, ~1600 lines) recursively traverses the AST:
Value *eval(Interp *ip, Node *n, Env *env) {
switch (n->kind) {
case ND_NUM: return val_num(n->num);
case ND_SET: {
Value *v = eval(ip, n->set.val, env);
env_def(env, n->set.name, v);
return val_nil();
}
/* ... */
}
}
Memory Management and Closures
Reference counting in the Value structure:
struct Value {
VType type;
int refs;
union { double num; char *str; KiteList *list; /* ... */ };
};
val_ref() / val_unref() manage deallocation. Closures capture Env:
typedef struct KiteFn {
char *name;
char **params;
int nparams;
Node *body;
Env *closure;
int refs;
} KiteFn;
Example:
def make_counter(start):
set count = start
def inc():
count += 1
give count
end
give inc
end
set c = make_counter(0)
say(c()) # 1
say(c()) # 2
Object-Oriented Programming
OOP via obj: classes are KiteClass with methods, instances are KiteInstance with fields. Inheritance, super, private fields with _.
obj Animal:
set name = nil
def init(self, name):
self.name = name
end
def speak(self):
say("${self.name} makes a sound")
end
end
obj Dog extends Animal:
set _tricks = 0
def learn(self):
self._tricks += 1
end
def speak(self):
super.speak(self)
say("${self.name}: Woof!")
end
end
set d = Dog.new("Buddy")
d.learn()
d.speak()
say(d is Dog) # true
Error Handling and Standard Libraries
do/err blocks:
do:
set data = file_read("config.txt")
err IOError:
say("File not found, creating...")
file_create("config.txt")
err:
say("Unexpected error: ${err_msg}")
end
Error types: ZeroDivisionError, NameError, IndexError, TypeError, IOError, AccessError.
Standard modules:
math:tan,log2,clamp,lerprand:rand(),rand_choice()string:str_pad_left,str_countlist:list_sum,list_zipio:file_read,file_linesos:os_env,os_shell
Compilation and Running in Termux
Installation:
pkg install clang maketar -xzf kite-lang.tar.gzcd kite-langmake CC=clangcp kite $PREFIX/bin/kitekite --version
REPL support, vim/micro with syntax highlighting.
File statistics:
| File | Lines | Role |
|----------|-------|-----------------------|
| kite.h | ~250 | Types, AST |
| lexer.c | ~200 | Tokenizer |
| parser.c | ~700 | Parser |
| value.c | ~300 | Values, refcount |
| interp.c | ~1600 | Interpreter |
| main.c | ~120 | REPL, entry |
Total: ~3200 lines.
Key Takeaways
- Minimalist syntax with
set/when/givefor readability and error prevention. - Reference counting without GC ensures predictable performance.
- Full-featured OOP with inheritance, closures, and encapsulation.
- Compilation on Android/Termux without dependencies.
- Open source (MIT), ready for forks and PRs.
— Editorial Team
No comments yet.