view pair.rhope @ 189:d0e3a13c1bd9 default tip

Remove old calculator example
author Mike Pavone <pavone@retrodev.com>
date Fri, 07 Oct 2011 00:24:04 -0700
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], ""]
}