Skip to content

Commit

Permalink
parse const the same as global and local. fixes #7314
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jun 19, 2014
1 parent 9a97c7f commit 135f2fb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
31 changes: 17 additions & 14 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1053,17 +1053,28 @@
(let ((ex (parse-block s)))
(expect-end s)
`(let ,ex ,@binds))))
((global local)
((global local const)
(let* ((lno (input-port-line (ts:port s)))
(const (and (eq? (peek-token s) 'const)
(take-token s)))
(also (and (memq (peek-token s) '(global local const))
(take-token s)))
(expr (cons word
(map (lambda (x)
(short-form-function-loc x lno))
(parse-comma-separated-assignments s)))))
(if const
`(const ,expr)
expr)))
(if (or (eq? word 'const) (eq? also 'const))
(for-each (lambda (x)
(if (not (assignment? x))
(error "expected assignment after \"const\"")))
(cdr expr)))
(cond ((not also)
expr)
;; put const outside global or local decls
((and (eq? also 'const) (not (eq? word also)))
`(const ,expr))
((and (eq? word 'const) (not (eq? word also)))
`(const (,also ,@(cdr expr))))
(else
(error (string "invalid \"" word "\" declaration"))))))
((function macro)
(let* ((paren (eqv? (require-token s) #\())
(sig (parse-call s))
Expand Down Expand Up @@ -1156,14 +1167,6 @@
(list 'return '(null))
(list 'return (parse-eq s)))))
((break continue) (list word))
((const)
(let ((assgn (parse-eq s)))
(if (not (and (pair? assgn)
(or (eq? (car assgn) '=)
(eq? (car assgn) 'global)
(eq? (car assgn) 'local))))
(error "expected assignment after \"const\"")
`(const ,assgn))))
((module baremodule)
(let* ((name (parse-atom s))
(body (parse-block s)))
Expand Down
44 changes: 26 additions & 18 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,25 @@
(break ,bb)))
(else (map (lambda (x) (replace-return x bb ret retval)) e))))

(define (process-const e)
(case (car e)
((global local)
(expand-binding-forms
(qualified-const-expr (cdr e) `(const ,e))))
((=)
(let ((lhs (cadr e))
(rhs (caddr e)))
(let ((vars (if (and (pair? lhs) (eq? (car lhs) 'tuple))
(cdr lhs)
(list lhs))))
`(block
,.(map (lambda (v)
`(const ,(const-check-symbol (decl-var v))))
vars)
,(expand-binding-forms `(= ,lhs ,rhs))))))
(else
(error "assertion failure"))))

(define (expand-binding-forms e)
(cond
((atom? e) e)
Expand Down Expand Up @@ -1167,25 +1186,14 @@
(map expand-binding-forms e)))

((const)
(if (atom? (cadr e))
e
(case (car (cadr e))
((global local)
(expand-binding-forms
(qualified-const-expr (cdr (cadr e)) e)))
((=)
(let ((lhs (cadr (cadr e)))
(rhs (caddr (cadr e))))
(let ((vars (if (and (pair? lhs) (eq? (car lhs) 'tuple))
(cdr lhs)
(list lhs))))
`(block
,.(map (lambda (v)
`(const ,(const-check-symbol (decl-var v))))
vars)
,(expand-binding-forms `(= ,lhs ,rhs))))))
(cond ((and (length= e 2)
(or (atom? (cadr e))
(not (memq (car (cadr e)) '(global local =)))))
e)
((length= e 2)
(process-const (cadr e)))
(else
e))))
`(block ,@(map process-const (cdr e))))))

((local global)
(if (and (symbol? (cadr e)) (length= e 2))
Expand Down

0 comments on commit 135f2fb

Please sign in to comment.