Skip to content

Language

Programming languages are defined by generating the specification for the grammar of the language.

A program of the language takes the form of a text file and a lexer and parser are combined to process the text file into an abstract syntax tree (AST) for the language.

Abstract Syntax Tree

Provides a tree representation for a program of the language as an algebraic data type (ADT) that can be algorithmically processed.

We implement the specification of the ADTs for the AST with custom Pydantic models.

Text Representation

The text representation for the program is a JSON schema inherited from Pydantic. Allowing us to make use of the serialization and parsing/deserialization already implemented by Pydantic.

Warning

To use the serialization and deserialization implementation in Pydantic, subclasses should not be used in the type hints.

A work around for when subclasses are needed is to use a separate variable assigned to be the union of the subclasses.

Example
from typing import Union
from oqd_compiler_infrastructure import TypeReflectBaseModel


class MySuperClass(TypeReflectBaseModel):
    pass

class MySubClassA(MySuperClass):
    pass

class MySubClassB(MySuperClass):
    pass

MySuperClassSubtypes = Union[MySubClassA, MySubClassB]

class MyClass(TypeReflectBaseModel):
    arg: MySuperClassSubtypes