shithub: hammer

ref: 0c8cf4098baef78d7c3da9d3c029cdce5bd8ebb2
dir: /n.y/

View raw version
%{

#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ctype.h>

#include "dat.h"
#include "fns.h"

int goteof;
int lexline;
char *modname;
char *lexfile = "<stdin>";
int yyparse(void);
void yyerror(char*);

void
loaduse(char*)
{
	fprint(2, "not implement: loaduse\n");
}

%}

%union
{
	char	*sval;
	long	ival;
	Typ	*typ;
	Nod	*nod;
	Nlst	lst;
}

%token FUNC DEF IF FOR MOD USE OR AND NOTEQ EQ SHIFTL SHIFTR STRUCT ELSE ARROWR ARROWL GTEQ LTEQ
%token TYPE NAME NUM

%token	<sval>	NAME;
%token	<ival>	NUM

%type<nod>	arg expr logexpr shifexpr addexpr mulexpr
%type<nod>	unary prefexpr sufexpr compexpr top stmt
%type<nod>	decl
%type<lst>	args prog stmts
%type<typ>	type return

%%

prog
:	prog top		{ $$ = append($1, $2); }
|	top			{ $$ = append(ZL, $1); }

top
:	FUNC NAME '(' args ')' return '{' stmts '}'
				{ $$ = mkfunc($2, $4, $6, $8); }
|	MOD NAME ';'		{ $$ = nil; modname = $2; }
|	USE NAME ';'		{ $$ = nil; loaduse($2); }
|	def ';'			{ $$ = nil; }

sdecls
:
|	sdecls NAME ':' type ';'

def
:	DEF NAME NAME		{ }
|	DEF NAME STRUCT '{' sdecls '}'
				{ }

type
:	NAME			{ $$ = nil; }
|	type '[' ']'		{ $$ = mktyslice($1); }
|	type '[' NUM ']'	{ $$ = mktyarray($1, mklit($3)); }
|	type '!'		{ $$ = $1; $$->linear = 1; }

return
:	ARROWR type		{ $$ = $2; }
|				{ $$ = mktype(Tvoid); }

unary
:	NUM			{ $$ = mkexpr(Olit, mklit($1), nil); }
|	NAME			{ $$ = mkexpr(Ovar, mksym($1), nil); }
|	'(' expr ')'		{ $$ = $2; }

sufexpr
:	sufexpr '--'		{ $$ = mkexpr(Odec, $1, nil); }
|	sufexpr '++'		{ $$ = mkexpr(Oinc, $1, nil); }
|	unary			{ $$ = $1; }

prefexpr
:	ARROWL sufexpr		{ $$ = mkexpr(Orcv, $2, nil); }
|	sufexpr			{ $$ = $1; }

mulexpr
:	mulexpr '*' prefexpr	{ $$ = mkexpr(Omul, $1, $3); }
|	mulexpr '/' prefexpr	{ $$ = mkexpr(Odiv, $1, $3); }
|	mulexpr '%' prefexpr	{ $$ = mkexpr(Omod, $1, $3); }
|	prefexpr			{ $$ = $1; }

addexpr
:	addexpr '+' mulexpr	{ $$ = mkexpr(Oadd, $1, $3); }
|	addexpr '-' mulexpr	{ $$ = mkexpr(Osub, $1, $3); }
|	mulexpr			{ $$ = $1; }

shifexpr
:	shifexpr SHIFTL addexpr	{ $$ = mkexpr(Oshl, $1, $3); }
|	shifexpr SHIFTR addexpr	{ $$ = mkexpr(Oshr, $1, $3); }
|	addexpr			{ $$ = $1; }

compexpr
:	compexpr '>' shifexpr	{ $$ = mkexpr(Ogt, $1, $3); }
|	compexpr '<' shifexpr	{ $$ = mkexpr(Olt, $1, $3); }
|	compexpr NOTEQ shifexpr	{ $$ = mkexpr(One, $1, $3); }
|	compexpr EQ shifexpr 	{ $$ = mkexpr(Oeq, $1, $3); }
|	compexpr LTEQ shifexpr	{ $$ = mkexpr(Oge, $1, $3); }
|	compexpr GTEQ shifexpr	{ $$ = mkexpr(Ole, $1, $3); }
|	shifexpr		{ $$ = $1; }


logexpr
:	logexpr OR compexpr	{ $$ = mkexpr(Olor, $1, $3); }
|	logexpr AND compexpr	{ $$ = mkexpr(Oland, $1, $3); }
|	compexpr		{ $$ = $1; }

expr
:				{ $$ = nil; }
|	expr '=' logexpr	{ $$ = mkexpr(Oasn, $1, $3); }
|	logexpr			{ $$ = $1; }

stmts
:				{ $$ = ZL; }
|	stmts stmt		{ $$ = append($1, $2); }

decl
:	NAME ':' type		{ $$ = mkdecl($1, $3, nil); }
|	NAME ':' '=' expr	{ $$ = mkdecl($1, nil, $4); }
|	NAME ':' type '=' expr	{ $$ = mkdecl($1, $3, $5); }

stmt
:	expr ';'		{ $$ = $1; }
|	'{' stmts '}'		{ $$ = mkblk($2); }
|	decl ';'		{ $$ = $1->dcl.init; }
|	FOR '(' unary ARROWL expr ')' stmt
				{ $$ = mkiter($3, $5, $7); }
|	FOR '(' expr ';' expr ';' expr ')' stmt
				{ $$ = mkfor($3, $5, $7, $9); }
|	IF '(' expr ')' stmt ELSE stmt
				{ $$ = mkif($3, $5, $7); }
|	IF '(' expr ')' stmt	{ $$ = mkif($3, $5, nil); }

arg
:	NAME ':' type	{ $$ = mkdecl($1, $3, nil); }

args
:			{ $$ = ZL; }
|	arg		{ $$ = append(ZL, $1); }
|	args ',' arg	{ $$ = append($1, $3); }

%%

struct {
	char *s;
	int type;
} keytab[] = {
	"fn", FUNC,
	"type", DEF,
	"if", IF,
	"for", FOR,
	"mod", MOD,
	"use", USE,
	"&&", AND,
	"||", OR,
	"!=", NOTEQ,
	"==", EQ,
	"<=", LTEQ,
	">=", GTEQ,
	"<<", SHIFTR,
	">>", SHIFTL,
	"struct", STRUCT,
	"else", ELSE,
	"->", ARROWR,
	"→", ARROWR,
	"<-", ARROWL,
	"←", ARROWL,
};

Biobuf *bin;

int
getch(void)
{
	int c;

	c = Bgetrune(bin);
	if(c == Beof){
		goteof = 1;
		return -1;
	}
	if(c == '\n')
		lexline++;
	return c;
}

void
ungetc(void)
{
	Bungetrune(bin);
}

void
wordlex(char *dst, int n)
{
	Rune c;
	char *e;

	for(e = dst + n - UTFmax; dst < e;){
		c = getch();
		if((c >= Runeself)
		|| isalnum(c)){
			dst += runetochar(dst, &c);
			continue;
		}
		ungetc();
		break;
	}
	if(dst > e - UTFmax*2)
		yyerror("symbol buffer overrun");
	*dst = '\0';
}

void
yyerror(char *s)
{
	fprint(2, "%d: %s\n", lexline, s);
	exits(s);
}

int
yylex2(void)
{
	static char buf[200];
	int c;
	int i;

Loop:
	c = getch();
	switch(c){
	case -1:
		return -1;
	case ' ':
	case '\t':
	case '\n':
		goto Loop;
	case '/':
		if(getch() != '*'){
			ungetc();
			goto Loop;
		}
More:
		while((c = getch()) > 0)
			if(c == '*')
				break;
		if(c != '*')
			goto Loop;
		if(getch() == '/')
			goto Loop;
		goto More;
	}

	switch(c){
	case ';': case '=':
	case '{': case '}':
	case '[': case ']':
	case '(': case ')':
	case '+': case '-':
	case '*': case '/':
	case '%': case ':':
	case '>': case '<':
	case ',': case '.':
		return c;
	}
	ungetc();
	wordlex(buf, sizeof buf);

	for(i = 0; i < nelem(keytab); i++)
		if(strcmp(keytab[i].s, buf) == 0)
			return keytab[i].type;

	if(isdigit(buf[0])){
		yylval.ival = atoi(buf);
		return NUM;
	}

	yylval.sval = strdup(buf);
	return NAME;
}

int debug;

int
yylex(void)
{
	int c;

	c = yylex2();
	if(!debug)
		return c;
	if(c < Runeself)
		fprint(2, "%c\n", c);
	else if(c == NAME)
		fprint(2, "NAME %s\n", yylval.sval);
	else if(c == TYPE)
		fprint(2, "TYPE %s\n", yylval.sval);
	else if(c == NUM)
		fprint(2, "NUM %ld\n", yylval.ival);
	return c;
}

void
usage(void)
{
	fprint(2, "usage: [-d] %s\n", argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	ARGBEGIN{
	case 'd':
		debug++;
		break;
	default:
		usage();
		break;
	}ARGEND;
	if(argc > 0)
		usage();
	bin = Bfdopen(0, OREAD);
	goteof = 0;
	while(!goteof)
		yyparse();
	Bterm(bin);
	exits(nil);
}