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 result
82318231 end
82328232
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 before
8236+ # scan_locals's first pass runs.
8237+ def simple_literal_type(nid)
8238+ if nid < 0
8239+ return ""
8240+ end
8241+ t = @nd_type[nid]
8242+ if t == "StringNode"
8243+ return "string"
8244+ end
8245+ if t == "IntegerNode"
8246+ return "int"
8247+ end
8248+ if t == "FloatNode"
8249+ return "float"
8250+ end
8251+ if t == "SymbolNode"
8252+ return "symbol"
8253+ end
8254+ if t == "TrueNode"
8255+ return "bool"
8256+ end
8257+ if t == "FalseNode"
8258+ return "bool"
8259+ end
8260+ if t == "NilNode"
8261+ return "nil"
8262+ end
8263+ ""
8264+ end
8265+
8266+ # Pre-populate @scope_names with simple-literal local writes so that
8267+ # scan_locals's pass-1 inference can resolve LocalVariableReadNode
8268+ # references during type inference. Without this, hash shorthand
8269+ # `{first:}` whose key resolves to a previously-written string-valued
8270+ # local mis-types because find_var_type runs against an empty scope and
8271+ # falls back to "int". Limited to leaf-literal initializers; method
8272+ # 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+ end
8282+ end
8283+ end
8284+ }
8285+ end
8286+
82338287 # ---- Feature detection ----
82348288 def detect_features
82358289 # Set up a temporary scope with main-level locals so feature detection
82368290 # can infer types of local variables correctly
82378291 push_scope
82388292 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"