#!/bin/rc
# Copyright 2010 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

# Using all the non-test *.go files in the named directory, write
# out a file /tmp/$USER.try.go to evaluate the expressions on the
# command line, perhaps to discover a function or method that
# gives the desired results.  See usage message.
# Compile the program and run it.

# Makes egrep,grep work better in general if we put them
# in ordinary C mode instead of what the current language is.
LANG=
LC_ALL=C
LC_CTYPE=C

if (~ $GOROOT '') GOROOT='$GOROOT_FINAL'
# GOROOT=${GOROOT:-"$GOROOT_FINAL"}
eval `{gomake -j1 --no-print-directory -f $GOROOT/src/Make.inc go-env}
if (test -z $"O) {
	echo >[2=1] 'missing '^$O^' - maybe no Make.'^$GOARCH^'?'
	exits missing $O
}

# Allow overrides
#GC="${_GC:-$GC} -I _test"
~ $_GC '' && _GC=($GC -I _test)
# GL="${GL:-$LD} -L _test"
~ $GL '' && GL=$LD
GL=($GL -L _test)
AS="$AS"
CC="$CC"
LD="$LD"

# Macros for tab and quotes for easier readability.
T='	'
BQ='`'
SQ=''''
DQ='"'
SD=$SQ$DQ
DS=$DQ$SQ

usage="usage: gotry [packagedirectory] expression ...
Given one expression, gotry attempts to evaluate that expression.
Given multiple expressions, gotry treats them as a list of arguments
and result values and attempts to find a function in the package
that, given the first few expressions as arguments, evaluates to
the remaining expressions as results.  If the first expression has
methods, it will also search for applicable methods.

If there are multiple expressions, a package directory must be
specified. If there is a package argument, the expressions are
evaluated in an environment that includes
	import . ${DQ}packagedirectory${DQ}

Examples:
	gotry 3+4
		# evaluates to 7
	gotry strings ${SD}abc${DS} ${SD}c${DS} 7-5
		# finds strings.Index etc.
	gotry regexp ${SQ}MustCompile(${DQ}^[0-9]+${DQ})${SQ} ${SD}12345${DS} true
		# finds Regexp.MatchString

"

fn fail {
	echo >[2=1] $"*
	exit fail
}

switch($#) {
	case 0
		fail $"usage
	case *
		switch($1) {
		case -*help -*'?' '?'
			fail $"usage
		}
		if (test -d $GOROOT/src/pkg/$1) {
			pkg=`{basename $1}
			dir=$GOROOT/src/pkg/$1
			importdir=$1
			shift
			switch($pkg) {
			case os syscall
				fail 'gotry: cannot try packages os or syscall; they are too dangerous'
			}
		}
}

spaces='[ 	][ 	]*'

fn getFunctions {
	if (~ $pkg '') {
		return
	}
	for (file in $dir/*.go) {
		switch($file) {
		case *_test*
			continue
		}
		grep 'func'^$spaces^'[A-Z]' $file |
			sed 's/fun'c^$spaces^'//;s/(.*//'
	} | sort -u
}

# Generate list of public functions.
functions=`{getFunctions}

# Write file to compile
file='/tmp/'^$USER.try
rm -f $file.go
@{
cat <<'!'
package main

import (
	"os"
	"try"
!

if (! ~ $pkg '') {
	echo $"T . $importdir
}

cat <<!
)
func main() {
	try.Main("$pkg", firstArg, functions, args)
}
var functions = map[string] interface{}{
!

for (i in $functions) {
	echo $"T"$i": $i,
}
echo '}'

echo 'var args = []interface{}{'

if (~ $# 1) {
	echo $"{T}^'toSlice('^$1^')',
}
if not {
	for (i in *) {
		echo $"T^$i,
	}
}
echo '}'

cat <<!
var firstArg = $BQ$1$BQ
var _ os.Error
func toSlice(a ...interface{}) []interface{} { return a }
!

}>$file.go

$GC $file.go &&
	$GL -o $file $file.$O &&
	$file _^$*
rm -f $file $file.go $file.$O
