Expression Parser
Expression Parser.
ExprParser
Bases: Object
ExprParser.
Attributes:
| Name | Type | Description |
|---|---|---|
namespace |
Namespace
|
Symbol namespace |
__call__
Parse Expression.
This is an alias to parse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr |
Parseable
|
Expression |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
only |
Limit expression to these final element type. |
|
types |
Limit expression type to to these types. |
parse_note
Parse Expression or Note.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr |
Parseable | Note
|
Expression |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
only |
Limit expression to these final element type. |
|
types |
Limit expression type to to these types. |
parse
Parse Expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr |
Parseable
|
Expression |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
only |
Limit expression to these final element type. |
|
types |
Limit expression type to to these types. |
Expression Parser Examples
Basics:
>>> import ucdp as u
>>> p = u.ExprParser()
>>> p.parse(10)
ConstExpr(IntegerType(default=10))
>>> p.parse('3h3')
ConstExpr(UintType(3, default=3))
>>> p.parse('3h3') * p.const(2)
Op(ConstExpr(UintType(3, default=3)), '*', ConstExpr(IntegerType(default=2)))
>>> p.parse((10, '10'))
ConcatExpr((ConstExpr(IntegerType(default=10)), ConstExpr(IntegerType(default=10))))
>>> p = u.ExprParser(namespace=u.Idents([
... u.Signal(u.UintType(16, default=15), 'uint_s'),
... u.Signal(u.SintType(16, default=-15), 'sint_s'),
... ]))
>>> expr = p.parse('uint_s[2]')
>>> expr
SliceOp(Signal(UintType(16, default=15), 'uint_s'), Slice('2'))
>>> expr = p.parse('uint_s * sint_s[2:1]')
>>> expr
Op(Signal(UintType(16, ...), 'uint_s'), '*', SliceOp(Signal(SintType(16, ...), 'sint_s'), Slice('2:1')))
>>> int(expr)
0
A more complex:
>>> namespace = u.Idents([
... u.Signal(u.UintType(2), 'a_s'),
... u.Signal(u.UintType(4), 'b_s'),
... u.Signal(u.SintType(8), 'c_s'),
... u.Signal(u.SintType(16), 'd_s'),
... ])
>>> p = u.ExprParser(namespace=namespace)
>>> expr = p.parse("ternary(b_s == const('4h3'), a_s, c_s)")
>>> expr
TernaryExpr(BoolOp(Signal(UintType(4), 'b_s'), '==', ..., Signal(SintType(8), 'c_s'))
Syntax Errors:
>>> parse("sig_s[2") # doctest: +SKIP
Traceback (most recent call last):
...
u.exceptions.InvalidExpr: 'sig_s[2': '[' was never closed (<string>, line 1)
const
Parse Constant.
Parser Example
Basics:
>>> import ucdp as u
>>> p = u.ExprParser()
>>> p.const('10')
ConstExpr(IntegerType(default=10))
>>> p.const(10)
ConstExpr(IntegerType(default=10))
>>> p.const("10'd20")
ConstExpr(UintType(10, default=20))
>>> p.const(u.ConstExpr(u.UintType(10, default=20)))
ConstExpr(UintType(10, default=20))
>>> p.const("4'h4")
ConstExpr(UintType(4, default=4))
>>> p.const("4'sh4")
ConstExpr(SintType(4, default=4))
>>> p.const("4'shC")
ConstExpr(SintType(4, default=-4))
concat
Parse ConcatExpr.
Concat Parser Example
Basics:
>>> import ucdp as u
>>> p = u.ExprParser()
>>> p.concat((10, "20"))
ConcatExpr((ConstExpr(IntegerType(default=10)), ConstExpr(IntegerType(default=20))))
>>> bool(p.concat((10, "20")))
True
ternary
TernaryExpr Statement.
Ternary Parser Example
Basics:
>>> import ucdp as u
>>> cond = u.Signal(u.UintType(2), 'if_s') == u.ConstExpr(u.UintType(2, default=1))
>>> one = u.Signal(u.UintType(16, default=10), 'one_s')
>>> other = u.Signal(u.UintType(16, default=20), 'other_s')
>>> p = u.ExprParser()
>>> expr = p.ternary(cond, one, other)
>>> expr
TernaryExpr(BoolOp(Signal(UintType(2), 'if_s'), '==', ..., Signal(UintType(16, default=20), 'other_s'))
>>> int(expr)
20
>>> expr.type_
UintType(16, default=10)
log2
Ceiling Logarithm to base of 2.
Log2 Parser Example
Basics:
>>> import ucdp as u
>>> p = u.ExprParser()
>>> log = p.log2("8'h8")
>>> log
Log2Expr(ConstExpr(UintType(8, default=8)))
>>> int(log)
3
>>> p.parse("log2('8h8')")
Log2Expr(ConstExpr(UintType(8, default=8)))
minimum
Lower value of one and other.
Minimum Parser Example
Basics:
>>> import ucdp as u
>>> p = u.ExprParser()
>>> val = p.minimum("8'h8", "8'h3")
>>> val
MinimumExpr((ConstExpr(UintType(8, default=8)), ConstExpr(UintType(8, default=3))))
>>> int(val)
3
>>> p.parse("minimum('8h8', '8h3')")
MinimumExpr((ConstExpr(UintType(8, default=8)), ConstExpr(UintType(8, default=3))))
maximum
Higher value of one and other.
Maximum Parser Example
Basics:
>>> import ucdp as u
>>> p = u.ExprParser()
>>> val = p.maximum("8'h8", "8'h3")
>>> val
MaximumExpr((ConstExpr(UintType(8, default=8)), ConstExpr(UintType(8, default=3))))
>>> int(val)
8
>>> p.parse("maximum('8h8', '8h3')")
MaximumExpr((ConstExpr(UintType(8, default=8)), ConstExpr(UintType(8, default=3))))