Permalink
Browse files

trying again. we're going to keep new(), but:

  - generate inline code for new for performance
  - instantiate constructors along with their type in a more intuitive way
  - allow omitting arguments to new() to make uninitialized instances
  • Loading branch information...
1 parent d9f8ddd commit a55300176924e4352d07ad8e5a3a82f4199b57d9 @JeffBezanson JeffBezanson committed Jun 23, 2011
Showing with 53 additions and 35 deletions.
  1. +9 −6 doc/todo
  2. +29 −29 src/julia-syntax.scm
  3. +15 −0 src/match.scm
View
@@ -472,10 +472,6 @@ issues 6/21/10
* hooking up the pieces to use inferred types in the compiler
. we could save tuples (type_sig, AST) as s-exprs to avoid the cost
of lowering and type inference.
-- make type parameters visible to user-defined constructors
- . in generic case, instantiate all signatures with the typevar mapping
- that's currently created in add_generic_ctor
- . in non-generic case, add static parameters
* limit specialization of vararg functions
* method cache invalidation, also invalidate all caches once type inference
is available (after loading start.j)
@@ -513,7 +509,7 @@ issues 6/21/10
* << >> precedence
* break in value position
* tintersect((Int,Int),(T,T)) is wrong when T is T<:Int, ok when T<:Any
-- use nothing instead of () where appropriate
+* use nothing instead of () where appropriate
method ordering tests:
//: (T,T) before (Any,Any)
@@ -615,7 +611,14 @@ default ctors would be
inside type block: Rational(n, d) = (this.num=n;this.den=d)
outside: Rational{T}(n::T, d::T) = Rational{T}(n,d)
-- rename clone to new
+ * frontend changes
+ - convert all .j to new constructors
+ - instantiate constructor factory with type's static parameters, and
+ type's name bound to the instantiated type also as a static parameter
+ - implement (new ...) in codegen
+ - (new ...) case in inference
+ - null check in field access
+ * rename clone
* type=>abstract, struct=>type
* do something about "global X+=1"
View
@@ -233,8 +233,7 @@
`(function (call ,name ,@(map (lambda (n t) `(:: ,n ,t))
field-names field-types))
(block
- ,@(map (lambda (n) `(= (|.| this ,n) ,n))
- field-names))))
+ (call new ,@field-names))))
(define (default-outer-ctor name field-names field-types params)
`(function (call (curly ,name ,@params)
@@ -243,38 +242,38 @@
(block
(call (curly ,name ,@params) ,@field-names))))
-(define (rewrite-ctor ctor Tname params)
- ;; insert definition and return of "this" variable
+(define (new-call Texpr args field-names)
+ (let ((g (gensy)))
+ `(block (= ,g (new ,Texpr))
+ ,@(map (lambda (fld val) `(= (|.| ,g ,fld) ,val))
+ field-names args)
+ ,g)))
+
+(define (rewrite-ctor ctor Tname params field-names)
(define (ctor-body body)
- `(block ;; hack - make the type parameters "global" so they can be
- ;; shadowed by static parameters
- (global (vars ,Tname ,@params))
- (= this (new ,(if (null? params)
- Tname
- `(curly ,Tname ,@params))))
- ,body
- (return this)))
- ;; TODO: error if return occurs in constructor
+ `(block ;; make type name global
+ (global ,Tname)
+ (pattern-replace (pattern-set
+ (pattern-lambda
+ (call (-/ new) . args)
+ (new-call (if (null? params)
+ Tname
+ `(curly ,Tname ,@params))
+ args
+ field-names)))
+ body)))
(or
((pattern-lambda (function (call name . sig) body)
- (if (eq? name Tname)
- `(function ,(cadr ctor) ,(ctor-body body))
- ctor))
+ `(function ,(cadr ctor) ,(ctor-body body)))
ctor)
((pattern-lambda (= (call name . sig) body)
- (if (eq? name Tname)
- `(= ,(cadr ctor) ,(ctor-body body))
- ctor))
+ `(= ,(cadr ctor) ,(ctor-body body)))
ctor)
((pattern-lambda (function (call (curly name . p) . sig) body)
- (if (eq? name Tname)
- `(function ,(cadr ctor) ,(ctor-body body))
- ctor))
+ `(function ,(cadr ctor) ,(ctor-body body)))
ctor)
((pattern-lambda (= (call (curly name . p) . sig) body)
- (if (eq? name Tname)
- `(= ,(cadr ctor) ,(ctor-body body))
- ctor))
+ `(= ,(cadr ctor) ,(ctor-body body)))
ctor)
ctor))
@@ -300,7 +299,7 @@
(call (top new_struct_fields)
,name ,super (tuple ,@field-types))
,@(map (lambda (c)
- (rewrite-ctor c name '()))
+ (rewrite-ctor c name '() field-names))
defs2)
(null))
`(block
@@ -312,12 +311,13 @@
(quote ,name)
(tuple ,@params)
(tuple ,@(map (lambda (x) `',x) field-names))
- (lambda (,name ,@params)
+ (lambda (,name)
(scope-block
(block
,@(map (lambda (c)
- (rewrite-ctor c name params))
- defs2))))))
+ (rewrite-ctor c name params field-names))
+ defs2)
+ ,name)))))
(call (top new_struct_fields)
,name ,super (tuple ,@field-types))))
,@(symbols->typevars params bounds))
View
@@ -173,6 +173,21 @@
; expr changed; iterate
(pattern-expand plist enew)))))
+;; finds and replaces pattern matches with their expansions
+;; one pass, does not expand recursively
+(define (pattern-replace plist expr)
+ (if (or (not (pair? expr)) (eq? (car expr) 'quote))
+ expr
+ (let ((enew (apply-patterns plist expr)))
+ (if (eq? enew expr)
+ ;; expr didn't change; move to subexpressions
+ (map (lambda (subex)
+ (if (not (pair? subex))
+ subex
+ (pattern-replace plist subex)))
+ expr)
+ enew))))
+
(define-macro (pattern-set . pats)
; (pattern-lambda (x ...) ...) => x
(define (pl-head p) (car (cadr p)))

0 comments on commit a553001

Please sign in to comment.