Welcome to the most delightfully silly and strangely powerful Calculator Lang Interpreter โ a tiny C-powered toy language that parses lines like ADD=5, SUB=2, MULTI=3, DIV=4 and folds them into a numeric result. Itโs quirky, itโs simple, and it does arithmetic with a wink ๐.
This README explains the language, the interpreter structure, how to build and run it, and shows fun examples so you can start hacking immediately.
- What it is ๐
- Language spec & syntax ๐งพ
- Tokenizer / Lexer / AST (the inner magic) โจ
- Build & Run ๐ ๏ธ
- Examples ๐
- Gotchas & notes
โ ๏ธ - Contributing / License ๐
A tiny interpreter written in C that:
- Reads a "program" from
include/main.cl. - Tokenizes lines like
ADD=42and converts them into tokens. - Applies tokens in sequence to a running integer
natija(result). - Supports ADD, SUB, MULTI, DIV operations.
- Extremely opinionated and minimal โ perfect for learning compilers, lexers, and ASTs with a goofy smile ๐.
- Statements are separated with semicolons
;. - Each statement format:
OP=NUMBERwhere OP is one of:ADDโ additionSUBโ subtractionMULTIโ multiplicationDIVโ integer division
- Example program:
The interpreter ignores characters outside the flagged region and collects parts between semicolons.
;ADD=5;SUB=2;MULTI=3;DIV=2;
Notes:
- The code uses uppercase letters to identify methods (A/S/M/D).
- Number parsing is simple: finds digits and builds an int. If no digit found, token value becomes -1.
- Division is integer division; no safety for division-by-zero in the minimal implementation.
High level flow:
_tokenizer__โ collects "parts" between semicolons;and stores them inparts[].lexer_partsโ for each part, splits theOP=NUMstring, detects method withgetMethod()(AโADD, SโSUB, etc.), extracts numeric argument withdigitFinder(), then emits aToken.- Tokens placed into
tokens[]. Each token is astruct _Token { METHOD method; int val; }. ast(token)โ applies the token to the globalnatija(result), using aswitchon the METHOD enum (ADD, SUB, MULTI, DIV).- After all tokens processed,
printf("RESULT: %d\n", natija);.
Implementation notes:
- Global arrays:
parts[100],tokens[100]. METHODis an enum:{ADD, SUB, MULTI, DIV}.natijais the accumulating integer result.- Code is intentionally simple; good for experimenting and improving.
- Place your program in
include/main.cl. Example content:;ADD=10;SUB=3;MULTI=2;DIV=7; - Compile the C interpreter (example):
or whatever your source file is named.
gcc -o calc main.c - Run:
Output:
./calcRESULT: 2
(Your project currently reads include/main.cl โ ensure that file exists, otherwise you'll see "Xato: Fayl topilmadi!" from the code.)
-
Simple:
;ADD=1;ADD=2;SUB=1; # natija = 0 + 1 + 2 - 1 => RESULT: 2 -
Multiplication and division:
;ADD=5;MULTI=3;DIV=2; # (0 + 5) * 3 / 2 => RESULT: 7 (integer division) -
Edge case (no numbers):
;ADD=;SUB=; # digitFinder returns -1 when no digit found โ results may be weird
- No error handling for divide-by-zero โ add checks before DIV in
ast. - Tokens and parts arrays have fixed capacity (100); consider dynamic arrays for bigger programs.
digitFindercurrently returns the first multi-digit integer it finds; it stops at a non-digit boundary.getMethoduses only the first letter โ ensure uppercaseA/S/M/D.- Code contains some Uzbek comments โ theyโre friendly hints! (e.g., โnatijaโ means result.)
- Add parentheses / precedence and real AST nodes.
- Support negative numbers, white-space robustness, lowercase ops.
- Add error reporting and source positions.
- Replace global arrays with dynamic vectors.
- Add unit tests and CI (GitHub Actions? yes please ๐).
This is a tiny learning project; contributions welcome. Keep PRs small and whimsical. No strict license included โ add one if you want collaboration rules.
Go forth and make arithmetic chaos with a grin ๐๐งฎ. If you want, I can help rewrite the lexer to support lowercase and spaces, add better error messages, or implement safe division and operator precedence โ just ask!