Skip to content

Commit

Permalink
some front-end code cleanup (JuliaLang#31360)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Mar 18, 2019
1 parent 6aeb7ef commit 4dc1593
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 83 deletions.
3 changes: 1 addition & 2 deletions src/ast.scm
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,7 @@
(define (return? e) (and (pair? e) (eq? (car e) 'return)))
(define (complex-return? e) (and (return? e)
(let ((x (cadr e)))
(not (or (simple-atom? x) (ssavalue? x)
(equal? x '(null)))))))
(not (simple-atom? x)))))

(define (eq-sym? a b)
(or (eq? a b) (and (ssavalue? a) (ssavalue? b) (eqv? (cdr a) (cdr b)))))
Expand Down
135 changes: 69 additions & 66 deletions src/jlfrontend.scm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,58 @@
'(error "malformed expression"))))
thk))

;; parser entry points

;; parse one expression (if greedy) or atom, returning end position
(define (jl-parse-one s pos0 greedy)
(let ((inp (open-input-string s)))
(io.seek inp pos0)
(let ((expr (error-wrap (lambda ()
(if greedy
(julia-parse inp)
(julia-parse inp parse-atom))))))
(cons expr (io.pos inp)))))

(define (parse-all- io filename)
(unwind-protect
(with-bindings ((current-filename (symbol filename)))
(let ((stream (make-token-stream io)))
(let loop ((exprs '()))
(let ((lineno (error-wrap
(lambda ()
(skip-ws-and-comments (ts:port stream))
(input-port-line (ts:port stream))))))
(if (pair? lineno)
(cons 'toplevel (reverse! (cons lineno exprs)))
(let ((expr (error-wrap
(lambda ()
(julia-parse stream)))))
(if (eof-object? expr)
(cons 'toplevel (reverse! exprs))
(let* ((iserr (and (pair? expr) (eq? (car expr) 'error)))
;; for error, get most recent line number (#16720)
(lineno (if iserr (input-port-line io) lineno))
(next (list* expr
;; include filename in first line node
(if (null? exprs)
`(line ,lineno ,(symbol filename))
`(line ,lineno))
exprs)))
(if iserr
(cons 'toplevel (reverse! next))
(loop next))))))))))
(io.close io)))

;; parse all expressions in a string, the same way files are parsed
(define (jl-parse-all str filename)
(parse-all- (open-input-string str) filename))

(define (jl-parse-file filename)
(trycatch
(parse-all- (open-input-file filename) filename)
(lambda (e) #f)))

;; lowering entry points

;; return a lambda expression representing a thunk for a top-level expression
;; note: expansion of stuff inside module is delayed, so the contents obey
Expand All @@ -53,15 +105,15 @@
(cadadr (lam:body th))
`(thunk ,th))))))

(define *in-expand* #f)

(define (toplevel-only-expr? e)
(and (pair? e)
(or (memq (car e) '(toplevel line module import using export
error incomplete))
(and (memq (car e) '(global const)) (every symbol? (cdr e))
(every (lambda (x) (not (memq x '(true false)))) (cdr e))))))

(define *in-expand* #f)

(define (expand-toplevel-expr e file line)
(cond ((or (atom? e) (toplevel-only-expr? e))
(if (underscore-symbol? e)
Expand All @@ -75,6 +127,21 @@
(begin0 (expand-toplevel-expr-- e file line)
(set! *in-expand* last))))))

; expand a piece of raw surface syntax to an executable thunk
(define (jl-expand-to-thunk expr file line)
(error-wrap (lambda ()
(expand-toplevel-expr expr file line))))

(define (jl-expand-to-thunk-stmt expr file line)
(jl-expand-to-thunk (if (toplevel-only-expr? expr)
expr
`(block ,expr (null)))
file line))

(define (jl-expand-macroscope expr)
(error-wrap (lambda ()
(julia-expand-macroscope expr))))

;; construct default definitions of `eval` for non-bare modules
;; called by jl_eval_module_expr
(define (module-default-defs e)
Expand All @@ -97,70 +164,6 @@
(call (top include) ,name ,x)))))
'none 0))

;; parse one expression (if greedy) or atom, returning end position
(define (jl-parse-one s pos0 greedy)
(let ((inp (open-input-string s)))
(io.seek inp pos0)
(let ((expr (error-wrap (lambda ()
(if greedy
(julia-parse inp)
(julia-parse inp parse-atom))))))
(cons expr (io.pos inp)))))

(define (parse-all- io filename)
(unwind-protect
(with-bindings ((current-filename (symbol filename)))
(let ((stream (make-token-stream io)))
(let loop ((exprs '()))
(let ((lineno (error-wrap
(lambda ()
(skip-ws-and-comments (ts:port stream))
(input-port-line (ts:port stream))))))
(if (pair? lineno)
(cons 'toplevel (reverse! (cons lineno exprs)))
(let ((expr (error-wrap
(lambda ()
(julia-parse stream)))))
(if (eof-object? expr)
(cons 'toplevel (reverse! exprs))
(let* ((iserr (and (pair? expr) (eq? (car expr) 'error)))
;; for error, get most recent line number (#16720)
(lineno (if iserr (input-port-line io) lineno))
(next (list* expr
;; include filename in first line node
(if (null? exprs)
`(line ,lineno ,(symbol filename))
`(line ,lineno))
exprs)))
(if iserr
(cons 'toplevel (reverse! next))
(loop next))))))))))
(io.close io)))

;; parse all expressions in a string, the same way files are parsed
(define (jl-parse-all str filename)
(parse-all- (open-input-string str) filename))

(define (jl-parse-file filename)
(trycatch
(parse-all- (open-input-file filename) filename)
(lambda (e) #f)))

; expand a piece of raw surface syntax to an executable thunk
(define (jl-expand-to-thunk expr file line)
(error-wrap (lambda ()
(expand-toplevel-expr expr file line))))

(define (jl-expand-to-thunk-stmt expr file line)
(jl-expand-to-thunk (if (toplevel-only-expr? expr)
expr
`(block ,expr (null)))
file line))

(define (jl-expand-macroscope expr)
(error-wrap (lambda ()
(julia-expand-macroscope expr))))

; run whole frontend on a string. useful for testing.
(define (fe str)
(expand-toplevel-expr (julia-parse str) 'none 0))
Expand Down
6 changes: 0 additions & 6 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,6 @@

(define current-filename 'none)

(define-macro (with-normal-ops . body)
`(with-bindings ((range-colon-enabled #t)
(space-sensitive #f)
(where-enabled #t))
,@body))

(define-macro (with-normal-context . body)
`(with-bindings ((range-colon-enabled #t)
(space-sensitive #f)
Expand Down
3 changes: 1 addition & 2 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2961,7 +2961,7 @@ f(x) = yt(x)
(Set '(quote top core line inert local local-def unnecessary copyast
meta inbounds boundscheck loopinfo decl aliasscope popaliasscope
struct_type abstract_type primitive_type thunk with-static-parameters
implicit-global global globalref outerref const-if-global
global globalref outerref const-if-global
const null ssavalue isdefined toplevel module lambda error
gc_preserve_begin gc_preserve_end import using export)))

Expand Down Expand Up @@ -3895,7 +3895,6 @@ f(x) = yt(x)
((moved-local)
(set-car! (lam:vinfo lam) (append (car (lam:vinfo lam)) `((,(cadr e) Any 2))))
#f)
((implicit-global) #f)
((const)
(if (local-in? (cadr e) lam)
(error (string "unsupported `const` declaration on local variable" (format-loc current-loc)))
Expand Down
7 changes: 0 additions & 7 deletions src/utils.scm
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@
((f (car xs)) (cons (car xs) (take-while f (cdr xs))))
(else '())))

(define (without alst remove)
(cond ((null? alst) '())
((null? remove) alst)
((memq (caar alst) remove) (without (cdr alst) remove))
(else (cons (car alst)
(without (cdr alst) remove)))))

(define (caddddr x) (car (cdr (cdr (cdr (cdr x))))))

(define (table.clone t)
Expand Down

0 comments on commit 4dc1593

Please sign in to comment.