Skip to content

Commit 0cc6dfa

Browse files
committed
Assignment 4 / spring 2021
1 parent c54ec93 commit 0cc6dfa

27 files changed

+16880
-888
lines changed

42.rkt

-2
This file was deleted.

Makefile

+14-10
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@ UNAME := $(shell uname)
33

44
ifeq ($(UNAME), Darwin)
55
format=macho64
6-
else ifeq ($(UNAME), Linux)
7-
format=elf64
86
else
9-
format=win64
7+
format=elf64
108
endif
119

12-
%.run: %.o main.o
13-
gcc main.o $< -o $@
10+
%.run: %.o runtime.o
11+
gcc runtime.o $< -o $@
12+
13+
runtime.o: main.o char.o io.o
14+
ld -r main.o char.o io.o -o runtime.o
1415

15-
main.o: main.c
16-
gcc -c main.c -o main.o
16+
main.o: main.c types.h runtime.h
17+
gcc -fPIC -c main.c -o main.o
18+
19+
char.o: char.c types.h
20+
gcc -fPIC -c char.c -o char.o
21+
22+
io.o: io.c runtime.h
23+
gcc -fPIC -c io.c -o io.o
1724

1825
%.o: %.s
1926
nasm -f $(format) -o $@ $<
@@ -23,6 +30,3 @@ main.o: main.c
2330

2431
clean:
2532
rm *.o *.s *.run
26-
27-
test: 42.run
28-
@test "$(shell ./42.run)" = "42"

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
Assignment 4: Variables, Binding, and Characters
44

5-
See: http://www.cs.umd.edu/class/fall2020/cmsc430/Assignments.html
5+
See: http://www.cs.umd.edu/class/spring2021/cmsc430/Assignments.html

asm/ast.rkt

-26
This file was deleted.

asm/interp.rkt

-23
This file was deleted.

asm/printer.rkt

-71
This file was deleted.

ast.rkt

+52-60
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,70 @@
66
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
77

88
;; type Expr =
9-
;; | Integer
10-
;; | Boolean
11-
;; | Character
12-
;; | Variable
13-
;; | Prim PrimOp Expr
14-
;; | if Expr Expr Expr
15-
;; | cond (Clause list) Expr
16-
;; | let (Binding list) Expr
17-
18-
;; type PrimOp =
19-
;; | add1 | sub1 | zero? | abs | - | char? | integer? | boolean?
20-
;; | integer->char | char->integer
21-
22-
;; type Clause = Expr Expr
23-
;; type Binding = Variable Expr
24-
25-
;; type Variable = Symbol (except 'let, 'add1, etc.)
9+
;; | (Eof)
10+
;; | (Int Integer)
11+
;; | (Bool Boolean)
12+
;; | (Char Character)
13+
;; | (Prim0 Op0)
14+
;; | (Prim1 Op1 Expr)
15+
;; | (Prim2 Op2 Expr Expr)
16+
;; | (PrimVar OpVar (ListOf Expr))
17+
;; | (If Expr Expr Expr)
18+
;; | (Begin Expr Expr)
19+
;; | (Let (ListOf Binding) Expr)
20+
;; | (Let* (ListOf Binding) Expr)
21+
;; | (Var Id)
22+
;; | (Cond Clauses (Expr))
23+
24+
;; type Clauses = Listof (Expr, Expr)
25+
;; type Id = Symbol
26+
;; type Op0 = 'read-byte
27+
;; type Op1 = 'add1 | 'sub1 | 'zero? | abs | - | not
28+
;; | 'char? | integer? | boolean?
29+
;; | 'integer->char | 'char->integer
30+
;; | 'write-byte | 'eof-object?
31+
;; type Op2 = '+ | '-
32+
;; type OpVar = '+
33+
;; type Binding = (Id, Expr)
2634

2735
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2836
;;;;;; The AST data structure
2937
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3038

3139
;; The AST can be viewed as having 'kinds' of nodes.
3240
;;
33-
;; * The nodes that represnt an expression themselves
41+
;; * The nodes that represent an expression themselves
3442
;;
3543
;; * The nodes that are part of an expression, but no an expression themselves
3644

3745
;; The below are the former:
3846

39-
(struct int-e (i) #:transparent)
40-
(struct bool-e (b) #:transparent)
41-
(struct char-e (c) #:transparent)
42-
(struct var-e (v) #:transparent)
43-
(struct prim-e (p e) #:transparent)
44-
(struct if-e (e t f) #:transparent)
45-
(struct cond-e (cs el) #:transparent)
46-
(struct let-e (bs b) #:transparent)
47+
(struct Eof () #:prefab)
48+
(struct Int (i) #:prefab)
49+
(struct Bool (b) #:prefab)
50+
(struct Char (c) #:prefab)
51+
(struct Prim0 (p) #:prefab)
52+
(struct Prim1 (p e) #:prefab)
53+
(struct Prim2 (p e1 e2) #:prefab)
54+
(struct PrimVar (p es) #:prefab)
55+
(struct If (e1 e2 e3) #:prefab)
56+
(struct Begin (e1 e2) #:prefab)
57+
(struct Let (bs e) #:prefab)
58+
(struct Let* (bs e) #:prefab)
59+
(struct Var (x) #:prefab)
60+
(struct Cond (cs e) #:prefab)
4761

4862
;; The next two are the latter:
4963

50-
;; a clause now takes an _arbitrary_ expression and a body.
51-
;; This is different than assignment 3! If you want to understand
52-
;; why, look at the lecture notes for Dupe.
53-
(struct clause (e body) #:transparent)
64+
;; A clause takes an _arbitrary_ expression and a body.
65+
(struct Clause (e body) #:prefab)
5466

5567
;; A binding holds a symbol representing the bound variable and
5668
;; Expr that represents the value that will be bound to that variable
57-
(struct binding (v e) #:transparent)
69+
(struct Binding (v e) #:prefab)
70+
71+
;; Finally, here's a struct to return when an expression is ill-formed.
72+
(struct IllFormedError () #:prefab)
5873

5974
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6075
;;;;;; AST utility functions (getters)
@@ -66,50 +81,27 @@
6681
(define (get-vars bs)
6782
(match bs
6883
['() '()]
69-
[(cons (binding v _) bs) (cons v (get-vars bs))]))
84+
[(cons (Binding v _) bs) (cons v (get-vars bs))]))
7085

7186
;; Get all of the _definitions_ from a list of bindings
7287
;; [Binding] -> [Expr]
7388
(define (get-defs bs)
7489
(match bs
7590
['() '()]
76-
[(cons (binding _ def) bs) (cons def (get-defs bs))]))
91+
[(cons (Binding _ def) bs) (cons def (get-defs bs))]))
7792

7893

7994
;; Get all of the predicate expressions from a list of clauses
80-
;; [Binding] -> [Expr]
95+
;; [Clause] -> [Expr]
8196
(define (get-preds cs)
8297
(match cs
8398
['() '()]
84-
[(cons (clause p _) cs) (cons p (get-preds cs))]))
99+
[(cons (Clause p _) cs) (cons p (get-preds cs))]))
85100

86101
;; Get all of the bodies expressions from a list of clauses
87-
;; [Binding] -> [Expr]
102+
;; [Clause] -> [Expr]
88103
(define (get-bods cs)
89104
(match cs
90105
['() '()]
91-
[(cons (clause _ b) cs) (cons b (get-preds cs))]))
92-
93-
;; Given an AST, construct an sexpr that has the same shape
94-
(define (ast->sexpr a)
95-
(match a
96-
[(int-e i) `(int-e ,i)]
97-
[(bool-e b) `(bool-e ,b)]
98-
[(char-e c) `(char-e ,c)]
99-
[(var-e v) `(var-e ,v)]
100-
[(prim-e p e) `(prim-e ,p ,e)]
101-
[(if-e e t f) `(if-e ,(ast->sexpr e)
102-
,(ast->sexpr t)
103-
,(ast->sexpr f))]
104-
[(cond-e cs f) `(cond-e ,(clauses->sexpr cs) ,(ast->sexpr f))]
105-
[(let-e bs b) `(let-e ,(binding->sexpr bs) ,(ast->sexpr b))]))
106-
107-
(define (binding->sexpr bnds)
108-
(match bnds
109-
['() '()]
110-
[(cons (binding v e) bnds) `((,v ,(ast->sexpr e)) ,@(binding->sexpr bnds))]))
106+
[(cons (Clause _ b) cs) (cons b (get-preds cs))]))
111107

112-
(define (clauses->sexpr cs)
113-
(match cs
114-
['() '()]
115-
[(cons (clause e b) cs) `((,(ast->sexpr e) ,(ast->sexpr b)) ,@(clauses->sexpr cs))]))

char.c

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <stdio.h>
2+
#include <inttypes.h>
3+
#include "types.h"
4+
5+
void print_codepoint(int64_t);
6+
7+
void print_char (int64_t v) {
8+
int64_t codepoint = v >> char_shift;
9+
printf("#\\");
10+
switch (codepoint) {
11+
case 0:
12+
printf("nul"); break;
13+
case 8:
14+
printf("backspace"); break;
15+
case 9:
16+
printf("tab"); break;
17+
case 10:
18+
printf("newline"); break;
19+
case 11:
20+
printf("vtab"); break;
21+
case 12:
22+
printf("page"); break;
23+
case 13:
24+
printf("return"); break;
25+
case 32:
26+
printf("space"); break;
27+
case 127:
28+
printf("rubout"); break;
29+
default:
30+
print_codepoint(v);
31+
}
32+
}
33+
34+
void print_codepoint(int64_t v) {
35+
int64_t codepoint = v >> char_shift;
36+
// Print using UTF-8 encoding of codepoint
37+
// https://en.wikipedia.org/wiki/UTF-8
38+
if (codepoint < 128) {
39+
printf("%c", (char) codepoint);
40+
} else if (codepoint < 2048) {
41+
printf("%c%c",
42+
(char)(codepoint >> 6) | 192,
43+
((char)codepoint & 63) | 128);
44+
} else if (codepoint < 65536) {
45+
printf("%c%c%c",
46+
(char)(codepoint >> 12) | 224,
47+
((char)(codepoint >> 6) & 63) | 128,
48+
((char)codepoint & 63) | 128);
49+
} else {
50+
printf("%c%c%c%c",
51+
(char)(codepoint >> 18) | 240,
52+
((char)(codepoint >> 12) & 63) | 128,
53+
((char)(codepoint >> 6) & 63) | 128,
54+
((char)codepoint & 63) | 128);
55+
}
56+
}
57+

0 commit comments

Comments
 (0)