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