shithub: hammer

Download patch

ref: e01f61f0d55b37630264e8d74107199752a6e76f
parent: c2086cda32afb3960938a81df6f5a6a209bfaf33
author: Jacob Moody <[email protected]>
date: Fri Apr 26 01:51:47 EDT 2024

string literals and tweak call parsing

Not exactly sure why using expr for the args is causing
yacc to give reduce conflicts but I am not particularly
tied to that grammer for now.

--- a/dat.h
+++ b/dat.h
@@ -80,6 +80,12 @@
 	Orcv,
 };
 
+enum {
+	Lint,
+	Lstr,
+	Lfloat,
+};
+
 struct Nod {
 	int	t;
 	Loc	loc;
@@ -111,7 +117,12 @@
 			Nod	*f;
 		} nif;
 		struct {
-			vlong	val;
+			int	t;
+			union {
+				vlong	i;
+				double	d;
+				char	*s;
+			};
 		} lit;
 		struct {
 			char	*sym;
--- a/fmt.c
+++ b/fmt.c
@@ -56,7 +56,14 @@
 	case Nsym:
 		return fmtprint(f, "%s", n->sym.sym);
 	case Nlit:
-		return fmtprint(f, "%lld", n->lit.val);
+		switch(n->lit.t){
+		case Lint:
+			return fmtprint(f, "%lld", n->lit.i);
+		case Lstr:
+			return fmtprint(f, "\"%s\"", n->lit.s);
+		default:
+			return fmtprint(f, "\n#error unknown literal type\n");
+		}
 	case Nexpr:
 		if(n->expr.op == Olit || n->expr.op == Ovar)
 			return fmtprint(f, "%N", n->expr.lhs);
@@ -116,7 +123,14 @@
 	case Nsym:
 		return fmtprint(f, "%s", n->sym.sym);
 	case Nlit:
-		return fmtprint(f, "%lld", n->lit.val);
+		switch(n->lit.t){
+		case Lint:
+			return fmtprint(f, "%lld", n->lit.i);
+		case Lstr:
+			return fmtprint(f, "\"%s\"", n->lit.s);
+		default:
+			return fmtprint(f, "\n#error unknown literal type\n");
+		}
 	case Nexpr:
 		if(n->expr.op == Olit || n->expr.op == Ovar)
 			return fmtprint(f, "%O", n->expr.lhs);
--- a/fns.h
+++ b/fns.h
@@ -6,7 +6,8 @@
 Nod*	mkif(Nod*, Nod*, Nod*);
 Nod*	mkfunc(char*, Nlst, Typ*, Nlst);
 Nod*	mkcall(char*, Nlst);
-Nod*	mklit(vlong);
+Nod*	mkintlit(vlong);
+Nod*	mkstrlit(char*);
 Nod*	mksym(char*);
 Nod*	mkdecl(char*, Typ*, Nod*);
 Nod*	mkblk(Nlst);
--- a/n.y
+++ b/n.y
@@ -74,7 +74,7 @@
 type
 :	NAME			{ $$ = mktype($1, Tunkn); }
 |	type '[' ']'		{ $$ = mktyslice($1); }
-|	type '[' NUM ']'	{ $$ = mktyarray($1, mklit($3)); }
+|	type '[' NUM ']'	{ $$ = mktyarray($1, mkintlit($3)); }
 |	type '!'		{ $$ = $1; $$->linear = 1; }
 
 return
@@ -82,14 +82,15 @@
 |				{ $$ = mktype("void", Tvoid); }
 
 unary
-:	NUM			{ $$ = mkexpr(Olit, mklit($1), nil); }
+:	NUM			{ $$ = mkexpr(Olit, mkintlit($1), nil); }
 |	NAME			{ $$ = mkexpr(Ovar, mksym($1), nil); }
+|	'"' NAME '"'		{ $$ = mkstrlit($2); }
 |	'(' expr ')'		{ $$ = $2; }
-|	NAME '(' args ')'	{ $$ = mkcall($1, $3); }
 
 sufexpr
 :	sufexpr DEC		{ $$ = mkexpr(Odec, $1, nil); }
 |	sufexpr INC		{ $$ = mkexpr(Oinc, $1, nil); }
+|	NAME '(' args ')'	{ $$ = mkcall($1, $3); }
 |	unary			{ $$ = $1; }
 
 prefexpr
@@ -161,10 +162,11 @@
 |	defarg			{ $$ = append(ZL, $1); }
 |	defargs ',' defarg	{ $$ = append($1, $3); }
 
+/* TODO: should be expr */
 args
-:			{ $$ = ZL; }
-|	expr		{ $$ = append(ZL, $1); }
-|	args ',' expr	{ $$ = append($1, $3); }
+:				{ $$ = ZL; }
+|	logexpr			{ $$ = append(ZL, $1); }
+|	args ',' logexpr	{ $$ = append($1, $3); }
 
 %%
 
--- a/nod.c
+++ b/nod.c
@@ -115,12 +115,24 @@
 }
 
 Nod*
-mklit(vlong v)
+mkintlit(vlong v)
 {
 	Nod *n;
 
 	n = new(Nlit);
-	n->lit.val = v;
+	n->lit.t = Lint;
+	n->lit.i = v;
+	return n;
+}
+
+Nod*
+mkstrlit(char *s)
+{
+	Nod *n;
+
+	n = new(Nlit);
+	n->lit.t = Lstr;
+	n->lit.s = s;
 	return n;
 }