Skip to main content

Module parser_ruby

Module parser_ruby 

Source
Expand description

Ruby parser for Rúnar contracts (.runar.rb).

Parses Ruby-style contract definitions using a hand-written tokenizer and recursive descent parser. Produces the same AST as the TypeScript and Python parsers.

§Expected format

require 'runar'

class P2PKH < Runar::SmartContract
  prop :pub_key_hash, Addr

  def initialize(pub_key_hash)
    super(pub_key_hash)
    @pub_key_hash = pub_key_hash
  end

  runar_public sig: Sig, pub_key: PubKey
  def unlock(sig, pub_key)
    assert hash160(pub_key) == @pub_key_hash
    assert check_sig(sig, pub_key)
  end
end

Key mappings:

  • class Foo < Runar::SmartContract -> contract
  • runar_public -> Visibility::Public for the next method
  • params -> pending param types for a private method
  • prop :name, Type -> PropertyNode
  • @ivar -> PropertyAccess (like this.prop)
  • assert expr -> assert(expr)
  • ** -> pow() call
  • and/or/not -> And/Or/Not operators
  • ==/!= -> StrictEq/StrictNe
  • for i in start...end -> ForStatement (exclusive)
  • for i in start..end -> ForStatement (inclusive)
  • unless -> if with negated condition
  • snake_case identifiers -> camelCase in AST

Functions§

parse_ruby
Parse a Ruby-format Rúnar contract source.