#!/usr/bin/env ascript
//
// make a ctype table
//
funcTbl = []

funcTbl.add(IsAlpha(ch) = (ord("a") <= ch && ch <= ord("z") || ord("A") <= ch && ch <= ord("Z")))

funcTbl.add(IsDigit(ch) = (ord("0") <= ch && ch <= ord("9")))

funcTbl.add(IsHexDigit(ch) = {
	ord("0") <= ch && ch <= ord("9") || \
	ord("A") <= ch && ch <= ord("F") || \
	ord("a") <= ch && ch <= ord("f")
})

funcTbl.add(IsOctDigit(ch) = (ord("0") <= ch && ch <= ord("7")))

funcTbl.add(IsWhite(ch) = (ch == ord(" ") || ch == ord("\t")))

funcTbl.add(IsSpace(ch) = {
	(ch == ord(" ") || ch == ord("\t") || ch == ord("\r") || ch == ord("\n"))
})

funcTbl.add(IsSJISFirst(ch) = {
	((0x81 <= ch && ch <= 0x9f) || \
	 (0xe0 <= ch && ch <= 0xef) || \
	 (0xfa <= ch && ch <= 0xfc))
})

funcTbl.add(IsSJISSecond(ch) = {
	((0x40 <= ch && ch <= 0x7e) || (0x80 <= ch && ch <= 0xfc))
})

funcTbl.add(IsUTF8First(ch) = ((ch & 0xc0) == 0xc0))

funcTbl.add(IsUTF8Follower(ch) = ((ch & 0xc0) == 0x80))

funcTbl.add(IsSymbolExtra(ch) = {
	ch == ord("_") || ch == ord("$") || ch == ord("@")
})

funcTbl.add(IsLower(ch) = (ord("a") <= ch && ch <= ord("z")))

funcTbl.add(IsUpper(ch) = (ord("A") <= ch && ch <= ord("Z")))

println("static const unsigned short __ctypeTbl[] = {")
for (ch in 0..255) {|i|
	(i % 8 == 0) && print("\t")
	bitPat = 0x0000
	for (func in funcTbl) {|iBit|
		if (func(ch)) {
			bitPat |= (1 << iBit)
		}
	}
	printf("0x%04x", bitPat)
	print(if ((i + 1) % 8 == 0) { ",\n" } else { ", " })
}
println("};")

import(re)

for (func in funcTbl) {|iBit|
	println()
	varName = "CTYPE_" + re.sub("^Is", "", func.name())
	printf("#define %s (1 << %d)\n", varName, iBit)
	printf("inline bool %s(char ch) { return (GetCType(ch) & %s) != 0; }\n",
														func.name(), varName)
}
