lex.c 17.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.


#define		EXTERN
#include	"go.h"
#include	"y.tab.h"

#define	DBG	if(!debug['x']);else print
enum
{
	EOF		= -1,
};

int
17
mainlex(int argc, char *argv[])
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
{
	int c;

	outfile = nil;
	package = "____";
	ARGBEGIN {
	default:
		c = ARGC();
		if(c >= 0 && c < sizeof(debug))
			debug[c]++;
		break;

	case 'o':
		outfile = ARGF();
		break;

	case 'k':
		package = ARGF();
		break;
	} ARGEND

	if(argc != 1)
		goto usage;

42 43 44 45
	pathname = mal(100);
	if(mygetwd(pathname, 99) == 0)
		strcpy(pathname, "/???");

46 47 48 49 50 51 52
	fmtinstall('O', Oconv);		// node opcodes
	fmtinstall('E', Econv);		// etype opcodes
	fmtinstall('J', Jconv);		// all the node flags
	fmtinstall('S', Sconv);		// sym pointer
	fmtinstall('T', Tconv);		// type pointer
	fmtinstall('N', Nconv);		// node pointer
	fmtinstall('Z', Zconv);		// escaped string
53
	fmtinstall('L', Lconv);		// line number
54 55
	
	lexinit();
56
	lineno = 1;
57

Ken Thompson's avatar
Ken Thompson committed
58
	infile = argv[0];
59
	linehist(infile, 0);
60

61 62
	curio.infile = infile;
	curio.bin = Bopen(infile, OREAD);
63
	if(curio.bin == nil)
64 65
		fatal("cant open: %s", infile);
	curio.peekc = 0;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

	externdcl = mal(sizeof(*externdcl));
	externdcl->back = externdcl;
	dclcontext = PEXTERN;

	exportlist = mal(sizeof(*exportlist));
	exportlist->back = exportlist;

	// function field skeleton
	fskel = nod(OLIST, N, nod(OLIST, N, N));
	fskel->left = nod(ODCLFIELD, N, N);
	fskel->right->left = nod(ODCLFIELD, N, N);
	fskel->right->right = nod(ODCLFIELD, N, N);

	nerrors = 0;
	yyparse();

83
	linehist(nil, 0);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	Bterm(curio.bin);

	if(nerrors)
		errorexit();

	dumpobj();

	myexit(0);
	return 0;

usage:
	print("flags:\n");
	print("  -d print declarations\n");
	print("  -f print stack frame structure\n");
	print("  -k name specify package name\n");
	print("  -o file specify output file\n");
	print("  -p print the assembly language\n");
	print("  -w print the parse tree after typing\n");
	print("  -x print lex tokens\n");
	print("  -h panic on an error\n");
	myexit(0);
	return 0;
}

void
importfile(Val *f)
{
	Biobuf *imp;
112
	char *file;
113 114 115 116 117 118 119
	long c;

	if(f->ctype != CTSTR) {
		yyerror("import statement not a string");
		return;
	}
	// BOTCH need to get .8 from backend
Ken Thompson's avatar
Ken Thompson committed
120
	snprint(namebuf, sizeof(namebuf), "%Z.6", f->sval);
121 122
	file = strdup(namebuf);
	linehist(file, 0);
123

124 125 126
	imp = Bopen(file, OREAD);
	if(imp == nil)
		fatal("cant open import: %s", namebuf);
127 128 129 130 131 132 133 134

	/*
	 * position the input right
	 * after (( and return
	 */
	pushedio = curio;
	curio.bin = imp;
	curio.peekc = 0;
135
	curio.infile = file;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	for(;;) {
		c = getc();
		if(c == EOF)
			break;
		if(c != '(')
			continue;
		c = getc();
		if(c == EOF)
			break;
		if(c != '(')
			continue;
		return;
	}
	yyerror("no import in: %Z", f->sval);
	unimportfile();
}

void
unimportfile(void)
{
156 157
	linehist(nil, 0);

158 159 160 161 162 163
	if(curio.bin != nil) {
		Bterm(curio.bin);
		curio.bin = nil;
	}
	curio = pushedio;
	pushedio.bin = nil;
Ken Thompson's avatar
Ken Thompson committed
164
	inimportsys = 0;
165 166 167 168 169
}

void
cannedimports(void)
{
170 171 172 173 174
	char *file;

	file = "sys.6";
	linehist(file, 0);

175 176 177
	pushedio = curio;
	curio.bin = nil;
	curio.peekc = 0;
178
	curio.infile = file;
179
	curio.cp = sysimport;
180

181
	pkgmyname = S;
Ken Thompson's avatar
Ken Thompson committed
182
	inimportsys = 1;
183 184 185 186 187
}

long
yylex(void)
{
Ken Thompson's avatar
Ken Thompson committed
188 189
	int c, c1;
	vlong v;
190 191
	char *cp;
	Rune rune;
Ken Thompson's avatar
Ken Thompson committed
192
	int escflag;
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	Sym *s;

l0:
	c = getc();
	if(isspace(c))
		goto l0;

	if(c >= Runeself) {
		/* all multibyte runes are alpha */
		cp = namebuf;
		goto talph;
	}

	if(isalpha(c)) {
		cp = namebuf;
		goto talph;
	}

	if(isdigit(c))
		goto tnum;

	switch(c) {
	case EOF:
		ungetc(EOF);
		return -1;

	case '_':
		cp = namebuf;
		goto talph;

	case '.':
		c1 = getc();
		if(isdigit(c1)) {
			cp = namebuf;
			*cp++ = c;
			c = c1;
			c1 = 0;
			goto casedot;
		}
		break;

	case '"':
		/* "..." */
		strcpy(namebuf, "\"<string>\"");
		cp = mal(sizeof(long));
		c1 = 4;

	caseq:
		for(;;) {
Ken Thompson's avatar
Ken Thompson committed
242
			if(escchar('"', &escflag, &v))
243
				break;
Ken Thompson's avatar
Ken Thompson committed
244
			if(v < Runeself || escflag) {
Ken Thompson's avatar
Ken Thompson committed
245 246 247
				cp = remal(cp, c1, 1);
				cp[c1++] = v;
			} else {
Ken Thompson's avatar
Ken Thompson committed
248 249
				// botch - this limits size of runes
				rune = v;
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
				c = runelen(rune);
				cp = remal(cp, c1, c);
				runetochar(cp+c1, &rune);
				c1 += c;
			}
		}
		goto catem;

	case '`':
		/* `...` */
		strcpy(namebuf, "`<string>`");
		cp = mal(sizeof(long));
		c1 = 4;

	casebq:
		for(;;) {
			c = getc();
			if(c == EOF || c == '`')
				break;
			cp = remal(cp, c1, 1);
			cp[c1++] = c;
		}

	catem:
		for(;;) {
			/* it takes 2 peekc's to skip comments */
			c = getc();
			if(isspace(c))
				continue;
			if(c == '"')
				goto caseq;
			if(c == '`')
				goto casebq;
			ungetc(c);
			break;
		}

		*(long*)cp = c1-4;	// length
		do {
			cp = remal(cp, c1, 1);
			cp[c1++] = 0;
		} while(c1 & MAXALIGN);
		yylval.val.sval = (String*)cp;
		yylval.val.ctype = CTSTR;
		DBG("lex: string literal\n");
		return LLITERAL;

	case '\'':
		/* '.' */
Ken Thompson's avatar
Ken Thompson committed
299
		if(escchar('\'', &escflag, &v))
Ken Thompson's avatar
Ken Thompson committed
300
			v = '\'';	// allow '''
Ken Thompson's avatar
Ken Thompson committed
301
		if(!escchar('\'', &escflag, &v)) {
302
			yyerror("missing '");
Ken Thompson's avatar
Ken Thompson committed
303
			ungetc(v);
304
		}
Ken Thompson's avatar
Ken Thompson committed
305
		yylval.val.vval = v;
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
		yylval.val.ctype = CTINT;
		DBG("lex: codepoint literal\n");
		return LLITERAL;

	case '/':
		c1 = getc();
		if(c1 == '*') {
			for(;;) {
				c = getr();
				while(c == '*') {
					c = getr();
					if(c == '/')
						goto l0;
				}
				if(c == EOF) {
					yyerror("eof in comment");
					errorexit();
				}
			}
		}
		if(c1 == '/') {
			for(;;) {
				c = getr();
				if(c == '\n')
					goto l0;
				if(c == EOF) {
					yyerror("eof in comment");
					errorexit();
				}
			}
		}
		if(c1 == '=') {
			c = ODIV;
			goto asop;
		}
		break;

	case ':':
		c1 = getc();
		if(c1 == '=') {
			c = LCOLAS;
			goto lx;
		}
		break;

	case '*':
		c1 = getc();
		if(c1 == '=') {
			c = OMUL;
			goto asop;
		}
		break;

	case '%':
		c1 = getc();
		if(c1 == '=') {
			c = OMOD;
			goto asop;
		}
		break;

	case '+':
		c1 = getc();
		if(c1 == '+') {
			c = LINC;
			goto lx;
		}
		if(c1 == '=') {
			c = OADD;
			goto asop;
		}
		break;

	case '-':
		c1 = getc();
		if(c1 == '-') {
			c = LDEC;
			goto lx;
		}
		if(c1 == '=') {
			c = OSUB;
			goto asop;
		}
		break;

	case '>':
		c1 = getc();
		if(c1 == '>') {
			c = LRSH;
			c1 = getc();
			if(c1 == '=') {
				c = ORSH;
				goto asop;
			}
			break;
		}
		if(c1 == '=') {
			c = LGE;
			goto lx;
		}
		c = LGT;
		break;

	case '<':
		c1 = getc();
		if(c1 == '<') {
			c = LLSH;
			c1 = getc();
			if(c1 == '=') {
				c = OLSH;
				goto asop;
			}
			break;
		}
		if(c1 == '=') {
			c = LLE;
			goto lx;
		}
		c = LLT;
		break;

	case '=':
		c1 = getc();
		if(c1 == '=') {
			c = LEQ;
			goto lx;
		}
		break;

	case '!':
		c1 = getc();
		if(c1 == '=') {
			c = LNE;
			goto lx;
		}
		break;

	case '&':
		c1 = getc();
		if(c1 == '&') {
			c = LANDAND;
			goto lx;
		}
		if(c1 == '=') {
			c = OAND;
			goto asop;
		}
		break;

	case '|':
		c1 = getc();
		if(c1 == '|') {
			c = LOROR;
			goto lx;
		}
		if(c1 == '=') {
			c = OOR;
			goto asop;
		}
		break;

	case '^':
		c1 = getc();
		if(c1 == '=') {
			c = OXOR;
			goto asop;
		}
		break;

	default:
		goto lx;
	}
	ungetc(c1);

lx:
	if(c > 0xff)
		DBG("lex: TOKEN %s\n", lexname(c));
	else
		DBG("lex: TOKEN '%c'\n", c);
	return c;

asop:
	yylval.val.vval = c;	// rathole to hold which asop
	DBG("lex: TOKEN ASOP %c\n", c);
	return LASOP;

talph:
	/*
	 * cp is set to namebuf and some
	 * prefix has been stored
	 */
	for(;;) {
		if(c >= Runeself) {
			for(c1=0;;) {
				cp[c1++] = c;
				if(fullrune(cp, c1))
					break;
				c = getc();
			}
			cp += c1;
			c = getc();
			continue;
		}
		if(!isalnum(c) && c != '_')
			break;
		*cp++ = c;
		c = getc();
	}
	*cp = 0;
	ungetc(c);

	s = lookup(namebuf);
	if(s->lexical == LIGNORE)
		goto l0;

	if(context != nil) {
		s = pkglookup(s->name, context);
		if(s->lexical == LIGNORE)
			goto l0;
	}

	DBG("lex: %S %s\n", s, lexname(s->lexical));
	yylval.sym = s;
	if(s->lexical == LBASETYPE)
		return LATYPE;
	return s->lexical;

tnum:
	c1 = 0;
	cp = namebuf;
	if(c != '0') {
		for(;;) {
			*cp++ = c;
			c = getc();
			if(isdigit(c))
				continue;
			goto dc;
		}
	}
	*cp++ = c;
	c = getc();
	if(c == 'x' || c == 'X')
		for(;;) {
			*cp++ = c;
			c = getc();
			if(isdigit(c))
				continue;
			if(c >= 'a' && c <= 'f')
				continue;
			if(c >= 'A' && c <= 'F')
				continue;
			if(cp == namebuf+2)
				yyerror("malformed hex constant");
			goto ncu;
		}
	if(c < '0' || c > '7')
		goto dc;
	for(;;) {
		if(c >= '0' && c <= '7') {
			*cp++ = c;
			c = getc();
			continue;
		}
		goto ncu;
	}

dc:
	if(c == '.')
		goto casedot;
	if(c == 'e' || c == 'E')
		goto casee;

ncu:
	*cp = 0;
	ungetc(c);
	if(mpatov(namebuf, &yylval.val.vval)) {
		yyerror("overflow in constant");
		yylval.val.vval = 0;
	}
	yylval.val.ctype = CTINT;
	DBG("lex: integer literal\n");
	return LLITERAL;

casedot:
	for(;;) {
		*cp++ = c;
		c = getc();
		if(!isdigit(c))
			break;
	}
	if(c != 'e' && c != 'E')
		goto caseout;

casee:
	*cp++ = 'e';
	c = getc();
	if(c == '+' || c == '-') {
		*cp++ = c;
		c = getc();
	}
	if(!isdigit(c))
		yyerror("malformed fp constant exponent");
	while(isdigit(c)) {
		*cp++ = c;
		c = getc();
	}

caseout:
	*cp = 0;
	ungetc(c);
	if(mpatof(namebuf, &yylval.val.dval)) {
		yyerror("overflow in float constant");
		yylval.val.dval = 0;
	}
	yylval.val.ctype = CTFLT;
	DBG("lex: floating literal\n");
	return LLITERAL;
}

int
getc(void)
{
	int c;

	c = curio.peekc;
	if(c != 0) {
		curio.peekc = 0;
		if(c == '\n')
634
			lineno++;
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
		return c;
	}

	if(curio.bin == nil) {
		c = *curio.cp & 0xff;
		if(c != 0)
			curio.cp++;
	} else
		c = Bgetc(curio.bin);

	switch(c) {
	case 0:
	case EOF:
		return EOF;

	case '\n':
651
		lineno++;
652 653 654 655 656 657 658 659 660 661
		break;
	}
	return c;
}

void
ungetc(int c)
{
	curio.peekc = c;
	if(c == '\n')
662
		lineno--;
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
}

long
getr(void)
{
	int c, i;
	char str[UTFmax+1];
	Rune rune;

	c = getc();
	if(c < Runeself)
		return c;
	i = 0;
	str[i++] = c;

loop:
	c = getc();
	str[i++] = c;
	if(!fullrune(str, i))
		goto loop;
	c = chartorune(&rune, str);
	if(rune == Runeerror && c == 1) {
		yyerror("illegal rune in string");
		for(c=0; c<i; c++)
			print(" %.2x", *(uchar*)(str+c));
		print("\n");
	}
	return rune;
}

int
getnsc(void)
{
	int c;

	c = getc();
	for(;;) {
		if(!isspace(c))
			return c;
		if(c == '\n') {
703
			lineno++;
704 705 706 707 708 709 710 711
			return c;
		}
		c = getc();
	}
	return 0;
}


Ken Thompson's avatar
Ken Thompson committed
712
int
Ken Thompson's avatar
Ken Thompson committed
713
escchar(int e, int *escflg, vlong *val)
714
{
Ken Thompson's avatar
Ken Thompson committed
715
	int i, c;
Ken Thompson's avatar
Ken Thompson committed
716
	vlong l;
717

Ken Thompson's avatar
Ken Thompson committed
718 719
	*escflg = 0;

720 721 722 723
loop:
	c = getr();
	if(c == '\n') {
		yyerror("newline in string");
Ken Thompson's avatar
Ken Thompson committed
724
		return 1;
725 726 727
	}
	if(c != '\\') {
		if(c == e)
Ken Thompson's avatar
Ken Thompson committed
728 729 730
			return 1;
		*val = c;
		return 0;
731
	}
Ken Thompson's avatar
Ken Thompson committed
732

733 734 735 736 737 738
	c = getr();
	switch(c) {
	case '\n':
		goto loop;

	case 'x':
Ken Thompson's avatar
Ken Thompson committed
739
		*escflg = 1;	// it's a byte
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
		i = 2;
		goto hex;

	case 'u':
		i = 4;
		goto hex;

	case 'U':
		i = 8;
		goto hex;

	case '0':
	case '1':
	case '2':
	case '3':
	case '4':
	case '5':
	case '6':
	case '7':
Ken Thompson's avatar
Ken Thompson committed
759
		*escflg = 1;	// it's a byte
760 761
		goto oct;

Ken Thompson's avatar
Ken Thompson committed
762 763 764 765 766 767 768 769
	case 'a': c = '\a'; break;
	case 'b': c = '\b'; break;
	case 'f': c = '\f'; break;
	case 'n': c = '\n'; break;
	case 'r': c = '\r'; break;
	case 't': c = '\t'; break;
	case 'v': c = '\v'; break;
	case '\\': c = '\\'; break;
770 771 772

	default:
		if(c != e)
Ken Thompson's avatar
Ken Thompson committed
773
			warn("unknown escape sequence: %c", c);
774
	}
Ken Thompson's avatar
Ken Thompson committed
775 776
	*val = c;
	return 0;
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797

hex:
	l = 0;
	for(; i>0; i--) {
		c = getc();
		if(c >= '0' && c <= '9') {
			l = l*16 + c-'0';
			continue;
		}
		if(c >= 'a' && c <= 'f') {
			l = l*16 + c-'a' + 10;
			continue;
		}
		if(c >= 'A' && c <= 'F') {
			l = l*16 + c-'A' + 10;
			continue;
		}
		warn("non-hex character in escape sequence: %c", c);
		ungetc(c);
		break;
	}
Ken Thompson's avatar
Ken Thompson committed
798 799
	*val = l;
	return 0;
800 801 802 803 804 805 806 807 808 809 810 811 812 813

oct:
	l = c - '0';
	for(i=2; i>0; i--) {
		c = getc();
		if(c >= '0' && c <= '7') {
			l = l*8 + c-'0';
			continue;
		}
		warn("non-oct character in escape sequence: %c", c);
		ungetc(c);
	}
	if(l > 255)
		warn("oct escape value > 255: %d", l);
Ken Thompson's avatar
Ken Thompson committed
814

Ken Thompson's avatar
Ken Thompson committed
815 816
	*val = l;
	return 0;
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
}

static	struct
{
	char*	name;
	int	lexical;
	int	etype;
} syms[] =
{
/*	name		lexical		etype
 */
/* basic types */
	"int8",		LBASETYPE,	TINT8,
	"int16",	LBASETYPE,	TINT16,
	"int32",	LBASETYPE,	TINT32,
	"int64",	LBASETYPE,	TINT64,

	"uint8",	LBASETYPE,	TUINT8,
	"uint16",	LBASETYPE,	TUINT16,
	"uint32",	LBASETYPE,	TUINT32,
	"uint64",	LBASETYPE,	TUINT64,

	"float32",	LBASETYPE,	TFLOAT32,
	"float64",	LBASETYPE,	TFLOAT64,
	"float80",	LBASETYPE,	TFLOAT80,

	"bool",		LBASETYPE,	TBOOL,
	"byte",		LBASETYPE,	TUINT8,
	"char",		LBASETYPE,	TUINT8,		// temp??
	"string",	LBASETYPE,	TSTRING,

848 849
	"any",		LBASETYPE,	TANY,

850
/* keywords */
851
//	"any",		LANY,		Txxx,
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
	"break",	LBREAK,		Txxx,
	"case",		LCASE,		Txxx,
	"chan",		LCHAN,		Txxx,
	"const",	LCONST,		Txxx,
	"continue",	LCONTINUE,	Txxx,
	"convert",	LCONVERT,	Txxx,
	"default",	LDEFAULT,	Txxx,
	"else",		LELSE,		Txxx,
	"export",	LEXPORT,	Txxx,
	"fallthrough",	LFALL,		Txxx,
	"false",	LFALSE,		Txxx,
	"for",		LFOR,		Txxx,
	"func",		LFUNC,		Txxx,
	"go",		LGO,		Txxx,
	"goto",		LGOTO,		Txxx,
	"if",		LIF,		Txxx,
	"import",	LIMPORT,	Txxx,
	"interface",	LINTERFACE,	Txxx,
	"iota",		LIOTA,		Txxx,
	"map",		LMAP,		Txxx,
	"new",		LNEW,		Txxx,
	"len",		LLEN,		Txxx,
	"nil",		LNIL,		Txxx,
	"package",	LPACKAGE,	Txxx,
	"panic",	LPANIC,		Txxx,
	"print",	LPRINT,		Txxx,
	"range",	LRANGE,		Txxx,
	"return",	LRETURN,	Txxx,
	"struct",	LSTRUCT,	Txxx,
	"switch",	LSWITCH,	Txxx,
	"true",		LTRUE,		Txxx,
	"type",		LTYPE,		Txxx,
	"var",		LVAR,		Txxx,
Ken Thompson's avatar
Ken Thompson committed
885
	"sys",		LPACK,		Txxx,
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922

	"notwithstanding",		LIGNORE,	Txxx,
	"thetruthofthematter",		LIGNORE,	Txxx,
	"despiteallobjections",		LIGNORE,	Txxx,
	"whereas",			LIGNORE,	Txxx,
	"insofaras",			LIGNORE,	Txxx,
};

void
lexinit(void)
{
	int i, etype, lex;
	Sym *s;
	Type *t;

	besetptr();

	for(i=TINT8; i<=TUINT64; i++)
		isint[i] = 1;
	for(i=TFLOAT32; i<=TFLOAT80; i++)
		isfloat[i] = 1;
	isptr[TPTR32] = 1;
	isptr[TPTR64] = 1;

	issigned[TINT8] = 1;
	issigned[TINT16] = 1;
	issigned[TINT32] = 1;
	issigned[TINT64] = 1;

	/*
	 * initialize okfor
	 */
	for(i=0; i<NTYPE; i++) {
		if(isint[i]) {
			okforeq[i] = 1;
			okforadd[i] = 1;
			okforand[i] = 1;
923
			issimple[i] = 1;
924 925 926 927
		}
		if(isfloat[i]) {
			okforeq[i] = 1;
			okforadd[i] = 1;
928
			issimple[i] = 1;
929 930 931
		}
		switch(i) {
		case TBOOL:
932 933
			issimple[i] = 1;

934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
		case TPTR32:
		case TPTR64:
			okforeq[i] = 1;
			break;
		}
		minfloatval[i] = 0.0;
		maxfloatval[i] = 0.0;
		minintval[i] = 0;
		maxintval[i] = 0;
	}

// this stuff smells - really need to do constants
// in multi precision arithmetic

	maxintval[TINT8] = 0x7f;
	minintval[TINT8] = -maxintval[TINT8]-1;
	maxintval[TINT16] = 0x7fff;
	minintval[TINT16] = -maxintval[TINT16]-1;
	maxintval[TINT32] = 0x7fffffffL;
	minintval[TINT32] = -maxintval[TINT32]-1;
	maxintval[TINT64] = 0x7fffffffffffffffLL;
	minintval[TINT64] = -maxintval[TINT64]-1;
	maxintval[TUINT8] = 0xff;
	maxintval[TUINT16] = 0xffff;
	maxintval[TUINT32] = 0xffffffffL;

	/* special case until we got to multiple precision */
	maxintval[TUINT64] = 0x7fffffffffffffffLL;
	minintval[TUINT64] = -maxintval[TUINT64]-1;

	maxfloatval[TFLOAT32] = 3.40282347e+38;
	minfloatval[TFLOAT32] = -maxfloatval[TFLOAT32];
	maxfloatval[TFLOAT64] = 1.7976931348623157e+308;
	minfloatval[TFLOAT64] = -maxfloatval[TFLOAT64]-1;

	/*
	 * initialize basic types array
	 * initialize known symbols
	 */
	for(i=0; i<nelem(syms); i++) {
		lex = syms[i].lexical;
		s = lookup(syms[i].name);
		s->lexical = lex;

		if(lex != LBASETYPE)
			continue;

		etype = syms[i].etype;
		if(etype < 0 || etype >= nelem(types))
			fatal("lexinit: %s bad etype", s->name);

		t = types[etype];
		if(t != T) {
			s->otype = t;
			continue;
		}
		t = typ(etype);
		switch(etype) {
		case TSTRING:
		case TCHAN:
		case TMAP:
			t = ptrto(t);
		}

		t->sym = s;
		t->recur = 1;	// supresses printing beyond name

		dowidth(t);
		types[etype] = t;
		s->otype = t;
	}

	/* pick up the backend typedefs */
	belexinit(LBASETYPE);

	booltrue = nod(OLITERAL, N, N);
	booltrue->val.ctype = CTBOOL;
	booltrue->val.vval = 1;
	booltrue->type = types[TBOOL];
	booltrue->addable = 1;

	boolfalse = nod(OLITERAL, N, N);
	boolfalse->val.ctype = CTBOOL;
	boolfalse->val.vval = 0;
	boolfalse->type = types[TBOOL];
	boolfalse->addable = 1;
}

struct
{
	int	lex;
	char*	name;
} lexn[] =
{
	LANDAND,	"ANDAND",
	LASOP,		"ASOP",
	LACONST,	"ACONST",
	LATYPE,		"ATYPE",
	LBASETYPE,	"BASETYPE",
	LBREAK,		"BREAK",
	LCASE,		"CASE",
	LCHAN,		"CHAN",
	LCOLAS,		"COLAS",
	LCONST,		"CONST",
	LCONTINUE,	"CONTINUE",
	LDEC,		"DEC",
	LELSE,		"ELSE",
	LEQ,		"EQ",
	LFUNC,		"FUNC",
	LGE,		"GE",
	LGO,		"GO",
	LGOTO,		"GOTO",
	LGT,		"GT",
	LIF,		"IF",
	LINC,		"INC",
	LINTERFACE,	"INTERFACE",
	LLE,		"LE",
	LLITERAL,	"LITERAL",
	LLSH,		"LSH",
	LLT,		"LT",
	LMAP,		"MAP",
	LNAME,		"NAME",
	LNE,		"NE",
	LOROR,		"OROR",
	LPACK,		"PACK",
	LRANGE,		"RANGE",
	LRETURN,	"RETURN",
	LRSH,		"RSH",
	LSTRUCT,	"STRUCT",
	LSWITCH,	"SWITCH",
	LTYPE,		"TYPE",
	LVAR,		"VAR",
	LFOR,		"FOR",
	LNEW,		"NEW",
	LLEN,		"LEN",
	LFALL,		"FALL",
	LCONVERT,	"CONVERT",
	LIOTA,		"IOTA",
	LPRINT,		"PRINT",
	LPACKAGE,	"PACKAGE",
	LIMPORT,	"IMPORT",
	LEXPORT,	"EXPORT",
	LPANIC,		"PANIC",
};

char*
lexname(int lex)
{
	int i;
	static char buf[100];

	for(i=0; i<nelem(lexn); i++)
		if(lexn[i].lex == lex)
			return lexn[i].name;
	snprint(buf, sizeof(buf), "LEX-%d", lex);
	return buf;
}

void
mkpackage(char* pkg)
{
	Sym *s;
	long h;
Ken Thompson's avatar
Ken Thompson committed
1097
	char *p;
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113

	if(bout != nil) {
		yyerror("mkpackage: called again %s %s", pkg, package);
		return;
	}

	// redefine all names to be this package
	package = pkg;
	for(h=0; h<NHASH; h++)
		for(s = hash[h]; s != S; s = s->link) {
			s->package = package;
			s->opackage = package;
		}

	if(outfile == nil) {
		// BOTCH need to get .6 from backend
Ken Thompson's avatar
Ken Thompson committed
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
		p = strrchr(infile, '/');
		if(p == nil)
			p = infile;
		else
			p = p+1;
		snprint(namebuf, sizeof(namebuf), "%s", p);
		p = strrchr(namebuf, '.');
		if(p != nil)
			*p = 0;
		strncat(namebuf, ".6", sizeof(namebuf));
1124 1125 1126
		outfile = strdup(namebuf);
	}
}