Skip to content

HeliseiKroopin/CalcLang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Calculator Lang Interpreter โ€” README ๐Ÿ˜บ๐Ÿงฎ๐ŸŽ‰

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.


Table of Contents

  • What it is ๐Ÿš€
  • Language spec & syntax ๐Ÿงพ
  • Tokenizer / Lexer / AST (the inner magic) โœจ
  • Build & Run ๐Ÿ› ๏ธ
  • Examples ๐Ÿ˜Ž
  • Gotchas & notes โš ๏ธ
  • Contributing / License ๐Ÿ“œ

What it is ๐Ÿš€

A tiny interpreter written in C that:

  • Reads a "program" from include/main.cl.
  • Tokenizes lines like ADD=42 and 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 ๐Ÿ˜.

Language spec & syntax ๐Ÿงพ

  • Statements are separated with semicolons ;.
  • Each statement format: OP=NUMBER where OP is one of:
    • ADD โ€” addition
    • SUB โ€” subtraction
    • MULTI โ€” multiplication
    • DIV โ€” integer division
  • Example program:
    ;ADD=5;SUB=2;MULTI=3;DIV=2;
    
    The interpreter ignores characters outside the flagged region and collects parts between semicolons.

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.

Tokenizer / Lexer / AST (how it works) โœจ

High level flow:

  1. _tokenizer__ โ€” collects "parts" between semicolons ; and stores them in parts[].
  2. lexer_parts โ€” for each part, splits the OP=NUM string, detects method with getMethod() (Aโ†’ADD, Sโ†’SUB, etc.), extracts numeric argument with digitFinder(), then emits a Token.
  3. Tokens placed into tokens[]. Each token is a struct _Token { METHOD method; int val; }.
  4. ast(token) โ€” applies the token to the global natija (result), using a switch on the METHOD enum (ADD, SUB, MULTI, DIV).
  5. After all tokens processed, printf("RESULT: %d\n", natija);.

Implementation notes:

  • Global arrays: parts[100], tokens[100].
  • METHOD is an enum: {ADD, SUB, MULTI, DIV}.
  • natija is the accumulating integer result.
  • Code is intentionally simple; good for experimenting and improving.

Build & Run ๐Ÿ› ๏ธ

  1. Place your program in include/main.cl. Example content:
    ;ADD=10;SUB=3;MULTI=2;DIV=7;
    
  2. Compile the C interpreter (example):
    gcc -o calc main.c
    
    or whatever your source file is named.
  3. Run:
    ./calc
    
    Output:
    RESULT: 2
    

(Your project currently reads include/main.cl โ€” ensure that file exists, otherwise you'll see "Xato: Fayl topilmadi!" from the code.)


Examples ๐Ÿ˜Ž

  • 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
    

Gotchas & notes โš ๏ธ

  • 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.
  • digitFinder currently returns the first multi-digit integer it finds; it stops at a non-digit boundary.
  • getMethod uses only the first letter โ€” ensure uppercase A/S/M/D.
  • Code contains some Uzbek comments โ€” theyโ€™re friendly hints! (e.g., โ€œnatijaโ€ means result.)

Where to improve (fun TODOs) ๐Ÿ› ๏ธ

  • 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 ๐ŸŽ‰).

Contributing / License ๐Ÿ“œ

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!

About

๐Ÿ”ข Calculator Lang in C Language

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages