Skip to content

Commit 305b103

Browse files
authored
fix var"true" and var"false" (#34077)
This changes the internal representation of booleans in the front end to `(true)` and `(false)` to make them easier to distinguish from identifiers.
1 parent f5a0624 commit 305b103

File tree

7 files changed

+73
-75
lines changed

7 files changed

+73
-75
lines changed

src/ast.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,13 +478,8 @@ static jl_value_t *scm_to_julia_(fl_context_t *fl_ctx, value_t e, jl_module_t *m
478478
return (jl_value_t*)jl_box_int32((int32_t)i64);
479479
#endif
480480
}
481-
if (issymbol(e)) {
482-
if (e == jl_ast_ctx(fl_ctx)->true_sym)
483-
return jl_true;
484-
else if (e == jl_ast_ctx(fl_ctx)->false_sym)
485-
return jl_false;
481+
if (issymbol(e))
486482
return (jl_value_t*)scmsym_to_julia(fl_ctx, e);
487-
}
488483
if (fl_isstring(fl_ctx, e))
489484
return jl_pchar_to_string((char*)cvalue_data(e), cvalue_len(e));
490485
if (iscons(e) || e == fl_ctx->NIL) {
@@ -501,6 +496,10 @@ static jl_value_t *scm_to_julia_(fl_context_t *fl_ctx, value_t e, jl_module_t *m
501496
return jl_box_slotnumber(numval(car_(cdr_(e))));
502497
else if (hd == jl_ast_ctx(fl_ctx)->null_sym && llength(e) == 1)
503498
return jl_nothing;
499+
else if (hd == jl_ast_ctx(fl_ctx)->true_sym && llength(e) == 1)
500+
return jl_true;
501+
else if (hd == jl_ast_ctx(fl_ctx)->false_sym && llength(e) == 1)
502+
return jl_false;
504503
}
505504
if (issymbol(hd))
506505
sym = scmsym_to_julia(fl_ctx, hd);
@@ -643,9 +642,9 @@ static int julia_to_scm_noalloc1(fl_context_t *fl_ctx, jl_value_t *v, value_t *r
643642
else if (jl_is_symbol(v))
644643
*retval = symbol(fl_ctx, jl_symbol_name((jl_sym_t*)v));
645644
else if (v == jl_true)
646-
*retval = jl_ast_ctx(fl_ctx)->true_sym;
645+
*retval = fl_cons(fl_ctx, jl_ast_ctx(fl_ctx)->true_sym, fl_ctx->NIL);
647646
else if (v == jl_false)
648-
*retval = jl_ast_ctx(fl_ctx)->false_sym;
647+
*retval = fl_cons(fl_ctx, jl_ast_ctx(fl_ctx)->false_sym, fl_ctx->NIL);
649648
else if (v == jl_nothing)
650649
*retval = fl_cons(fl_ctx, jl_ast_ctx(fl_ctx)->null_sym, fl_ctx->NIL);
651650
else

src/ast.scm

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
(define (deparse e (ilvl 0))
5050
(cond ((or (symbol? e) (number? e)) (string e))
5151
((string? e) (print-to-string e))
52-
((eq? e #t) "true")
53-
((eq? e #f) "false")
5452
((eq? (typeof e) 'julia_value)
5553
(let ((s (string e)))
5654
(if (string.find s "#<julia: ")
@@ -75,6 +73,9 @@
7573
(string (deparse (cadr e)) " " (car e) " " (deparse (caddr e)))))
7674
(else
7775
(case (car e)
76+
((null) "nothing")
77+
((true) "true")
78+
((false) "false")
7879
;; calls and operators
7980
((call)
8081
(cond ((and (eq? (cadr e) ':) (or (length= e 4) (length= e 5)))
@@ -86,7 +87,7 @@
8687
(string #\( (deparse (caddr e)) " " (cadr e) " " (deparse (cadddr e)) #\) ))
8788
(else
8889
(deparse-prefix-call (cadr e) (cddr e) #\( #\)))))
89-
(($ &) (if (and (pair? (cadr e)) (not (eq? (caadr e) 'outerref)))
90+
(($ &) (if (and (pair? (cadr e)) (not (memq (caadr e) '(outerref null true false))))
9091
(string (car e) "(" (deparse (cadr e)) ")")
9192
(string (car e) (deparse (cadr e)))))
9293
((|::|) (if (length= e 2)
@@ -170,7 +171,7 @@
170171
(indented-block (cdr (cadr e)) ilvl)
171172
(if (and (pair? (cadddr e)) (eq? (car (cadddr e)) 'block))
172173
(string (string.rep " " ilvl) "catch"
173-
(if (eq? (caddr e) 'false)
174+
(if (equal? (caddr e) '(false))
174175
""
175176
(string " " (caddr e)))
176177
"\n"
@@ -192,15 +193,15 @@
192193
(deparse-arglist args))
193194
(cdr body) ilvl)))
194195
((struct)
195-
(string (if (eq? (cadr e) 'true) "mutable " "")
196+
(string (if (equal? (cadr e) '(true)) "mutable " "")
196197
"struct "
197198
(deparse-block (deparse (caddr e)) (cdr (cadddr e)) ilvl)))
198199
((abstract)
199200
(string "abstract type " (deparse (cadr e)) " end"))
200201
((primitive)
201202
(string "primitive type " (deparse (cadr e)) " " (deparse (caddr e)) " end"))
202203
((module)
203-
(string (if (eq? (cadr e) 'true) "module " "baremodule ")
204+
(string (if (equal? (cadr e) '(true)) "module " "baremodule ")
204205
(caddr e) "\n"
205206
(string.join (map deparse (cdr (cadddr e))) "\n") "\n"
206207
"end"))
@@ -289,7 +290,7 @@
289290
(error (string #\" (deparse v) #\" " is not a valid function argument name")))
290291

291292
(define (valid-name? s)
292-
(not (memq s '(true false ccall cglobal))))
293+
(not (memq s '(ccall cglobal))))
293294

294295
(define (arg-name v)
295296
(cond ((and (symbol? v) (valid-name? v))
@@ -351,17 +352,16 @@
351352
(and (pair? e) (eq? (car e) 'globalref)))
352353

353354
(define (symbol-like? e)
354-
(or (and (symbol? e) (not (eq? e 'true)) (not (eq? e 'false)))
355-
(ssavalue? e)))
355+
(or (symbol? e) (ssavalue? e)))
356356

357357
(define (simple-atom? x)
358-
(or (number? x) (string? x) (char? x) (eq? x 'true) (eq? x 'false)
359-
(and (pair? x) (memq (car x) '(ssavalue null)))
358+
(or (number? x) (string? x) (char? x)
359+
(and (pair? x) (memq (car x) '(ssavalue null true false)))
360360
(eq? (typeof x) 'julia_value)))
361361

362362
;; identify some expressions that are safe to repeat
363363
(define (effect-free? e)
364-
(or (not (pair? e)) (ssavalue? e) (sym-dot? e) (quoted? e) (equal? e '(null))))
364+
(or (not (pair? e)) (ssavalue? e) (sym-dot? e) (quoted? e) (memq (car e) '(null true false))))
365365

366366
;; get the variable name part of a declaration, x::int => x
367367
(define (decl-var v)

src/jlfrontend.scm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@
111111
(and (pair? e)
112112
(or (memq (car e) '(toplevel line module import using export
113113
error incomplete))
114-
(and (memq (car e) '(global const)) (every symbol? (cdr e))
115-
(every (lambda (x) (not (memq x '(true false)))) (cdr e))))))
114+
(and (memq (car e) '(global const)) (every symbol? (cdr e))))))
116115

117116
(define *in-expand* #f)
118117

src/julia-parser.scm

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@
13061306
(if (reserved-word? (peek-token s))
13071307
(error (string "invalid type name \"" (take-token s) "\"")))
13081308
(let ((sig (parse-subtype-spec s)))
1309-
(begin0 (list 'struct (if mut? 'true 'false) sig (parse-block s))
1309+
(begin0 (list 'struct (if mut? '(true) '(false)) sig (parse-block s))
13101310
(expect-end s word))))
13111311

13121312
;; consume any number of line endings from a token stream
@@ -1472,8 +1472,8 @@
14721472
(take-token s)
14731473
(cond
14741474
((eq? nxt 'end)
1475-
(list* 'try try-block (or catchv 'false)
1476-
(or catchb (if finalb 'false (error "try without catch or finally")))
1475+
(list* 'try try-block (or catchv '(false))
1476+
(or catchb (if finalb '(false) (error "try without catch or finally")))
14771477
(if finalb (list finalb) '())))
14781478
((and (eq? nxt 'catch)
14791479
(not catchb))
@@ -1487,8 +1487,7 @@
14871487
finalb)
14881488
(let* ((loc (line-number-node s))
14891489
(var (if nl #f (parse-eq* s)))
1490-
(var? (and (not nl) (or (and (symbol? var) (not (eq? var 'false))
1491-
(not (eq? var 'true)))
1490+
(var? (and (not nl) (or (symbol? var)
14921491
(and (length= var 2) (eq? (car var) '$))
14931492
(error (string "invalid syntax \"catch " (deparse var) "\"")))))
14941493
(catch-block (if (eq? (require-token s) 'finally)
@@ -1502,7 +1501,7 @@
15021501
(linenum? (cadr catch-block)))
15031502
'()
15041503
(cdr catch-block))))
1505-
(if var? var 'false)
1504+
(if var? var '(false))
15061505
finalb)))))
15071506
((and (eq? nxt 'finally)
15081507
(not finalb))
@@ -1533,7 +1532,7 @@
15331532
(if (reserved-word? name)
15341533
(error (string "invalid module name \"" name "\"")))
15351534
(expect-end s word)
1536-
(list 'module (if (eq? word 'module) 'true 'false) name
1535+
(list 'module (if (eq? word 'module) '(true) '(false)) name
15371536
`(block ,loc ,@(cdr body)))))
15381537
((export)
15391538
(let ((es (map macrocall-to-atsym
@@ -2290,22 +2289,23 @@
22902289
(if (closing-token? t)
22912290
(error (string "unexpected \"" (take-token s) "\"")))))
22922291
(take-token s)
2293-
(if (and (eq? t 'var)
2294-
(if (or (ts:pbtok s) (ts:last-tok s))
2295-
(and (eqv? (peek-token s) #\") (not (ts:space? s)))
2296-
;; Hack: avoid peek-token if possible to preserve
2297-
;; (io.pos (ts:port s)) for non-greedy Meta.parse
2298-
(eqv? (peek-char (ts:port s)) #\")))
2299-
(begin
2300-
;; var"funky identifier" syntax
2301-
(peek-token s)
2302-
(take-token s) ;; leading "
2303-
(let ((str (parse-raw-literal s #\"))
2304-
(nxt (peek-token s)))
2305-
(if (and (symbol? nxt) (not (operator? nxt)) (not (ts:space? s)))
2306-
(error (string "suffix not allowed after `var\"" str "\"`")))
2307-
(symbol str)))
2308-
t))
2292+
(cond ((and (eq? t 'var)
2293+
(if (or (ts:pbtok s) (ts:last-tok s))
2294+
(and (eqv? (peek-token s) #\") (not (ts:space? s)))
2295+
;; Hack: avoid peek-token if possible to preserve
2296+
;; (io.pos (ts:port s)) for non-greedy Meta.parse
2297+
(eqv? (peek-char (ts:port s)) #\")))
2298+
;; var"funky identifier" syntax
2299+
(peek-token s)
2300+
(take-token s) ;; leading "
2301+
(let ((str (parse-raw-literal s #\"))
2302+
(nxt (peek-token s)))
2303+
(if (and (symbol? nxt) (not (operator? nxt)) (not (ts:space? s)))
2304+
(error (string "suffix not allowed after `var\"" str "\"`")))
2305+
(symbol str)))
2306+
((eq? t 'true) '(true))
2307+
((eq? t 'false) '(false))
2308+
(else t)))
23092309

23102310
;; parens or tuple
23112311
((eqv? t #\( )

0 commit comments

Comments
 (0)