Article View: comp.lang.misc
Article #3605Re: Type Systems and Dynamic Binding
From: Joseph Allen
Date: Fri, 19 Apr 1991 08:52
Date: Fri, 19 Apr 1991 08:52
36 lines
2040 bytes
2040 bytes
In article <17APR91.21054415@uc780.umd.edu> cs450a03@uc780.umd.edu writes:
>Let's say I was trying to implement a dynamically typed language in C.
>(Of course, I wouldn't _really_ be that crazy :-) Let's say I wanted
>to detect when, for instance, the addition of 2 integers produces an
>invalid result. Let's say that I can not accept a severe performance
>hit for this checking (most of the time, it's not going to happen.
>When it does happen, I'm going to switch to float -- performance must
>be better than floating point).
>How do I do it?
#define add(a,b) (a<0 && b<0 && a+b>=0 || a>=0 && b>=0 && a+b<0 ? error():a+b)
:-)
I believe C is not really the problem. What you want is for the processor
to give an integer overflow signal so that the calculation can be repeated
in floating point. Unfortunately most (all?) don't.
Even in an interpretive language you'd have to:
add
bov error
sub
bov error
mul
bov error
which is quite gross.
--
#define h 23 /* Height */ /* jallen@ic.sunysb.edu (129.49.12.74) */
#define w 79 /* Width */ /* Amazing */
int i,r,b[]={-w,w,1,-1},d,a[w*h];m(p){a[p]=2;while(d=(p>2*w?!a[p-w-w]?1:0:0)|(
p<w*(h-2)?!a[p+w+w]?2:0:0)|(p%w!=w-2?!a[p+2]?4:0:0)|(p%w!=1?!a[p-2]?8:0:0)){do
i=3&(r=(r*57+1))/d;while(!(d&(1<<i)));a[p+b[i]]=2;m(p+2*b[i]);}}main(){r=time(
0L);m(w+1);for(i=0;i%w?0:printf("\n"),i!=w*h;i++)printf("#\0 "+a[i]);}
Technical Details
Message-ID:
<1991Apr19.085233.2352@sbcs.sunysb.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!hsdndev!cmcl2!sbcs!eeserv1.ic.sunysb.edu!jallenReferences:
<3843@ssc-bee.ssc-vax.UUCP> <27313:Apr1623:30:0391@kramden.acf.nyu.edu> <17APR91.21054415@uc780.umd.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!hsdndev!cmcl2!sbcs!eeserv1.ic.sunysb.edu!jallen From: jallen@eeserv1.ic.sunysb.edu (Joseph Allen) Newsgroups: comp.lang.misc,comp.object Subject: Re: Type Systems and Dynamic Binding Message-ID: <1991Apr19.085233.2352@sbcs.sunysb.edu> Date: 19 Apr 91 08:52:33 GMT References: <3843@ssc-bee.ssc-vax.UUCP> <27313:Apr1623:30:0391@kramden.acf.nyu.edu> <17APR91.21054415@uc780.umd.edu> Sender: usenet@sbcs.sunysb.edu (Usenet poster) Organization: State University of New York at Stony Brook Lines: 36