Commit 3dd8b7d2
3dd8b7d2fcb1Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #143 from ryanseys/rs-parse-hash-shorthand
feat(parse,codegen): support hash shorthand ({ x:, y: })
Details
- Commit
- 3dd8b7d2fcb1
- Parent
- e4e800bfc4c0
- Merge parent
- b12c4fabe3f8
spinel_codegen.rbmodifiedView file
@@ -8230,12 +8230,67 @@
82308230
result82318231
end82328232
8233+
# Map a simple-literal AST node to its canonical type name. Returns ""8234+
# for anything that isn't a leaf-literal (hashes, arrays, calls, etc.).8235+
# Used by pre_scan_simple_local_writes to seed @scope_names before8236+
# scan_locals's first pass runs.8237+
def simple_literal_type(nid)8238+
if nid < 08239+
return ""8240+
end8241+
t = @nd_type[nid]8242+
if t == "StringNode"8243+
return "string"8244+
end8245+
if t == "IntegerNode"8246+
return "int"8247+
end8248+
if t == "FloatNode"8249+
return "float"8250+
end8251+
if t == "SymbolNode"8252+
return "symbol"8253+
end8254+
if t == "TrueNode"8255+
return "bool"8256+
end8257+
if t == "FalseNode"8258+
return "bool"8259+
end8260+
if t == "NilNode"8261+
return "nil"8262+
end8263+
""8264+
end8265+
8266+
# Pre-populate @scope_names with simple-literal local writes so that8267+
# scan_locals's pass-1 inference can resolve LocalVariableReadNode8268+
# references during type inference. Without this, hash shorthand8269+
# `{first:}` whose key resolves to a previously-written string-valued8270+
# local mis-types because find_var_type runs against an empty scope and8271+
# falls back to "int". Limited to leaf-literal initializers; method8272+
# calls and composite literals still go through the regular passes.8273+
def pre_scan_simple_local_writes(stmts)8274+
stmts.each { |sid|8275+
if @nd_type[sid] == "LocalVariableWriteNode"8276+
lname = @nd_name[sid]8277+
if find_var_type(lname) == ""8278+
st = simple_literal_type(@nd_expression[sid])8279+
if st != ""8280+
declare_var(lname, st)8281+
end8282+
end8283+
end8284+
}8285+
end8286+
82338287
# ---- Feature detection ----82348288
def detect_features82358289
# Set up a temporary scope with main-level locals so feature detection82368290
# can infer types of local variables correctly82378291
push_scope82388292
stmts = get_body_stmts(@root_id)8293+
pre_scan_simple_local_writes(stmts)82398294
lnames = "".split(",")82408295
ltypes = "".split(",")82418296
empty_p = "".split(",")@@ -13332,6 +13387,7 @@
1333213387
ltypes = "".split(",")1333313388
1333413389
empty_params = "".split(",")13390+
pre_scan_simple_local_writes(stmts)1333513391
stmts.each { |sid|1333613392
if @nd_type[sid] != "DefNode"1333713393
if @nd_type[sid] != "ClassNode"spinel_parse.cmodifiedView file
Loading diff…
test/hash_shorthand.rbaddedView file
Loading diff…
test/hash_shorthand_str.rbaddedView file
Loading diff…