 # Delta: An Incremental Compiler Step 7: Unary Operators

## Description

Add support for the following unary operators:

• Unary plus operator (+): it produces the value of its operand. In other words, it does nothing.

• Unary minus operator (-): it produces the negative (opposite sign) value of its operand.

• Logical negation operator (!): it reverses the meaning of its operand. The result is true (1) if its operand is false (0); the result is false if the operand is truthy (any value not equal to 0).

These operators have higher precedence than the multiplicative operators and associate from right to left.

## Example

The Delta program:

+ - ! ! 2


should produce the following WAT code:

(module
(func
(export "_start")
(result i32)
i32.const 0
i32.const 2
i32.eqz
i32.eqz
i32.sub
)
)


The above WAT function’s return value should be:

-1

## Unit Tests

# File: tests/test_07_unary.py

from unittest import TestCase
from delta import Compiler, SyntaxMistake

class TestUnary(TestCase):

def setUp(self):
self.c = Compiler('program')

def test_syntax_mistake1(self):
with self.assertRaises(SyntaxMistake):
self.c.realize('5-')

def test_neg(self):
self.assertEqual(-42,
self.c.realize('-42'))

def test_pos(self):
self.assertEqual(42,
self.c.realize('+42'))

def test_not_false(self):
self.assertEqual(1,
self.c.realize('!false'))

def test_not_true(self):
self.assertEqual(0,
self.c.realize('!true'))

def test_not_0(self):
self.assertEqual(1,
self.c.realize('!0'))

def test_not_1(self):
self.assertEqual(0,
self.c.realize('!1'))

def test_not_42(self):
self.assertEqual(0,
self.c.realize('!42'))

def test_mix1(self):
self.assertEqual(1,
self.c.realize('!!--+5'))

def test_mix2(self):
self.assertEqual(0,
self.c.realize('!!!--+5'))

def test_mix3(self):
self.assertEqual(-1,
self.c.realize('+-!!3'))

def test_mix4(self):
self.assertEqual(1,
self.c.realize('!!-+3'))