Skip to content

Commit

Permalink
b1scad add support for $fn identifier and negative numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
bat52 committed Dec 26, 2024
1 parent b1a0fde commit d53e178
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/b1scad/scad/model14.scad
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sphere($fn = 16, r = 10);
7 changes: 4 additions & 3 deletions src/b1scad/scad2ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class OpenSCADLexer(Lexer):
TRANSLATE, ROTATE, SCALE, UNION, DIFFERENCE, HULL,
LBRACE, RBRACE, LPAREN, RPAREN, LSQUARE, RSQUARE, COMMA, SEMICOLON,
NUMBER,
IDENTIFIER, EQU,
IDENTIFIER, SFN, EQU,
)
ignore = ' \t\n'

Expand All @@ -31,8 +31,9 @@ class OpenSCADLexer(Lexer):
SCALE = r'scale'
HULL = r'hull'

IDENTIFIER = r'[\$a-zA-Z_][a-zA-Z0-9_]*'
NUMBER = r'\d+(\.\d+)?'
IDENTIFIER = r'[a-zA-Z_][a-zA-Z0-9_]*'
SFN = r'\$fn'
NUMBER = r'[+-]?\d+(\.\d+)?'

LBRACE = r'\{'
RBRACE = r'\}'
Expand Down
11 changes: 9 additions & 2 deletions src/b1scad/scad2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,20 @@ def shape(self, p):
# probably still needs fixing
return f"self.api.cylinder_z({p.args})"

@_('SFN EQU NUMBER')
def args(self, p):
return ""

@_('IDENTIFIER EQU NUMBER')
def args(self, p):
id = p.IDENTIFIER.replace('$','_')
return f"{id} = {str(p.NUMBER)}"
return f"{p.IDENTIFIER} = {str(p.NUMBER)}"

@_('args COMMA args')
def args(self, p):
if p.args0 == "":
return f"{p.args1}"
if p.args1 == "":
return f"{p.args0}"
return f"{p.args0}, {p.args1}"

@_('NUMBER')
Expand Down
2 changes: 1 addition & 1 deletion src/b1scad/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class B1scadTestMethods(unittest.TestCase):
"""Pylele Test Class"""
def test_all_scad(self):
scaddir = os.path.join(os.path.abspath(os.path.dirname(__file__)),"scad")
for idx in range(14):
for idx in range(15):
fname = f"model{idx:02}"
scadfname = f"{fname}.scad"
fullscadfile = os.path.join(scaddir,scadfname)
Expand Down

0 comments on commit d53e178

Please sign in to comment.