Compare commits
3 Commits
0a565046af
...
913af0b463
| Author | SHA1 | Date | |
|---|---|---|---|
| 913af0b463 | |||
| 6cda0873c6 | |||
| 0a5eb1e0ce |
54
semen.scm
54
semen.scm
@@ -546,8 +546,9 @@
|
||||
;;;
|
||||
;;; (var n int (match s ...)) -> int n = switch (s.tag) { ...
|
||||
;;;
|
||||
;;; A condition is not tracked: `walk-parts' takes every slot of an `if'
|
||||
;;; alike.
|
||||
;;; and in a control form's test, which C reads as an expression too:
|
||||
;;;
|
||||
;;; (if (match s ...) ...) -> if (switch (s.tag) { ...
|
||||
(define operand-position? (make-parameter #f))
|
||||
|
||||
(define (walk-operand form env)
|
||||
@@ -583,10 +584,13 @@
|
||||
(cons walk-embed-result (walk-body expansion env)))))
|
||||
(else
|
||||
(case (car form)
|
||||
((do for while if switch)
|
||||
((do)
|
||||
(parameterize ((operand-position? #f))
|
||||
(with-scope env (lambda () (walk-parts form env)))))
|
||||
|
||||
((for while if switch)
|
||||
(with-scope env (lambda () (walk-control form env))))
|
||||
|
||||
((case case/fallthrough default)
|
||||
(parameterize ((operand-position? #f))
|
||||
(walk-parts form env)))
|
||||
@@ -711,7 +715,12 @@
|
||||
(convert-to-closure type arg env form)
|
||||
(let ((given (expression-type arg env)))
|
||||
(cond
|
||||
((brace-initializer? arg) arg)
|
||||
((brace-initializer? arg)
|
||||
(let ((held (array-length type)))
|
||||
(when (and held (not (= held (length (remove comment-form?
|
||||
(vector->list arg))))))
|
||||
(sex-error form "array is not as long as its field" (first field))))
|
||||
arg)
|
||||
((not (array-form? given))
|
||||
(sex-error form "an array field takes an array" (first field)))
|
||||
((not (eqv? (array-length given) (array-length type)))
|
||||
@@ -874,6 +883,29 @@
|
||||
(define (walk-parts form env)
|
||||
(copy-form-source! form (walk-body form env)))
|
||||
|
||||
;;; A control form, its tests walked as values and the rest as
|
||||
;;; statements
|
||||
(define (walk-control form env)
|
||||
(let ((slots (count (lambda (part) (not (comment-form? part))) (cdr form))))
|
||||
(let loop ((parts (cdr form)) (i 0) (acc (list (car form))))
|
||||
(cond
|
||||
((null? parts) (copy-form-source! form (reverse acc)))
|
||||
((comment-form? (car parts)) (loop (cdr parts) i (cons (car parts) acc)))
|
||||
(else
|
||||
(let ((walked (parameterize ((operand-position?
|
||||
(test-slot? (car form) i slots)))
|
||||
(walk-body (list (car parts)) env))))
|
||||
(loop (cdr parts) (+ i 1) (append-reverse walked acc))))))))
|
||||
|
||||
;;; Whether the Ith of a control form's SLOTS is read as a value: an
|
||||
;;; `if''s tests but not its trailing else, a `for''s header, the
|
||||
;;; scrutinee of a `while' or a `switch'
|
||||
(define (test-slot? head i slots)
|
||||
(case head
|
||||
((if) (and (even? i) (< (+ i 1) slots)))
|
||||
((for) (< i 3))
|
||||
(else (= i 0))))
|
||||
|
||||
;;; (match s ((circle (r)) ...) ((empty ()) ...)) is a switch over the
|
||||
;;; tag, each arm a block binding that variant's fields as locals. `_'
|
||||
;;; drops one. An arm the sum does not have, a wrong number of
|
||||
@@ -1846,7 +1878,8 @@
|
||||
core
|
||||
(if (> (length core) 3)
|
||||
(append (take core 3)
|
||||
(walk-operands core (drop core 3) env))
|
||||
(map bare-braces
|
||||
(walk-operands core (drop core 3) env)))
|
||||
core))
|
||||
env)))
|
||||
(form (if qualifier
|
||||
@@ -1856,6 +1889,17 @@
|
||||
(add-name-type! (second core) (third core)))
|
||||
(cons form (append (hash-table-ref env :lambda-aux-code) acc))))
|
||||
|
||||
;;; A compound literal is not a constant, though cc lets one pass at file
|
||||
;;; scope unless asked to be pedantic; where it initializes, its braces
|
||||
;;; alone say the same:
|
||||
;;;
|
||||
;;; (var g t (t/a 1)) -> static t g = {.tag = tǂa, .u = {.a = {1}}}
|
||||
(define (bare-braces form)
|
||||
(if (vector? form)
|
||||
(let ((parts (list-split (vector->list form) ':)))
|
||||
(list->vector (map bare-braces (last parts))))
|
||||
form))
|
||||
|
||||
;;; Utils
|
||||
(define (non-empty-list? form)
|
||||
(and (list? form)
|
||||
|
||||
@@ -203,7 +203,11 @@
|
||||
(not (emits? "(pub sum t ((a ((x int)))))(fn f () t (return (t/a 1)))"
|
||||
"t tːa (")))
|
||||
(test-assert "so it is a constant at file scope"
|
||||
(emits? "(sum t ((a ((x int)))))(var g t (t/a 1))" "static t g = (t){"))
|
||||
(emits? "(sum t ((a ((x int)))))(var g t (t/a 1))"
|
||||
"static t g = {.tag = tǂa, .u = {.a = {1}}};"))
|
||||
(test-assert "as is any literal a global starts from"
|
||||
(emits? "(record point ((x int) (y int)))(var g point #(point : 1 2))"
|
||||
"static point g = {1, 2};"))
|
||||
(test-assert "and an lvalue a method can take the address of"
|
||||
(emits? (string-append
|
||||
"(sum t ((a ((x int)))))"
|
||||
@@ -228,6 +232,12 @@
|
||||
(test-assert "and as many as the field holds"
|
||||
(reports? "(sum t ((a ((xs [int 2])))))(fn f () t (var v [int 3] #(1 2 3)) (return (t/a v)))"
|
||||
"array is not as long as its field"))
|
||||
(test-assert "and so must a literal"
|
||||
(reports? "(sum t ((a ((xs [int 2])))))(fn f () t (return (t/a #(1 2 3))))"
|
||||
"array is not as long as its field"))
|
||||
(test-assert "not filling it with zeros either"
|
||||
(reports? "(sum t ((a ((xs [int 2])))))(fn f () t (return (t/a #(1))))"
|
||||
"array is not as long as its field"))
|
||||
(test-assert "an array field takes an array"
|
||||
(reports? "(sum t ((a ((xs [int 2])))))(fn f () t (var p (* int) 0) (return (t/a p)))"
|
||||
"an array field takes an array"))
|
||||
@@ -432,6 +442,20 @@
|
||||
(test-assert "not even as an argument"
|
||||
(reports? (sum-src "(= out (match s ((circle (r)) r) (else 0.0)))")
|
||||
"match is a statement"))
|
||||
(test-assert "nor as an if's test"
|
||||
(reports? (sum-src "(if (match s ((circle (r)) 1) (else 0)) (= out 1.0))")
|
||||
"match is a statement"))
|
||||
(test-assert "nor as a while's"
|
||||
(reports? (sum-src "(while (match s ((circle (r)) 1) (else 0)) (= out 1.0))")
|
||||
"match is a statement"))
|
||||
(test-assert "nor in a for's header"
|
||||
(reports? (sum-src "(for (var i int 0) (match s ((circle (r)) 1) (else 0)) (++ i)"
|
||||
" (= out 1.0))")
|
||||
"match is a statement"))
|
||||
(test-assert "but an if's branches are statements, the else among them"
|
||||
(emits? (sum-src "(if (== out 0.0) (match s ((circle (r)) (= out r)) (else (= out 1.0)))"
|
||||
" (match s ((circle (r)) (= out 2.0)) (else (= out 3.0))))")
|
||||
"out = 3.0;"))
|
||||
(test-assert "but a case body is a statement"
|
||||
(emits? (sum-src "(switch 1 (case 1 (match s ((circle (r)) (= out r))"
|
||||
" (else (= out 0.0)))))")
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(compilation "-- -std=c11 -pedantic-errors")
|
||||
(input)
|
||||
(output "c11: 42 43 6")
|
||||
(output "c11: 42 43 6 4 6")
|
||||
(return 0)
|
||||
|
||||
;;; C11 is the floor, and this is the program that holds it there: the
|
||||
@@ -10,7 +10,8 @@
|
||||
;;;
|
||||
;;; A capturing closure is the one that leans on C11 itself, through
|
||||
;;; max_align_t in the environment and `_Static_assert' on its size;
|
||||
;;; the record, the method and the match are here for what they emit.
|
||||
;;; the record, the method and the match are here for what they emit,
|
||||
;;; and the globals for initializers that have to be constants.
|
||||
|
||||
(include stdio.h)
|
||||
|
||||
@@ -22,6 +23,9 @@
|
||||
|
||||
(sum shape ((square ((side int))) (dot ())))
|
||||
|
||||
(var corner point #(point : 2 3))
|
||||
(var unit shape (shape/square 1))
|
||||
|
||||
(fn sides ((s shape)) int
|
||||
(var n int 0)
|
||||
(match s
|
||||
@@ -34,6 +38,7 @@
|
||||
(var k int 1)
|
||||
(var add-k (closure ((int)) int)
|
||||
(closure ((v int)) int (k) (return (+ v k))))
|
||||
(printf "c11: %d %d %d\n"
|
||||
(.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2))
|
||||
(printf "c11: %d %d %d %d %d\n"
|
||||
(.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2)
|
||||
(sides unit) (.area corner))
|
||||
(return 0))
|
||||
|
||||
Reference in New Issue
Block a user