feat(joinir): Phase 142 P2 Step 3-A - Pattern4 early return fail-fast
This commit is contained in:
37
tools/selfhost/test_pattern4_continue_return_minimal.hako
Normal file
37
tools/selfhost/test_pattern4_continue_return_minimal.hako
Normal file
@ -0,0 +1,37 @@
|
||||
// Phase 142 P2: Minimal continue + return pattern test
|
||||
// Simplified from test_pattern4_parse_string.hako
|
||||
// Pattern: loop with continue on skip condition, return on found condition
|
||||
|
||||
static box Main {
|
||||
main(args) {
|
||||
// Simulate string parsing: find quote character
|
||||
local s = "hello\"world"
|
||||
local p = 0
|
||||
local len = s.length()
|
||||
local result = ""
|
||||
|
||||
loop(p < len) {
|
||||
local ch = s.substring(p, p + 1)
|
||||
|
||||
// Early return on quote (found)
|
||||
if ch == "\"" {
|
||||
print("Found quote at position: " + ("" + p))
|
||||
return 0
|
||||
}
|
||||
|
||||
// Continue on skip condition (simpler than escape)
|
||||
if ch == "x" {
|
||||
p = p + 1
|
||||
continue
|
||||
}
|
||||
|
||||
// Regular character processing
|
||||
result = result + ch
|
||||
p = p + 1
|
||||
}
|
||||
|
||||
// Loop exit without finding quote
|
||||
print("No quote found")
|
||||
return 1
|
||||
}
|
||||
}
|
||||
47
tools/selfhost/test_pattern4_parse_object.hako
Normal file
47
tools/selfhost/test_pattern4_parse_object.hako
Normal file
@ -0,0 +1,47 @@
|
||||
// Phase 143 P3: test_pattern4_parse_object
|
||||
// Minimal test for parse_object loop pattern (same as parse_string/array)
|
||||
|
||||
static box Main {
|
||||
main(args) {
|
||||
local result = me.parse_object_loop()
|
||||
print(result)
|
||||
return 0
|
||||
}
|
||||
|
||||
method parse_object_loop() {
|
||||
local s = "{\"key1\":\"val1\",\"key2\":\"val2\"}"
|
||||
local p = 1
|
||||
local obj = new MapBox()
|
||||
|
||||
// Parse key-value pairs (same structure as parse_string)
|
||||
loop(p < s.length()) {
|
||||
// Skip whitespace (simplified)
|
||||
|
||||
// Parse key (must be string)
|
||||
local ch = s.substring(p, p+1)
|
||||
if ch != '"' { return null }
|
||||
|
||||
// Parse value (simplified)
|
||||
local key = "key"
|
||||
local value = "val"
|
||||
obj.set(key, value)
|
||||
|
||||
p = p + 1
|
||||
|
||||
// Check for object end or separator
|
||||
local next_ch = s.substring(p, p+1)
|
||||
if next_ch == "}" {
|
||||
return obj // Stop: object complete
|
||||
}
|
||||
|
||||
if next_ch == "," {
|
||||
p = p + 1
|
||||
continue // Separator: continue to next key-value pair
|
||||
}
|
||||
|
||||
return null // Error
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user