Skip to content

Latest commit

 

History

History
74 lines (58 loc) · 1.97 KB

README.md

File metadata and controls

74 lines (58 loc) · 1.97 KB

hoc - higher order calculator

Overview

hoc is a command-line calculator directly inspired by Kernighan and Pike[1].
The structure of the input accepted by hoc is specified by a formal grammar, and the Look-Ahead Left-Right parser for this grammar is automatically generated by yacc[2].

The accompanying low-level input lexical analyzer for hoc is written in Go, thus goyacc is used as the parser-generator to generate a Go parser and control the input process.

A feature of hoc is that it supports 64-bit floating point values as opposed to simple 64-bit integer values. Thus a possible name for the program could be fhoc, but I'll leave that as an exercise for the reader who forks the project.

Requirements

Built-in Functions

  • sin, asin
  • cos, acos
  • tan, atan
  • log, log2, log10
  • sqrt, exponentiation with ^
  • PI
  • todeg, torad

Generate, compile and run hoc

# As of go 1.20.2:
$ go install golang.org/x/tools/cmd/goyacc@latest
$ cd src/github.com/jfinken/hoc
$ goyacc -o hoc.go -p Hoc hoc.y
$ go mod tidy
$ go install

# As of early versions of go:
$ go get golang.org/x/tools/cmd/goyacc
$ go get github.com/chzyer/readline
$ goyacc -o hoc.go -p Hoc hoc.y
$ go build 

then:

$ hoc
hoc> 2.35 * 4.5
10.5750
hoc> a = 2*1024*1024
hoc> a
2097152.0000
hoc> a / 2048
1024.0000
hoc> acos(-1)
3.1416
hoc> a = 4^2; sqrt(a)
4.0000
hoc> 2*PI*todeg
360.0000

References

  1. B. W. Kernighan and R. Pike, The UNIX Programming Environment, AT&T Bell Labs, Prentice-Hall, 1984.
  2. S. C. Johnson, Yacc: Yet Another Compiler-Compiler, AT&T Bell Labs, Murray Hill, New Jersey.