shithub: libmujs

Download patch

ref: 27ce19d25de84f23f10905db49bf83e95fea4257
parent: 948d6a9fa86fa0e10f0a7674873ac08f0627a173
author: Tor Andersson <[email protected]>
date: Fri Jan 10 05:35:37 EST 2014

Remove pointless STM_BLOCK nodes.

--- a/js-ast.c
+++ b/js-ast.c
@@ -336,8 +336,11 @@
 	case STM_SWITCH:
 		printf("switch (");
 		printast(n->a, level);
-		printf(")");
-		printstm(n->b, level);
+		printf(") {\n");
+		printblock(n->b, level);
+		putchar('\n');
+		indent(level);
+		printf("}");
 		break;
 
 	case STM_CASE:
@@ -403,8 +406,13 @@
 		printast(n->a, level);
 		printf("(");
 		printlist(n->b, level, ", ");
-		printf(")");
-		printstm(n->c, level);
+		printf(")\n");
+		indent(level);
+		printf("{\n");
+		printblock(n->c, level + 1);
+		printf("\n");
+		indent(level);
+		printf("}");
 		break;
 
 	case EXP_FUNC:
@@ -413,9 +421,11 @@
 			printast(n->a, level);
 		printf("(");
 		printlist(n->b, level, ", ");
-		printf(")");
-		printstm(n->c, level);
-		printf(")");
+		printf(") {\n");
+		printblock(n->c, level + 1);
+		printf("\n");
+		indent(level);
+		printf("})");
 		break;
 
 	case EXP_OBJECT:
--- a/js-parse.c
+++ b/js-parse.c
@@ -23,7 +23,7 @@
 static js_Ast *assignment(js_State *J, int notin);
 static js_Ast *memberexp(js_State *J);
 static js_Ast *statement(js_State *J);
-static js_Ast *functionbody(js_State *J);
+static js_Ast *funcbody(js_State *J);
 
 static const char *tokenstring[] = {
 	"(end-of-file)",
@@ -192,7 +192,7 @@
 		name = propname(J);
 		expect(J, '(');
 		expect(J, ')');
-		body = functionbody(J);
+		body = funcbody(J);
 		return EXP2(PROP_GET, name, body);
 	}
 
@@ -202,7 +202,7 @@
 		expect(J, '(');
 		arg = identifier(J);
 		expect(J, ')');
-		body = functionbody(J);
+		body = funcbody(J);
 		return EXP3(PROP_SET, name, arg, body);
 	}
 
@@ -298,7 +298,7 @@
 		expect(J, '(');
 		b = paramlist(J);
 		expect(J, ')');
-		c = functionbody(J);
+		c = funcbody(J);
 		return EXP3(FUNC, a, b, c);
 	}
 	return primary(J);
@@ -557,7 +557,7 @@
 
 	expect(J, '{');
 	if (accept(J, '}'))
-		return STM1(BLOCK, NULL);
+		return NULL;
 
 	node = caseclause(J);
 	head = tail = LIST(node);
@@ -567,7 +567,7 @@
 	}
 
 	expect(J, '}');
-	return STM1(BLOCK, head);
+	return head;
 }
 
 static js_Ast *block(js_State *J)
@@ -576,7 +576,7 @@
 
 	expect(J, '{');
 	if (accept(J, '}'))
-		return NULL;
+		return STM1(BLOCK, NULL);
 
 	node = statement(J);
 	head = tail = LIST(node);
@@ -764,25 +764,21 @@
 	return NULL;
 }
 
-static js_Ast *fundec(js_State *J)
+static js_Ast *chunknode(js_State *J)
 {
 	js_Ast *a, *b, *c;
-	a = identifier(J);
-	expect(J, '(');
-	b = paramlist(J);
-	expect(J, ')');
-	c = functionbody(J);
-	return STM3(FUNC, a, b, c);
-}
-
-static js_Ast *sourceelement(js_State *J)
-{
-	if (accept(J, TK_FUNCTION))
-		return fundec(J);
+	if (accept(J, TK_FUNCTION)) {
+		a = identifier(J);
+		expect(J, '(');
+		b = paramlist(J);
+		expect(J, ')');
+		c = funcbody(J);
+		return STM3(FUNC, a, b, c);
+	}
 	return statement(J);
 }
 
-static js_Ast *sourcelist(js_State *J)
+static js_Ast *chunk(js_State *J)
 {
 	js_Ast *head, *tail, *node;
 
@@ -789,24 +785,22 @@
 	if (J->lookahead == '}' || J->lookahead == 0)
 		return NULL;
 
-	node = sourceelement(J);
+	node = chunknode(J);
 	head = tail = LIST(node);
 	while (J->lookahead != '}' && J->lookahead != 0) {
-		node = sourceelement(J);
+		node = chunknode(J);
 		tail = tail->b = LIST(node);
 	}
 
-	return STM1(BLOCK, head);
+	return head;
 }
 
-static js_Ast *functionbody(js_State *J)
+static js_Ast *funcbody(js_State *J)
 {
 	js_Ast *a;
-
 	expect(J, '{');
-	a = sourcelist(J);
+	a = chunk(J);
 	expect(J, '}');
-
 	return a;
 }
 
@@ -836,7 +830,7 @@
 	}
 
 	next(J);
-	printblock(sourcelist(J)->a, 0);
+	printblock(chunk(J), 0);
 	putchar('\n');
 
 	// TODO: compile to bytecode