1
0
mirror of https://github.com/golang/go synced 2024-11-23 05:20:11 -07:00
go/usr/austin/eval/abort.go
Austin Clements c90bc34d75 Implement cap, len, and make, as well as the general framework
for built-in functions and type conversions.  Extract out
common operations on expression nodes for converting them to
ints and implicitly dereferencing arrays.

R=rsc
APPROVED=rsc
DELTA=442  (365 added, 50 deleted, 27 changed)
OCL=34064
CL=34064
2009-08-28 18:03:03 -07:00

88 lines
1.8 KiB
Go

// Copyright 2009 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.
package eval
import (
"fmt";
"os";
"runtime";
)
// TODO(austin) This is not thread-safe. We could include the abort
// channel in the Frame structure, but then the Value methods need to
// take the Frame. However, passing something to the Value methods
// might be necessary to generate back traces.
var abortChan = make(chan os.Error)
// Abort aborts the current computation. If this is called within the
// extent of a Try call, this immediately returns to the Try with the
// given error. If not, then this panic's.
func Abort(e os.Error) {
if abortChan == nil {
panic("Abort: " + e.String());
}
abortChan <- e;
runtime.Goexit();
}
// Try executes a computation with the ability to Abort.
func Try(f func()) os.Error {
abortChan = make(chan os.Error);
go func() {
f();
abortChan <- nil;
}();
res := <-abortChan;
abortChan = nil;
return res;
}
type DivByZero struct {}
func (DivByZero) String() string {
return "divide by zero";
}
type NilPointer struct {}
func (NilPointer) String() string {
return "nil pointer dereference";
}
type IndexOutOfBounds struct {
Idx, Len int64;
}
func (e IndexOutOfBounds) String() string {
if e.Idx < 0 {
return fmt.Sprintf("negative index: %d", e.Idx);
}
return fmt.Sprintf("index %d exceeds length %d", e.Idx, e.Len);
}
type KeyNotFound struct {
Key interface {};
}
func (e KeyNotFound) String() string {
return fmt.Sprintf("key '%v' not found in map", e.Key);
}
type NegativeLength struct {
Len int64;
}
func (e NegativeLength) String() string {
return fmt.Sprintf("negative length: %d", e.Len);
}
type NegativeCapacity struct {
Len int64;
}
func (e NegativeCapacity) String() string {
return fmt.Sprintf("negative capacity: %d", e.Len);
}