shithub: lpa

Download patch

ref: 50104ba72987b26febfa4e398458d47db20e9bea
parent: dcb761aebd2815fdf2d04c0b05724292de9dc98b
author: Peter Mikkelsen <peter@pmikkelsen.com>
date: Sun Jul 21 09:45:01 EDT 2024

Parse strands

--- a/dat.h
+++ b/dat.h
@@ -165,6 +165,7 @@
 	AstDyadic,
 	AstConst,
 	AstPrim,
+	AstStrand,
 };
 
 typedef struct Ast Ast;
--- a/fns.h
+++ b/fns.h
@@ -12,6 +12,7 @@
 
 /* memory.c */
 void *alloc(int);
+int getalloctag(void *);
 void setroot(void *, int);
 void *allocextra(void *, usize);
 
--- a/memory.c
+++ b/memory.c
@@ -63,6 +63,13 @@
 	return (Allocation*)p;
 }
 
+int
+getalloctag(void *d)
+{
+	Allocation *a = allocptr(d);
+	return a->tag;
+}
+
 void
 setroot(void *d, int v)
 {
--- a/parse.c
+++ b/parse.c
@@ -11,6 +11,7 @@
 static void match(TokenList *, int);
 static void addchild(Ast *, Ast *);
 static int issep(TokenList *t);
+static int isexprsep(TokenList *t);
 static int nameclass(char *, Ast *);
 
 static void parsesep(TokenList *);
@@ -95,6 +96,20 @@
 }
 
 static int
+isexprsep(TokenList *t)
+{
+	switch(peek(t)){
+	case TokNewline:
+	case TokDiamond:
+	case TokDel:
+	case TokEnd:
+		return 1;
+	default:
+		return 0;
+	}
+}
+
+static int
 nameclass(char *name, Ast *func)
 {
 	int class;
@@ -218,7 +233,7 @@
 
 	depth = 0;
 	start = t->offset;
-	while(!(issep(t) || (peek(t) == TokDel) || (peek(t) == TokEnd)) || depth != 0){
+	while(!isexprsep(t) || depth != 0){
 		switch(peek(t)){
 		case TokLparen: depth++; break;
 		case TokRparen: depth--; break;
@@ -250,8 +265,10 @@
 static Ast *
 parseexprsub(TokenList *t)
 {
-	Ast *expr, *val;
-
+	Ast *expr, *val, *strand;
+	
+	strand = nil;
+again:
 	if(peek(t) == TokLparen){
 		match(t, TokLparen);
 		val = parseexprsub(t);
@@ -269,7 +286,8 @@
 			expr->tag = AstMonadic;
 		expr->func = parsefunc(t);
 		expr->right = parseexprsub(t);
-		return expr;
+		val = expr;
+		goto end;
 	}
 
 	if(peek(t) == TokName){
@@ -280,16 +298,32 @@
 			expr->tag = AstAssign;
 			expr->left = val;
 			expr->right = parseexprsub(t);
-			return expr;
+			val = expr;
+			goto end;
 		}
 	}
 
-	/* We need a value now. Stranding is not implemented */
+	/* We need a value now */
 	if(val == nil)
 		val = parseconst(t);
 
 	if(peekclass(t) == NameclassFunc)
 		goto func;
+
+	if(!(isexprsep(t) || peek(t) == TokRparen)){ /* Stranding */
+		if(!strand){
+			strand = alloc(DataAst);
+			strand->tag = AstStrand;
+		}
+		addchild(strand, val);
+		goto again;
+	}
+
+end:
+	if(strand){
+		addchild(strand, val);
+		val = strand;
+	}
 	
 	return val;
 }
--- a/util.c
+++ b/util.c
@@ -99,6 +99,10 @@
 	case AstPrim:
 		print("prim: %d\n", ast->prim);
 		break;
+	case AstStrand:
+		print("strand\n");
+		printchildren = 1;
+		break;
 	default:
 		print("<ast node %d>\n", ast->tag);
 		break;
--- a/value.c
+++ b/value.c
@@ -9,10 +9,17 @@
 char *
 printval(void *v)
 {
-	if(v)
-		return smprint("some value: %p :)", v);
-	else
-		return smprint("no value :(");
+	int tag;
+	if(v){
+		tag = getalloctag(v);
+		switch(tag){
+		case DataArray:
+			return smprint("%s\n", printarray(v));
+		default:
+			return smprint("some value of type %d\n", tag);
+		}
+	}else
+		return smprint("no value :(\n");
 }
 
 void *