Class: Pyper::PostfixMachine
- Includes:
- ControlCharacters
- Defined in:
- lib/pyper/postfix_machine.rb,
lib/pyper/postfix_machine/argument_source.rb
Overview
PostfixMachine is a simple compiler of Pyper method symbols. Each written method has two pipelines: ‘alpha’ (no. 0) and ‘beta’ (no. 1). Variables ‘alpha’ and ‘beta’ are local to the main scope of a Pyper method.
When blocks are used inside a Pyper method, variable ‘delta’ local to the block is used to hold the pipeline inside the block. For blocks with arity 1, variable named ‘epsilon’ is used to hold the block argument. For blocks with arity 2, variables named ‘epsilon’, resp. ‘zeta’ are used to hold 1st, resp. 2nd block argument. Blocks with arity higher than 2 are not allowed in Pyper methods. (However, Pyper methods may receive external block of arbitrary construction.)
Defined Under Namespace
Classes: ArgumentSource
Constant Summary collapse
- SUCC =
successor table
{ alpha: :beta, beta: :alpha,
- PRE =
predecessor table
{ alpha: :beta, beta: :alpha,
- DEF_LINE =
Template for the def line of the method being written:
-> name { "def #{name}( *args, &block )" }
Constants included from ControlCharacters
ControlCharacters::PREFIX_CHARACTERS
Instance Method Summary collapse
-
#compile(method_name, op: 1, ret: 1) ⇒ Object
Algorithmically writes a Ruby method, whose name is given in the first argument.
-
#initialize(command_string) ⇒ PostfixMachine
constructor
PostfixMachine init.
Methods included from ControlCharacters
#A, #B, #C, #D, #E, #G, #H, #I, #J, #M, #O, #R, #T, #U, #V, #W, #X, #Z, #_, #a, #b, #c, #d, #e, #f, #g, #h, #iC, #iE, #iM, #iX, #ij, #im, #ip, #it, #ix, #j, #l0, #l1, #l2, #l3, #l4, #l5, #l6, #l7, #l8, #l9, #lA, #lH, #ln, #lς, #m, #o8, #oA, #oG, #oI, #oM, #oO, #oS, #oa, #od, #oi, #om, #op, #os, #p, #q, #r, #s, #tA, #tH, #tS, #tf, #th, #ti, #ts, #u, #v, #w, #x, #y, #z, #², #Π, #Ω, #α, #β, #γ, #δ, #ε, #ζ, #ιA, #ιG, #ιH, #ιI, #ιS, #ιn, #ιΩ, #ια, #ιβ, #ιδ, #ιε, #ιζ, #ιλ, #ιρ, #ις, #ιψ, #ιω, #λ, #ρ, #σ, #ψ, #ω, #ᴘ, #ⁱ, #∅
Constructor Details
#initialize(command_string) ⇒ PostfixMachine
PostfixMachine init. Requires the command string as an argument – yes, for each command string, a new PostfixMachine is instantiated.
30 31 32 |
# File 'lib/pyper/postfix_machine.rb', line 30 def initialize command_string @cmds = parse_command_string( command_string.to_s ) end |
Instance Method Details
#compile(method_name, op: 1, ret: 1) ⇒ Object
Algorithmically writes a Ruby method, whose name is given in the first argument. The options hash expects 2 named arguments – :op and :ret:
op: when 1 (single pipe), makes no assumption about the receiver
When 2 (twin pipe), assumes the receiver is a size 2 array,
consisting of pipes alpha, beta
When -2 (twin pipe with a swap), assumes the same as above and
swaps the pipes immediately (alpha, beta = beta, alpha)
ret: when 1 (single return value), returns the current pipe only
when 2 (return both pipes), returns size 2 array, consisting
of pipes a, b
when -2 (return both pipes with a swap), returns size 2 array
containing the pipes' results in reverse order [b, a]
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/pyper/postfix_machine.rb', line 49 def compile( method_name, op: 1, ret: 1 ) @opts = { op: op, ret: ret }.tap do |oo| oo.define_singleton_method :op do self[:op] end oo.define_singleton_method :ret do self[:ret] end end # Set up compile-time argument sourcing. @argsrc = ArgumentSource.new # Write the method skeleton. initialize_writer_state write_method_head_skeleton( method_name ) write_initial_pipeline write_method_tail_skeleton # Now that we have the skeleton, let's write the meat. write_method_meat puts "head is #@head\npipe is #@pipe\ntail is #@tail" if Pyper::DEBUG > 1 # Finally, close any blocks and return autoclose_open_blocks_and_return end |