Skip to content

Latest commit

 

History

12 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

My programming language

I invented a programming language with a syntax in between C++ and Python. This repo implements a runtime that executes code written in it.

It parses code into an abstract syntax tree and recursively evaluates and collapses branches with depth-first traversal. The implementation of instruction nodes (such as LoopInstr for while loops) and expression evaluation (arithmetic and logic operations such as addition) is written in Python.

These are some of the constructs that my language can handle:

  • Variable declaration and assignment, scoped within frames
  • Arithmetic and logic operations
  • If-else
  • While loops
  • The print keyword

Example

For example, this code:

int a = 10 + 2 * 4;
int b = 5;

if (a > b + 6){
  print a;
} else {
  print b + 2 ^ (a + 1);
}

while (b < 8){
  b = b + 1;
  print b;
}

Gets parsed into this tree:

DeclareIntInstr(
    name='a',
    expr=[10, Op(ADD), 2, Op(MUL), 4],
)
DeclareIntInstr(
    name='b',
    expr=[5],
)
CondInstr(
    cond=['a', Op(GT), 'b', Op(ADD), 6],
    instrs=[
        PrintInstr(expr=['a']),
    ],
    instrs_else=[
        PrintInstr(expr=['b', Op(ADD), 2, Op(POW), ['a', Op(ADD), 1]]),
    ],
)
LoopInstr(
    cond=['b', Op(LT), 8],
    instrs=[
        SetVarInstr(name='b', expr=['b', Op(ADD), 1]),
        PrintInstr(expr=['b']),
    ],
)

Running the code prints:

18
6
7
8

How to use it

Set up the environment:

uv sync

Use the Python entrypoints:

from main import resolve_instructions
from parsers import parse_instructions

code = "print 12 * (6 - 1);"

instructions = parse_instructions(code)
resolve_instructions(instructions)  # prints 60

About

Executes code written in my programming language.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages