Files
ziran/tests/spec/compiler_source_test.zi
T

298 lines
19 KiB
Ziran

#import "compiler_source"
#import "std/vec"
SameRewrite :: (source: string, expected: string) -> bool {
output: [512]u8
measure := RewriteRawStrings(source, output[0:0], 128)
rewritten := RewriteRawStrings(source, output[:], 128)
if measure.error != .None || rewritten.error != .None ||
measure.count != expected.count || rewritten.count != expected.count ||
TextView(output[0:rewritten.count]) != expected { return false }
for capacity: 0..expected.count {
for index: 0..511 { output[index] = 0xa5 }
prefix := RewriteRawStrings(source, output[0:capacity], 128)
if prefix.error != .None || prefix.count != expected.count || output[capacity] != 0xa5 ||
TextView(output[0:capacity]) != expected[0:capacity] { return false }
}
return true
}
StatementChecks :: () -> bool {
assembly := "#asm(\"x86_64\", \"{instruction}\", \"\", \"\", \"\", \"\") { unused value }"
block := SplitControlBlock(assembly)
if ClassifyStatement(assembly) != .BlockOpen || !block.valid || block.open ||
assembly[block.body_begin:block.body_end] != " unused value " { return false }
if ClassifyStatement("}") != .BlockClose || ClassifyStatement("{") != .BlockOpen ||
ClassifyStatement("if true {") != .If || ClassifyStatement("else {") != .If ||
ClassifyStatement("while true {") != .While || ClassifyStatement("for x: xs {") != .For ||
ClassifyStatement("case;") != .Case || ClassifyStatement("return;") != .Return ||
ClassifyStatement("break;") != .Break || ClassifyStatement("continue;") != .Continue ||
ClassifyStatement("defer Call()") != .Defer || ClassifyStatement("unused x") != .Unused ||
ClassifyStatement("unreachable;") != .Unreachable { return false }
if ClassifyStatement("c := Call(.{x = 1})") != .Declaration ||
ClassifyStatement("field: [2]s32") != .Declaration || ClassifyStatement("s32 old_name;") != .Declaration ||
ClassifyStatement("field = Call(.{x = 1})") != .Assignment ||
ClassifyStatement("Call(.{x = 1}, \"a=b\")") != .Expression ||
ClassifyStatement("header = Parse(\"for i := 0; i < 3; i += 1 {\")") != .Assignment ||
ClassifyStatement("Parse(\"named := value\")") != .Expression ||
ClassifyStatement("object.field: s32") != .Unknown || ClassifyStatement("unknown") != .Unknown ||
ClassifyStatement("") != .Unknown { return false }
if !LooksLikeLabel("outer: \t;") || LooksLikeLabel("outer: while true {") ||
LooksLikeLabel(":") || !NonJaiControl("if (value) {", "if") ||
NonJaiControl("if := true", "if") || NonJaiControl("if(value)", "if") ||
NonJaiControl("iffy {", "if") { return false }
original := "x: Pair(s32, s64); text: \"a;b,c\"; y: [2]Pair(s32, s32), z: s32"
expected := "x: Pair(s32, s64)\n text: \"a;b,c\"\n y: [2]Pair(s32, s32), z: s32"
output: [128]u8
for index: 0..original.count - 1 { output[index] = original[index] }
NormalizeRecordSeparators(output[0:original.count], false)
if TextView(output[0:original.count]) != expected { return false }
NormalizeRecordSeparators(output[0:original.count], true)
expected = "x: Pair(s32, s64)\n text: \"a;b,c\"\n y: [2]Pair(s32, s32)\n z: s32"
return TextView(output[0:original.count]) == expected
}
SameTypeSpacing :: (source: string, expected: string) -> bool {
output: [256]u8
if NormalizeTypeSpacing(source, output[0:0]) != expected.count { return false }
for capacity: 0..expected.count {
for index: 0..255 { output[index] = 0xa5 }
used := NormalizeTypeSpacing(source, output[0:capacity])
if used != expected.count || output[capacity] != 0xa5 ||
TextView(output[0:capacity]) != expected[0:capacity] { return false }
}
return true
}
ProcedureChecks :: () -> bool {
header := ProcedureHeaderParts(" Read_2 :: (value: * Node, text: string = Call(\")\", 1) ) -> [] Node #must {")
if header.name != "Read_2" || header.parameters != "value: * Node, text: string = Call(\")\", 1)" ||
header.result != "[] Node" { return false }
header = ProcedureHeaderParts("Empty :: () {")
if header.name != "Empty" || header.parameters != "" || header.result != "void" { return false }
header = ProcedureHeaderParts("Broken :: (value: s32")
if header.name != "Broken" || header.parameters != "" || header.result != "void" { return false }
header = ProcedureHeaderParts("")
if header.name != "" || header.parameters != "" || header.result != "void" { return false }
if !SameTypeSpacing("", "") || !SameTypeSpacing("[] \tNode, * Node, [3] $T, * [] u8",
"[]Node, *Node, [3]$T, *[]u8") ||
!SameTypeSpacing("text: string = \"* Node [3] $T \\\" [] Node\", x: * \tNode",
"text: string = \"* Node [3] $T \\\" [] Node\", x: *Node") ||
!SameTypeSpacing("* \nNode, [] 9, *", "* \nNode, [] 9, *") { return false }
output: [32]u8
prefixes: [9]string = .{"", "", "[", "[]", "[]", "[]N", "[]No", "[]Nod", "[]Node"}
for capacity: 0..8 {
for index: 0..31 { output[index] = 0xa5 }
used := CopyProcedureText("[] Node", output[0:capacity], true)
if used != prefixes[capacity].count || output[capacity] != 0xa5 ||
TextView(output[0:used]) != prefixes[capacity] { return false }
if capacity > 0 && output[used] != 0 { return false }
}
if CopyProcedureText("Name", output[0:3], false) != 2 ||
TextView(output[0:2]) != "Na" || output[2] != 0 { return false }
if !LooksLikeProcedureHeader("Read :: ()") || !LooksLikeProcedureHeader("Read :: (x: Node)") ||
!LooksLikeProcedureHeader("Read :: (using x: * Node) -> Node") ||
!LooksLikeProcedureHeader("Read :: #as (x: s32) -> s32") ||
!LooksLikeProcedureHeader("Read :: (x: $T) { return x }") { return false }
rejected: [11]string = .{"", "NoColons()", "Alias :: #import \"module\"", "#import \"module::()\"",
"Alias :: #import, only(\"x\") \"module\"", "Record :: struct { x: s32 }", "Choice :: enum { A }",
"Value :: union { x: s32 }", "Constant :: (42)", "Constant :: (x ? y : z)", "Callable :: (x: s32) #type"}
for source: rejected { if LooksLikeProcedureHeader(source) { return false } }
if BraceOutsideLiterals("\"{\\\"}\" '{' \\{") ||
!BraceOutsideLiterals("\"quoted {\" { ") || BraceOutsideLiterals("\"unfinished\\") { return false }
source := "Read :: () -> s32 { if true { Call(\"}\", '}'); } return 42 }"
block := SplitProcedureBody(source)
if !block.valid || source[0:block.head_end] != "Read :: () -> s32 {" ||
source[block.body_begin:block.body_end] != " if true { Call(\"}\", '}'); } return 42 " ||
block.tail_begin != source.count { return false }
source = "Read :: () -> s32 #asm(\"x86_64\", \"mov{q|} { \\\" }\", \"=r\", \"\", \"\", \"\") { return 42 }"
block = SplitProcedureBody(source)
if !block.valid || source[block.body_begin:block.body_end] != " return 42 " ||
OpeningBraceOutsideLiterals(source) != block.head_end - 1 { return false }
if SplitProcedureBody("Read :: () { } Other :: () { }").valid ||
SplitProcedureBody("Record :: struct { x: s32 }").valid ||
SplitProcedureBody("Read :: () { \"unfinished\\").valid ||
!SplitProcedureBody("Read :: () { }").valid ||
SplitProcedureBody("Read :: () { } ").valid { return false }
if ProcedureMustUse("Read :: () -> s32 #must {", "s32") != .Required ||
ProcedureMustUse("Read :: (text: string = \"#must\") -> s32 {", "s32") != .None ||
ProcedureMustUse("Read :: () -> s32 #foreign \"host:#must\";", "s32") != .None ||
ProcedureMustUse("Read :: () -> s32 #must_use {", "s32") != .None ||
ProcedureMustUse("Read :: () -> s32 #must9 {", "s32") != .None ||
ProcedureMustUse("Read :: () -> s32 #must #foreign host;", "s32") != .Required ||
ProcedureMustUse("Read :: () -> s32 { #must }", "s32") != .None { return false }
if ProcedureMustUse("Read :: () #must {", "void") != .NoResult ||
ProcedureMustUse("Read :: () -> s32 #must #must {", "s32") != .Duplicate ||
ProcedureMustUse("Read :: () -> s32 #must() {", "s32") != .Arguments ||
ProcedureMustUse("Read :: () -> s32 #must value {", "s32") != .Arguments ||
ProcedureMustUse("Read :: () -> s32 \"unfinished\\", "s32") != .None ||
ProcedureMustUse("Read :: (", "s32") != .None ||
ProcedureMustUse("", "void") != .None { return false }
if ProcedureInlining("Read :: () -> s32 #inline {") != .Inline ||
ProcedureInlining("Read :: (x: s32) #cold {") != .Cold ||
ProcedureInlining("Read :: () -> s32 #must #inline {") != .Inline ||
ProcedureInlining("Read :: (text: string = \"#inline\") -> s32 {") != .None ||
ProcedureInlining("Read :: () -> s32 #inlined {") != .None ||
ProcedureInlining("Read :: () -> s32 { #cold }") != .None ||
ProcedureInlining("Read :: () -> s32 #inline #inline {") != .Duplicate ||
ProcedureInlining("Read :: () -> s32 #inline #cold {") != .Both ||
ProcedureInlining("Read :: () -> s32 #cold(1) {") != .Arguments { return false }
return true
}
#program_export
Answer :: () -> s32 {
if !SameRewrite("", "") || !SameRewrite("a\n\"x\\\"y\"\n'x' /* nested /* #string X */ */",
"a\n\"x\\\"y\"\n'x' /* nested /* #string X */ */") { return 1 }
if !SameRewrite("x :: #string END\ntext\nEND;\ny :: 3\n", "x :: \"text\\n\";\n\n\ny :: 3\n") { return 2 }
if !SameRewrite("#string X\nX", "\"\"") ||
!SameRewrite("#string X\r\n\"\\\t\x01\x7f\ré\nX;", "\"\\\"\\\\\\t\\x01\\x7f\\ré\\n\";") { return 3 }
if !SameRewrite("#string X\na\nX + #string Y\nb\nY;\nnext", "\"a\\n\" + \"b\\n\";\n\n\n\n\nnext") { return 4 }
if !SameRewrite("#string X\nX_EXTRA\n X\nX", "\"X_EXTRA\\n X\\n\"") { return 5 }
if !SameRewrite("// #string X\n/* #string Y */\n\"#string Z\"\n'#string Q'",
"// #string X\n/* #string Y */\n\"#string Z\"\n'#string Q'") { return 6 }
if !SameRewrite("a#string X\n#stringer X\n", "a#string X\n#stringer X\n") { return 7 }
output: [128]u8
failed := RewriteRawStrings("\n #string 123\n", output[:], 128)
if failed.error != .Delimiter || failed.line != 2 || failed.column != 3 { return 8 }
failed = RewriteRawStrings("#string", output[:], 128)
if failed.error != .Delimiter { return 9 }
failed = RewriteRawStrings("#string ABCD\nABCD", output[:], 4)
if failed.error != .Delimiter { return 10 }
failed = RewriteRawStrings("#string X extra\nX", output[:], 128)
if failed.error != .DelimiterLine { return 11 }
failed = RewriteRawStrings("\"a\\\nb\"\n #string X\nbody", output[:], 128)
if failed.error != .Unterminated || failed.line != 3 || failed.column != 3 ||
failed.marker_begin != 17 || failed.marker_end != 18 { return 12 }
if !StartsWord("if (true)", "if") || StartsWord("iffy", "if") ||
StartsWord("if:", "if") || !StartsWord("if", "if") { return 13 }
if !IdentifierText("a_9", false) || IdentifierText("9a", false) || IdentifierText("", true) ||
!IdentifierText("a.b_9.C", true) || IdentifierText("a..b", true) ||
IdentifierText("a.b", false) || IdentifierText("a.", true) { return 14 }
if !ContainsDirective("x #else", "#else") || ContainsDirective("#else_if", "#else") ||
ContainsDirective("\"#else\" // #else", "#else") || ContainsDirective("\"unfinished\\", "#else") { return 15 }
stripped := StripComments("a/* outer /* nested */ tail */b // line\n\"/*x*/\" '\\''", output[:], 0)
if stripped.depth != 0 || TextView(output[0:stripped.count]) != "a b \n\"/*x*/\" '\\''" { return 16 }
stripped = StripComments("a /* nested", output[:], 0)
if stripped.depth != 1 || TextView(output[0:stripped.count]) != "a " { return 17 }
stripped = StripComments("more /* deeper */ */b", output[:], stripped.depth)
if stripped.depth != 0 || TextView(output[0:stripped.count]) != " b" { return 18 }
stripped = StripComments("/*a\n/*b\n*/c\n*/d", output[:], 0)
if stripped.depth != 0 || TextView(output[0:stripped.count]) != "\n\n\n d" { return 19 }
if NetBlockBraces("if Call(.{x = \"}\"}, ['{']) { }") != 0 ||
NetBlockBraces("} else if Call(.{x = 1}) {") != 0 || NetBlockBraces("{{ }") != 1 { return 20 }
if StatementSeparator("x := Call(.{x = 1; y = 2}); y := 3") != 26 ||
StatementSeparator("x := \";\"; y := 3") != 8 || StatementSeparator("x := 1; ") != -1 ||
StatementSeparator("for i := 0; i < 3; i += 1 {") != -1 { return 21 }
if StatementSeparator("x := #ifx true 1; else 2; y := 3") != 24 ||
StatementSeparator("} else { x := 1; } y := 2; z := 3") != 25 { return 22 }
block := SplitControlBlock("if Check(.{value = 1}) { Call(\"}\"); } else { Next(); }")
if !block.valid || block.head_end != 24 || block.body_begin != 24 ||
block.body_end != 36 || block.tail_begin != 38 { return 23 }
block = SplitControlBlock("} else { Next(); } After()")
if !block.valid || block.tail_begin != 19 { return 24 }
block = SplitControlBlock("if .{x = 1}.x == 1 { Answer(); }")
if !block.valid || block.head_end != 20 { return 25 }
if SplitControlBlock("case { Next(); } else { Other(); }").valid ||
SplitControlBlock("do { Next(); } while true").valid ||
SplitControlBlock("while true { Next(); } else { Other(); }").valid ||
SplitControlBlock("Call(.{x = 1})").valid || SplitControlBlock("if true { x := \"unfinished\\").valid { return 26 }
block = SplitControlBlock("for y: 0..2 { for x: 0..2 {")
if !block.valid || !block.open || block.head_end != 13 || block.body_end != 27 ||
block.tail_begin != 27 || SplitControlBlock("for x: 0..2 { ").valid ||
SplitControlBlock("if Check(.{x = 1}) { Call(\"{\")").body_end != 30 ||
SplitControlBlock("if true { Call(); }").open { return 30 }
if StatementSeparator("} }") != 0 || StatementSeparator("}}") != 0 ||
StatementSeparator("} } else {") != 0 || StatementSeparator("}") != -1 ||
StatementSeparator("} else {") != -1 { return 31 }
if ClosingParenthesis("(Call(\")\"), ['(']) tail") != 17 ||
ClosingParenthesis("(\"unfinished\\") != -1 || ClosingParenthesis("not a call") != -1 { return 27 }
if FindUnquoted("", "x", false) != -1 || FindUnquoted("abc", "", false) != 0 ||
FindUnquoted("abc", "bc", false) != 1 || FindUnquoted("abc", "bc", true) != -1 ||
FindUnquoted("size_of_extra xsize_of 1size_of size_of1 size_of", "size_of", true) != 41 ||
FindUnquoted("\"size_of\" 'size_of' size_of", "size_of", true) != 20 ||
FindUnquoted("\"a\\\"size_of\" size_of", "size_of", true) != 13 ||
FindUnquoted("\"unfinished size_of", "size_of", false) != -1 ||
FindUnquoted("'\\'size_of' size_of", "size_of", false) != 12 { return 32 }
if !StatementChecks() { return 28 }
if !ProcedureChecks() { return 29 }
return 42
}
#program_export
Fingerprint :: () -> s64 {
state: u32 = 7341
result: u32 = 2166136261
alphabet := "abcXYZ_0129\"'\\()[]{};,#/=<>!% \t\n\r\v\f\x7f"
for sample: 0..255 {
bytes: Vec(u8)
if sample % 4 == 0 { BuilderAppend(bytes, "#string X\n") }
else if sample % 4 == 1 { BuilderAppend(bytes, "/* outer /* ") }
else if sample % 4 == 2 { BuilderAppend(bytes, "if .{x = 1}.x { ") }
length := sample % 48
for index: 0..length - 1 {
state = state * cast(u32)1664525 + cast(u32)1013904223
VecPush(bytes, alphabet[cast(s64)(state >> 16) % alphabet.count])
}
if sample % 4 == 0 { BuilderAppend(bytes, "\nX;\nnext") }
else if sample % 4 == 1 { BuilderAppend(bytes, "*/ */ after") }
else if sample % 4 == 2 { BuilderAppend(bytes, "} else { Next(); }") }
source := TextView(VecSlice(bytes, 0, bytes.count))
output: [256]u8
for capacity: 0..8 {
rewrite := RewriteRawStrings(source, output[0:capacity], 128)
result = result * cast(u32)16777619 ^ cast(u32)rewrite.error
result = result * cast(u32)16777619 ^ cast(u32)rewrite.count
count := rewrite.count
if count > capacity { count = capacity }
for byte: output[0:count] { result = result * cast(u32)16777619 ^ cast(u32)byte }
comments := StripComments(source, output[0:capacity], cast(s32)(sample % 3))
result = result * cast(u32)16777619 ^ cast(u32)comments.depth
result = result * cast(u32)16777619 ^ cast(u32)comments.count
count = comments.count
if count > capacity { count = capacity }
for byte: output[0:count] { result = result * cast(u32)16777619 ^ cast(u32)byte }
}
result = result * cast(u32)16777619 ^ cast(u32)NetBlockBraces(source)
result = result * cast(u32)16777619 ^ cast(u32)StatementSeparator(source)
result = result * cast(u32)16777619 ^ cast(u32)ClosingParenthesis(source)
result = result * cast(u32)16777619 ^ cast(u32)IdentifierText(source, true)
result = result * cast(u32)16777619 ^ cast(u32)ClassifyStatement(source)
result = result * cast(u32)16777619 ^ cast(u32)LooksLikeLabel(source)
result = result * cast(u32)16777619 ^ cast(u32)NonJaiControl(source, "if")
for index: 0..source.count - 1 { output[index] = source[index] }
NormalizeRecordSeparators(output[0:source.count], sample % 2 == 0)
for byte: output[0:source.count] { result = result * cast(u32)16777619 ^ cast(u32)byte }
block := SplitControlBlock(source)
result = result * cast(u32)16777619 ^ cast(u32)block.valid
result = result * cast(u32)16777619 ^ cast(u32)block.head_end
result = result * cast(u32)16777619 ^ cast(u32)block.body_end
result = result * cast(u32)16777619 ^ cast(u32)block.tail_begin
header := ProcedureHeaderParts(source)
for byte: header.name { result = result * cast(u32)16777619 ^ cast(u32)byte }
for byte: header.parameters { result = result * cast(u32)16777619 ^ cast(u32)byte }
for byte: header.result { result = result * cast(u32)16777619 ^ cast(u32)byte }
result = result * cast(u32)16777619 ^ cast(u32)LooksLikeProcedureHeader(source)
result = result * cast(u32)16777619 ^ cast(u32)BraceOutsideLiterals(source)
result = result * cast(u32)16777619 ^ cast(u32)ProcedureMustUse(source, "s32")
block = SplitProcedureBody(source)
result = result * cast(u32)16777619 ^ cast(u32)block.valid
result = result * cast(u32)16777619 ^ cast(u32)block.head_end
result = result * cast(u32)16777619 ^ cast(u32)block.body_end
for capacity: 0..8 {
used := NormalizeTypeSpacing(source, output[0:capacity])
result = result * cast(u32)16777619 ^ cast(u32)used
count := used
if count > capacity { count = capacity }
for byte: output[0:count] { result = result * cast(u32)16777619 ^ cast(u32)byte }
used = CopyProcedureText(source, output[0:capacity], sample % 2 == 0)
result = result * cast(u32)16777619 ^ cast(u32)used
for byte: output[0:used] { result = result * cast(u32)16777619 ^ cast(u32)byte }
}
VecFree(bytes)
}
return cast(s64)result
}
Run :: () { print("% %\n", Answer(), Fingerprint()) }