2025-10-08

vak: (Знайка)
Помните книжку Этюды для программистов? Там в предпоследней главе было крышесносное задание: построить компилятор для паскалеподобного языка.
Easy Does It
                   or...
A COMPILER FOR AN
ALGEBRAIC LANGUAGE

A compiler is always a large program. To write one from scratch, even in a pedagogical environment, is a major undertaking. Although Easy is designed to reduce the pain while providing as much enlightenment as possible, this still is the hardest problem in the book. Do not tackle it unless you (and some helpful friends) have plenty of time and energy.

THE EASY LANGUAGE

Easy is a general-purpose, procedural, algebraic programming language. Its roots lie in ALGOL, ALGOL 68, and PASCAL. Like them, it is designed to be compiled, loaded, and executed on a reasonably conventional computer (the EC-1 described in Chapter 25 is a good example). The syntax is described by a context-free grammar suitable for parsing by LR(1) techniques. The semantics are similar to the languages described above, and we will let an informal description suffice, trusting to the reader’s skill to fill any gaps. In the text below, logically connected portions of the grammar are described with the associated semantics.
Так вот, один гениальный человек ([personal profile] begoon) такой компилятор зафигачил. Исходники проекта: github.com/begoon/easy

Пример кода на языке Easy, игра Жизнь: life.easy

Компилируем, запускаем:
git clone https://github.com/begoon/easy.git
cd easy
node easyc.ts life.easy
cc life.c -o life -I.
./life
Получаем:
** [ EASY LIFE ] ***************************************************************** 
* xx *
* xx xx *
* x x xxx *
* xx x xxx *
* x x x xx *
* xx x x x xx *
* xxx x x *
* x xxx xxx *
* xxxxx xx xx *
* xx x x x *
* x x xx *
* x xx *
* xxx *
* x *
* x xx x *
* xxx xxx x x *
* x x x *
* x x xx *
* x x *
* *
* *
* *
* xx *
* xx xx *
* xx *
**********************************************************************************
GENERATION: 104
vak: (Знайка)
(отсюда)

В главе 19 руководства к ZX Spectrum 1982 года (PDF) объясняется «BEEP» — команда BASIC, которая заставляла Spectrum издавать звуки. Приводится пример исполнения траурного марша из Первой симфонии австрийского композитора Малера.



Глава заканчивается парой упражнений для улучшения навыков BEEP, второе из которых — «Запрограммируйте компьютер так, чтобы он играл не только траурный марш, но и остальную часть Первой симфонии Малера».



Четыре части симфонии длятся около 50 минут, что делает такую ​​программу нетривиальной. Однако нынешним хакерам семь вёрст не крюк. Для исполнения симфонии потребовалась распределённая система из кучи Спектрумов (Spectra?). Интерфейс Spectranet объединил машины в сеть, управляемую Raspberry Pi. В нужный момент каждый Спектрум делает соответствующий BEEP, исполняя свою музыкальную партию.