Commit f21e731c authored by Ken Thompson's avatar Ken Thompson

added printn and panicn

prints that insert spaces and new line

R=r
OCL=16370
CL=16370
parent 0216273c
......@@ -269,7 +269,7 @@ enum
ORETURN, OFOR, OIF, OSWITCH,
OAS, OASOP, OCASE, OXCASE, OFALL, OXFALL,
OGOTO, OPROC, ONEW, OEMPTY, OSELECT,
OLEN, OCAP, OPANIC, OPRINT, OTYPEOF,
OLEN, OCAP, OPANIC, OPANICN, OPRINT, OPRINTN, OTYPEOF,
OOROR,
OANDAND,
......@@ -695,7 +695,7 @@ Node* ascompatee(int, Node**, Node**);
Node* ascompatet(int, Node**, Type**, int);
Node* ascompatte(int, Type**, Node**, int);
int ascompat(Type*, Type*);
Node* prcompat(Node*);
Node* prcompat(Node*, int);
Node* nodpanic(int32);
Node* newcompat(Node*);
Node* stringop(Node*, int);
......
......@@ -18,7 +18,7 @@
%token <sym> LPACKAGE LIMPORT LEXPORT
%token <sym> LMAP LCHAN LINTERFACE LFUNC LSTRUCT
%token <sym> LCOLAS LFALL LRETURN
%token <sym> LNEW LLEN LCAP LTYPEOF LPANIC LPRINT
%token <sym> LNEW LLEN LCAP LTYPEOF LPANIC LPANICN LPRINT LPRINTN
%token <sym> LVAR LTYPE LCONST LCONVERT LSELECT
%token <sym> LFOR LIF LELSE LSWITCH LCASE LDEFAULT
%token <sym> LBREAK LCONTINUE LGO LGOTO LRANGE
......@@ -361,10 +361,18 @@ noninc_stmt:
{
$$ = nod(OPRINT, $3, N);
}
| LPRINTN '(' oexpr_list ')'
{
$$ = nod(OPRINTN, $3, N);
}
| LPANIC '(' oexpr_list ')'
{
$$ = nod(OPANIC, $3, N);
}
| LPANICN '(' oexpr_list ')'
{
$$ = nod(OPANICN, $3, N);
}
inc_stmt:
expr LINC
......@@ -896,7 +904,9 @@ keyword:
| LLEN
| LCAP
| LPANIC
| LPANICN
| LPRINT
| LPRINTN
| LNEW
| LBASETYPE
| LTYPEOF
......
......@@ -998,7 +998,9 @@ static struct
"nil", LNIL, Txxx,
"package", LPACKAGE, Txxx,
"panic", LPANIC, Txxx,
"panicn", LPANICN, Txxx,
"print", LPRINT, Txxx,
"printn", LPRINTN, Txxx,
"range", LRANGE, Txxx,
"return", LRETURN, Txxx,
"select", LSELECT, Txxx,
......
......@@ -693,7 +693,9 @@ opnames[] =
[OFALL] = "FALL",
[OXFALL] = "XFALL",
[OPANIC] = "PANIC",
[OPANICN] = "PANICN",
[OPRINT] = "PRINT",
[OPRINTN] = "PRINTN",
[OXXX] = "XXX",
};
......
......@@ -17,6 +17,8 @@ func printint(int64);
func printstring(string);
func printpointer(*any);
func printinter(any);
func printnl();
func printsp();
func catstring(string, string) string;
func cmpstring(string, string) int32;
......@@ -92,6 +94,8 @@ export
printstring
printpointer
printinter
printnl
printsp
// op string
catstring
......
This diff is collapsed.
......@@ -124,7 +124,9 @@ loop:
case OSEND:
case ORECV:
case OPRINT:
case OPRINTN:
case OPANIC:
case OPANICN:
case OFOR:
case OIF:
case OSWITCH:
......@@ -203,14 +205,28 @@ loop:
if(top != Etop)
goto nottop;
walktype(n->left, Erv);
indir(n, prcompat(n->left));
indir(n, prcompat(n->left, 0));
goto ret;
case OPRINTN:
if(top != Etop)
goto nottop;
walktype(n->left, Erv);
indir(n, prcompat(n->left, 1));
goto ret;
case OPANIC:
if(top != Etop)
goto nottop;
walktype(n->left, Erv);
indir(n, list(prcompat(n->left), nodpanic(n->lineno)));
indir(n, list(prcompat(n->left, 0), nodpanic(n->lineno)));
goto ret;
case OPANICN:
if(top != Etop)
goto nottop;
walktype(n->left, Erv);
indir(n, list(prcompat(n->left, 1), nodpanic(n->lineno)));
goto ret;
case OLITERAL:
......@@ -1598,23 +1614,33 @@ ascompat(Type *t1, Type *t2)
}
Node*
prcompat(Node *n)
prcompat(Node *n, int fmt)
{
Node *l, *r;
Node *on;
Type *t;
Iter save;
int w;
int w, notfirst;
r = N;
l = listfirst(&save, &n);
notfirst = 0;
loop:
if(l == N) {
if(fmt) {
on = syslook("printnl", 0);
r = list(r, nod(OCALL, on, N));
}
walktype(r, Etop);
return r;
}
if(notfirst) {
on = syslook("printsp", 0);
r = list(r, nod(OCALL, on, N));
}
w = whatis(l);
switch(w) {
default:
......@@ -1663,12 +1689,10 @@ loop:
l->type = t;
}
if(r == N)
r = nod(OCALL, on, l);
else
r = list(r, nod(OCALL, on, l));
r = list(r, nod(OCALL, on, l));
out:
notfirst = fmt;
l = listnext(&save);
goto loop;
}
......
......@@ -166,3 +166,15 @@ sys·printstring(string v)
if(v != nil)
sys·write(1, v->str, v->len);
}
void
sys·printsp(void)
{
sys·write(1, " ", 1);
}
void
sys·printnl(void)
{
sys·write(1, "\n", 1);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment