Compiler Design

Delta: An Incremental Compiler
Step 4: Additive Operators

Description

Add support for integer additive binary operators: addition (+) and subtraction (-). These two operators have the same precedence and they associate from left to right.

Example

The Delta program:

12 + 34 - 56

should produce the following WAT code:

(module
  (func
    (export "_start")
    (result i32)
    i32.const 12
    i32.const 34
    i32.add
    i32.const 56
    i32.sub
  )
)

The above WAT function’s return value should be:

-10

Unit Tests

# File: tests/test_04_additive.py

from unittest import TestCase
from delta import Compiler, SyntaxMistake


class TestAdditive(TestCase):

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

    def test_syntax_mistake(self):
        with self.assertRaises(SyntaxMistake):
            self.c.realize('1 +')

    def test_add1(self):
        self.assertEqual(3,
                         self.c.realize('1 + 2'))

    def test_add2(self):
        self.assertEqual(13,
                         self.c.realize('1 + 2 + 10'))

    def test_add3(self):
        self.assertEqual(55,
                         self.c.realize('1+2+3+4+5+6+7+8+9+10'))

    def test_sub1(self):
        self.assertEqual(3,
                         self.c.realize('10-7'))

    def test_sub2(self):
        self.assertEqual(-3,
                         self.c.realize('7-10'))

    def test_sub3(self):
        self.assertEqual(-13,
                         self.c.realize('1-2-3-4-5'))

    def test_sub4(self):
        self.assertEqual(-130,
                         self.c.realize('10-20-30-40-50'))

    def test_mix(self):
        self.assertEqual(50,
                         self.c.realize('10 + 20 - 30 + 40 - 50 + 60'))