view pair.rhope @ 75:0083b2f7b3c7

Partially working implementation of List. Modified build scripts to allow use of other compilers. Fixed some bugs involving method implementations on different types returning different numbers of outputs. Added Fold to the 'builtins' in the comipler.
author Mike Pavone <pavone@retrodev.com>
date Tue, 06 Jul 2010 07:52:59 -0400
parents 76568becd6d6
children
line wrap: on
line source

/*
	This example program defines a new object type Pair with two properties and one worker
*/

//We're importing extendlib for access to Pretty Print
Import extendlib.rhope

//Blueprint statements define new object types
Blueprint Pair
{
	//We define two fields here First and Second
	First
	Second
}

//There's nothing special about worker's named New. It's just a convention.
New@Pair[first,second:out]
{
	//The Build worker creates a new unpopulated object of a given type based on its blueprint
	out <- [[Build["Pair"]
	//Here we set the properties of the new object, First and Second, to the values passed to the constructor
	]First <<[first]
	]Second <<[second]
}

//Rhope has no concept of operators; everything is a worker. Here we define a worker for adding two Pairs
+@Pair[left,right:sum]
{
	//Get the sum of the two fields
	first <- [[left]First >>] + [[right]First >>]
	second <- [[left]Second >>] + [[right]Second >>]
	//Return a modified Pair
	sum <-	[[left]First <<[first]]Second <<[second]
}

Main[]
{
	a <- New@Pair[2,3]
	b <- New@Pair[5,-1]
	//Pretty Print can print out user defined objects and most builtin objects in a readable format
	Pretty Print[[a]+[b], ""]
}