Initial revision
This commit is contained in:
parent
8b90e3480a
commit
e97f144bf4
3 changed files with 138 additions and 0 deletions
62
include/occam/dec.ocm
Normal file
62
include/occam/dec.ocm
Normal file
|
@ -0,0 +1,62 @@
|
|||
-- decin/decout - Decimal i/o
|
||||
|
||||
proc decin(chan in, var d, c)=
|
||||
-- Reads a decimal number from input into d. c is used as the first input
|
||||
-- character, and it contains the character just after the decimal number
|
||||
-- when decin exits.
|
||||
|
||||
var neg:
|
||||
seq
|
||||
while (c='*s') or (c='*t') or (c='*n')
|
||||
in ? c
|
||||
if
|
||||
(c='-') or (c='+')
|
||||
seq
|
||||
neg:=(c='-')
|
||||
in ? c
|
||||
true
|
||||
neg:=false
|
||||
d:=0
|
||||
while ('0'<=c) and (c<='9')
|
||||
seq
|
||||
d:=(d*10)+(c-'0')
|
||||
in ? c
|
||||
if
|
||||
neg
|
||||
d:= -d
|
||||
:
|
||||
proc decout(chan out, value d, w)=
|
||||
-- Sends d to channel out in decimal on a field of w spaces. It is just like
|
||||
-- fprintf(out, "%*d", w, d);
|
||||
|
||||
var dec[BYTE 12], di, dd, d0, neg:
|
||||
seq
|
||||
di:=0
|
||||
if
|
||||
d<0
|
||||
seq
|
||||
neg:=true
|
||||
dd:= -d
|
||||
d>=0
|
||||
seq
|
||||
neg:=false
|
||||
dd:=d
|
||||
d0:=1
|
||||
while d0<>0
|
||||
seq
|
||||
dec[BYTE di]:=(dd\10)+'0'
|
||||
di:=di+1
|
||||
dd:=dd/10
|
||||
d0:=dd
|
||||
if
|
||||
neg
|
||||
seq
|
||||
dec[BYTE di]:='-'
|
||||
di:=di+1
|
||||
seq i=[0 for w-di]
|
||||
out ! '*s'
|
||||
while di<>0
|
||||
seq
|
||||
di:=di-1
|
||||
out ! dec[BYTE di]
|
||||
:
|
36
include/occam/printd.ocm
Normal file
36
include/occam/printd.ocm
Normal file
|
@ -0,0 +1,36 @@
|
|||
proc printd(chan out, value fmt[], d)=
|
||||
-- Like fprintf(out, fmt, d), with only %d or %<width>d in fmt[]
|
||||
|
||||
def otherwise=true:
|
||||
var i, len:
|
||||
seq
|
||||
len:=fmt[byte 0]
|
||||
i:=1
|
||||
|
||||
while i<=len
|
||||
if
|
||||
fmt[byte i] = '%'
|
||||
var w:
|
||||
seq
|
||||
i:=i+1
|
||||
w:=0
|
||||
while (i<=len) and ('0'<=fmt[byte i]) and (fmt[byte i]<='9')
|
||||
seq
|
||||
w:=(w*10)+(fmt[byte i]-'0')
|
||||
i:=i+1
|
||||
if
|
||||
i<=len
|
||||
var key:
|
||||
seq
|
||||
key:=fmt[byte i]
|
||||
if
|
||||
key='d'
|
||||
decout(out, d, w)
|
||||
otherwise
|
||||
out ! key
|
||||
i:=i+1
|
||||
otherwise
|
||||
seq
|
||||
out ! fmt[byte i]
|
||||
i:=i+1
|
||||
:
|
40
include/occam/prints.ocm
Normal file
40
include/occam/prints.ocm
Normal file
|
@ -0,0 +1,40 @@
|
|||
proc prints(chan out, value fmt[], str[]) =
|
||||
-- Like fprintf(out, fmt, str), with only %s or %<width>s in fmt[]
|
||||
|
||||
def otherwise=true:
|
||||
var i, len:
|
||||
seq
|
||||
len:=fmt[byte 0]
|
||||
i:=1
|
||||
|
||||
while i<=len
|
||||
if
|
||||
fmt[byte i] = '%'
|
||||
var w:
|
||||
seq
|
||||
i:=i+1
|
||||
w:=0
|
||||
while (i<=len) and ('0'<=fmt[byte i]) and (fmt[byte i]<='9')
|
||||
seq
|
||||
w:=(w*10)+(fmt[byte i]-'0')
|
||||
i:=i+1
|
||||
if
|
||||
i<=len
|
||||
var key:
|
||||
seq
|
||||
key:=fmt[byte i]
|
||||
if
|
||||
key='s'
|
||||
seq
|
||||
seq i=[0 for w-str[byte 0]]
|
||||
out ! ' '
|
||||
seq i=[0 for str[byte 0]]
|
||||
out ! str[byte i]
|
||||
otherwise
|
||||
out ! key
|
||||
i:=i+1
|
||||
otherwise
|
||||
seq
|
||||
out ! fmt[byte i]
|
||||
i:=i+1
|
||||
:
|
Loading…
Reference in a new issue