Add encapsulation, sum types, type constraints and all the required machinery. Also -> threading macro #64
14
Makefile
@@ -20,8 +20,16 @@ CSC_FLAGS += -K prefix -static
|
||||
prefix = /usr/local
|
||||
exec_prefix = $(prefix)
|
||||
bindir = $(exec_prefix)/bin
|
||||
datarootdir = $(prefix)/share
|
||||
datadir = $(datarootdir)
|
||||
# sexc looks for the standard library beside itself: next to the binary
|
||||
# in a work tree, and bindir/../share/sex once installed
|
||||
sexlibdir = $(datadir)/sex
|
||||
INSTALL = install
|
||||
INSTALL_PROGRAM = $(INSTALL)
|
||||
INSTALL_DATA = $(INSTALL) -m 644
|
||||
|
||||
SEX_LIB = $(wildcard lib/*.sex)
|
||||
|
||||
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
|
||||
|
||||
@@ -114,6 +122,7 @@ SEX_TEST_PROGRAMS = c99 \
|
||||
serialize \
|
||||
specs \
|
||||
sums \
|
||||
threading \
|
||||
type-shapes \
|
||||
unicode \
|
||||
unnamed-params \
|
||||
@@ -133,15 +142,18 @@ check run-tests: sexc sex-tests sextest
|
||||
|
||||
install: all installdirs
|
||||
$(INSTALL_PROGRAM) sexc $(DESTDIR)$(bindir)/sexc
|
||||
$(INSTALL_DATA) $(SEX_LIB) $(DESTDIR)$(sexlibdir)
|
||||
|
||||
install-strip:
|
||||
$(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' install
|
||||
|
||||
installdirs:
|
||||
$(INSTALL) -d $(DESTDIR)$(bindir)
|
||||
$(INSTALL) -d $(DESTDIR)$(bindir) $(DESTDIR)$(sexlibdir)
|
||||
|
||||
uninstall:
|
||||
rm -f $(DESTDIR)$(bindir)/sexc
|
||||
rm -f $(SEX_LIB:lib/%=$(DESTDIR)$(sexlibdir)/%)
|
||||
-rmdir $(DESTDIR)$(sexlibdir)
|
||||
|
||||
$(EGGS_STAMP) deps-update: export CHICKEN_EGG_CACHE := $(EGGS_DIR)/cache
|
||||
$(EGGS_STAMP) deps-update: export CHICKEN_INSTALL_REPOSITORY := $(EGGS_DIR)
|
||||
|
||||
59
Readme.org
@@ -45,7 +45,9 @@ tree's ~.eggs~ path):
|
||||
#+end_src
|
||||
|
||||
** Installation
|
||||
GNU directory variables: ~prefix~, ~exec_prefix~, ~bindir~, ~DESTDIR~.
|
||||
GNU directory variables: ~prefix~, ~exec_prefix~, ~bindir~, ~datadir~,
|
||||
~DESTDIR~. The binary goes to ~bindir~ and the standard library to
|
||||
~$(datadir)/sex~.
|
||||
|
||||
#+begin_src sh
|
||||
# default installation (/usr/local/bin/)
|
||||
@@ -132,17 +134,66 @@ Sex offers automatic kebabification of all symbols, i.e. no more
|
||||
ugly ~GL_ARRAY_BUFFER~ s in your code, they may be written in their
|
||||
proper form: ~GL-ARRAY-BUFFER~.
|
||||
|
||||
** Member access
|
||||
~(. x field ...)~ reads a path, and writes C's ~.~ or its ~->~ according
|
||||
to what it is reading from. Each step asks the type in hand, so one path
|
||||
may be both:
|
||||
|
||||
#+begin_src scheme
|
||||
(struct point ((x int) (y int)))
|
||||
(struct line ((a (* struct point)) (b (struct point))))
|
||||
|
||||
(. p x) ; p : (struct point) -> p.x
|
||||
(. pp x) ; pp : (* struct point) -> pp->x
|
||||
(. l a x) ; l.a is a pointer -> l.a->x
|
||||
(. lp b y) ; lp is, l.b is not -> lp->b.y
|
||||
#+end_src
|
||||
|
||||
A receiver whose type cannot be worked out --- something out of a header
|
||||
nothing has parsed --- is read as a value, with a warning saying so.
|
||||
|
||||
A method is ~(.name receiver args ...)~ and a field is ~(. receiver
|
||||
name)~, so the shape of a form says which of the two it is without
|
||||
knowing any types: ~(. a b c)~ is a path and ~(.b a c)~ is a call.
|
||||
|
||||
** Modules
|
||||
Each source file is a module. Module can provide public interface and
|
||||
be imported by using ~(import path/to/module)~ expression. Module
|
||||
search path consists of two parts: first is relative to the source
|
||||
being compiled location, and the second is ~SEX_MODULE_PATH~
|
||||
environment variable.
|
||||
search path has three parts: first is relative to the source being
|
||||
compiled location, then the ~SEX_MODULE_PATH~ environment variable,
|
||||
and last the standard library.
|
||||
|
||||
Module's public interface consists of everything declared
|
||||
~pub~. Structures, function, macros, types, variables can be
|
||||
public.
|
||||
|
||||
** Standard library
|
||||
~lib/~ holds the modules that come with the compiler. They are imported
|
||||
like any other, and being searched last, a module of your own by the
|
||||
same name wins over one of ours.
|
||||
|
||||
~sexc~ looks for them beside itself, so a work tree needs nothing set
|
||||
up; installed, ~$(bindir)/sexc~ finds them under ~$(prefix)/share/sex~.
|
||||
|
||||
There is one so far.
|
||||
|
||||
|
|
||||
*** threading-macros
|
||||
~->~ passes a value through a chain of forms, each step taking what came
|
||||
|
pkulev
commented
I would look ahead a little and avoided introducing the whole standard library in the README. It'll better to create separate org file and link it here. I would look ahead a little and avoided introducing the whole standard library in the README. It'll better to create separate org file and link it here.
alex-eg
commented
Indeed. But I don't think that one file will be enough. Every module should have it's own file at least. Indeed. But I don't think that one file will be enough. Every module should have it's own file at least.
|
||||
before it as its first argument, so the chain reads in the order it
|
||||
happens instead of inside out. Clojure spells it this way and this is
|
||||
the same thing:
|
||||
|
||||
#+begin_src scheme
|
||||
(import threading-macros)
|
||||
|
||||
(-> 5 double-it (add 3) double-it) ; (double-it (add (double-it 5) 3))
|
||||
(-> lp (. a x)) ; lp->a->x
|
||||
(-> q .length-sq) ; a method is a step like any other
|
||||
#+end_src
|
||||
|
||||
A step that is not a list is a call of one argument: ~(-> x f)~ is
|
||||
~(f x)~.
|
||||
|
||||
** Read-time feature expressions
|
||||
Sex is able to use ~#+~ and ~#-~ for conditional compilation: the form that
|
||||
follows is kept only when the feature expression is true, and otherwise
|
||||
|
||||
@@ -9,25 +9,25 @@
|
||||
(fn-name (cat 'make-list- type)))
|
||||
`(,@(if is-public? '(pub) '()) fn ,fn-name () (* ,list-type)
|
||||
(var list (* ,list-type) (cast (malloc (sizeof ,list-type)) (* ,list-type)))
|
||||
(= (-> list next) NULL)
|
||||
(= (. list next) NULL)
|
||||
(return list))))
|
||||
|
||||
(pub defmacro (add-value-list-T type is-public?)
|
||||
(let ((list-type (list 'struct (cat 'list- type)))
|
||||
(fn-name (cat 'add-value-list- type)))
|
||||
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list * ,list-type) (value ,type)) void
|
||||
(while (!= (-> list next) NULL)
|
||||
(= list (-> list next)))
|
||||
(= (-> list next) (,(cat 'make-list- type)))
|
||||
(= (-> list value) value))))
|
||||
(while (!= (. list next) NULL)
|
||||
(= list (. list next)))
|
||||
(= (. list next) (,(cat 'make-list- type)))
|
||||
(= (. list value) value))))
|
||||
|
||||
(pub defmacro (length-list-T type is-public?)
|
||||
(let ((fn-name (cat 'length-list- type))
|
||||
(list-type (list 'struct (cat 'list- type))))
|
||||
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list ,(cons '* list-type))) size-t
|
||||
(var n size-t 0)
|
||||
(while (!= (-> list next) NULL)
|
||||
(= list (-> list next))
|
||||
(while (!= (. list next) NULL)
|
||||
(= list (. list next))
|
||||
(++ n))
|
||||
(return n))))
|
||||
|
||||
@@ -35,14 +35,14 @@
|
||||
`(,@(if is-public? '(pub) '()) fn ,(cat 'is-empty-list- type)
|
||||
((list ,(list '* 'struct (cat 'list- type))))
|
||||
bool
|
||||
(return (== (-> list next) NULL))))
|
||||
(return (== (. list next) NULL))))
|
||||
|
||||
(pub defmacro (list-for-each list-type list-var elt-type elt-var what-do)
|
||||
(let ((list-var-2 (cat list-var '-2)))
|
||||
`(do
|
||||
(var ,list-var-2 (* ,list-type) ,list-var)
|
||||
(var ,elt-var ,elt-type (-> ,list-var-2 value))
|
||||
(while (!= (-> ,list-var-2 next) NULL)
|
||||
(var ,elt-var ,elt-type (. ,list-var-2 value))
|
||||
(while (!= (. ,list-var-2 next) NULL)
|
||||
,what-do
|
||||
(= ,list-var-2 (-> ,list-var-2 next))
|
||||
(= ,elt-var (-> ,list-var-2 value))))))
|
||||
(= ,list-var-2 (. ,list-var-2 next))
|
||||
(= ,elt-var (. ,list-var-2 value))))))
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
((* char) "%s")
|
||||
(else (error "serialize-struct: unsupported field type"
|
||||
type-name name type))))
|
||||
(-> v ,name))))))
|
||||
(. v ,name))))))
|
||||
(if (not printers)
|
||||
(error "serialize-struct: no such struct" type-name)
|
||||
`(pub fn ,(cat 'serialize- type-name)
|
||||
|
||||
@@ -190,6 +190,10 @@ forms, and what remains."
|
||||
;; (dot-access obj field ...) -> obj.field... member access. `%.'
|
||||
;; is the fmt-c directive for the `.' operator (see sex-fmt-c).
|
||||
(('dot-access . rest) (cons '%. (map walk-expr rest)))
|
||||
;; `%->' is the C arrow. Nothing writes it: it is what a member
|
||||
;; path lowers to where the thing it reads from is a pointer, the
|
||||
;; name `->' itself being the threading macro
|
||||
|
pkulev
commented
Why C writer would know anything about what you do with the sex standard library? I suppose that mention should be rather deleted. Why C writer would know anything about what you do with the sex standard library? I suppose that mention should be rather deleted.
alex-eg
commented
Well... Because it's a name clash with very well known and widely used bit of C syntax. So one might want to know why it's missing from Sex, while most of other semantics is accounted for. Well... Because it's a name clash with very well known and widely used bit of C syntax. So one might want to know why it's missing from Sex, while most of other semantics is accounted for.
However I don't have strong preference, might as well drop the comment.
|
||||
(('%-> . rest) (cons '%-> (map walk-expr rest)))
|
||||
(('var . _) (walk-var form))
|
||||
;; An expression has no room for a statement, so a comment in a
|
||||
;; cast is dropped rather than relocated.
|
||||
|
||||
20
lib/threading-macros.sex
Normal file
@@ -0,0 +1,20 @@
|
||||
;;; Threading macros
|
||||
;;;
|
||||
;;; `->' passes a value through a chain of forms, each step taking what
|
||||
;;; came before it as its first argument, so a chain of calls reads in
|
||||
;;; the order it happens rather than inside out. Clojure spells it this
|
||||
;;; way and this is the same thing:
|
||||
;;;
|
||||
;;; (-> 5 double-it (add 3)) == (add (double-it 5) 3)
|
||||
;;;
|
||||
;;; A step that is not a list is a call of one argument, so `(-> x f)'
|
||||
;;; is `(f x)' and a method is a step like any other.
|
||||
|
||||
(pub defmacro (-> x . steps)
|
||||
(let thread ((acc x) (steps steps))
|
||||
(if (null? steps)
|
||||
acc
|
||||
(thread (if (pair? (car steps))
|
||||
(cons (car (car steps)) (cons acc (cdr (car steps))))
|
||||
(list (car steps) acc))
|
||||
(cdr steps)))))
|
||||
16
semen.scm
@@ -777,7 +777,7 @@
|
||||
(cond ((symbol? expr) #t)
|
||||
((not (pair? expr)) #f)
|
||||
(else (case (car expr)
|
||||
((dot-access ->) (simple-path? (second expr)))
|
||||
((dot-access %->) (simple-path? (second expr)))
|
||||
((*) (and (= 2 (length expr)) (simple-path? (second expr))))
|
||||
(else #f)))))
|
||||
|
||||
@@ -907,7 +907,7 @@
|
||||
(unless (or type told?)
|
||||
(sex-warning form "cannot type this; `.' reads it as a value"
|
||||
base))
|
||||
(step (list (if target '-> 'dot-access) expr (car fields))
|
||||
(step (list (if target '%-> 'dot-access) expr (car fields))
|
||||
(field-type (or target type) (car fields))
|
||||
(cdr fields)
|
||||
(or told? (not type))))))))
|
||||
@@ -917,7 +917,7 @@
|
||||
((vector? expr) #t) ; a compound literal is one
|
||||
((not (pair? expr)) #f)
|
||||
(else (case (car expr)
|
||||
((dot-access -> ¤) #t)
|
||||
((dot-access %-> ¤) #t)
|
||||
((*) (= 2 (length expr)))
|
||||
(else #f)))))
|
||||
|
||||
@@ -991,7 +991,7 @@
|
||||
(arithmetic-type expr env)))
|
||||
((dot-access) (member-path-type (expression-type (second expr) env)
|
||||
(cddr expr)))
|
||||
((->) (member-path-type (pointer-target (expression-type (second expr) env))
|
||||
((%->) (member-path-type (pointer-target (expression-type (second expr) env))
|
||||
(cddr expr)))
|
||||
((cast) (and (= 3 (length expr)) (third expr)))
|
||||
((sizeof) 'size-t)
|
||||
@@ -1351,7 +1351,7 @@
|
||||
(iota (length (second type))))))
|
||||
(unless (hash-table-exists? +closure-conversions+ convert)
|
||||
(hash-table-set! +closure-conversions+ convert #t)
|
||||
(let ((call `((-> ƛcaptures f) ,@(map first params))))
|
||||
(let ((call `((%-> ƛcaptures f) ,@(map first params))))
|
||||
(for-each
|
||||
(lambda (emitted)
|
||||
(set! *pending-closure-structs*
|
||||
@@ -1369,7 +1369,7 @@
|
||||
(= (dot-access ƛc code) ,thunk)
|
||||
(var ƛcaptures (* (struct ,record))
|
||||
(cast (& (dot-access ƛc env)) (* (struct ,record))))
|
||||
(= (-> ƛcaptures f) f)
|
||||
(= (%-> ƛcaptures f) f)
|
||||
(return ƛc))))))
|
||||
convert))
|
||||
|
||||
@@ -1487,7 +1487,7 @@
|
||||
`((var ƛcaptures (* (struct ,record)) ƛe)
|
||||
,@(map (lambda (cap)
|
||||
`(var ,(first cap) ,(second cap)
|
||||
(-> ƛcaptures ,(first cap))))
|
||||
(%-> ƛcaptures ,(first cap))))
|
||||
caps)))
|
||||
,@body)
|
||||
|
||||
@@ -1505,7 +1505,7 @@
|
||||
`((var ƛcaptures (* (struct ,record))
|
||||
(cast (& (dot-access ƛc env)) (* (struct ,record))))
|
||||
,@(map (lambda (cap)
|
||||
`(= (-> ƛcaptures ,(first cap)) ,(first cap)))
|
||||
`(= (%-> ƛcaptures ,(first cap)) ,(first cap)))
|
||||
caps)))
|
||||
(return ƛc))))))))
|
||||
(else (sex-error form "malformed closure" form))))
|
||||
|
||||
@@ -306,8 +306,6 @@
|
||||
(cond
|
||||
((eq? (car x) (string->symbol "."))
|
||||
((apply c-op "." (cdr x)) st))
|
||||
((eq? (car x) (string->symbol "->"))
|
||||
((apply c-op "->" (cdr x)) st))
|
||||
((eq? (car x) (string->symbol "++"))
|
||||
((apply c-op "++" (cdr x)) st))
|
||||
((eq? (car x) (string->symbol "--"))
|
||||
|
||||
@@ -43,7 +43,20 @@
|
||||
|
||||
(define (get-module-paths)
|
||||
(cons (current-directory)
|
||||
+persistent-module-paths+))
|
||||
(append +persistent-module-paths+ (standard-library-paths))))
|
||||
|
||||
;;; Where the modules shipped with the compiler live. Asked of the
|
||||
;;; binary rather than baked in when it is built: in a work tree sexc
|
||||
;;; sits beside lib/, and installed it sits in bindir with the modules
|
||||
;;; under ../share/sex. Last in the search order, so a module of one's
|
||||
;;; own wins over one of ours by the same name
|
||||
(define (standard-library-paths)
|
||||
(let ((binary (executable-pathname)))
|
||||
(if binary
|
||||
(let ((bin (pathname-directory binary)))
|
||||
(list (make-pathname bin "lib")
|
||||
(make-pathname (list (pathname-directory bin) "share") "sex")))
|
||||
(list))))
|
||||
|
||||
(define (locate-module name)
|
||||
;; Module locations: relative to file being compiled, or in what was
|
||||
|
||||
@@ -684,7 +684,12 @@ compiles."
|
||||
(test-assert "a type we cannot work out is read as a value"
|
||||
(emits? (in-line "(= (. (guess) x) 1)") "guess().x = 1"))
|
||||
(test-assert "and said so"
|
||||
(warns? (in-line "(= (. (guess) x) 1)") "cannot type this")))
|
||||
(warns? (in-line "(= (. (guess) x) 1)") "cannot type this"))
|
||||
;; 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
|
||||
(test-assert "a bare -> is not the arrow"
|
||||
(emits? (in-line "(= (-> pp x) 1)") "->(pp, x) = 1")))
|
||||
|
||||
;; Toplevel forms used to be separated by a blank line unconditionally,
|
||||
;; which spread a block of adjacent typedefs over twice the lines its
|
||||
|
||||
@@ -58,7 +58,7 @@
|
||||
|
||||
(ops vec2
|
||||
(pub fn manhattan ((self (* const vec2))) int
|
||||
(return (+ (-> self x) (-> self y)))))
|
||||
(return (+ (. self x) (. self y)))))
|
||||
|
||||
;;; A pub sum is pasted whole, so the importer builds its own copy of
|
||||
;;; the declarations -- and of the constructors, which is why those are
|
||||
@@ -77,7 +77,7 @@
|
||||
|
||||
(pub realize (printable vec2)
|
||||
(fn show ((self (* const vec2))) void
|
||||
(printf "vec2(%d %d)" (-> self x) (-> self y))))
|
||||
(printf "vec2(%d %d)" (. self x) (. self y))))
|
||||
|
||||
;;; Not `pub': invisible to importers, and static in the generated C.
|
||||
(fn unused-helper () void
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
;; a compound literal is an lvalue, so its address can be taken --
|
||||
;; until the end of the enclosing block, and no longer
|
||||
(var pp (* struct point) (& #(struct point : 9 9)))
|
||||
(printf "through a pointer %d\n" (-> pp x))
|
||||
(printf "through a pointer %d\n" (. pp x))
|
||||
|
||||
;; an array literal decays the way an array does
|
||||
(var a (* int) #([int 3] : 10 20 30))
|
||||
|
||||
@@ -29,14 +29,14 @@
|
||||
;;; the self-application hidden inside
|
||||
(fn fix ((step (* (struct rec)))) (closure ((int)) int)
|
||||
(return (closure ((n int)) int (step)
|
||||
(return ((-> step f) step n)))))
|
||||
(return ((. step f) step n)))))
|
||||
|
||||
(pub fn main () int
|
||||
(var fac-knot (struct rec))
|
||||
(= (. fac-knot f)
|
||||
(closure ((self (* (struct rec))) (n int)) int ()
|
||||
(if (<= n 1) (return 1))
|
||||
(return (* n ((-> self f) self (- n 1))))))
|
||||
(return (* n ((. self f) self (- n 1))))))
|
||||
|
||||
;; the knot applied to itself directly, without fix
|
||||
(printf "direct: %d\n" ((. fac-knot f) (& fac-knot) 5))
|
||||
@@ -45,8 +45,8 @@
|
||||
(= (. fib-knot f)
|
||||
(closure ((self (* (struct rec))) (n int)) int ()
|
||||
(if (< n 2) (return n))
|
||||
(return (+ ((-> self f) self (- n 1))
|
||||
((-> self f) self (- n 2))))))
|
||||
(return (+ ((. self f) self (- n 1))
|
||||
((. self f) self (- n 2))))))
|
||||
|
||||
;; one combinator, two different recursions
|
||||
(var fac (closure ((int)) int) (fix (& fac-knot)))
|
||||
|
||||
@@ -43,8 +43,8 @@
|
||||
(var m (struct point))
|
||||
;; No `_' here: `m' has no initializer to infer from. Inference fills
|
||||
;; in a type, it does not invent one.
|
||||
(= (. m x) (/ (+ (-> a x) (-> b x)) 2))
|
||||
(= (. m y) (/ (+ (-> a y) (-> b y)) 2))
|
||||
(= (. m x) (/ (+ (. a x) (. b x)) 2))
|
||||
(= (. m y) (/ (+ (. a y) (. b y)) 2))
|
||||
(return m))
|
||||
|
||||
;;; The closure's type is written once, in the signature; `_' reads it
|
||||
@@ -118,7 +118,7 @@
|
||||
(var p (* _) (& origin))
|
||||
|
||||
;; Member access reads the same type database the macros do.
|
||||
(var x _ (-> p x))
|
||||
(var x _ (. p x))
|
||||
(printf "%d\n" x)
|
||||
|
||||
;; A call into a function Sex has actually parsed: the return type is
|
||||
|
||||
@@ -9,25 +9,25 @@
|
||||
(fn-name (cat 'make-list- type)))
|
||||
`(,@(if is-public? '(pub) '()) fn ,fn-name () (* ,list-type)
|
||||
(var list (* ,list-type) (cast (malloc (sizeof ,list-type)) (* ,list-type)))
|
||||
(= (-> list next) NULL)
|
||||
(= (. list next) NULL)
|
||||
(return list))))
|
||||
|
||||
(pub defmacro (add-value-list-T type is-public?)
|
||||
(let ((list-type (list 'struct (cat 'list- type)))
|
||||
(fn-name (cat 'add-value-list- type)))
|
||||
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list * ,list-type) (value ,type)) void
|
||||
(while (!= (-> list next) NULL)
|
||||
(= list (-> list next)))
|
||||
(= (-> list next) (,(cat 'make-list- type)))
|
||||
(= (-> list value) value))))
|
||||
(while (!= (. list next) NULL)
|
||||
(= list (. list next)))
|
||||
(= (. list next) (,(cat 'make-list- type)))
|
||||
(= (. list value) value))))
|
||||
|
||||
(pub defmacro (length-list-T type is-public?)
|
||||
(let ((fn-name (cat 'length-list- type))
|
||||
(list-type (list 'struct (cat 'list- type))))
|
||||
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list ,(cons '* list-type))) size-t
|
||||
(var n size-t 0)
|
||||
(while (!= (-> list next) NULL)
|
||||
(= list (-> list next))
|
||||
(while (!= (. list next) NULL)
|
||||
(= list (. list next))
|
||||
(++ n))
|
||||
(return n))))
|
||||
|
||||
@@ -35,14 +35,14 @@
|
||||
`(,@(if is-public? '(pub) '()) fn ,(cat 'is-empty-list- type)
|
||||
((list ,(list '* 'struct (cat 'list- type))))
|
||||
bool
|
||||
(return (== (-> list next) NULL))))
|
||||
(return (== (. list next) NULL))))
|
||||
|
||||
(pub defmacro (list-for-each list-type list-var elt-type elt-var what-do)
|
||||
(let ((list-var-2 (cat list-var '-2)))
|
||||
`(do
|
||||
(var ,list-var-2 (* ,list-type) ,list-var)
|
||||
(var ,elt-var ,elt-type (-> ,list-var-2 value))
|
||||
(while (!= (-> ,list-var-2 next) NULL)
|
||||
(var ,elt-var ,elt-type (. ,list-var-2 value))
|
||||
(while (!= (. ,list-var-2 next) NULL)
|
||||
,what-do
|
||||
(= ,list-var-2 (-> ,list-var-2 next))
|
||||
(= ,elt-var (-> ,list-var-2 value))))))
|
||||
(= ,list-var-2 (. ,list-var-2 next))
|
||||
(= ,elt-var (. ,list-var-2 value))))))
|
||||
|
||||
@@ -23,12 +23,12 @@
|
||||
(ops vec2
|
||||
(pub fn length ((self (* const vec2))) float
|
||||
"Pointer self, so (.length v) has to take the address."
|
||||
(return (sqrtf (+ (* (-> self x) (-> self x))
|
||||
(* (-> self y) (-> self y))))))
|
||||
(return (sqrtf (+ (* (. self x) (. self x))
|
||||
(* (. self y) (. self y))))))
|
||||
|
||||
(fn scale ((self (* vec2)) (k float)) void
|
||||
(*= (-> self x) k)
|
||||
(*= (-> self y) k))
|
||||
(*= (. self x) k)
|
||||
(*= (. self y) k))
|
||||
|
||||
(fn sum-of ((self vec2)) float
|
||||
;; value self, so (.sum-of p) has to dereference
|
||||
@@ -38,7 +38,7 @@
|
||||
|
||||
(ops raw
|
||||
(fn total ((self (* const (struct raw)))) int
|
||||
(return (+ (-> self a) (-> self b)))))
|
||||
(return (+ (. self a) (. self b)))))
|
||||
|
||||
(pub fn main () int
|
||||
(var v vec2 #(vec2 : 3.0 4.0))
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
(struct raw ((a int) (b int)))
|
||||
|
||||
(fn shift ((p (* point)) (dx int) (dy int)) void
|
||||
(+= (-> p x) dx)
|
||||
(+= (-> p y) dy))
|
||||
(+= (. p x) dx)
|
||||
(+= (. p y) dy))
|
||||
|
||||
(fn area ((p point)) int
|
||||
(return (* (. p x) (. p y))))
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
((* const char) "%s")
|
||||
(else (error "print-struct: unsupported field type"
|
||||
name type))))
|
||||
(-> v ,name))))))
|
||||
(. v ,name))))))
|
||||
(if (not printers)
|
||||
(error "print-struct: no such struct" type-name)
|
||||
`(fn ,(cat 'print- type-name) ((v (* const struct ,type-name))) void
|
||||
|
||||
@@ -30,15 +30,15 @@
|
||||
|
||||
(realize (printable money)
|
||||
(fn show ((self (* const money))) void
|
||||
(printf "$%d.%02d" (/ (-> self cents) 100) (% (-> self cents) 100))))
|
||||
(printf "$%d.%02d" (/ (. self cents) 100) (% (. self cents) 100))))
|
||||
|
||||
(realize (printable point)
|
||||
(fn show ((self (* const point))) void
|
||||
(printf "(%d, %d)" (-> self x) (-> self y))))
|
||||
(printf "(%d, %d)" (. self x) (. self y))))
|
||||
|
||||
(realize (ord money)
|
||||
(fn less ((a (* const money)) (b (* const money))) bool
|
||||
(return (< (-> a cents) (-> b cents)))))
|
||||
(return (< (. a cents) (. b cents)))))
|
||||
|
||||
;;; A bounded generic: the macro refuses before it expands
|
||||
(defmacro (max-of type)
|
||||
|
||||
50
tests/sex-programs/threading.sex
Normal file
@@ -0,0 +1,50 @@
|
||||
(input)
|
||||
|
pkulev
commented
Dunno, maybe stdlib tests deserve their own space. Dunno, maybe stdlib tests deserve their own space.
alex-eg
commented
They do, but I think we should do it later. Probably also groom sex-programs too, or even eliminate it altogether. I envision something like gcc's test suite, or autotools' feature test programs. In any case, for now test programs will do. They do, but I think we should do it later. Probably also groom sex-programs too, or even eliminate it altogether. I envision something like gcc's test suite, or autotools' feature test programs. In any case, for now test programs will do.
|
||||
(output "arithmetic: 26"
|
||||
"a bare step needs no parens: 20"
|
||||
"through a path: 1 4"
|
||||
"mixed with a method: 25"
|
||||
"one step is just the value: 7")
|
||||
(return 0)
|
||||
|
||||
;;; `->' used to be C's arrow. The arrow is what `.' writes now when
|
||||
|
pkulev
commented
Why that story is important? Why that story is important?
The language is in alpha stage, everything changes from PR to PR, no need to write it down everywhere.
alex-eg
commented
I agree. I agree.
|
||||
;;; the thing it reads from is a pointer, which left the name free for
|
||||
;;; threading -- the first module of the standard library.
|
||||
;;;
|
||||
;;; What this pins is that a step may be a call, a bare name or a
|
||||
;;; method, that the value is threaded into the *first* argument, and
|
||||
;;; that the compiler's own generated arrows survived the rename --
|
||||
;;; `ops' below lifts a method, which is where the captures live.
|
||||
|
||||
(include stdio.h)
|
||||
(import threading-macros)
|
||||
|
||||
(struct point ((x int) (y int)))
|
||||
(struct line ((a (* struct point)) (b (struct point))))
|
||||
|
||||
(fn double-it ((n int)) int
|
||||
(return (* n 2)))
|
||||
|
||||
(fn add ((n int) (m int)) int
|
||||
(return (+ n m)))
|
||||
|
||||
(ops point
|
||||
(pub fn length-sq ((self (* const struct point))) int
|
||||
(return (+ (* (. self x) (. self x))
|
||||
(* (. self y) (. self y))))))
|
||||
|
||||
(pub fn main () int
|
||||
;; (add (double-it 5) 3) doubled: inside out, written outside in
|
||||
(printf "arithmetic: %d\n" (-> 5 double-it (add 3) double-it))
|
||||
(printf "a bare step needs no parens: %d\n" (-> 5 double-it double-it))
|
||||
|
||||
(var p (struct point) #(1 2))
|
||||
(var l (struct line) #(struct line : .a (& p) .b #(3 4)))
|
||||
(var lp (* struct line) (& l))
|
||||
;; a member path is an ordinary step, and reads through both pointers
|
||||
(printf "through a path: %d %d\n" (-> lp (. a x)) (. lp b y))
|
||||
|
||||
(var q (struct point) #(3 4))
|
||||
(printf "mixed with a method: %d\n" (-> q .length-sq))
|
||||
|
||||
(printf "one step is just the value: %d\n" (-> 7))
|
||||
(return 0))
|
||||
@@ -42,7 +42,7 @@
|
||||
(printf "members: %d %g\n" px py)
|
||||
|
||||
(var pp _ (& p))
|
||||
(printf "pointers: %d %g\n" (-> pp x) (-> pp y))
|
||||
(printf "pointers: %d %g\n" (. pp x) (. pp y))
|
||||
|
||||
(var table (¤ int 3))
|
||||
(= (¤ table 0) 10)
|
||||
@@ -77,7 +77,7 @@
|
||||
|
||||
;; a wildcard inside a written type: only it is solved
|
||||
(var pp2 (* _) (& p))
|
||||
(printf "partial: %d %g\n" (-> pp2 x) (-> pp2 y))
|
||||
(printf "partial: %d %g\n" (. pp2 x) (. pp2 y))
|
||||
|
||||
;; C's usual arithmetic conversions, far enough to answer `_'
|
||||
(var d double 1.5)
|
||||
|
||||
That is a bit excessive. There will be others for sure.
True.