-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add python3 backend. #1
Conversation
Looks good to me, are you planning to do more work on this? The way I was going to approach this was to start with JS and develop the grammar against the old tests and then work on the other languages. |
Makefile
Outdated
@@ -12,7 +12,7 @@ clean-java: | |||
rm -f java/* | |||
|
|||
python3: $(GRAMMAR) $(GRAMMAR_FILES) | |||
$(ANTLR) -Dlanguage=Python3 $< -o python3/ | |||
$(ANTLR) -Dlanguage=Python3 $< -o python3/antlr | |||
|
|||
clean-python3: | |||
rm -f python3/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably want to change the clean directory too.
python3/electro_grammar.py
Outdated
def enterCapacitance(self, ctx): | ||
cprefix_lookup = {'u': 10e-6, 'n': 10e-9, 'p': 10e-12} | ||
number = float(str(ctx.NUMBER())) | ||
cprefix = str(ctx.CPREFIX()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you forgot to use the cprefix_lookup
. I kind of want to make the conversion and normalisation part of the grammar but that may be awkward or impossible.
Also, maybe we should be using decimal
for any values as I observed floating point errors in my previous python port.
js/lib/index.js
Outdated
var parser = new ElectroGrammarParser(tokens); | ||
parser.buildParseTrees = true; | ||
|
||
try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completely useless, haven't figured out how to detect errors yet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we need to create an ErrorListener
. Do you have a copy of the Antlr4 book?
Current goal is feature parity to previous electro grammar version in js and python backends. I'm thinking about adding a yosys-json frontend where the 'electro-grammar' attribute is parsed and a 'cpl' attribute is added. This would make a python version unnecessary for pycircuit. Do you think it's in the scope of electro-grammar or belongs into pycircuit? |
.gitignore
Outdated
coverage/ | ||
.nyc_output/ | ||
node_modules/ | ||
test-lib/ | ||
|
||
**/ElectroGrammar* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this ignore ElectorGrammar.g4?
.gitignore
Outdated
@@ -1,4 +1,7 @@ | |||
package-lock.json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should commit the lock.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aren't locks just for applications? this is a library, so the versions should match what the end user uses.
python3/.gitignore
Outdated
MANIFEST | ||
.cache/ | ||
*.pyc | ||
Pipfile.lock |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, we can commit the locks.
js/test/test_parse.js
Outdated
}) | ||
assert(parse('0603').component.type === 'unknown'); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are having issues with the formatting then we could just apply prettier to all the JS code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The index.js file uses 2 spaces indentation and has semicolons at the end. Was updating it to match. What's your preference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the prettier code formatter, you can apply a uniform style to everything in js/* by doing npm r fmt
.
package.json
Outdated
"transform-object-assign" | ||
] | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have a package.json for the js bit. I guess you want one for the CPL script, it doesn't need all these deps then though.
prepare_cpl.js
Outdated
@@ -1,6 +1,6 @@ | |||
const yaml = require('js-yaml') | |||
const fs = require('fs') | |||
const parse = require('./src/parse') | |||
const parse = require('./js/lib/index.js').parse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do
const {parse} = require('./js/lib')
src/ElectroGrammar.g4
Outdated
|
||
capacitance : NUMBER CPREFIX FARAD?; | ||
electro_grammar : capacitor; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine for now but ultimately I want the parser to work on individual specs like 50V
and 10%
without outputting a complete component.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear: it's fine to just port the existing functionality for now. Just something to keep in mind for the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that makes sense
Could you explain this a bit more? The frontend would transform a Yosys JSON netlist? |
A cell in a yosys netlist has an attributes key with arbitrary key value pairs. I think that we can write a little script that looks for the attribute called 'electro-grammar' in each cell and extracts whatever information it can find and adds it as new attributes. |
Ah, neat, didn't know that. You'de probably still want the Python port to report errors to the user though? I think it should probably be a separate package but it can live in this repo. Feel free to add it in |
Please ping me when you plan to take a larger break in working on this, I'll merge then. Please also make sure to push/pull-request any ongoing work so we have less risk of duplicating effort (as I will likely resume work on this too). |
We can also use it to pass stuff to netlistsvg. netlistsvg-component or something and use the cell name as a fallback if not specified. There is also support for models in the netlist format:
not sure yet if we can find a format that is simple and general enough that we can standardize on for analog circuit models. Maybe something touchstone inspired would work [0] which is a list of n-port matrices linearized at multiple operating points. Have you made any progress on your hdl? |
Sounds good, yeah. Be sure to open up another issue/PR for it. My netlist HDL (called replicad) is what I got distracted by after I did the Antlr stubs for this. |
e48849e
to
9262da0
Compare
src/Units.g4
Outdated
|
||
temperature: NUMBER tunit tolerance?; | ||
tunit: KELVIN | 'u{00b0}'? CELSIUS; | ||
KELVIN: K | K E L V I N; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering if there is any issues with having two lexer rules that look for k
.
Grammar doesn't work yet, but I think a human gets what it's supposed to do even if antlr doesn't. Will have to wait till tomorrow :/ |
I am going to hack on this branch a bit tonight. Please make sure you have pushed everything so we don't get repeated work or conflicts. |
I merged everything except |
Actually, scratch that, I have merged up to and including 3a6b1d3 because after that you seem to have switched methodology, stopped testing and started experimenting, leaving behind the 2 previously passing tests. It's fine to experiment of course but I don't want to merge in anything without tests at this stage. I think it's super easy to introduce regressions when expanding the grammar. If you want to have the work on transistors etc. merged I would suggest you wait till after the initial port and then include test cases in those PRs. |
Yep it's not finished, started adding tests in last commit |
Hmm, I can't really figure out how to continue work off of 3a6b1d3 without causing conflicts with all the rest of the work you did. Without tests I don't know if it's any good either though :). Also, you seem to be steering it steadily towards the pycircuit use-case (which makes sense, but we need to figure out how to resolve these two use-case directions we are seeing). Do you mind just letting me do the initial Antlr port and then you can expand on top of that? |
Ok |
Yeah that is really a problem. I looked at the js testcases and realized that it seems like quite a lot of work that doesn't help pycircuit... |
Well, I don't mind doing the work because my initial use-case was search. I think there is still a massive overlap between the two uses, we are just highlighting the differences at the moment. |
self.obj['dtype'] = 'led' | ||
|
||
def exitSchottky(self, ctx): | ||
self.obj['dtype'] = 'schottky' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use more verbose names? diode_type
instead of dtype
. transistor_type
instead of ttype
.
d = parse('sk BAT54S') | ||
assert d['type'] == 'diode' | ||
assert d['dtype'] == 'schottky' | ||
assert d['code'] == 'BAT54S' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the code
? Is it the manufacturer part number?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, we can change it to mpn. the difference is only that for semiconductors the mpn has a structure, for arbitrary ic's it does not.
Nice work! Does this have feature parity with the initial version now, in terms of recognizing capacitors, resistors and LEDs? |
Have you had any ambiguity warnings yet? We should check if the DiagnosticErrorListener is working if not. |
src/ElectroGrammar.g4
Outdated
|
||
WHITESPACE: [\p{White_Space}] -> skip; | ||
|
||
THROUGHHOLE: T H R O U G H '-'? H O L E -> skip; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like you want a more lax parser ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah just started hacking on the lax parser, commit is already dropped ;)
I got some ambiguity warnings, but passing tests definitely swallow them, which isn't nice... Once the lax parser starts ignoring unknown input we should be close to feature parity with the initial version. |
The current issue I'm trying to resolve is that it only recongizes a unit if it is followed by whitespace, so that Resistor is 'unlexable' instead of R 'es' i 'sto' r. |
I am going to merge up to and including 6de3e3c though there are still some minor issues (commented out code, console.logs and the output data schema). We can resolve those later. Can you open new, separate pull requests for further fixes and features please? It will make it easier to keep up and review the changes. |
Hmmm, it might require a different approach. Maybe a sort of pre-lexer that splits things into words and then feeds them one at a time. |
@dvc94ch, are you sure you want to look at the lax parser right now? That's what I was going to work on... |
I created a new PR with my changes up to now. I'll start on something else... |
No description provided.