shithub: hammer

Download patch

ref: 72abaa282749e37df7831959b1a344664b1827f3
parent: 1ed1ea5a5c5fe9701feb49382ca0912e044e2144
author: Jacob Moody <moody@posixcafe.org>
date: Fri Nov 17 20:42:11 EST 2023

start of AST from ori and remove of lex type junk

--- /dev/null
+++ b/dat.h
@@ -1,0 +1,115 @@
+typedef struct Nod Nod;
+typedef struct Nlst Nlst;
+typedef struct Typ Typ;
+typedef struct Loc Loc;
+
+#define ZL	(Nlst){nil, 0, 0}
+
+struct Typ {
+	int	t;
+	char	linear;
+	char	*name;
+	Typ	*base;
+	Nod	*size;
+};
+
+struct Nlst {
+	Nod	**v;
+	int	nv;
+	int	cap;
+};
+
+struct Loc {
+	int	line;
+	char	*file;
+};
+
+enum {
+	Tarray,
+	Tslice,
+};
+
+enum {
+	Nexpr,
+	Nfor,
+	Niter,
+	Nblk,
+	Nlit,
+	Nsym,
+	Ndcl,
+};
+
+enum {
+	Olit,
+	Ovar,
+	Oasn,
+	Oadd,
+	Osub,
+	Omul,
+	Odiv,
+	Omod,
+	Oshl,
+	Oshr,
+	Olor,
+	Oland,
+	Ogt,
+	Olt,
+	Oge,
+	Ole,
+	Oeq,
+	One,
+	Oinc,
+	Odec,
+	Orcv,
+};
+
+struct Nod {
+	int	t;
+	Loc	loc;
+	union {
+		struct {
+			int	nsub;
+			Nod	*sub;
+		} top;
+		struct {
+			int	op;
+			Nod	*lhs;
+			Nod	*rhs;
+			Typ	*type;
+		} expr;
+		struct {
+			Nod	*idx;
+			Nod	*arg;
+			Nod	*body;
+		} iter;
+		struct {
+			Nod	*init;
+			Nod	*cond;
+			Nod	*step;
+			Nod	*body;
+		} nfor;
+		struct {
+			vlong	val;
+		} lit;
+		struct {
+			char	*sym;
+		} sym;
+		struct {
+			char	*name;
+			Nlst	arg;
+			Nod	*body;
+		} fndef;
+		struct {
+			char	*name;
+			char	*pkg;
+			Typ	*type;
+			Nod	*init;
+		} dcl;
+		struct {
+			Nlst	body;
+		} blk;
+	};
+};
+
+extern int	lexline;
+extern char*	lexfile;
--- /dev/null
+++ b/fns.h
@@ -1,0 +1,9 @@
+Nlst	append(Nlst, Nod*);
+Nod*	mkexpr(int, Nod*, Nod*);
+Nod*	mkiter(Nod*, Nod*, Nod*);
+Nod*	mkfor(Nod*, Nod*, Nod*, Nod*);
+Nod*	mklit(vlong);
+Nod*	mksym(char*);
+Nod*	mkdecl(char*, Typ*, Nod*);
+Typ*	mktyarray(Typ*, Nod*);
+Typ*	mktyslice(Typ*);
--- a/mkfile
+++ b/mkfile
@@ -4,7 +4,11 @@
 TARG=nl
 OFILES=\
 	y.tab.$O\
+	nod.$O\
 
+HFILES=\
+	dat.h\
+	fns.h
 YFILES=\
 	n.y\
 
--- a/n.y
+++ b/n.y
@@ -5,55 +5,37 @@
 #include <bio.h>
 #include <ctype.h>
 
+#include "dat.h"
+#include "fns.h"
+
 int goteof;
-int lineno;
+int lexline;
+char *lexfile = "<stdin>";
 int yyparse(void);
 void yyerror(char*);
 
-struct {
-	char *s;
-	/* todo */
-} typetab[1000] = {
-	"byte",
-	"int",
-	"long",
-	"vlong",
-	"uint",
-	"ulong",
-	"uvlong",
-	"float32",
-	"float64",
-
-	nil,
-};
-
-void
-addtype(char *s)
-{
-	int i;
-
-	for(i = 0; i < nelem(typetab)-1; i++){
-		if(typetab[i].s != nil)
-			continue;
-		typetab[i].s = s;
-		typetab[i+1].s = nil;
-	}
-}
-
 %}
 
 %union
 {
-	char *sval;
-	long ival;
+	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 TYPE;
+%token	<sval>	NAME;
 %token	<ival>	NUM
 
+%type<nod>	arg expr logexpr shifexpr addexpr mulexpr
+%type<nod>	unary sufexpr compexpr
+%type<lst>	args
+%type<typ>	type
+
 %%
 
 prog
@@ -71,71 +53,70 @@
 |	sdecls NAME ':' type ';'
 
 def
-:	DEF NAME TYPE
+:	DEF NAME NAME
 	{
-		addtype($2);
 	}
 |	DEF NAME STRUCT '{' sdecls '}'
 	{
-		addtype($2);
 	}
 
 type
-:	TYPE
-|	type '[' ']'
-|	type '[' NUM ']'
-|	type '!'
+:	NAME			{ $$ = nil; }
+|	type '[' ']'		{ $$ = mktyslice($1); }
+|	type '[' NUM ']'	{ $$ = mktyarray($1, mklit($3)); }
+|	type '!'		{ $$ = $1; $$->linear = 1; }
 
 return
 :	ARROWR type
 |
 
+
 unary
-:	NUM
-|	NAME
-|	ARROWL NAME
-|	'(' expr ')'
+:	NUM			{ $$ = mkexpr(Olit, mklit($1), nil); }
+|	NAME			{ $$ = mkexpr(Ovar, mksym($1), nil); }
+|	ARROWL unary		{ $$ = mkexpr(Orcv, $2, nil); }
+|	'(' expr ')'		{ $$ = $2; }
 
 sufexpr
-:	unary '--'
-|	unary '++'
-|	unary
+:	unary '--'		{ $$ = mkexpr(Odec, $1, nil); }
+|	unary '++'		{ $$ = mkexpr(Oinc, $1, nil); }
+|	unary			{ $$ = $1; }
 
 mulexpr
-:	mulexpr '*' sufexpr
-|	mulexpr '/' sufexpr
-|	mulexpr '%' sufexpr
-|	sufexpr
+:	mulexpr '*' sufexpr	{ $$ = mkexpr(Omul, $1, $3); }
+|	mulexpr '/' sufexpr	{ $$ = mkexpr(Odiv, $1, $3); }
+|	mulexpr '%' sufexpr	{ $$ = mkexpr(Omod, $1, $3); }
+|	sufexpr			{ $$ = $1; }
 
 addexpr
-:	addexpr '+' mulexpr
-|	addexpr '-' mulexpr
-|	mulexpr
+:	addexpr '+' mulexpr	{ $$ = mkexpr(Oadd, $1, $3); }
+|	addexpr '-' mulexpr	{ $$ = mkexpr(Osub, $1, $3); }
+|	mulexpr			{ $$ = $1; }
 
 shifexpr
-:	shifexpr SHIFTL addexpr
-|	shifexpr SHIFTR addexpr
-|	addexpr
+:	shifexpr SHIFTL addexpr	{ $$ = mkexpr(Oshl, $1, $3); }
+|	shifexpr SHIFTR addexpr	{ $$ = mkexpr(Oshr, $1, $3); }
+|	addexpr			{ $$ = $1; }
 
 compexpr
-:	compexpr '>' shifexpr
-|	compexpr '<' shifexpr
-|	compexpr NOTEQ shifexpr
-|	compexpr EQ shifexpr
-|	compexpr LTEQ shifexpr
-|	compexpr GTEQ shifexpr
-|	shifexpr
+:	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
-|	logexpr AND compexpr
-|	compexpr
+:	logexpr OR compexpr	{ $$ = mkexpr(Olor, $1, $3); }
+|	logexpr AND compexpr	{ $$ = mkexpr(Oland, $1, $3); }
+|	compexpr		{ $$ = $1; }
 
 expr
-:	
-|	expr '=' logexpr
-|	logexpr
+:				{ $$ = nil; }
+|	expr '=' logexpr	{ $$ = mkexpr(Oasn, $1, $3); }
+|	logexpr			{ $$ = $1; }
 
 stmts
 :
@@ -155,12 +136,12 @@
 |	IF '(' expr ')' stmt
 
 arg
-:	NAME ':' type
+:	NAME ':' type	{ $$ = mkdecl($1, $3, nil); }
 
 args
-:	
-|	arg
-|	args ',' arg
+:			{ $$ = ZL; }
+|	arg		{ $$ = append(ZL, $1); }
+|	args ',' arg	{ $$ = append($1, $3); }
 
 %%
 
@@ -203,7 +184,7 @@
 		return -1;
 	}
 	if(c == '\n')
-		lineno++;
+		lexline++;
 	return c;
 }
 
@@ -237,7 +218,7 @@
 void
 yyerror(char *s)
 {
-	fprint(2, "%d: %s\n", lineno, s);
+	fprint(2, "%d: %s\n", lexline, s);
 	exits(s);
 }
 
@@ -291,15 +272,6 @@
 	for(i = 0; i < nelem(keytab); i++)
 		if(strcmp(keytab[i].s, buf) == 0)
 			return keytab[i].type;
-
-	for(i = 0; i < nelem(typetab); i++){
-		if(typetab[i].s == nil)
-			break;
-		if(strcmp(typetab[i].s, buf) == 0){
-			yylval.sval = strdup(buf);
-			return TYPE;
-		}
-	}
 
 	if(isdigit(buf[0])){
 		yylval.ival = atoi(buf);
--- /dev/null
+++ b/nod.c
@@ -1,0 +1,129 @@
+#include <u.h>
+#include <libc.h>
+
+#include "dat.h"
+#include "fns.h"
+
+Nlst
+append(Nlst l, Nod *n)
+{
+	if(l.nv == l.cap){
+		if(l.cap == 0)
+			l.cap = 1;
+		l.cap *= 2;
+		l.v = realloc(l.v, l.cap*sizeof(Nod*));
+		if(l.v == nil)
+			abort();
+	}
+	l.v[l.nv++] = n;
+	return l;
+}
+
+Nod*
+new(int t)
+{
+	Nod *n;
+
+	if((n = mallocz(sizeof(Nod), 1)) == nil)
+		abort();
+	n->loc.line = lexline;
+	n->loc.file = lexfile;
+	n->t = t;
+	return n;
+}
+
+Nod*
+mkexpr(int op, Nod *lhs, Nod *rhs)
+{
+	Nod *e;
+
+	e = new(Nexpr);
+	e->expr.op = op;
+	e->expr.lhs = lhs;
+	e->expr.rhs = rhs;
+	e->expr.type = nil;
+	return e;
+}
+
+Nod*
+mkiter(Nod *idx, Nod *arg, Nod *body)
+{
+	Nod *n;
+
+	n = new(Niter);
+	n->iter.idx = idx;
+	n->iter.arg = arg;
+	n->iter.body = body;
+	return n;
+}
+
+Nod*
+mkfor(Nod *init, Nod *cond, Nod *step, Nod *body)
+{
+	Nod *n;
+
+	n = new(Nfor);
+	n->nfor.init = init;
+	n->nfor.cond= cond;
+	n->nfor.step = step;
+	n->nfor.body = body;
+	return n;
+}
+
+Nod*
+mklit(vlong v)
+{
+	Nod *n;
+
+	n = new(Nlit);
+	n->lit.val = v;
+	return n;
+}
+
+Nod*
+mksym(char *s)
+{
+	Nod *n;
+
+	n = new(Nlit);
+	n->sym.sym = s;
+	return n;
+
+}
+
+Nod*
+mkdecl(char *s, Typ *t, Nod *i)
+{
+	Nod *n;
+
+	n = new(Ndcl);
+	n->dcl.name = s;
+	n->dcl.type = t;
+	n->dcl.init = i;
+	return n;
+}
+
+Typ*
+mktyarray(Typ *bt, Nod *sz)
+{
+	Typ *t;
+
+	if((t = mallocz(sizeof(Typ), 1)) == nil)
+		abort();
+	t->t = Tarray;
+	t->base = bt;
+	t->size = sz;
+	return t;
+}
+
+Typ*
+mktyslice(Typ *bt)
+{
+	Typ *t;
+
+	t = calloc(1, sizeof(Typ));
+	t->t = Tslice;
+	t->base = bt;
+	return t;
+}
+