Jump to content

Hoc (programming language)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nyh (talk | contribs) at 14:44, 5 April 2012 (clarify in which version of Research Unix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

hoc, an acronym for High Order Calculator, is an interpreted programming language that was used in the 1984 book The Unix Programming Environment to demonstrate how to build interpreters using Yacc.

Hoc was developed by Brian Kernighan and Rob Pike as a glorified interactive calculator. Its basic functionality is to evaluate floating-point numerical expressions, e.g., "1+2*sin(0.7)". Then, variables were added, conditionals, loops, user-defined functions, simple IO, and more, using a syntax resembling C.

An improved Hoc interpreter was included in Eighth Edition Research Unix in 1985, but it has not been generally adopted by commercial Unix systems or by Linux distributions. Instead, the earlier calculator languages dc and bc have become widespread on those systems. Hoc survived and continued to evolve as part of the Plan 9 operating system. Several improved versions of Hoc were released as free software by Bell Labs and other individuals (see list below).

Examples

The following is a simple example of an interactive calculator session in Hoc; bold text represents hoc's output:

1+2*3
    7
angle=PI/3
r=sin(angle)
r
    0.866025
r*2
    1.73205

And a simple example of functions and flow control:

func atan2(){
        if($1>0){
                return atan($2/$1)
        } else if ($1<0){
                return atan($2/$1)+PI
        } else if ($2>0){
                return PI/2
        } else if ($2<0){
                return -PI/2
        } else {
                print "atan2 domain error"
                return 0
        }
}

atan2(2,3)
    0.982794
atan2(0,0)
atan2 domain error
    0.0

References

  • Kernighan, Brian W.; Pike, Rob (1984). The Unix Programming Environment. Prentice Hall. ISBN 013937681X.

Hoc implementations and versions