Skip to content

Commit

Permalink
Initial Protected Sections. Much debug of library handling. Enough to…
Browse files Browse the repository at this point in the history
… get to issue #2
  • Loading branch information
dan-fritchman committed Apr 15, 2022
1 parent 796fcab commit cbcc128
Show file tree
Hide file tree
Showing 11 changed files with 540 additions and 194 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

# Local files
scratch
*.json
.vscode/settings.json
tests/test_site.py
Expand Down
176 changes: 119 additions & 57 deletions netlist/data/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,20 @@
from pydantic.dataclasses import dataclass

# Local Imports
from .shared import SourceInfo, Ident, Int, Float, MetricNum, UnaryOperator, BinaryOperator
from .shared import (
SourceInfo,
Ident,
Int,
Float,
MetricNum,
UnaryOperator,
BinaryOperator,
)

# Keep a list of datatypes defined here,
# primarily so that we can update their forward-references at the end of this module.
datatypes = []
unions = []


def datatype(cls: type) -> type:
Expand Down Expand Up @@ -111,12 +120,33 @@ class Primitive:
kwargs: List[ParamVal]


@datatype
class QuotedString:
""" Quoted String Value """

txt: str


# Simulation Options can take on the values of expressions, e.g. parameter combinations,
# and those of quoted strings, often for file-path.
OptionVal = Union[QuotedString, "Expr"]
unions.append(OptionVal)


@datatype
class Option:
""" Simulation Option """

name: Ident # Option Name
val: OptionVal # Option Value


@datatype
class Options:
""" Simulation Options """

name: Optional[Ident] # Option Name. FIXME: could this be removed
vals: List[ParamVal] # List of name: value pairs
name: Optional[Ident] # Name of the Option-set. Used by some dialects.
vals: List[Option] # List of [`Option`]s


@datatype
Expand Down Expand Up @@ -227,6 +257,27 @@ class LibSection:
entries: List["Entry"] # Entry List


@datatype
class StartProtectedSection:
""" Start of a `ProtectedSection` """

... # Empty


@datatype
class EndProtectedSection:
""" End of a `ProtectedSection` """

... # Empty


@datatype
class ProtectedSection:
""" Protected Section """

entries: List["Entry"] # Entry List


@datatype
class Library:
""" Library, as Generated by the Spice `.lib` Definition Card
Expand All @@ -249,7 +300,7 @@ class UseLib:
class End:
""" Empty class represents `.end` Statements """

...
... # Empty


@datatype
Expand Down Expand Up @@ -283,58 +334,6 @@ class DialectChange:
dialect: str


# Union of "flat" statements lacking (substantial) hierarchy
FlatStatement = Union[
Instance,
Primitive,
ParamDecls,
ModelDef,
ModelVariant,
ModelFamily,
DialectChange,
"FunctionDef",
Unknown,
Options,
Include,
AhdlInclude,
UseLib,
StatisticsBlock,
]

# Statements which indicate the beginning and end of hierarchical elements,
# and ultimately disappear into the structured AST
DelimStatement = Union[
StartLib, EndLib, StartLibSection, EndLibSection, End,
]

# Statements
# The union of types which can appear in first-pass parsing netlist
Statement = Union[FlatStatement, DelimStatement]

# Entries - the union of types which serve as "high-level" AST nodes,
# i.e. those which can be the direct children of a `SourceFile`.
Entry = Union[FlatStatement, SubcktDef, Library, LibSection, End]


@datatype
class SourceFile:
path: Path # Source File Path
contents: List[Entry] # Statements and their associated SourceInfo


@datatype
class Program:
"""
# Multi-File "Netlist Program"
The name of this type is a bit misleading, but borrowed from more typical compiler-parsers.
Spice-culture generally lacks a term for "the totality of a simulator invocation input",
or even "a pile of source-files to be used together".
So, `Program` it is.
"""

files: List[SourceFile] # List of Source-File Contents


@datatype
class Int:
""" Integer Number """
Expand Down Expand Up @@ -392,6 +391,7 @@ class Return:
# Types which can be used inside a function definition.
# Will of course grow, in time.
FuncStatement = Union[Return]
unions.append(FuncStatement)


@datatype
Expand All @@ -408,6 +408,7 @@ class FunctionDef:
# Everything which can be used as a mathematical expression,
# and ultimately resolves to a scalar value at runtime.
Expr = Union["UnaryOp", "BinaryOp", "TernOp", Int, Float, MetricNum, Ident, Call]
unions.append(Expr)


class UnaryOperator(Enum):
Expand All @@ -426,7 +427,6 @@ class UnaryOp:
targ: Expr # Target Expression



@datatype
class BinaryOp:
""" Binary Operation """
Expand All @@ -445,6 +445,68 @@ class TernOp:
if_false: Expr # Value if `cond` is False


# Union of "flat" statements lacking (substantial) hierarchy
FlatStatement = Union[
Instance,
Primitive,
ParamDecls,
ModelDef,
ModelVariant,
ModelFamily,
DialectChange,
FunctionDef,
Unknown,
Options,
Include,
AhdlInclude,
UseLib,
StatisticsBlock,
]
unions.append(FlatStatement)

# Statements which indicate the beginning and end of hierarchical elements,
# and ultimately disappear into the structured AST
DelimStatement = Union[
StartLib,
EndLib,
StartLibSection,
EndLibSection,
StartProtectedSection,
EndProtectedSection,
End,
]
unions.append(DelimStatement)

# Statements
# The union of types which can appear in first-pass parsing netlist
Statement = Union[FlatStatement, DelimStatement]
unions.append(Statement)

# Entries - the union of types which serve as "high-level" AST nodes,
# i.e. those which can be the direct children of a `SourceFile`.
Entry = Union[FlatStatement, SubcktDef, Library, LibSection, ProtectedSection, End]
unions.append(Entry)


@datatype
class SourceFile:
path: Path # Source File Path
contents: List[Entry] # Statements and their associated SourceInfo


@datatype
class Program:
"""
# Multi-File "Netlist Program"
The name of this type is a bit misleading, but borrowed from more typical compiler-parsers.
Spice-culture generally lacks a term for "the totality of a simulator invocation input",
or even "a pile of source-files to be used together".
So, `Program` it is.
"""

files: List[SourceFile] # List of Source-File Contents


# Update all the forward type-references
for tp in datatypes:
tp.__pydantic_model__.update_forward_refs()
Expand Down
14 changes: 3 additions & 11 deletions netlist/data/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@
"""

# Std-Lib Imports
from enum import Enum
from typing import Union
from enum import Enum, auto
from pathlib import Path
from typing import Optional, Union, Dict, Generic, TypeVar, Sequence, Tuple, Any, List

# PyPi Imports
from pydantic.dataclasses import dataclass


class NetlistParseError(Exception):
""" Netlist Parse Error """

@staticmethod
def throw(*args, **kwargs):
""" Exception-raising debug wrapper. Breakpoint to catch `NetlistParseError`s. """
raise NetlistParseError(*args, **kwargs)


class NetlistDialects(Enum):
""" Enumerated, Supported Netlist Dialects """

Expand Down
2 changes: 1 addition & 1 deletion netlist/dialects/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .base import DialectParser
from .base import DialectParser, NetlistParseError
from .spice import SpiceDialectParser, NgSpiceDialectParser
from .spectre import SpectreDialectParser, SpectreSpiceDialectParser
Loading

0 comments on commit cbcc128

Please sign in to comment.