Compare commits
43 Commits
f9809c19c4
...
c317d9950c
| Author | SHA1 | Date | |
|---|---|---|---|
| c317d9950c | |||
| 6ced17c20e | |||
| dd1718dffb | |||
| 4ac386f373 | |||
| 1599832ece | |||
| 99a41b9d04 | |||
| 4d31e2ed9b | |||
| d0e806bf84 | |||
| d92b5fc696 | |||
| fd6520dbe8 | |||
| 4ad4133d92 | |||
| 6a8c68fe3d | |||
| ed7bf0879a | |||
| a95e55fddc | |||
| 9b503c1017 | |||
| 8db435f994 | |||
| 286936488b | |||
| 86830ee4c9 | |||
| 17badcb966 | |||
| 31d876e30e | |||
| d94daa9066 | |||
| 24f11cdcba | |||
| acd7010e6b | |||
| f8c03d17c0 | |||
| 250713e01d | |||
| 314bdc4e2d | |||
| b9b3f9da5f | |||
| 2673b25f1d | |||
| c162005241 | |||
| 12d0d13a7c | |||
| 36ff456d24 | |||
| 566e8326a3 | |||
| d41960e407 | |||
| 85511ae037 | |||
| 1acdd0e708 | |||
| c06845a80a | |||
| 816c1db9ed | |||
| e465f6892a | |||
| 3701aeab3f | |||
| 8d1c12d03b | |||
| 913af0b463 | |||
| 6cda0873c6 | |||
| 0a5eb1e0ce |
68
semen.scm
68
semen.scm
@@ -553,8 +553,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)
|
||||
@@ -590,10 +591,13 @@
|
||||
(cons walk-embed-result (walk-body expansion env)))))
|
||||
(else
|
||||
(case (car form)
|
||||
((do for while if switch case case/fallthrough default)
|
||||
((do case case/fallthrough default)
|
||||
(parameterize ((operand-position? #f))
|
||||
(with-scope env (lambda () (walk-parts form env)))))
|
||||
|
||||
((for while if switch)
|
||||
(with-scope env (lambda () (walk-control form env))))
|
||||
|
||||
((lambda)
|
||||
(let ((name (aux-name! env make-lambda-name)))
|
||||
(add-aux-code! env (lift-lambda name form))
|
||||
@@ -722,7 +726,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)))
|
||||
@@ -906,6 +915,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
|
||||
@@ -1084,7 +1116,8 @@
|
||||
;;; A type we cannot work out is read as a value, which is what the
|
||||
;;; path would have meant before any of this. The warning names what
|
||||
;;; could not be typed: the base, or the path up to a field of a struct
|
||||
;;; only C knows -- (. font handle), not font.
|
||||
;;; only C knows -- (. font handle x) warns of (. font handle), not
|
||||
;;; font. A field a struct declared here lacks is an error.
|
||||
(define (walk-member-path form env)
|
||||
(let ((base (second form)))
|
||||
(let step ((expr (walk-operand base env))
|
||||
@@ -1093,7 +1126,10 @@
|
||||
(told? #f))
|
||||
(if (null? fields)
|
||||
(copy-form-source! form expr)
|
||||
(let ((target (pointer-target type)))
|
||||
(let* ((target (pointer-target type))
|
||||
(known (type-fields (or target type))))
|
||||
(when (and (pair? known) (not (assq (field-name (car fields)) known)))
|
||||
(sex-error form "no field of this name in" (or target type) (car fields)))
|
||||
(unless (or type told?)
|
||||
(let ((walked (drop-right (cddr form) (length fields))))
|
||||
(sex-warning form "cannot type this; `.' reads it as a value"
|
||||
@@ -1103,6 +1139,12 @@
|
||||
(cdr fields)
|
||||
(or told? (not type))))))))
|
||||
|
||||
;;; A field of a path, indexed or not: xs, [xs 0] -> xs
|
||||
(define (field-name field)
|
||||
(if (and (pair? field) (eq? '¤ (car field)) (pair? (cdr field)))
|
||||
(second field)
|
||||
field))
|
||||
|
||||
(define (lvalue? expr)
|
||||
(cond ((symbol? expr) #t)
|
||||
((vector? expr) #t) ; a compound literal is one
|
||||
@@ -1883,7 +1925,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
|
||||
@@ -1893,6 +1936,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)
|
||||
|
||||
@@ -202,7 +202,10 @@
|
||||
;;;
|
||||
;;; (* const cell) -> ((const . #t) (cell . #t))
|
||||
;;; [cell 4] -> ((cell . #f))
|
||||
;;; (fn ((cell)) int) -> ((cell . #t) (int . #t)) ; declaring is enough
|
||||
;;; (fn ((cell)) int) -> ((cell . #f) (int . #f))
|
||||
;;;
|
||||
;;; A function type takes and returns by value whatever it is held
|
||||
;;; through: C wants the body to define or call one of its functions.
|
||||
(define (type-uses type behind?)
|
||||
(cond
|
||||
((symbol? type) (list (cons type behind?)))
|
||||
@@ -215,7 +218,7 @@
|
||||
(if (array-bound? type) (bound-uses (last type)) (list)))))
|
||||
((fn closure)
|
||||
(if (and (pair? (cdr type)) (list? (second type)))
|
||||
(append-map (lambda (t) (type-uses t #t))
|
||||
(append-map (lambda (t) (type-uses t #f))
|
||||
(signature-types (second type) (last type)))
|
||||
(list)))
|
||||
((struct union enum)
|
||||
|
||||
12
sexc.scm
12
sexc.scm
@@ -59,8 +59,8 @@
|
||||
(value #f)
|
||||
(single-char #\m))
|
||||
(output ,(fmt #f "Write output to file. Default file name is a.out." nl
|
||||
(pad padding) "If -C or -m options are provided, defaults to stdout;" nl
|
||||
(pad padding) "`-' is stdout too")
|
||||
(pad padding) "With -C, -m or --public-interface, defaults to stdout," nl
|
||||
(pad padding) "which `-' names too")
|
||||
(required #f)
|
||||
(value #t)
|
||||
(single-char #\o))
|
||||
@@ -145,6 +145,12 @@
|
||||
(define (stdout-output? output)
|
||||
(or (eq? output 'default) (equal? output "-")))
|
||||
|
||||
;;; Whether ARGS ask for text, which `-o -' can send to stdout. An
|
||||
;;; executable or an object goes to cc's -o, where `-' is a file name
|
||||
(define (text-output? args)
|
||||
(any (lambda (option) (get-arg args option #f))
|
||||
'(emit-c macro-expand public-interface)))
|
||||
|
||||
(define (write-to-file-or-stdout output what)
|
||||
(if (stdout-output? output)
|
||||
(what)
|
||||
@@ -254,6 +260,8 @@ status, which is ours to pass on."
|
||||
(when help
|
||||
(print-help)
|
||||
(return #f))
|
||||
(when (and (equal? output "-") (not (text-output? args)))
|
||||
(error "-o - is stdout, which takes only -C, -m or --public-interface"))
|
||||
;; Read time comes before everything, so the features have to be
|
||||
;; in place before the first form is read
|
||||
(current-features
|
||||
|
||||
@@ -742,7 +742,8 @@ whitespace aside, THEN?"
|
||||
(not (emits? "(struct pt ((x int))) (fn f ((p (* (struct pt)))) int (return (. (* p) x)))"
|
||||
"*(p).x")))
|
||||
(test-assert "member access through a cast"
|
||||
(emits? (in-fn "(var n int (. (* (cast a (* (struct s)))) f))")
|
||||
(emits? (string-append "(struct s ((f int)))"
|
||||
(in-fn "(var n int (. (* (cast a (* (struct s)))) f))"))
|
||||
"(*(struct s*)a).f"))
|
||||
;; ...without gaining parens where none are due
|
||||
(test-assert "a bare dereference is left alone"
|
||||
@@ -774,7 +775,14 @@ whitespace aside, THEN?"
|
||||
(test-assert "nor is an aggregate tag"
|
||||
(emits? "(struct t ((z int))) (struct s ((a [struct t])))" "struct t a[]"))
|
||||
(test-assert "nor one behind a pointer"
|
||||
(emits? "(struct t ((z int))) (struct s ((a [* struct t])))" "struct t* a[]")))
|
||||
(emits? "(struct t ((z int))) (struct s ((a [* struct t])))" "struct t* a[]"))
|
||||
;; a qualifier waits for a type name only when none came before it
|
||||
(test-assert "a name after a qualified type is a bound"
|
||||
(emits? (in-fn "(var b [int const N])") "int const b[N]"))
|
||||
(test-assert "after a qualified name too"
|
||||
(emits? (in-fn "(var b [mytype const N])") "mytype const b[N]"))
|
||||
(test-assert "but a qualifier alone still waits for the type"
|
||||
(emits? "(typedef mytype int) (struct s ((a [const mytype])))" "const mytype a[]")))
|
||||
|
||||
;; `sizeof' takes a type or an expression, and `(* x)' is either.
|
||||
;; What is in scope says which: a name C alone knows is a type
|
||||
@@ -884,6 +892,14 @@ whitespace aside, THEN?"
|
||||
(test-assert "which one step through it does not need"
|
||||
(not (warns? (in-fn "(var t (* struct tm) 0) (= (. t a) 1)")
|
||||
"cannot type")))
|
||||
(test-assert "but a struct declared here has only the fields it names"
|
||||
(reports? (in-line "(= (. p z) 1)") "no field of this name"))
|
||||
(test-assert "the last step included"
|
||||
(reports? (in-line "(= (. lp a z) 1)") "no field of this name"))
|
||||
(test-assert "an indexed one by its name"
|
||||
(reports? (string-append "(struct v ((xs [int 2])))"
|
||||
(in-fn "(var w (struct v)) (= (. w [ys 0]) 1)"))
|
||||
"no field of this name"))
|
||||
;; the arrow is `%->' and nothing else writes one, so a bare `->'
|
||||
;; is an ordinary head -- a threading macro that nobody imported,
|
||||
;; and a call like any other unknown
|
||||
|
||||
@@ -229,7 +229,11 @@ ahead of its pub forms."
|
||||
(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)))))"
|
||||
@@ -254,6 +258,12 @@ ahead of its pub forms."
|
||||
(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"))
|
||||
@@ -458,6 +468,20 @@ ahead of its pub forms."
|
||||
(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)))))")
|
||||
@@ -503,6 +527,15 @@ ahead of its pub forms."
|
||||
"held by value"))
|
||||
(test-assert "returned"
|
||||
(interface-reports? "(record cell ((v int))) (pub fn f () cell)" "held by value"))
|
||||
(test-assert "or taken by a function a pub one takes"
|
||||
(interface-reports? "(record cell ((v int))) (pub fn take ((cb (fn ((c cell)) void))) void)"
|
||||
"held by value"))
|
||||
(test-assert "or returned by one"
|
||||
(interface-reports? "(record cell ((v int))) (pub fn take ((cb (fn () cell))) void)"
|
||||
"held by value"))
|
||||
(test "but a pointer there is still a pointer"
|
||||
'((record cell))
|
||||
(interface-of "(record cell ((v int))) (pub fn take ((cb (fn ((c (* cell))) void))) void)"))
|
||||
(test-assert "or taken by a pub method"
|
||||
(interface-reports? "(record cell ((v int))) (ops cell (pub fn get ((self cell)) int))"
|
||||
"held by value"))
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
# The failure paths.
|
||||
#
|
||||
# Both need a process to show themselves, so neither fits the unit
|
||||
# suite: sexc has to fail when cc fails -- exiting 0 after a failed
|
||||
# compile makes every driver, sextest included, read it as success --
|
||||
# and it has to clean up its temporary .c when its own emission throws.
|
||||
# Each needs a process to show itself, so none fits the unit suite:
|
||||
# sexc has to fail when cc fails -- exiting 0 after a failed compile
|
||||
# makes every driver, sextest included, read it as success -- it has to
|
||||
# clean up its temporary .c when its own emission throws, and it has to
|
||||
# refuse `-o -' for an executable, which cc would write to a file named
|
||||
# `-'.
|
||||
#
|
||||
# Everything is built inside a scratch TMPDIR, so nothing is left here
|
||||
# to clean up.
|
||||
# Everything is built inside a scratch directory, so nothing is left
|
||||
# here to clean up.
|
||||
|
||||
SEXC ?= ../../sexc
|
||||
|
||||
@@ -21,6 +23,10 @@ check:
|
||||
echo "exit code FAILED: sexc reported success after cc failed"; \
|
||||
rm -rf $$d; exit 1; \
|
||||
fi; \
|
||||
if (cd $$d && $(abspath $(SEXC)) $(CURDIR)/hello.sex -o - >/dev/null 2>&1) || [ -e $$d/- ]; then \
|
||||
echo "exit code FAILED: -o - built an executable"; \
|
||||
rm -rf $$d; exit 1; \
|
||||
fi; \
|
||||
rm -rf $$d; echo "exit code ok"
|
||||
|
||||
.PHONY: check
|
||||
|
||||
@@ -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))
|
||||
|
||||
17
types.scm
17
types.scm
@@ -409,14 +409,14 @@
|
||||
arglist))
|
||||
|
||||
;;; The last element is a bound unless what precedes it is still
|
||||
;;; waiting for a type name -- a `*', a qualifier, a tag's keyword. C
|
||||
;;; spells a type with one name at most, so a name after a name, or
|
||||
;;; after a grouped type, can only be the bound:
|
||||
;;; waiting for a type name -- a `*', a tag's keyword, a qualifier with
|
||||
;;; no type before it. C spells a type with one name at most, so a name
|
||||
;;; after a name, or after a grouped type, can only be the bound:
|
||||
;;;
|
||||
;;; (¤ int 4) four of int (¤ unsigned int) unsized
|
||||
;;; (¤ pthread-t CAP) CAP of pthread-t (¤ struct point) unsized
|
||||
;;; (¤ (* T) CAP) CAP of (* T) (¤ const mytype) unsized
|
||||
;;; (¤ * size-t) unsized
|
||||
;;; (¤ int const N) N of const int (¤ * size-t) unsized
|
||||
;;;
|
||||
;;; TYPE is the whole `(¤ ...)' form.
|
||||
(define (array-bound? type)
|
||||
@@ -426,8 +426,13 @@
|
||||
(cond
|
||||
((not (symbol? bound)) #t)
|
||||
((or (memq bound +c-specifiers+) (memq bound +c-qualifiers+)) #f)
|
||||
(else (not (or (memq preceding '(* struct union enum))
|
||||
(memq preceding +c-qualifiers+))))))))
|
||||
((memq preceding '(* struct union enum)) #f)
|
||||
((memq preceding +c-qualifiers+)
|
||||
(any (lambda (word)
|
||||
(not (or (memq word '(* struct union enum))
|
||||
(memq word +c-qualifiers+))))
|
||||
(drop-right (cdr type) 2)))
|
||||
(else #t)))))
|
||||
|
||||
;;; What one element of a written array type is:
|
||||
;;; (¤ struct point 2) -> (struct point), (¤ * const char 2) -> (* const char)
|
||||
|
||||
Reference in New Issue
Block a user