Design a new programming language grammar

What a fascinating task!

Here's a design for a new programming language grammar, which we'll call "Nexa". Nexa is a statically-typed, object-oriented language with a focus on simplicity, readability, and expressiveness.

Syntax

Nexa's syntax is designed to be concise and easy to read. It uses a combination of keywords, symbols, and indentation to define the structure of programs.

Basic Syntax Elements

Lexical Structure

Nexa's lexical structure is composed of the following elements:

Syntax Rules

Here are the basic syntax rules for Nexa:

  1. Program: A Nexa program consists of one or more statements, separated by semicolons (;).
    program {
     // statements
    }
  2. Statement: A statement is either a declaration, a function, or an expression.
    statement ::= declaration | function | expression
  3. Declaration: A declaration defines a variable, constant, or type.
    declaration ::= "var" identifier type | "const" identifier literal | "type" identifier type
  4. Function: A function defines a block of code that can be called multiple times.
    function ::= "function" identifier "(" parameter-list ")" block
  5. Parameter-list: A list of parameters passed to a function.
    parameter-list ::= identifier ("," identifier)*
  6. Block: A block of code, denoted by indentation.
    block ::= "{" statement* "}"
  7. Expression: An expression is a combination of values, operators, and functions.
    expression ::= term ((ADD | SUB) term)*
    term ::= factor ((MUL | DIV) factor)*
    factor ::= identifier | literal | "(" expression ")"
  8. Operators: Nexa supports the following operators:
    • + (addition)
    • - (subtraction)
    • * (multiplication)
    • / (division)
    • = (assignment)
    • == (equality)
    • != (inequality)

Type System

Nexa has a statically-typed type system, which means that the type of every expression is known at compile-time. The type system is based on the following principles:

Semantics

Nexa's semantics are defined by the following rules:

Example Code

Here's an example of a simple Nexa program:

program {
    var x: int = 5;
    var y: int = 3;

    function add(a: int, b: int): int {
        return a + b;
    }

    print(add(x, y));
}

This program defines two variables x and y of type int, a function add that takes two int arguments and returns an int value, and a print statement that calls the add function and prints the result.

This is just a starting point, and Nexa's grammar can be refined and extended as needed. I hope this gives you a good idea of what a new programming language grammar might look like!