ast.rb 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. # Copyright (C) 2011 Apple Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  13. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  14. # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  15. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  16. # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  17. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  18. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  19. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  20. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  21. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  22. # THE POSSIBILITY OF SUCH DAMAGE.
  23. require "config"
  24. #
  25. # Base utility types for the AST.
  26. #
  27. # Valid methods for Node:
  28. #
  29. # node.children -> Returns an array of immediate children.
  30. #
  31. # node.descendents -> Returns an array of all strict descendants (children
  32. # and children of children, transitively).
  33. #
  34. # node.flatten -> Returns an array containing the strict descendants and
  35. # the node itself.
  36. #
  37. # node.filter(type) -> Returns an array containing those elements in
  38. # node.flatten that are of the given type (is_a? type returns true).
  39. #
  40. # node.mapChildren{|v| ...} -> Returns a new node with all children
  41. # replaced according to the given block.
  42. #
  43. # Examples:
  44. #
  45. # node.filter(Setting).uniq -> Returns all of the settings that the AST's
  46. # IfThenElse blocks depend on.
  47. #
  48. # node.filter(StructOffset).uniq -> Returns all of the structure offsets
  49. # that the AST depends on.
  50. class Node
  51. attr_reader :codeOrigin
  52. def initialize(codeOrigin)
  53. @codeOrigin = codeOrigin
  54. end
  55. def codeOriginString
  56. @codeOrigin.to_s
  57. end
  58. def descendants
  59. children.collect{|v| v.flatten}.flatten
  60. end
  61. def flatten
  62. [self] + descendants
  63. end
  64. def filter(type)
  65. flatten.select{|v| v.is_a? type}
  66. end
  67. end
  68. class NoChildren < Node
  69. def initialize(codeOrigin)
  70. super(codeOrigin)
  71. end
  72. def children
  73. []
  74. end
  75. def mapChildren
  76. self
  77. end
  78. end
  79. class StructOffsetKey
  80. attr_reader :struct, :field
  81. def initialize(struct, field)
  82. @struct = struct
  83. @field = field
  84. end
  85. def hash
  86. @struct.hash + @field.hash * 3
  87. end
  88. def eql?(other)
  89. @struct == other.struct and @field == other.field
  90. end
  91. end
  92. #
  93. # AST nodes.
  94. #
  95. class StructOffset < NoChildren
  96. attr_reader :struct, :field
  97. def initialize(codeOrigin, struct, field)
  98. super(codeOrigin)
  99. @struct = struct
  100. @field = field
  101. end
  102. @@mapping = {}
  103. def self.forField(codeOrigin, struct, field)
  104. key = StructOffsetKey.new(struct, field)
  105. unless @@mapping[key]
  106. @@mapping[key] = StructOffset.new(codeOrigin, struct, field)
  107. end
  108. @@mapping[key]
  109. end
  110. def dump
  111. "#{struct}::#{field}"
  112. end
  113. def <=>(other)
  114. if @struct != other.struct
  115. return @struct <=> other.struct
  116. end
  117. @field <=> other.field
  118. end
  119. def address?
  120. false
  121. end
  122. def label?
  123. false
  124. end
  125. def immediate?
  126. true
  127. end
  128. def register?
  129. false
  130. end
  131. end
  132. class Sizeof < NoChildren
  133. attr_reader :struct
  134. def initialize(codeOrigin, struct)
  135. super(codeOrigin)
  136. @struct = struct
  137. end
  138. @@mapping = {}
  139. def self.forName(codeOrigin, struct)
  140. unless @@mapping[struct]
  141. @@mapping[struct] = Sizeof.new(codeOrigin, struct)
  142. end
  143. @@mapping[struct]
  144. end
  145. def dump
  146. "sizeof #{@struct}"
  147. end
  148. def <=>(other)
  149. @struct <=> other.struct
  150. end
  151. def address?
  152. false
  153. end
  154. def label?
  155. false
  156. end
  157. def immediate?
  158. true
  159. end
  160. def register?
  161. false
  162. end
  163. end
  164. class Immediate < NoChildren
  165. attr_reader :value
  166. def initialize(codeOrigin, value)
  167. super(codeOrigin)
  168. @value = value
  169. raise "Bad immediate value #{value.inspect} at #{codeOriginString}" unless value.is_a? Integer
  170. end
  171. def dump
  172. "#{value}"
  173. end
  174. def ==(other)
  175. other.is_a? Immediate and other.value == @value
  176. end
  177. def address?
  178. false
  179. end
  180. def label?
  181. false
  182. end
  183. def immediate?
  184. true
  185. end
  186. def register?
  187. false
  188. end
  189. end
  190. class AddImmediates < Node
  191. attr_reader :left, :right
  192. def initialize(codeOrigin, left, right)
  193. super(codeOrigin)
  194. @left = left
  195. @right = right
  196. end
  197. def children
  198. [@left, @right]
  199. end
  200. def mapChildren
  201. AddImmediates.new(codeOrigin, (yield @left), (yield @right))
  202. end
  203. def dump
  204. "(#{left.dump} + #{right.dump})"
  205. end
  206. def address?
  207. false
  208. end
  209. def label?
  210. false
  211. end
  212. def immediate?
  213. true
  214. end
  215. def register?
  216. false
  217. end
  218. end
  219. class SubImmediates < Node
  220. attr_reader :left, :right
  221. def initialize(codeOrigin, left, right)
  222. super(codeOrigin)
  223. @left = left
  224. @right = right
  225. end
  226. def children
  227. [@left, @right]
  228. end
  229. def mapChildren
  230. SubImmediates.new(codeOrigin, (yield @left), (yield @right))
  231. end
  232. def dump
  233. "(#{left.dump} - #{right.dump})"
  234. end
  235. def address?
  236. false
  237. end
  238. def label?
  239. false
  240. end
  241. def immediate?
  242. true
  243. end
  244. def register?
  245. false
  246. end
  247. end
  248. class MulImmediates < Node
  249. attr_reader :left, :right
  250. def initialize(codeOrigin, left, right)
  251. super(codeOrigin)
  252. @left = left
  253. @right = right
  254. end
  255. def children
  256. [@left, @right]
  257. end
  258. def mapChildren
  259. MulImmediates.new(codeOrigin, (yield @left), (yield @right))
  260. end
  261. def dump
  262. "(#{left.dump} * #{right.dump})"
  263. end
  264. def address?
  265. false
  266. end
  267. def label?
  268. false
  269. end
  270. def immediate?
  271. true
  272. end
  273. def register?
  274. false
  275. end
  276. end
  277. class NegImmediate < Node
  278. attr_reader :child
  279. def initialize(codeOrigin, child)
  280. super(codeOrigin)
  281. @child = child
  282. end
  283. def children
  284. [@child]
  285. end
  286. def mapChildren
  287. NegImmediate.new(codeOrigin, (yield @child))
  288. end
  289. def dump
  290. "(-#{@child.dump})"
  291. end
  292. def address?
  293. false
  294. end
  295. def label?
  296. false
  297. end
  298. def immediate?
  299. true
  300. end
  301. def register?
  302. false
  303. end
  304. end
  305. class OrImmediates < Node
  306. attr_reader :left, :right
  307. def initialize(codeOrigin, left, right)
  308. super(codeOrigin)
  309. @left = left
  310. @right = right
  311. end
  312. def children
  313. [@left, @right]
  314. end
  315. def mapChildren
  316. OrImmediates.new(codeOrigin, (yield @left), (yield @right))
  317. end
  318. def dump
  319. "(#{left.dump} | #{right.dump})"
  320. end
  321. def address?
  322. false
  323. end
  324. def label?
  325. false
  326. end
  327. def immediate?
  328. true
  329. end
  330. def register?
  331. false
  332. end
  333. end
  334. class AndImmediates < Node
  335. attr_reader :left, :right
  336. def initialize(codeOrigin, left, right)
  337. super(codeOrigin)
  338. @left = left
  339. @right = right
  340. end
  341. def children
  342. [@left, @right]
  343. end
  344. def mapChildren
  345. AndImmediates.new(codeOrigin, (yield @left), (yield @right))
  346. end
  347. def dump
  348. "(#{left.dump} & #{right.dump})"
  349. end
  350. def address?
  351. false
  352. end
  353. def label?
  354. false
  355. end
  356. def immediate?
  357. true
  358. end
  359. def register?
  360. false
  361. end
  362. end
  363. class XorImmediates < Node
  364. attr_reader :left, :right
  365. def initialize(codeOrigin, left, right)
  366. super(codeOrigin)
  367. @left = left
  368. @right = right
  369. end
  370. def children
  371. [@left, @right]
  372. end
  373. def mapChildren
  374. XorImmediates.new(codeOrigin, (yield @left), (yield @right))
  375. end
  376. def dump
  377. "(#{left.dump} ^ #{right.dump})"
  378. end
  379. def address?
  380. false
  381. end
  382. def label?
  383. false
  384. end
  385. def immediate?
  386. true
  387. end
  388. def register?
  389. false
  390. end
  391. end
  392. class BitnotImmediate < Node
  393. attr_reader :child
  394. def initialize(codeOrigin, child)
  395. super(codeOrigin)
  396. @child = child
  397. end
  398. def children
  399. [@child]
  400. end
  401. def mapChildren
  402. BitnotImmediate.new(codeOrigin, (yield @child))
  403. end
  404. def dump
  405. "(~#{@child.dump})"
  406. end
  407. def address?
  408. false
  409. end
  410. def label?
  411. false
  412. end
  413. def immediate?
  414. true
  415. end
  416. def register?
  417. false
  418. end
  419. end
  420. class RegisterID < NoChildren
  421. attr_reader :name
  422. def initialize(codeOrigin, name)
  423. super(codeOrigin)
  424. @name = name
  425. end
  426. @@mapping = {}
  427. def self.forName(codeOrigin, name)
  428. unless @@mapping[name]
  429. @@mapping[name] = RegisterID.new(codeOrigin, name)
  430. end
  431. @@mapping[name]
  432. end
  433. def dump
  434. name
  435. end
  436. def address?
  437. false
  438. end
  439. def label?
  440. false
  441. end
  442. def immediate?
  443. false
  444. end
  445. def register?
  446. true
  447. end
  448. end
  449. class FPRegisterID < NoChildren
  450. attr_reader :name
  451. def initialize(codeOrigin, name)
  452. super(codeOrigin)
  453. @name = name
  454. end
  455. @@mapping = {}
  456. def self.forName(codeOrigin, name)
  457. unless @@mapping[name]
  458. @@mapping[name] = FPRegisterID.new(codeOrigin, name)
  459. end
  460. @@mapping[name]
  461. end
  462. def dump
  463. name
  464. end
  465. def address?
  466. false
  467. end
  468. def label?
  469. false
  470. end
  471. def immediate?
  472. false
  473. end
  474. def register?
  475. true
  476. end
  477. end
  478. class SpecialRegister < NoChildren
  479. def initialize(name)
  480. @name = name
  481. end
  482. def address?
  483. false
  484. end
  485. def label?
  486. false
  487. end
  488. def immediate?
  489. false
  490. end
  491. def register?
  492. true
  493. end
  494. end
  495. class Variable < NoChildren
  496. attr_reader :name
  497. def initialize(codeOrigin, name)
  498. super(codeOrigin)
  499. @name = name
  500. end
  501. @@mapping = {}
  502. def self.forName(codeOrigin, name)
  503. unless @@mapping[name]
  504. @@mapping[name] = Variable.new(codeOrigin, name)
  505. end
  506. @@mapping[name]
  507. end
  508. def dump
  509. name
  510. end
  511. def inspect
  512. "<variable #{name} at #{codeOriginString}>"
  513. end
  514. end
  515. class Address < Node
  516. attr_reader :base, :offset
  517. def initialize(codeOrigin, base, offset)
  518. super(codeOrigin)
  519. @base = base
  520. @offset = offset
  521. raise "Bad base for address #{base.inspect} at #{codeOriginString}" unless base.is_a? Variable or base.register?
  522. raise "Bad offset for address #{offset.inspect} at #{codeOriginString}" unless offset.is_a? Variable or offset.immediate?
  523. end
  524. def withOffset(extraOffset)
  525. Address.new(codeOrigin, @base, Immediate.new(codeOrigin, @offset.value + extraOffset))
  526. end
  527. def children
  528. [@base, @offset]
  529. end
  530. def mapChildren
  531. Address.new(codeOrigin, (yield @base), (yield @offset))
  532. end
  533. def dump
  534. "#{offset.dump}[#{base.dump}]"
  535. end
  536. def address?
  537. true
  538. end
  539. def label?
  540. false
  541. end
  542. def immediate?
  543. false
  544. end
  545. def register?
  546. false
  547. end
  548. end
  549. class BaseIndex < Node
  550. attr_reader :base, :index, :scale, :offset
  551. def initialize(codeOrigin, base, index, scale, offset)
  552. super(codeOrigin)
  553. @base = base
  554. @index = index
  555. @scale = scale
  556. raise unless [1, 2, 4, 8].member? @scale
  557. @offset = offset
  558. end
  559. def scaleShift
  560. case scale
  561. when 1
  562. 0
  563. when 2
  564. 1
  565. when 4
  566. 2
  567. when 8
  568. 3
  569. else
  570. raise "Bad scale at #{codeOriginString}"
  571. end
  572. end
  573. def withOffset(extraOffset)
  574. BaseIndex.new(codeOrigin, @base, @index, @scale, Immediate.new(codeOrigin, @offset.value + extraOffset))
  575. end
  576. def children
  577. [@base, @index, @offset]
  578. end
  579. def mapChildren
  580. BaseIndex.new(codeOrigin, (yield @base), (yield @index), @scale, (yield @offset))
  581. end
  582. def dump
  583. "#{offset.dump}[#{base.dump}, #{index.dump}, #{scale}]"
  584. end
  585. def address?
  586. true
  587. end
  588. def label?
  589. false
  590. end
  591. def immediate?
  592. false
  593. end
  594. def register?
  595. false
  596. end
  597. end
  598. class AbsoluteAddress < NoChildren
  599. attr_reader :address
  600. def initialize(codeOrigin, address)
  601. super(codeOrigin)
  602. @address = address
  603. end
  604. def withOffset(extraOffset)
  605. AbsoluteAddress.new(codeOrigin, Immediate.new(codeOrigin, @address.value + extraOffset))
  606. end
  607. def dump
  608. "#{address.dump}[]"
  609. end
  610. def address?
  611. true
  612. end
  613. def label?
  614. false
  615. end
  616. def immediate?
  617. false
  618. end
  619. def register?
  620. false
  621. end
  622. end
  623. class Instruction < Node
  624. attr_reader :opcode, :operands, :annotation
  625. def initialize(codeOrigin, opcode, operands, annotation=nil)
  626. super(codeOrigin)
  627. @opcode = opcode
  628. @operands = operands
  629. @annotation = annotation
  630. end
  631. def children
  632. operands
  633. end
  634. def mapChildren(&proc)
  635. Instruction.new(codeOrigin, @opcode, @operands.map(&proc), @annotation)
  636. end
  637. def dump
  638. "\t" + opcode.to_s + " " + operands.collect{|v| v.dump}.join(", ")
  639. end
  640. def lowerDefault
  641. case opcode
  642. when "localAnnotation"
  643. $asm.putLocalAnnotation
  644. when "globalAnnotation"
  645. $asm.putGlobalAnnotation
  646. else
  647. raise "Unhandled opcode #{opcode} at #{codeOriginString}"
  648. end
  649. end
  650. end
  651. class Error < NoChildren
  652. def initialize(codeOrigin)
  653. super(codeOrigin)
  654. end
  655. def dump
  656. "\terror"
  657. end
  658. end
  659. class ConstDecl < Node
  660. attr_reader :variable, :value
  661. def initialize(codeOrigin, variable, value)
  662. super(codeOrigin)
  663. @variable = variable
  664. @value = value
  665. end
  666. def children
  667. [@variable, @value]
  668. end
  669. def mapChildren
  670. ConstDecl.new(codeOrigin, (yield @variable), (yield @value))
  671. end
  672. def dump
  673. "const #{@variable.dump} = #{@value.dump}"
  674. end
  675. end
  676. $labelMapping = {}
  677. class Label < NoChildren
  678. attr_reader :name
  679. def initialize(codeOrigin, name)
  680. super(codeOrigin)
  681. @name = name
  682. end
  683. def self.forName(codeOrigin, name)
  684. if $labelMapping[name]
  685. raise "Label name collision: #{name}" unless $labelMapping[name].is_a? Label
  686. else
  687. $labelMapping[name] = Label.new(codeOrigin, name)
  688. end
  689. $labelMapping[name]
  690. end
  691. def dump
  692. "#{name}:"
  693. end
  694. end
  695. class LocalLabel < NoChildren
  696. attr_reader :name
  697. def initialize(codeOrigin, name)
  698. super(codeOrigin)
  699. @name = name
  700. end
  701. @@uniqueNameCounter = 0
  702. def self.forName(codeOrigin, name)
  703. if $labelMapping[name]
  704. raise "Label name collision: #{name}" unless $labelMapping[name].is_a? LocalLabel
  705. else
  706. $labelMapping[name] = LocalLabel.new(codeOrigin, name)
  707. end
  708. $labelMapping[name]
  709. end
  710. def self.unique(comment)
  711. newName = "_#{comment}"
  712. if $labelMapping[newName]
  713. while $labelMapping[newName = "_#{@@uniqueNameCounter}_#{comment}"]
  714. @@uniqueNameCounter += 1
  715. end
  716. end
  717. forName(nil, newName)
  718. end
  719. def cleanName
  720. if name =~ /^\./
  721. "_" + name[1..-1]
  722. else
  723. name
  724. end
  725. end
  726. def dump
  727. "#{name}:"
  728. end
  729. end
  730. class LabelReference < Node
  731. attr_reader :label
  732. def initialize(codeOrigin, label)
  733. super(codeOrigin)
  734. @label = label
  735. end
  736. def children
  737. [@label]
  738. end
  739. def mapChildren
  740. LabelReference.new(codeOrigin, (yield @label))
  741. end
  742. def name
  743. label.name
  744. end
  745. def dump
  746. label.name
  747. end
  748. def address?
  749. false
  750. end
  751. def label?
  752. true
  753. end
  754. def immediate?
  755. false
  756. end
  757. end
  758. class LocalLabelReference < NoChildren
  759. attr_reader :label
  760. def initialize(codeOrigin, label)
  761. super(codeOrigin)
  762. @label = label
  763. end
  764. def children
  765. [@label]
  766. end
  767. def mapChildren
  768. LocalLabelReference.new(codeOrigin, (yield @label))
  769. end
  770. def name
  771. label.name
  772. end
  773. def dump
  774. label.name
  775. end
  776. def address?
  777. false
  778. end
  779. def label?
  780. true
  781. end
  782. def immediate?
  783. false
  784. end
  785. end
  786. class Sequence < Node
  787. attr_reader :list
  788. def initialize(codeOrigin, list)
  789. super(codeOrigin)
  790. @list = list
  791. end
  792. def children
  793. list
  794. end
  795. def mapChildren(&proc)
  796. Sequence.new(codeOrigin, @list.map(&proc))
  797. end
  798. def dump
  799. list.collect{|v| v.dump}.join("\n")
  800. end
  801. end
  802. class True < NoChildren
  803. def initialize
  804. super(nil)
  805. end
  806. @@instance = True.new
  807. def self.instance
  808. @@instance
  809. end
  810. def value
  811. true
  812. end
  813. def dump
  814. "true"
  815. end
  816. end
  817. class False < NoChildren
  818. def initialize
  819. super(nil)
  820. end
  821. @@instance = False.new
  822. def self.instance
  823. @@instance
  824. end
  825. def value
  826. false
  827. end
  828. def dump
  829. "false"
  830. end
  831. end
  832. class TrueClass
  833. def asNode
  834. True.instance
  835. end
  836. end
  837. class FalseClass
  838. def asNode
  839. False.instance
  840. end
  841. end
  842. class Setting < NoChildren
  843. attr_reader :name
  844. def initialize(codeOrigin, name)
  845. super(codeOrigin)
  846. @name = name
  847. end
  848. @@mapping = {}
  849. def self.forName(codeOrigin, name)
  850. unless @@mapping[name]
  851. @@mapping[name] = Setting.new(codeOrigin, name)
  852. end
  853. @@mapping[name]
  854. end
  855. def dump
  856. name
  857. end
  858. end
  859. class And < Node
  860. attr_reader :left, :right
  861. def initialize(codeOrigin, left, right)
  862. super(codeOrigin)
  863. @left = left
  864. @right = right
  865. end
  866. def children
  867. [@left, @right]
  868. end
  869. def mapChildren
  870. And.new(codeOrigin, (yield @left), (yield @right))
  871. end
  872. def dump
  873. "(#{left.dump} and #{right.dump})"
  874. end
  875. end
  876. class Or < Node
  877. attr_reader :left, :right
  878. def initialize(codeOrigin, left, right)
  879. super(codeOrigin)
  880. @left = left
  881. @right = right
  882. end
  883. def children
  884. [@left, @right]
  885. end
  886. def mapChildren
  887. Or.new(codeOrigin, (yield @left), (yield @right))
  888. end
  889. def dump
  890. "(#{left.dump} or #{right.dump})"
  891. end
  892. end
  893. class Not < Node
  894. attr_reader :child
  895. def initialize(codeOrigin, child)
  896. super(codeOrigin)
  897. @child = child
  898. end
  899. def children
  900. [@child]
  901. end
  902. def mapChildren
  903. Not.new(codeOrigin, (yield @child))
  904. end
  905. def dump
  906. "(not #{child.dump})"
  907. end
  908. end
  909. class Skip < NoChildren
  910. def initialize(codeOrigin)
  911. super(codeOrigin)
  912. end
  913. def dump
  914. "\tskip"
  915. end
  916. end
  917. class IfThenElse < Node
  918. attr_reader :predicate, :thenCase
  919. attr_accessor :elseCase
  920. def initialize(codeOrigin, predicate, thenCase)
  921. super(codeOrigin)
  922. @predicate = predicate
  923. @thenCase = thenCase
  924. @elseCase = Skip.new(codeOrigin)
  925. end
  926. def children
  927. if @elseCase
  928. [@predicate, @thenCase, @elseCase]
  929. else
  930. [@predicate, @thenCase]
  931. end
  932. end
  933. def mapChildren
  934. IfThenElse.new(codeOrigin, (yield @predicate), (yield @thenCase), (yield @elseCase))
  935. end
  936. def dump
  937. "if #{predicate.dump}\n" + thenCase.dump + "\nelse\n" + elseCase.dump + "\nend"
  938. end
  939. end
  940. class Macro < Node
  941. attr_reader :name, :variables, :body
  942. def initialize(codeOrigin, name, variables, body)
  943. super(codeOrigin)
  944. @name = name
  945. @variables = variables
  946. @body = body
  947. end
  948. def children
  949. @variables + [@body]
  950. end
  951. def mapChildren
  952. Macro.new(codeOrigin, @name, @variables.map{|v| yield v}, (yield @body))
  953. end
  954. def dump
  955. "macro #{name}(" + variables.collect{|v| v.dump}.join(", ") + ")\n" + body.dump + "\nend"
  956. end
  957. end
  958. class MacroCall < Node
  959. attr_reader :name, :operands, :annotation
  960. def initialize(codeOrigin, name, operands, annotation)
  961. super(codeOrigin)
  962. @name = name
  963. @operands = operands
  964. raise unless @operands
  965. @operands.each{|v| raise unless v}
  966. @annotation = annotation
  967. end
  968. def children
  969. @operands
  970. end
  971. def mapChildren(&proc)
  972. MacroCall.new(codeOrigin, @name, @operands.map(&proc), @annotation)
  973. end
  974. def dump
  975. "\t#{name}(" + operands.collect{|v| v.dump}.join(", ") + ")"
  976. end
  977. end