NAME

VM::Dreamer::Util - Utilities for Deamer

DESCRIPTION

These functions contain some of the core logic in Dreamer and help the higher level functions do their work.

stringify_array

Takes an array of single digits and turns it into a string;

my $string = stringify_array( [ 5, 3, 2, 1, 0, 8, 7, 5 ] ); # '53210875'

arrayify_string

Take a string of single digits and turns each one into successive elements of an array. Returns a reference to said array.

my $aref = arrayify_string('53210875'); # [ 5, 3, 2, 1, 0, 7, 7, 5 ]

parse_program_line

Takes a line of input from a program for your machine and returns the address in which to store the instruction and the instruction itself.

my ( $address, $instruction ) = parse_program_line("15\t342"); ( 15, 342 )

This function really just splits on the separator.

parse_next_instruction

Splits an instruction into the op_code and the operand.

my $machine = { next_instruction => '1101011100111010', meta => { width => { op_code => 4, operand => 12, }, }, }; my( $op_code, $operand ) = parse_next_instruction($machine); # ( 1101, 11100111010 );

add_two_arrays

Takes two references to arrays whose elements are single digits and the greatest value for any of the digits and adds them together.

my $augend = [ 0, 5, 3, 2 ]; my $addend = [ 3, 9, 4, 8 ];

my $greatest_digit = 9;

my $sum = add_two_arrays( $augend, $addend, $greatest_digit ); # [ 4, 4, 8, 0 ]

Really, this is just adding 532 and 3948, but since the base is arbitrary, I found it easier to implement in this way.

The arrays are almost like old-fashioned adding machines where each element is a "wheel" of digits and the greatest_digit tells you when to carry.

subtract_two_arrays

my $minuend = [ 1, 0, 1, 1, 0, 0, 1, 0 ]; my $subtrahend = [ 1, 0, 0, 0, 1, 0, 1, 0 ];

my $greatest_digt = 1;

my $difference = subtract_two_arrays( $minuend, $subtrahend, $greatest_digit ); # [ 0, 0, 1, 0, 0, 1, 0, 0 ]

Similarly to carrying in addition, greatest_digit helps us when we need to borrow during subtraction.

AUTHOR

William Stevenson <william at coders dot coop>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2013 by William Stevenson.

This is free software, licensed under:

 The Artistic License 2.0 (GPL Compatible)