diff options
| author | Leway Colin | 2019-06-21 06:27:23 +0800 |
|---|---|---|
| committer | mergify[bot] | 2019-06-20 22:27:23 +0000 |
| commit | 20208967b3a39c0ef3c1af40b03dc8d5ee923f45 (patch) | |
| tree | d278ff42832f565a2daec9144b4bc14488406f6d /src | |
| parent | fb4133cd76600cc8707e9a7b2f639cf120bd825c (diff) | |
Use pattern match instead of hardcode position (#1102)
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/scala/firrtl/Visitor.scala | 97 |
1 files changed, 51 insertions, 46 deletions
diff --git a/src/main/scala/firrtl/Visitor.scala b/src/main/scala/firrtl/Visitor.scala index 9914de70..5dda1df3 100644 --- a/src/main/scala/firrtl/Visitor.scala +++ b/src/main/scala/firrtl/Visitor.scala @@ -291,54 +291,59 @@ class Visitor(infoMode: InfoMode) extends FIRRTLBaseVisitor[FirrtlNode] { private def visitExp[FirrtlNode](ctx: ExpContext): Expression = { val ctx_exp = ctx.exp.asScala - if (ctx.getChildCount == 1) - Reference(ctx.getText, UnknownType) - else - ctx.getChild(0).getText match { - case "UInt" => - if (ctx.getChildCount > 4) { - val width = IntWidth(string2BigInt(ctx.intLit(0).getText)) - val value = string2BigInt(ctx.intLit(1).getText) - UIntLiteral(value, width) - } else { - val value = string2BigInt(ctx.intLit(0).getText) - UIntLiteral(value) - } - case "SInt" => - if (ctx.getChildCount > 4) { - val width = string2BigInt(ctx.intLit(0).getText) - val value = string2BigInt(ctx.intLit(1).getText) - SIntLiteral(value, IntWidth(width)) - } else { - val str = ctx.intLit(0).getText - val value = string2BigInt(str) - SIntLiteral(value) - } - case "validif(" => ValidIf(visitExp(ctx_exp(0)), visitExp(ctx_exp(1)), UnknownType) - case "mux(" => Mux(visitExp(ctx_exp(0)), visitExp(ctx_exp(1)), visitExp(ctx_exp(2)), UnknownType) - case _ => - ctx.getChild(1).getText match { - case "." => - val expr1 = visitExp(ctx_exp(0)) - // TODO Workaround for #470 - if (ctx.fieldId == null) { - ctx.DoubleLit.getText.split('.') match { - case Array(a, b) if legalId(a) && legalId(b) => - val inner = new SubField(expr1, a, UnknownType) - new SubField(inner, b, UnknownType) - case Array() => throw new ParserException(s"Illegal Expression at ${ctx.getText}") - } - } else { - new SubField(expr1, ctx.fieldId.getText, UnknownType) + ctx.getChild(0) match { + case _: IdContext => Reference(ctx.getText, UnknownType) + case _: ExpContext => + ctx.getChild(1).getText match { + case "." => + val expr1 = visitExp(ctx_exp(0)) + // TODO Workaround for #470 + if (ctx.fieldId == null) { + ctx.DoubleLit.getText.split('.') match { + case Array(a, b) if legalId(a) && legalId(b) => + val inner = new SubField(expr1, a, UnknownType) + new SubField(inner, b, UnknownType) + case Array() => throw new ParserException(s"Illegal Expression at ${ctx.getText}") } - case "[" => if (ctx.exp(1) == null) + } else { + new SubField(expr1, ctx.fieldId.getText, UnknownType) + } + case "[" => + if (ctx.exp(1) == null) new SubIndex(visitExp(ctx_exp(0)), string2Int(ctx.intLit(0).getText), UnknownType) - else new SubAccess(visitExp(ctx_exp(0)), visitExp(ctx_exp(1)), UnknownType) - // Assume primop - case _ => DoPrim(visitPrimop(ctx.primop), ctx_exp.map(visitExp), - ctx.intLit.asScala.map(x => string2BigInt(x.getText)), UnknownType) - } - } + else + new SubAccess(visitExp(ctx_exp(0)), visitExp(ctx_exp(1)), UnknownType) + } + case _: PrimopContext => + DoPrim(visitPrimop(ctx.primop), + ctx_exp.map(visitExp), + ctx.intLit.asScala.map(x => string2BigInt(x.getText)), + UnknownType) + case _ => + ctx.getChild(0).getText match { + case "UInt" => + if (ctx.getChildCount > 4) { + val width = IntWidth(string2BigInt(ctx.intLit(0).getText)) + val value = string2BigInt(ctx.intLit(1).getText) + UIntLiteral(value, width) + } else { + val value = string2BigInt(ctx.intLit(0).getText) + UIntLiteral(value) + } + case "SInt" => + if (ctx.getChildCount > 4) { + val width = string2BigInt(ctx.intLit(0).getText) + val value = string2BigInt(ctx.intLit(1).getText) + SIntLiteral(value, IntWidth(width)) + } else { + val str = ctx.intLit(0).getText + val value = string2BigInt(str) + SIntLiteral(value) + } + case "validif(" => ValidIf(visitExp(ctx_exp(0)), visitExp(ctx_exp(1)), UnknownType) + case "mux(" => Mux(visitExp(ctx_exp(0)), visitExp(ctx_exp(1)), visitExp(ctx_exp(2)), UnknownType) + } + } } // stripSuffix("(") is included because in ANTLR concrete syntax we have to include open parentheses, |
