slicc: improve support for prefix operations
This patch fixes the type handling when prefix operations are used. Previously prefix operators would assume a void return type, which made it impossible to combine prefix operations with other expressions. This patch allows SLICC programmers to use prefix operations more naturally.
This commit is contained in:
parent
ee0d414fa8
commit
93fff6f636
1 changed files with 15 additions and 1 deletions
|
@ -107,8 +107,22 @@ class PrefixOperatorExprAST(ExprAST):
|
||||||
opcode = self.slicc.codeFormatter()
|
opcode = self.slicc.codeFormatter()
|
||||||
optype = self.operand.generate(opcode)
|
optype = self.operand.generate(opcode)
|
||||||
|
|
||||||
|
# Figure out what the input and output types should be
|
||||||
|
opmap = {"!": "bool", "-": "int", "++": "Scalar"}
|
||||||
|
if self.op in opmap:
|
||||||
|
output = opmap[self.op]
|
||||||
|
type_in_symtab = self.symtab.find(opmap[self.op], Type)
|
||||||
|
if (optype != type_in_symtab):
|
||||||
|
self.error("Type mismatch: right operand of " +
|
||||||
|
"unary operator '%s' must be of type '%s'. ",
|
||||||
|
self.op, type_in_symtab)
|
||||||
|
else:
|
||||||
|
self.error("Invalid prefix operator '%s'",
|
||||||
|
self.op)
|
||||||
|
|
||||||
|
# All is well
|
||||||
fix = code.nofix()
|
fix = code.nofix()
|
||||||
code("(${{self.op}} $opcode)")
|
code("(${{self.op}} $opcode)")
|
||||||
code.fix(fix)
|
code.fix(fix)
|
||||||
|
|
||||||
return self.symtab.find("void", Type)
|
return self.symtab.find(output, Type)
|
||||||
|
|
Loading…
Reference in a new issue