Compiler Design

Delta: An Incremental Compiler
Step 5: Comments

Description

Add support for two kinds of comments:

Example

The Delta program:

// A line comment.

0

/* 
   A block
   comment.
*/

should produce the following WAT code:

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

The above WAT function’s return value should be:

0

Unit Tests

# File: tests/test_05_comments.py

from unittest import TestCase
from delta import Compiler, SyntaxMistake


class TestComment(TestCase):

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

    def test_syntax_mistake(self):
        with self.assertRaises(SyntaxMistake):
            self.c.realize('/* this is comment')

    def test_line_comment(self):
        self.assertEqual(0,
                         self.c.realize('// this is a comment\n0'))

    def test_block_comment1(self):
        self.assertEqual(0,
                         self.c.realize('/* this is a comment */\n0'))

    def test_block_comment2(self):
        self.assertEqual(0,
                         self.c.realize(
                             '/* this\nis\na\ncomment */\n0'))

    def test_mix(self):
        self.assertEqual(2,
                         self.c.realize(
                             '''
                             /* first comment */
                             1
                             // second comment
                             +
                             // third
                             1
                             /*
                                fourth
                                comment
                             */
                             '''))