Skip to content

Commit 92ab043

Browse files
committed
[Heavy] Add scheme integer ordering functions
1 parent 153fda5 commit 92ab043

File tree

14 files changed

+215
-164
lines changed

14 files changed

+215
-164
lines changed

heavy/include/heavy/Context.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class Context : public ContinuationStack<Context>,
226226
// PushEnvFrame - Creates and pushes an EnvFrame to the
227227
// current environment (EnvStack)
228228
EnvFrame* PushEnvFrame(llvm::ArrayRef<Value> Names);
229-
void PopEnvFrame();
229+
void PopEnvFrame(EnvFrame*);
230230
void PushLocalBinding(Binding* B);
231231

232232
// PushLambdaFormals - Check formals, create an EnvFrame,
@@ -303,7 +303,7 @@ class Context : public ContinuationStack<Context>,
303303
}
304304
}
305305

306-
Value RebuildLiteral(Value V);
306+
Value RebuildLiteral(Value V, Value Env = nullptr);
307307

308308
Heap<Context>& getAllocator() { return *this; }
309309

@@ -395,7 +395,8 @@ class Context : public ContinuationStack<Context>,
395395
return new (*this) BuiltinSyntax(Fn);
396396
}
397397

398-
SyntaxClosure* CreateSyntaxClosure(SourceLocation Loc, Value Node,
398+
Value CreateSyntaxClosure(SourceLocation Loc, Value Node, Value Env);
399+
SyntaxClosure* CreateSyntaxClosure(SourceLocation Loc, Symbol* S,
399400
Value Env);
400401

401402
SourceValue* CreateSourceValue(SourceLocation Loc) {

heavy/include/heavy/OpGen.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ class OpGen : public ValueVisitor<OpGen, mlir::Value> {
374374
return createLiteral(V);
375375
}
376376

377+
mlir::Value VisitUndefined(Undefined U) {
378+
return createUndefined();
379+
}
380+
377381
// VisitOperation and VisitContArg are both idempotent
378382
// so they are declared static for reuse in the static
379383
// value conversion functions.

heavy/include/heavy/Value.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,7 @@ class Module : public ValueBase {
16341634
}
16351635

16361636
void Insert(String* Id, String* MangledName) {
1637+
assert(!MangledName->getStringRef().empty());
16371638
Map[Id] = MangledName;
16381639
}
16391640

@@ -1840,6 +1841,7 @@ class Environment : public ValueBase {
18401841

18411842
// Add a named location or syntax keyword.
18421843
void Insert(Symbol* S, String* MangledName) {
1844+
assert(!MangledName->getStringRef().empty());
18431845
EnvMap[S->getString()] = MangledName;
18441846
}
18451847

heavy/include/heavy/base.sld

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
(define-library (heavy base)
44
(import (heavy builtins)
55
(heavy base r7rs-syntax)
6-
(heavy base list))
6+
(heavy base list)
7+
(heavy base int))
78
(begin
89

910
) ; end of begin
@@ -18,7 +19,6 @@
1819
quote
1920
set!
2021
syntax-rules
21-
ir-macro-transformer
2222
begin
2323
cond-expand
2424
define-library
@@ -32,8 +32,8 @@
3232
-
3333
/
3434
*
35-
>
36-
<
35+
< <= > >=
36+
positive? zero?
3737
apply
3838
append
3939
call-with-values
@@ -68,7 +68,7 @@
6868
; (heavy base list)
6969
caar cadr cdar cddr
7070
member memq memv
71-
map reverse
71+
reverse map
7272

7373
; eval stuff
7474
compile

heavy/include/heavy/base/int.sld

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
(import (heavy builtins))
2+
3+
(define-library (heavy base int)
4+
(import (heavy builtins)
5+
(heavy base r7rs-syntax))
6+
(begin
7+
(define (< x1 x2 . xN)
8+
(if (positive? (- x2 x1))
9+
(if (pair? xN)
10+
(apply < x2 xN)
11+
#t)
12+
#f))
13+
14+
(define (<= x1 x2 . xN)
15+
(let ((Difference (- x2 x1)))
16+
(if (or (positive? Difference) (zero? Difference))
17+
(if (pair? xN)
18+
(apply <= x2 xN)
19+
#t)
20+
#f)))
21+
22+
(define (> x1 x2 . xN)
23+
(if (positive? (- x1 x2))
24+
(if (pair? xN)
25+
(apply < x2 xN)
26+
#t)
27+
#f))
28+
29+
(define (>= x1 x2 . xN)
30+
(let ((Difference (- x1 x2)))
31+
(if (or (positive? Difference) (zero? Difference))
32+
(if (pair? xN)
33+
(apply >= x2 xN)
34+
#t)
35+
#f)))
36+
37+
) ; end begin
38+
(export
39+
< <= > >=
40+
))

heavy/include/heavy/base/list.sld

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
(define-library (heavy base list)
44
(import (heavy builtins)
5-
(heavy base r7rs-syntax))
5+
(heavy base r7rs-syntax)
6+
(only (heavy base int) < <=))
67
(begin
78
(define (caar x) (car (car x)))
89
(define (cadr x) (car (cdr x)))
@@ -34,10 +35,42 @@
3435
(cons (car List) NewList))
3536
NewList)))
3637

38+
(define (map Proc . InputLists)
39+
(define (MapFast FastProc List)
40+
(if (pair? List)
41+
(cons (FastProc (car List))
42+
(MapFast FastProc (cdr List)))
43+
'()))
44+
(define MaxLen
45+
(let Loop ((Lists InputLists)
46+
(MinLength -1))
47+
(dump Lists)
48+
(if (pair? Lists)
49+
(let ((Len (length (car Lists))))
50+
(Loop
51+
(cdr Lists)
52+
(if (<= 0 Len MinLength)
53+
Len
54+
MinLength)))
55+
MinLength)))
56+
(dump 'wtf)
57+
(when (< MaxLen 0)
58+
(error "expecting at least one finite list" InputLists))
59+
(let Loop ((I 0)
60+
(Lists InputLists)
61+
(Result '()))
62+
(if (< I MaxLen)
63+
(let ((Args (MapFast car Lists))
64+
(NextLists (MapFast cdr Lists)))
65+
(Loop (+ I 1) NextLists (cons (apply Proc Args))))
66+
Result)))
67+
68+
69+
3770
) ; end of begin
3871
(export
3972
caar cadr cdar cddr
4073
member memq memv
41-
reverse
74+
reverse map
4275
)
4376
)

heavy/include/heavy/base/r7rs-syntax.sld

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
(define-library (heavy base r7rs-syntax)
66
(import (heavy builtins))
77
(begin
8-
; lambda already gives a nice error for an empty body.
98
(define-syntax letrec*
109
(syntax-rules ()
11-
((letrec* ((var init) ...) body ...)
12-
((lambda () (define var init) ... body ...)))))
10+
((letrec* ((var init) ...) body1 body ...)
11+
((lambda () (define var init) ... body1 body ...)))))
1312

1413
(define-syntax letrec
1514
(syntax-rules ()
@@ -18,14 +17,16 @@
1817

1918
(define-syntax let
2019
(syntax-rules ()
21-
((let ((name val) ...) body ...)
22-
((lambda (name ...) body ...) val ...))
23-
((let tag ((name val) ...) body ...)
20+
((let ((name val) ...) body1 body ...)
21+
((lambda (name ...) body1 body ...) val ...))
22+
((let tag ((name val) ...) body1 body ...)
2423
((letrec
25-
((tag (lambda (name ...) body ...)))
24+
((tag (lambda (name ...) body1 body ...)))
2625
tag)
2726
val ...))))
2827

28+
; FIXME cond should be in the environment within syntax body.
29+
; (this applies to all define-syntax)
2930
(define-syntax cond
3031
(syntax-rules (else =>)
3132
((cond (else result1 result2 ...))

0 commit comments

Comments
 (0)