A language over Python

ORD is a language for designing integrated circuits, from the ordec project: Python with a few statements of its own. A cell holds view generators, and inside them a statement places an instance or declares a pin, port, net or path, and sets the attributes and parameters of what it placed. A program of ORD runs as a Python module. This page shows ORD written for fpeg, a grammar that extends the Python grammar and a transformer that extends the one to Python’s ast, checked against the language’s own parser.

The language

cell Divider:
    viewgen symbol(self) -> Symbol:
        input vin
        output vout

    viewgen schematic(self) -> Schematic:
        port vin: .pos=(0, 6)
        port vout: .pos=(8, 3)
        Res r1: .$r=10k; .p -- vin; .m -- vout
        Res r2: .$r=4.7k; .p -- vout; .m -- gnd
        ! .vout.pos.y < .vin.pos.y
  • cell Divider: is a class, and viewgen schematic(self) -> Schematic: a method that builds a view of it.

  • input vin declares a pin, port vin: ... a port, and Res r1: ... places an instance of the cell Res, each with a body that the placed thing is the root of.

  • .pos is an attribute of that root, .$r one of its parameters, 10k a number with an SI prefix, and ! states a constraint.

  • .p -- vin connects two nets, which needs nothing from the grammar: Python reads it as .p - (-vin).

The grammar

// ORD, a language for designing integrated circuits, as an extension of the Python grammar: cells
// and their view generators, statements that place instances and declare ports, nets and paths,
// `.name` for an attribute of the view's root, `.$name` for a parameter, `!` for a constraint,
// and numbers with an SI prefix.

%extends "python"
%keywords soft "cell" "viewgen" "net" "path" "anonymous"

// Python's compound statements come first: each starts with a word of its own, and `match` with
// its case blocks would otherwise read as an instance placed under the kind `match`.
?compound: ...
         | celldef
         | viewgen
         | net_block
         | path_block
         | anonymous_block
         | node_block

// The simple statements of ORD come first, since Python's expression statement would take the
// first word of `input vin` and leave the line to fail. Each ends where the statement ends, so
// that `type X = int` is left to Python.
?simple: constrain
       | net_line
       | path_line
       | anonymous_line
       | node_line
       | ...

decorated: decorators=(decorator %same)+ definition=(function_def | class_def | celldef | viewgen)

celldef: "cell" name=NAME ":" body=block
viewgen: "viewgen" name=NAME "(" %on(bracket, parameters=parameters? ")") ("->" returns=expression)? ":" body=block
constrain: "!" test=expression

// A statement that places an instance or declares a port names its kind, then what it places.
node_block: kind=primary target=context_target ":" body=block
node_line: kind=primary targets=(context_target ++ ",") &_STATEMENT_END
anonymous_block: "anonymous" kind=primary target=context_target ":" body=block
anonymous_line: "anonymous" kind=primary targets=(context_target ++ ",") &_STATEMENT_END
net_block: "net" target=context_target ":" body=block
net_line: "net" targets=(context_target ++ ",") &_STATEMENT_END
path_block: "path" target=context_target ":" body=block
path_line: "path" targets=(context_target ++ ",") &_STATEMENT_END
_STATEMENT_END: ";" | _NEWLINE | !.

?context_target: target=context_target "." name=NAME -> target_attribute
               | target=context_target "[" %on(bracket, slices=slices "]") -> target_item
               | NAME

// Remembered as Python's is, since a replacing rule says its own attributes, and every statement
// of ORD that places an instance tries a primary before the statements of Python try it again.
@memo
?primary: value=primary ".$" name=NAME -> getparam
        | ...

?atom: SI_NUMBER
     | ...
     | ".$" name=NAME -> root_getparam
     | "." name=NAME? -> root

SI_NUMBER "number with an SI prefix": (FLOAT_NUMBER | INT_NUMBER) [afpnumkMGT] !\p{XID_Continue}

Every rule that ORD changes is an ordinary rule of the Python grammar: the compound and the simple statements, the decorated definitions, the primary expressions and the atoms. ... keeps what Python has, and where it stands decides which reading wins. Python’s compound statements come first, since each starts with a word of its own and a match with its case blocks would otherwise read as an instance placed under the kind match. The new simple statements come first, since input vin would otherwise start an expression statement with input, and the line would fail at its second name. Each of them ends where the statement ends, so that type X = int is left to Python. A statement of ORD tries a primary before Python’s statements try the same primary again, which @memo turns into one try.

The transformer

The transformer, tests/ord_ast.py in the repository, is a subclass of fpeg.python.ToAst with a method for each new rule. A cell gives a ClassDef, a view generator a decorated FunctionDef, and a statement that places something an assignment from the view’s context with a with around its body. A method gives a list where one statement of ORD becomes two. ast.fix_missing_locations gives the nodes that a method builds the position of the statement around them.

Checked against the language’s own parser

The tests read the 76 programs of the application at a pinned commit with this grammar and transformer, and compare the ast with the one the language’s own parser makes, by ast.dump with every field shown. All 76 agree. The language’s parser also takes two statements on one line without a ;, as net a b, which Python rejects and this grammar rejects too.

Time

The benchmarks run the language’s own pipeline and fpeg’s on the same file, from source text to an ast with positions, and the benchmarks page shows both.