Class: CodegenClassic
- Inherits:
-
Object
- Object
- CodegenClassic
- Defined in:
- lib/rubidity/codegen.rb
Overview
use static methods for now - why? why not?
Class Method Summary collapse
Class Method Details
.generate(source) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/rubidity/codegen.rb', line 68 def self.generate( source ) pp source contract_classes = {} ## lookup by name struct_classes = {} ## lookup by name source.contracts.each do |contract| puts "==> generate contract #{contract.name}..." base = contract.is.empty? ? Contract : contract_classes[contract.is[0]] puts " base: #{base}" pp contract.is contract_class = Class.new( base ) ## Use Kernel / global ?? scope for now scope = Object scope.const_set( contract.name, contract_class ) contract_classes[ contract.name ] = contract_class ## add events if any ## ## event :Transfer, { from: :address, to: :address, amount: :uint256 } ## event :Approval, { owner: :address, spender: :address, amount: :uint256 } ## ## event Transfer {:from=>:address, :to=>:address, :amount=>:uint256} ## event Approval {:owner=>:address, :spender=>:address, :amount=>:uint256} contract.events.each do |event_name, event_args| print "event #{event_name}" pp event_args attributes = event_args.map { |name, type| [name, spec_to_type(type)] }.to_h pp attributes contract_class.event( event_name, **attributes ) end ## add structs if any contract.structs.each do |struct_name, struct_args| print "struct #{struct_name}" pp struct_args attributes = struct_args.map { |name, type| [name, spec_to_type(type)] }.to_h pp attributes struct_class = contract_class.struct( struct_name, **attributes ) struct_classes[ struct_name ] = struct_class end ## add storage/state if any ## ## storage name{:type=>:string, :args=>[:public]} ## storage symbol{:type=>:string, :args=>[:public]} ## storage decimals{:type=>:uint8, :args=>[:public]} ## storage totalSupply{:type=>:uint256, :args=>[:public]} ## storage balanceOf{:type=>:mapping, :key_type=>:address, :value_type=>:uint256, :args=>[:public]} ## storage allowance{:type=>:mapping, ## :key_type=>:address, ## :value_type=>{:type=>:mapping, :key_type=>:address, :value_type=>:uint256, :args=>[]}, ## :args=>[:public]} contract.storage.each do |storage_name, storage_args| print "storage #{storage_name}" pp storage_args kwargs = { storage_name => spec_to_type( storage_args, structs: struct_classes ) } contract_class.storage( **kwargs ) end ## add functions if any ## constructor: ## {:args=>{:name=>:string, :symbol=>:string, :decimals=>:uint8}, ## :options=>[], ## :body=>"..."} ## function approve: ## {:args=>{:spender=>:address, :amount=>:uint256}, ## :options=>[:public, :virtual], ## :returns=>[:bool], ## :body=>"..."} ## function transfer: ## {:args=>{:to=>:address, :amount=>:uint256}, ## :options=>[:public, :virtual], ## :returns=>[:bool], ## :body=>"..."} contract.functions.each do |function_name, function_args| print "function #{function_name}" pp function_args input_types = function_args[:args].values.map { |type| spec_to_type(type) } kwargs = function_args[:args].keys.map {|arg| "#{arg}:" }.join( ', ' ) puts " kwargs: #{kwargs}" body = function_args[:body] body = patch( body ) ## use ruby-ish conventions if function_name == :constructor puts "==> add constructor..." contract_class.sig input_types ## add unsafe method code =<<RUBY def constructor( #{kwargs} ) puts "==> calling #{contract_class.name}.constructor..." puts " self: \#{self.class.name}" #{body} end RUBY puts " code:" puts code contract_class.class_eval( code ) else output_types = function_args[:returns].map { |type| spec_to_type(type) } puts "==> add #{function_name}..." contract_class.sig input_types, returns: output_types ## add unsafe method code =<<RUBY def #{function_name}( #{kwargs} ) puts "==> calling #{contract_class.name}.#{function_name}..." puts " self: \#{self.class.name}" #{body} end RUBY puts " code:" puts code contract_class.class_eval( code ) end end end end |