Skip to content

Commit

Permalink
Add sessions to parser grammar
Browse files Browse the repository at this point in the history
Ref. #291
  • Loading branch information
treiher committed Aug 25, 2020
1 parent 7d385ff commit 3d72920
Show file tree
Hide file tree
Showing 8 changed files with 1,465 additions and 232 deletions.
48 changes: 37 additions & 11 deletions rflx/declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
from typing import TYPE_CHECKING, Mapping, Sequence

from rflx.common import generic_repr
from rflx.error import Location
from rflx.identifier import ID, StrID

if TYPE_CHECKING:
from rflx.expression import Expr


class Declaration(ABC):
def __init__(self, identifier: StrID) -> None:
def __init__(self, identifier: StrID, location: Location = None):
self.__identifier = ID(identifier)
self.location = location
self.__refcount = 0

def __eq__(self, other: object) -> bool:
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
return {k: v for k, v in self.__dict__.items() if k != "location"} == {
k: v for k, v in other.__dict__.items() if k != "location"
}
return NotImplemented

def __repr__(self) -> str:
Expand All @@ -38,13 +42,20 @@ def validate(self, declarations: Mapping[ID, "Declaration"]) -> None:


class VariableDeclaration(Declaration):
def __init__(self, identifier: StrID, type_name: StrID = None, expression: "Expr" = None):
super().__init__(identifier)
def __init__(
self,
identifier: StrID,
type_name: StrID = None,
expression: "Expr" = None,
location: Location = None,
):
super().__init__(identifier, location)
self.__type_name = ID(type_name) if type_name else None
self.__expression = expression

def validate(self, declarations: Mapping[ID, "Declaration"]) -> None:
pass
if self.__expression:
self.__expression.validate(declarations)


class PrivateDeclaration(Declaration):
Expand All @@ -71,8 +82,14 @@ def validate(self, declarations: Mapping[ID, "Declaration"]) -> None:


class SubprogramDeclaration(Declaration):
def __init__(self, identifier: StrID, arguments: Sequence[Argument], return_type: StrID):
super().__init__(identifier)
def __init__(
self,
identifier: StrID,
arguments: Sequence[Argument],
return_type: StrID,
location: Location = None,
):
super().__init__(identifier, location)
self.__arguments = arguments
self.__return_type = ID(return_type)

Expand All @@ -82,8 +99,10 @@ def validate(self, declarations: Mapping[ID, "Declaration"]) -> None:


class RenamingDeclaration(Declaration):
def __init__(self, identifier: StrID, type_name: StrID, expression: "Expr"):
super().__init__(identifier)
def __init__(
self, identifier: StrID, type_name: StrID, expression: "Expr", location: Location = None
):
super().__init__(identifier, location)
self.__type_name = ID(type_name)
self.__expression = expression

Expand All @@ -92,8 +111,15 @@ def validate(self, declarations: Mapping[ID, "Declaration"]) -> None:


class ChannelDeclaration(Declaration):
def __init__(self, identifier: StrID, readable: bool = False, writable: bool = False):
super().__init__(identifier)
def __init__(
self,
identifier: StrID,
readable: bool = False,
writable: bool = False,
location: Location = None,
):
assert readable or writable
super().__init__(identifier, location)
self.__readable = readable
self.__writable = writable

Expand Down
2 changes: 1 addition & 1 deletion rflx/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ def variables(self) -> List["Variable"]:
return [self]

def validate(self, declarations: Mapping[ID, Declaration]) -> None:
builtin_types = map(ID, ["Boolean"])
builtin_types = map(ID, ["Boolean", "True", "False"])
if self.identifier in builtin_types:
return
if self.identifier not in declarations:
Expand Down
4 changes: 3 additions & 1 deletion rflx/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
class Base(ABC):
def __eq__(self, other: object) -> bool:
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
return {k: v for k, v in self.__dict__.items() if k != "location"} == {
k: v for k, v in other.__dict__.items() if k != "location"
}
return NotImplemented

def __repr__(self) -> str:
Expand Down
10 changes: 9 additions & 1 deletion rflx/parser/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rflx.expression import TRUE, UNDEFINED, Expr
from rflx.identifier import ID, StrID
from rflx.model import Type
from rflx.session import Session


class SyntaxTree:
Expand Down Expand Up @@ -53,10 +54,17 @@ def __init__(


class PackageSpec(SyntaxTree):
def __init__(self, identifier: StrID, types: List[Type], end_identifier: StrID = None) -> None:
def __init__(
self,
identifier: StrID,
types: List[Type],
sessions: List[Session],
end_identifier: StrID = None,
) -> None:
self.identifier = ID(identifier)
self.end_identifier = ID(end_identifier) if end_identifier else self.identifier
self.types = types
self.sessions = sessions


class ContextSpec(SyntaxTree):
Expand Down
Loading

0 comments on commit 3d72920

Please sign in to comment.