shithub: mc

ref: da5bfc6d15a50130afdcd37ed9af737a81724506
dir: /mparse/dump.myr/

View raw version
use std

use "types.use"
use "util.use"

pkg parse =
;;

const __init__ = {
	var f : file
	var d : dcl
	var st : stab
	var e : expr

	std.fmtinstall(std.typeof(&f), fmtfile, [][:])
	std.fmtinstall(std.typeof(&d), fmtdcl, [][:])
	std.fmtinstall(std.typeof(&e), fmtexpr, [][:])
	std.fmtinstall(std.typeof(&st), fmtstab, [][:])
}

const fmtfile = {sb, ap, opts
	var f : file#
	f = std.vanext(ap)
	filedump(sb, f, 0)
}

const fmtdcl = {sb, ap, opts
	var f : dcl#
	f = std.vanext(ap)
	dcldump(sb, f, 0)
}

const fmtstab = {sb, ap, opts
	var f : stab#
	f = std.vanext(ap)
	stabdump(sb, f, 0)
}

const fmtexpr = {sb, ap, opts
	var f : expr#
	f = std.vanext(ap)
	exprdump(sb, f)
}


const filedump = {sb, f, ind
	ifmt(sb, ind, "file: {}\n", f.path)
	for u in f.uses
		match u
		| `Ulocal l:	ifmt(sb, ind + 1, "local-use:\t{}\n", l)
		| `Ulib l:	ifmt(sb, ind + 1, "lib-use:\t{}\n", l)
		;;
	;;
	ifmt(sb, ind, "libs: {}\n", f.extlibs)
	ifmt(sb, ind, "extlibs: {}\n", f.extlibs)

	for i in f.extinit
		dcldump(sb, i, ind + 1)
	;;

	match f.init
	| `std.Some d:	dcldump(sb, d, ind + 1)
	| `std.None:
	;;

	stabdump(sb, f.builtin, ind + 1)
	stabdump(sb, f.globls, ind + 1)

	for s in f.dcls
		dcldump(sb, s, ind + 1)
	;;
}

const dcldump = {sb, dcl, ind
	ifmt(sb, ind, "{}: {} {}: {}\t", dcl.id, dcl.vis, dcl.name, dcl.ty)
	ifmt(sb, ind + 2, "globl:{}, const:{}, noret:{}, init:{}, exportinit:{}\n", \
		dcl.isglobl, dcl.isconst, dcl.isnoret, dcl.isinit, dcl.isexportinit)
	ifmt(sb, ind + 2, "extern:{}, import:{}, hidden:{}, pkglocal:{}, generic:{}\n", \
		dcl.isextern, dcl.isimport, dcl.ishidden, dcl.ispkglocal, dcl.isgeneric)
	ifmt(sb, ind + 1, "{}", dcl.init)
}

const exprdump = {sb, e
	match e#
	| [.e = `Oadd (a, b)]:	std.sbfmt(sb, "{} + {}", a, b)
	| [.e = `Osub (a, b)]:	std.sbfmt(sb, "{} - {}", a, b)
	| [.e = `Omul (a, b)]:	std.sbfmt(sb, "{} * {}", a, b)
	| [.e = `Odiv (a, b)]:	std.sbfmt(sb, "{} / {}", a, b)
	| [.e = `Omod (a, b)]:	std.sbfmt(sb, "{} % {}", a, b)
	| [.e = `Oneg exp]:	std.sbfmt(sb, "-{}", exp)
	| [.e = `Obor (a, b)]:	std.sbfmt(sb, "{} | {}", a, b)
	| [.e = `Oband (a, b)]:	std.sbfmt(sb, "{} & {}", a, b)
	| [.e = `Obxor (a, b)]:	std.sbfmt(sb, "{} ^ {}", a, b)
	| [.e = `Obsl (a, b)]:	std.sbfmt(sb, "{} << {}", a, b)
	| [.e = `Obsr (a, b)]:	std.sbfmt(sb, "{} >> {}", a, b)
	| [.e = `Obnot exp]:	std.sbfmt(sb, "~{}", exp)
	| [.e = `Opreinc exp]:	std.sbfmt(sb, "++{}", exp)
	| [.e = `Opostinc exp]:	std.sbfmt(sb, "{}++", exp)
	| [.e = `Opredec exp]:	std.sbfmt(sb, "++{}", exp)
	| [.e = `Opostdec exp]:	std.sbfmt(sb, "{}--", exp)
	| [.e = `Oaddr exp]:	std.sbfmt(sb, "&{}", exp)
	| [.e = `Oderef exp]:	std.sbfmt(sb, "{}#", exp)
	| [.e = `Olor (a, b)]:	std.sbfmt(sb, "{} || {}", a, b)
	| [.e = `Oland (a, b)]:	std.sbfmt(sb, "{} && {}", a, b)
	| [.e = `Olnot exp]:	std.sbfmt(sb, "!{}", exp)
	| [.e = `Oeq (a, b)]:	std.sbfmt(sb, "{} == {}", a, b)
	| [.e = `One (a, b)]:	std.sbfmt(sb, "{} != {}", a, b)
	| [.e = `Ogt (a, b)]:	std.sbfmt(sb, "{} > {}", a, b)
	| [.e = `Oge (a, b)]:	std.sbfmt(sb, "{} >= {}", a, b)
	| [.e = `Olt (a, b)]:	std.sbfmt(sb, "{} < {}", a, b)
	| [.e = `Ole (a, b)]:	std.sbfmt(sb, "{} <= {}", a, b)
	| [.e = `Oasn (a, b)]:	std.sbfmt(sb, "{} = {}", a, b)
	| [.e = `Oaddeq (a, b)]:	std.sbfmt(sb, "{} += {}", a, b)
	| [.e = `Osubeq (a, b)]:	std.sbfmt(sb, "{} -= {}", a, b)
	| [.e = `Omuleq (a, b)]:	std.sbfmt(sb, "{} *= {}", a, b)
	| [.e = `Odiveq (a, b)]:	std.sbfmt(sb, "{} /= {}", a, b)
	| [.e = `Omodeq (a, b)]:	std.sbfmt(sb, "{} %= {}", a, b)
	| [.e = `Oboreq (a, b)]:	std.sbfmt(sb, "{} |= {}", a, b)
	| [.e = `Obandeq (a, b)]:	std.sbfmt(sb, "{} &= {}", a, b)
	| [.e = `Obxoreq (a, b)]:	std.sbfmt(sb, "{} ^= {}", a, b)
	| [.e = `Obsleq (a, b)]:	std.sbfmt(sb, "{} <<= {}", a, b)
	| [.e = `Obsreq (a, b)]:	std.sbfmt(sb, "{} >>= {}", a, b)
	| [.e = `Oidx (a, b)]:	std.sbfmt(sb, "{}[{}]", a, b)
	| [.e = `Oslice (a, b, c)]:	std.sbfmt(sb, "{}[{} : {}]", a, b, c)
	| [.e = `Omemb (a, memb)]:	std.sbfmt(sb, "{}.{}", a, memb)
	| [.e = `Osize ty]:	std.sbfmt(sb, "sizeof({})", ty)
	| [.e = `Ocast exp]:	std.sbfmt(sb, "{} castto({})", exp)
	| [.e = `Oret exp]:	std.sbfmt(sb, "-> {}", exp)
	| [.e = `Ojmp name]:	std.sbfmt(sb, "goto {}", name)
	| [.e = `Obreak]:		std.sbfmt(sb, "break")
	| [.e = `Ocontinue]:	std.sbfmt(sb, "continue")
	| [.e = `Ovar name]:	std.sbfmt(sb, "{}", name)
	| [.e = `Ogap]:		std.sbfmt(sb, "_")
	| [.e = `Olit lit]:	std.sbfmt(sb, "{}", lit)
	| [.e = `Ocall (f, args)]:	std.sbfmt(sb, "{}({})", f, args)
	| [.e = `Oucon (t, `std.Some v)]:
		std.sbfmt(sb, "{} {}", t, v)
	| [.e = `Oucon (t, `std.None)]:
		std.sbfmt(sb, "{}", t)
	| [.e = `Otup al]:
		std.sbfmt(sb, "{}", al)
	| [.e = `Ostruct ml]:
		std.sbfmt(sb, "{}", ml)
	| [.e = `Oarr il]:
		std.sbfmt(sb, "{}", il)
	| [.e = `Oidxlen]:
		std.sbfmt(sb, "$")
	;;
}

const stabdump = {sb, st, ind
	var keys

	if st.name.len != 0
		ifmt(sb, ind, "ns: {}", st.name)
	;;
	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:	
			match uc.ety
			| `std.Some t:	ifmt(sb, ind, "{} : {}\n", k, t)
			| `std.None:	ifmt(sb, ind, "{}\n", k)
			;;
		;;
	;;
	std.slfree(keys)

	keys = std.htkeys(st.types)
	for k in keys
		match std.get(std.htget(st.types, k))
		| `Tyfwd:	ifmt(sb, ind, "type {} [PROTO]", k)
		| `Trfwd:	ifmt(sb, ind, "trait {} [PROTO]", k)
		| `Tydef d:	ifmt(sb, ind, "type {} = {}\n", k, d)
		| `Trdef tr:	ifmt(sb, ind, "trait {} = ...\n", k)
		;;
	;;
	std.slfree(keys)
}