Compiladores

Delta: An Incremental Compiler
Step 6: Parenthesis

Description

Add support for parenthesis: ( and ). When parentheses are encountered the entire subexpression between the parentheses should be evaluated immediately when the term is required.

Example

The Delta program:

(1 + 2) * 3

should produce the following WAT code:

(module
  (func
    (export "_start")
    (result i32)
    i32.const 1
    i32.const 2
    i32.add
    i32.const 3
    i32.mul
  )
)

The above WAT function’s return value should be:

9

Unit Tests

# File: tests/test_06_parenthesis.py

from unittest import TestCase
from delta import Compiler, SyntaxMistake


class TestParenthesis(TestCase):

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

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

    def test_syntax_mistake2(self):
        with self.assertRaises(SyntaxMistake):
            self.c.realize('(((((((42))))))))')

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

    def test_parenthesis2(self):
        self.assertEqual(9,
                         self.c.realize('(1+2)*3'))

    def test_parenthesis3(self):
        self.assertEqual(12,
                         self.c.realize('(1+2)*(10-6)'))

    def test_parenthesis4(self):
        self.assertEqual(61,
                         self.c.realize(
                            '((((1) + (2) + (3)) * (10)) + (1))'))

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