Skip to content

Commit 65e4f46

Browse files
authored
Merge pull request #466 from maheshejs/main
Add bracket
2 parents 433a68c + f5b00ba commit 65e4f46

20 files changed

+1486
-0
lines changed

bracket/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# bracket
2+
3+
A compiler connecting two educational intermediate languages: [exprs-lang-v7](https://www.students.cs.ubc.ca/~cs-411/2022w2/v7_Languages.html#%28def._%28%28lib._cpsc411%2Flangs%2Fv7..rkt%29._exprs-lang-v7%29%29), Racket's subset from UBC's [CPSC411](https://www.students.cs.ubc.ca/~cs-411/2022w2/index.html) (Compiler Construction) course, and [Bril](https://capra.cs.cornell.edu/bril/), the Big Red Intermediate Language.
4+
5+
## Overview
6+
7+
`bracket` compiles programs from exprs-lang-v7 to Bril JSON. This enables Racket's expressive subset to serve as a rich frontend for generating Bril programs.
8+
9+
## Installation
10+
11+
### Prerequisites
12+
13+
1. **Install Racket**
14+
```bash
15+
# Download from https://racket-lang.org/
16+
```
17+
18+
2. **Install CPSC411 compiler library (2022w2 version)**
19+
```bash
20+
raco pkg install https://github.com/cpsc411/cpsc411-pub.git?path=cpsc411-lib#2022w2
21+
```
22+
23+
3. **Install Bril tools** (for interpreting/viewing output)
24+
```bash
25+
# Follow instructions at https://github.com/sampsyo/bril
26+
```
27+
### Building bracket
28+
29+
```bash
30+
raco exe -o bracket bracket.rkt
31+
```
32+
33+
## Usage
34+
35+
### Basic Compilation
36+
37+
Compile an exprs-lang-v7 program to Bril JSON:
38+
```bash
39+
./bracket tests/fib_recursive.rkt
40+
```
41+
42+
### View as Bril Text
43+
44+
Convert the JSON output to human-readable Bril text:
45+
```bash
46+
./bracket tests/fib_recursive.rkt | bril2txt
47+
```
48+
49+
### Interpret with brili
50+
51+
Execute the compiled program:
52+
```bash
53+
./bracket tests/fib_recursive.rkt | brili
54+
```
55+
56+
**Expected output:** `55` (the 10th Fibonacci number as computed in `tests/fib_recursive.rkt`)
57+
58+
## Example
59+
60+
**Input** (`tests/fib_recursive.rkt`):
61+
```racket
62+
(module
63+
(define fib
64+
(lambda (n)
65+
(if (call <= n 1)
66+
1
67+
(call + (call fib (call - n 1))
68+
(call fib (call - n 2))))))
69+
(let ([arg.x 10])
70+
(call fib arg.x)))
71+
```
72+
73+
**Output:** Bril JSON program that computes the 10th Fibonacci number
74+
75+
## References
76+
77+
- [More about bracket](https://www.cs.cornell.edu/courses/cs6120/2025fa/blog/bracket/)
78+
- [exprs-lang-v7 Grammar](https://www.students.cs.ubc.ca/~cs-411/2022w2/v7_Languages.html)
79+
- [CPSC411 Course Materials](https://www.students.cs.ubc.ca/~cs-411/2022w2/index.html)
80+
- [Bril Documentation](https://capra.cs.cornell.edu/bril/)

bracket/bracket.rkt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#lang racket
2+
3+
;;Copyright (C) 2025 Joseph Maheshe.
4+
5+
(require
6+
json
7+
cpsc411/compiler-lib
8+
"src/uniquify.rkt"
9+
"src/implement-safe-primops.rkt"
10+
"src/specify-representation.rkt"
11+
"src/remove-complex-opera.rkt"
12+
"src/sequentialize-let.rkt"
13+
"src/normalize-bind.rkt"
14+
"src/compile-bril.rkt")
15+
16+
(provide
17+
bracket)
18+
19+
(define (bracket program)
20+
(define all-passes
21+
(list
22+
compile-bril
23+
normalize-bind
24+
sequentialize-let
25+
remove-complex-opera*
26+
specify-representation
27+
implement-safe-primops
28+
uniquify))
29+
30+
(define pipeline
31+
(apply compose all-passes))
32+
(pipeline program))
33+
34+
(module+ main
35+
(define args (current-command-line-arguments))
36+
37+
(define in
38+
(cond
39+
[(= (vector-length args) 0)
40+
(current-input-port)]
41+
[(= (vector-length args) 1)
42+
(open-input-file (vector-ref args 0))]
43+
[else
44+
(error 'bracket "expected at most one input file, got ~a"
45+
(vector-length args))]))
46+
47+
(define program (read in))
48+
(write-json (bracket program) (current-output-port)))
49+

0 commit comments

Comments
 (0)