-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from dataclasses import dataclass as _dataclass | ||
|
||
|
||
def _language_node(cls): | ||
return _dataclass(eq=True, frozen=True)(cls) | ||
|
||
|
||
@_language_node | ||
class SignalDef: | ||
name: str | ||
direction: str | ||
width: int | ||
|
||
|
||
@_language_node | ||
class Port: | ||
signal_defs: list[SignalDef] | ||
|
||
|
||
@_language_node | ||
class Connection: | ||
_to: str | ||
_from: str | ||
|
||
|
||
@_language_node | ||
class Connections: | ||
children: list[Connection] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from lark import Transformer | ||
|
||
from elasticai.creator.hdl.vhdl.language import Connection, Connections, Port, SignalDef | ||
|
||
from .standalone_parser import Lark_StandAlone | ||
|
||
|
||
class TreeToVHDL(Transformer): | ||
def connection(self, s): | ||
return Connection(_to=s[0].value, _from=s[1].value) | ||
|
||
def connections(self, s): | ||
return Connections(s) | ||
|
||
def port(self, s): | ||
return Port(s[0]) | ||
|
||
def vhdl(self, s): | ||
return s[0] | ||
|
||
def signal_defs(self, s): | ||
return s | ||
|
||
def signal_type(self, s): | ||
v = s[0].data | ||
if v.value == "std_logic": | ||
return 0 | ||
else: | ||
return int(s[1].value) - 1 | ||
|
||
def signal_def(self, s): | ||
name, direction, width = s | ||
name = name.value | ||
direction = direction.data | ||
return SignalDef(name=name, direction=direction, width=width) | ||
|
||
|
||
parser = Lark_StandAlone(transformer=TreeToVHDL()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
vhdl : port | connections | ||
connections: connection+ | ||
port : "port" "(" signal_defs ")" ";" | ||
signal_defs : signal_def | signal_def+ ";" signal_def | ||
signal_def : ID ":" direction signal_type | ||
direction: "in" -> in | ||
| "out" -> out | ||
signal_type: std_logic | ||
| std_logic_vector "(" NUMBER "-" "1" "downto" "0" ")" | ||
std_logic_vector: "std_logic_vector" | ||
std_logic: "std_logic" | ||
connection: ID "<" "=" ID ";" | ||
|
||
ID: /[a-zA-Z_][a-zA-Z0-9_]*/ | ||
NUMBER: /[0-9]+/ | ||
COMMENT: /--.*$/ | ||
WHITESPACE: /\s+/ | ||
|
||
%ignore WHITESPACE | ||
%ignore COMMENT |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from elasticai.creator.hdl.vhdl.language import Connection, Connections, Port, SignalDef | ||
from elasticai.creator.hdl.vhdl.parser.parser import parser | ||
|
||
|
||
def test_parsing_connections(): | ||
expected = Connections( | ||
[Connection(_from="y", _to="x"), Connection(_from="b", _to="a")] | ||
) | ||
code = "x <= y; a <= b;" | ||
actual = parser.parse(code) | ||
assert actual == expected | ||
|
||
|
||
def test_parsing_port(): | ||
v = parser.parse( | ||
"port ( my_signal : in std_logic; my_other : out std_logic_vector(5 - 1 downto" | ||
" 0));" | ||
) | ||
|
||
assert v == Port( | ||
[ | ||
SignalDef(n, d, w) | ||
for n, d, w in (("my_signal", "in", 0), ("my_other", "out", 4)) | ||
] | ||
) |