shithub: mc

Download patch

ref: a3e640134dc9e002a2b56ee14f7250c36871d1e8
parent: ac297020506255ca01cde01c14145f3203d1a155
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Jan 8 20:22:25 EST 2016

Don't loop infinitely when parsing.

--- a/mparse/dump.myr
+++ b/mparse/dump.myr
@@ -14,20 +14,11 @@
 
 	std.fmtinstall(std.typeof(&f), fmtfile, [][:])
 	std.fmtinstall(std.typeof(&d), fmtdcl, [][:])
-	/*std.fmtinstall(std.typeof(&n), fmtnode, [][:])*/
 	std.fmtinstall(std.typeof(&e), fmtexpr, [][:])
 	std.fmtinstall(std.typeof(&st), fmtstab, [][:])
 }
 
-/*
-const fmtnode = {sb, ap, opts
-	var f : node#
-	f = std.vanext(ap)
-	nodedump(sb, f, 0)
-}
-*/
-
-generic fmtfile = {sb, ap, opts
+const fmtfile = {sb, ap, opts
 	var f : file#
 	f = std.vanext(ap)
 	filedump(sb, f, 0)
@@ -166,8 +157,13 @@
 	keys = std.htkeys(st.syms)
 	for k in keys
 		match std.get(std.htget(st.syms, k))
-		| `Dcl d:	ifmt(sb, ind, "{} : {}\n", k, d.ty)
-		| `Ucon uc:	ifmt(sb, ind, "{} : {}\n", k, uc.ety)
+		| `Dcl d:	
+			ifmt(sb, ind, "{} : {}\n", k, d.ty)
+		| `Ucon uc:	
+			match uc.ety
+			| `std.Some t:	ifmt(sb, ind, "{} : {}\n", k, t)
+			| `std.None:	ifmt(sb, ind, "{}\n", k)
+			;;
 		;;
 	;;
 	std.slfree(keys)
--- a/mparse/main.myr
+++ b/mparse/main.myr
@@ -2,10 +2,13 @@
 
 use "tok.use"
 use "parse.use"
+use "dump.use"
 
 const main = {
-	var ts
+	var ts, f
 
-	ts = parse.tokinitf(0)
-	parse.tsfile(ts)
+	ts = parse.tokinitf(0, "stdin")
+	f = parse.tsfile(ts)
+	std.put("parsed\n")
+	std.put("{}\n", f)
 }
--- a/mparse/parse.myr
+++ b/mparse/parse.myr
@@ -18,11 +18,12 @@
 		.uses = [][:],
 		.libs = [][:],
 		.extlibs = [][:],
-		.stmts = [][:],
+		.dcls = [][:],
 		.extinit = [][:],
 		.init = `std.None,
 		.globls = mkstab(),
 		.builtin = mkstab(),
+		.ns = std.mkht(std.strhash, std.streq),
 	])
 	f.globls.super = `std.Some f.builtin
 	while true
@@ -30,8 +31,7 @@
 		| (loc, `Teof):	
 			break
 		| (loc, `Tendln):
-			continue
-
+			toknext(ts)
 		| (loc, `Tuse):
 			usestmt(ts, f)
 		| (loc, `Tpkg):
@@ -73,6 +73,7 @@
 }
 
 const pkgbody = {ts, f
+	endlns(ts)
 }
 
 const traitdef = {ts, f
@@ -82,6 +83,15 @@
 }
 
 const decl = {ts, f
+}
+
+const endlns = {ts
+	while true
+		match tokpeek(ts)
+		| (loc, `Tendln):	toknext(ts)
+		| _:	break
+		;;
+	;;
 }
 
 const expectendblk = {ts, f
--- a/mparse/stab.myr
+++ b/mparse/stab.myr
@@ -16,6 +16,7 @@
 		.name = "",
 		.isfunc = false,
 		.syms = std.mkht(stnamehash, stnameeq),
+		.types = std.mkht(stnamehash, stnameeq),
 		.impls = std.mkht(stnamehash, stnameeq),
 		.env = std.mkht(stnamehash, stnameeq),
 	])
@@ -29,7 +30,7 @@
 	std.htput(f.ns, name, st)
 }
 
-const stnamehash = {n : name#
+const stnamehash = {n
 	-> std.strhash(n.name)
 }
 
--- a/mparse/tok.myr
+++ b/mparse/tok.myr
@@ -13,7 +13,7 @@
 	;;
 
 	const tokinit	: (path : byte[:]	-> tokstream#)
-	const tokinitf	: (path : std.fd	-> tokstream#)
+	const tokinitf	: (fd : std.fd, path : byte[:]	-> tokstream#)
 	const tokclose	: (ts : tokstream#	-> void)
 
 	const toknext	: (ts : tokstream#	-> (srcloc, tok))
@@ -24,18 +24,27 @@
 
 const tokinit = {path
 	match std.slurp(path)
-	| `std.Ok data:	-> std.mk([.next=`std.None, .rest=data, .data=data])
+	| `std.Ok data: -> mkparser(path, data)
 	| `std.Fail e:	std.fatal("could not read file {}: {}\n", path, e)
 	;;
 }
 
-const tokinitf = {fd
+const tokinitf = {fd, name
 	match std.fslurp(fd)
-	| `std.Ok data:	-> std.mk([.next=`std.None, .rest=data, .data=data])
+	| `std.Ok data: -> mkparser(name, data)
 	| `std.Fail e:	std.fatal("could not read file {}: {}\n", fd, e)
 	;;
 }
 
+const mkparser = {name, data
+	-> std.mk([
+		.loc = [.file=name, .line=1, .col=1],
+		.next=`std.None,
+		.rest=data,
+		.data=data,
+	])
+}
+
 const tokclose = {ts
 	std.slfree(ts.data)
 	std.free(ts)
@@ -380,7 +389,6 @@
 	var t, chr
 
 	chr = takec(ts)
-	t = `Tobrace
 	match chr
 	| '{': t = `Tobrace
 	| '}': t = `Tcbrace
--- a/mparse/types.myr
+++ b/mparse/types.myr
@@ -22,7 +22,7 @@
 		init	: std.option(dcl#)
 		builtin	: stab#
 		globls	: stab#
-		ns	: std.htab(byte[:], node#)#
+		ns	: std.htab(byte[:], stab#)#
 		dcls	: dcl#[:]
 	;;