shithub: mc

Download patch

ref: 957f1f01589ac9278578e2f6cdcab0c6fb5f77fb
parent: 29c376fb854767c551415cd858c462f8ac03280d
author: Ori Bernstein <[email protected]>
date: Mon Aug 6 07:43:52 EDT 2012

More work towards str formatting.

--- a/fmt.myr
+++ b/fmt.myr
@@ -5,12 +5,29 @@
 
 pkg std =
 	const bfmt	: (buf : byte[,], fmt : byte[,], args:... -> size)
-	const fmt	: (fmt : byte[,], args:... -> size)
+	const put	: (fmt : byte[,], args:... -> size)
 ;;
 
+const put = {fmt, args
+	var buf : byte[2048]
+	var n
+	
+	n = bfmt(buf[0,2048], fmt, args)
+	write(1, buf[0,n])
+	-> n
+}
+
 const bfmt = {buf, fmt, args
 	var c
 	var n
+	var ap
+	var s_val;
+	var b_val;
+	/*
+	var w_val;
+	*/
+	var i_val;
+	var l_val;
 
 	n = 0
 	while fmt.len
@@ -17,13 +34,31 @@
 		(c, fmt) = striter(fmt)
 		if c == '%'
 			(c, fmt) = striter(fmt)
+			ap = &args
 			match c
 			's':
-				write(1, "str\n")
+				s_val = *(ap castto(byte[,]*))
+				n += strfmt(buf[n, buf.len], s_val)
 				;;
-			'd':
+			/* format integers */
+			'b':
+				b_val = *(ap castto(int8*))
 				write(1, "int\n")
 				;;
+			/*
+			'w':
+				w_val = *(ap castto(int16*))
+				write(1, "int\n")
+				;;
+			*/
+			'i':
+				i_val = *(ap castto(int32*))
+				write(1, "int\n")
+				;;
+			'l':
+				l_val = *(ap castto(int64*))
+				write(1, "int\n")
+				;;
 			'p':
 				write(1, "ptr\n")
 				;;
@@ -35,11 +70,26 @@
 	-> n
 }
 
-const fmt = {fmt, args
-	var buf : byte[2048]
-	var n
+const strfmt = {buf, str
+	var i
 	
-	n = bfmt(buf[0,2048], fmt, args)
-	write(1, buf[0,n])
-	-> n
+	for i = 0; i < min(str.len, buf.len); i++
+		buf[i] = str[i]
+	;;
+	-> i
+}
+
+/*
+generic intfmt = {buf, val:@a::tcnum, base
+	write("int \n")
+	-> 0
+}
+*/
+
+const min = {a : size, b : size
+	if a < b
+		-> b
+	else
+		-> a
+	;;
 }
--- a/test.myr
+++ b/test.myr
@@ -25,7 +25,7 @@
 	;;
 	std.write(1, "Hello, 世界\n")
 	chartypes()
-	std.fmt("format output %d\n", 123)
+	std.put("format output %s\n", "x")
 }
 
 const chartypes = {